当前位置:网站首页>[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
2022-04-23 16:21:00 【Hi! Chestnut classmate】
Preface
Hee hee , I haven't updated new content for you for a long time ~ There's a reason , The article will be put on the shelf again this time !
After that, I will write new articles every day ~ In this issue of this article, let's make some simple drops , Let's do one
Next Python edition 《 Angry birds 》 The simple version comes with a less simple version ~
Complete material for all articles + The source code is in
Fans white whoring source code welfare , Please move to CSDN Community
Why is it a simple version ? Because the first version has been studied for a long time , It's not written yet , A work that is regarded as a failure , There are no games
The experience code is also super simple ,2333 I'm going to continue to look for the code written by other big guys as a reference ! Later, I thought about continuing to write a
The simple version and the less simple version ~ It's too simple. I'm afraid everyone will do it ~ Ha ha ha ha .jpg

![]()
Text
The game of this article is about an angry bird duangduangduang Back and forth ! Smile to cry
Because it's easy for you to mark the code directly
One 、 Simple version
Environmental installation
Python3、 Pycharm 、Pygame modular .( To install the package 、 Activation code and other direct private mail, I can install and answer questions
Everything is OK ~)
Third party library installation :pip install pygame
1) Code display
import pygame,sys
pygame.init()# Initialization operation
# Save window size
width,height=600,400
screen=pygame.display.set_mode([width,height])# Create game window
# Set the window title
pygame.display.set_caption(" Angry birds ")
# Load bird material
player=pygame.image.load("xiaoniao.png")
# Gets the position of the image rectangle
rect=player.get_rect()
# Statement XY List of motion speeds
speed = [3,2]
left_head = pygame.transform.flip(player,True,False)
right_head = player
# Infinite loop
while True:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
exit()
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player = left_head # The bird's head is to the left
speed=[-2,1]
if event.key == pygame.K_RIGHT:
player = right_head # The bird's head is to the left
speed=[2,1]
if event.key == pygame.K_UP:
player = left_head # The bird's head is to the left
speed=[2,-1]
if event.key == pygame.K_DOWN:
player = right_head # The bird's head is to the left
speed=[2,1]
rect =rect.move(speed)
if rect.right>width or rect.left<0:
# Flip the picture horizontally Invert objects Whether to reverse horizontally Whether to flip vertically
player = pygame.transform.flip(player,True,False)
speed[0]=-speed[0]
if rect.bottom>height or rect.top<0:
speed[1]=-speed[1]
screen.fill((255,255,255))
screen.blit(player,rect)
pygame.display.update()
pygame.time.delay(10)
2) Effect display
In fact, the screenshot doesn't show much effect ——
In fact, this game can float
Two 、 A less minimalist version
Environmental installation
Python3、 Pycharm 、Pygame modular .( To install the package 、 Activation code and other direct private mail, I can install and answer questions
Everything is OK ~)
Third party library installation :pip install pygame
1) Code display
import pygame
import sys
from pygame.locals import *
from random import randint
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(' Angry birds background sound .wav')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(loops=-1)
bg = pygame.image.load(' Angry bird background 3.jpg')
bg_position = bg.get_rect()
size = width, height = 1000, 570
screen = pygame.display.set_mode(size)
pygame.display.set_caption(' Angry birds !')
def main():
class Bird(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
position=100,100
self.image=pygame.image.load(' The bird in the angry bird .png')
self.rect=self.image.get_rect()
self.rect.center=position
def move_left(self):
self.speed=[-5,0]
if self.rect.left<=0:
self.rect.left=0
else:
self.rect=self.rect.move(self.speed)
def move_right(self):
self.speed=[5,0]
if self.rect.right>=1000:
self.rect.right=1000
else:
self.rect=self.rect.move(self.speed)
def move_up(self):
self.speed=[0,-5]
if self.rect.top<=0:
self.rect.top=0
else:
self.rect=self.rect.move(self.speed)
def move_down(self):
self.speed=[0,5]
if self.rect.bottom>=570:
self.rect.bottom=570
else:
self.rect=self.rect.move(self.speed)
class Pig(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
y=randint(0,570)
position=[1000,y]
self.image=pygame.image.load(' The pig in the angry bird .png')
self.rect=self.image.get_rect()
self.rect.center=position
self.speed=[-4,0]
def move(self):
self.rect=self.rect.move(self.speed)
bird=Bird()
i=0
group=pygame.sprite.Group()
state=True
while state:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
key=pygame.key.get_pressed()
if key[K_LEFT]:
bird.move_left()
if key[K_RIGHT]:
bird.move_right()
if key[K_UP]:
bird.move_up()
if key[K_DOWN]:
bird.move_down()
screen.blit(bg, bg_position)
screen.blit(bird.image,bird.rect)
i=i+1
if i%10==0:
pig=Pig()
group.add(pig)
for p in group.sprites():
p.move()
screen.blit(p.image,p.rect)
if pygame.sprite.collide_mask(bird,p):
state=False
pause()
pygame.display.flip()
pygame.time.Clock().tick(60)
def pause():
bg_go = pygame.image.load(' Angry birds gameover chart .jpg')
bg_go_pos = bg_go.get_rect()
size = width, height = 1000, 570
screen = pygame.display.set_mode(size)
pygame.display.set_caption('GameOver!')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
img_src=pygame.image.load(' Replay button .jpg')
img_src_pos=img_src.get_rect()
mouse_press=pygame.mouse.get_pressed()
mouse_pos=pygame.mouse.get_pos()
left=img_src_pos.left
right=img_src_pos.right
top=img_src_pos.top
bottom=img_src_pos.bottom
if left+100<mouse_pos[0]<right+100 and top+185<mouse_pos[1]<bottom+185:
img_src=pygame.image.load(' Replay button 2.jpg')
if mouse_press[0]:
main()
img_src_pos = img_src.get_rect().center = 100, 185
screen.blit(bg_go, bg_go_pos)
screen.blit(img_src,img_src_pos)
pygame.display.flip()
main()
2) Effect display
Game interface :( After the game runs, there are sound effects , The rule of the game is to avoid the pig on the right )
Game over !
summary
After many years , The level of small code writing is flying , It shows that the technology has been improved , Rest assured to watch ! There are still many in the past
The source code is waiting for you to see for yourself !
Complete material, etc : You can also drop me ! Or click the end of the article to get it for free ~
Recommended reading in the past ——
project 0.5 【 Video playback artifact 】 Super play : Never rewind the video , It's too magical to stop ......
project 0.6 【Python Applet 】 Hide the confession diagram :“ You never know I like you ”( It is recommended to keep )
A summary of the article ——
project 1.0 Python—2021 | Summary of existing articles | Continuous updating , Just read this article directly
( More + The source code is summarized in the article !! Welcome to ~)
A summary of the article ——
( There are more cases waiting for you to learn ~ You can find me for free !)

版权声明
本文为[Hi! Chestnut classmate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231613306089.html
边栏推荐
- 捡起MATLAB的第(3)天
- Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
- Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
- Construction of esp32 compilation environment
- Algorithem_ ReverseLinkedList
- 【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
- Meaning and usage of volatile
- Day (2) of picking up matlab
- Grbl learning (II)
- Nacos 详解,有点东西
猜你喜欢

Day (5) of picking up matlab

Questions about disaster recovery? Click here

R语言中绘制ROC曲线方法二:pROC包

How important is the operation and maintenance process? I heard it can save 2 million a year?
![Oak-d raspberry pie cloud project [with detailed code]](/img/03/2d464d42614cd65877c645b60047ae.png)
Oak-d raspberry pie cloud project [with detailed code]

C language self compiled string processing function - string segmentation, string filling, etc

OAK-D树莓派点云项目【附详细代码】

Install redis and deploy redis high availability cluster

【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......

Implement default page
随机推荐
Filter usage of spark operator
Day 9 static abstract class interface
【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
Day (3) of picking up matlab
Cloud migration practice in the financial industry Ping An financial cloud integrates hypermotion cloud migration solution to provide migration services for customers in the financial industry
About JMeter startup flash back
How important is the operation and maintenance process? I heard it can save 2 million a year?
Unity shader learning
下载并安装MongoDB
js正则判断域名或者IP的端口路径是否正确
Day (9) of picking up matlab
Master vscode remote GDB debugging
Day (2) of picking up matlab
力扣-198.打家劫舍
各大框架都在使用的Unsafe类,到底有多神奇?
捡起MATLAB的第(3)天
Day (5) of picking up matlab
第十天 异常机制
How to upgrade openstack across versions