使用pygame制作flappybird游戏
程序员文章站
2022-07-13 08:29:14
...
利用pygame开发游戏
-
学习python尝试做到的第一个项目,不是很规范
-
成品展示
-
源代码,可直接使用
import pygame
import random
class Background(pygame.sprite.Sprite):
def __init__(self, is_alt=False):
super().__init__()
self.image = pygame.image.load("./images/bg02.png")
self.rect = self.image.get_rect()
self.speed = 2
if is_alt:
self.rect.x = self.rect.x + 1428
def update(self):
self.rect.x -= self.speed
if self.rect.x <= -1428:
self.rect.x = 1428
class Bar(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("./images/barrier.png")
self.rect = self.image.get_rect()
self.rect.x = 1400
self.rect.y = random.randint(250, 730)
def update(self):
self.rect.x -= 5
if self.rect.x <= -300:
self.kill()
class BarUp(Bar):
def __init__(self):
super().__init__()
self.image = pygame.image.load("./images/barrier2.png")
self.rect.y = -1600
class Brid(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("./images/bird-02.png")
self.rect = self.image.get_rect()
self.rect.x = 250
self.rect.y = 200
self.fly_list = [pygame.image.load("./images/bird-01.png"),
pygame.image.load("./images/bird-02.png"),
pygame.image.load("./images/bird-03.png"),
pygame.image.load("./images/bird-02.png"),
pygame.image.load("./images/bird-1.png"),
pygame.image.load("./images/bird-2.png"),
pygame.image.load("./images/bird-3.png"),
pygame.image.load("./images/bird-2.png"),
pygame.image.load("./images/bird-04.png")]
def update(self, I):
self.image = self.fly_list[I]
pygame.mixer.pre_init(44100, -16, 2, 1024)
pygame.mixer.init()
pygame.init()
i = 0
j = 0
V = 0.0 # 速度
A = 28.0 # 加速度
t = 0.1 # 时间间隔
I = -95 # 能量
J = 0 # 起飞次数
K = 0
G = 0 # 得分
O = 1
R = 255
T = 0
S = 255
image = pygame.image.load("./images/bird-2.png")
image2 = pygame.image.load("./images/bg02.png")
CREATE_ENEMY_EVENT = pygame.USEREVENT
pygame.time.set_timer(CREATE_ENEMY_EVENT, 3500)
my_font = pygame.font.SysFont('arial', 30, True)
screen = pygame.display.set_mode((1428, 791))
screen.blit(image2, (0, 0))
screen.blit(image, (250, 200))
text_0 = my_font.render("Click the up arrow key", False, (255, 0, 0))
screen.blit(text_0, (180, 150))
pygame.display.set_caption("Flappy_Bird")
pygame.display.set_icon(image)
pygame.display.update()
bg1 = Background()
bg2 = Background(True)
back_group = pygame.sprite.Group(bg1, bg2)
group = pygame.sprite.Group()
brid = Brid()
brids = pygame.sprite.Group(brid)
clock = pygame.time.Clock()
channel = pygame.mixer.Channel(2)
channe2 = pygame.mixer.Channel(3)
sound = pygame.mixer.Sound('./images/katong_fly.wav')
sound2 = pygame.mixer.Sound('./images/katong_pengzhuang2.wav')
sound3 = pygame.mixer.Sound('./images/go.wav')
pygame.mixer.music.load("./images/bgmusic2.mp3")
channe2.play(sound3)
clock.tick(1)
pygame.mixer.music.play()
clock.tick(1)
while True:
clock.tick(60)
S += 1
if S >= 220:
S = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == CREATE_ENEMY_EVENT:
R = random.randint(0, 255)
G += 1
bar = Bar()
bar_up = BarUp()
bar_up.rect.y = bar.rect.y - 2000
if (J >= 30) and (J <= 59):
K += 2
bar_up.rect.y += K
elif (J >= 60):
bar_up.rect.y += 40
group.add(bar)
group.add(bar_up)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
channel.play(sound)
clock.tick(26)
O = random.randint(0, 3)
if O != 0:
O = 1
T = 50
else:
T = 100
J += 1
if J <= 39:
V = -95
elif (J >= 28) and (J <= 50):
V = I
I += 0.2
else:
V = I
I += 0.4
pygame.sprite.groupcollide(brids, group, O, 1)
brid.rect.y += round(V * t + 0.5 * A * t * t)
V += A * t
B = len(brids)
if (brid.rect.y >=790) or (B <= 0):
channel.play(sound2)
clock.tick(30)
my_font1 = pygame.font.SysFont('arial', 60, True)
text_1 = my_font1.render("Grade %s" % G, False, (255, 0, 0))
screen.blit(text_1, (brid.rect.x + 30, brid.rect.y - 20))
pygame.display.update()
clock.tick(0.4)
pygame.quit()
exit()
j += 1
if j == 3:
i += 1
j = 0
if V <= -10:
if i >= 4:
i = 0
elif (V > -10)and(V <= 55):
if i >= 8:
i = 4
elif V > 55:
i = 8
back_group.update()
back_group.draw(screen)
group.update()
group.draw(screen)
brids.update(i)
brids.draw(screen)
text_surface = my_font.render("Grade %s" % G, False, (R, T, S))
screen.blit(text_surface, (20, 20))
pygame.display.update()
- 重要代码分析(基础部分就不分析了)
背景墙类的创建
is_alt=False
判断背景壁纸先后,并放置在合适的位置,进行背景循环
bg1 = Background()
bg2 = Background(True)
back_group = pygame.sprite.Group(bg1, bg2)
class Background(pygame.sprite.Sprite):
def __init__(self, is_alt=False):
super().__init__()
self.image = pygame.image.load("./images/bg02.png")
self.rect = self.image.get_rect()
self.speed = 2
if is_alt:
self.rect.x = self.rect.x + 1428
鸟类的创建
将图片放到self.fly_list列表中
循环出现,产生动画效果
def update(self, I):
self.image = self.fly_list[I]
class Brid(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("./images/bird-02.png")
self.rect = self.image.get_rect()
self.rect.x = 250
self.rect.y = 200
self.fly_list = [pygame.image.load("./images/bird-01.png"),
pygame.image.load("./images/bird-02.png"),
pygame.image.load("./images/bird-03.png"),
pygame.image.load("./images/bird-02.png"),
pygame.image.load("./images/bird-1.png"),
pygame.image.load("./images/bird-2.png"),
pygame.image.load("./images/bird-3.png"),
pygame.image.load("./images/bird-2.png"),
pygame.image.load("./images/bird-04.png")]
def update(self, I):
self.image = self.fly_list[I]
障碍物随机出现
设置定时器
CREATE_ENEMY_EVENT = pygame.USEREVENT
pygame.time.set_timer(CREATE_ENEMY_EVENT, 3500)
利用事件监听,随机创建障碍物
elif event.type == CREATE_ENEMY_EVENT:
R = random.randint(0, 255)
G += 1
bar = Bar()
bar_up = BarUp()
bar_up.rect.y = bar.rect.y - 2000
if (J >= 30) and (J <= 59):
K += 2
bar_up.rect.y += K
elif (J >= 60):
bar_up.rect.y += 40
group.add(bar)
group.add(bar_up)
这是程序用到的文件: