import pygame
import sys
import random
class Bird(object): # 小鸟类
def __init__(self):
self.pic = [pygame.image.load('materials/birds/鸟0_0.png'),
pygame.image.load('materials/birds/鸟0_1.png'),
pygame.image.load('materials/birds/鸟0_2.png')] # 载入鸟图片
self.death = False # 鸟初始状态为活着
self.x = 60 # 鸟坐标,x坐标始终不变,只变y坐标
self.y = 256
self.up_speed = 50 # 点按一次上升距离
self.down_speed = 1 # 不点按时下降距离
self.status = 1 # 鸟状态(上升,平飞,下降)对应self.pic的三个图片
def move(self): # 鸟的移动
if not self.death: # 如果鸟没死
self.status = 2 # 鸟状态设为下降状态
self.y += self.down_speed # y坐标增加,鸟下降
if self.y > 512 - 37: # 如果鸟下落后位置超过屏幕底部,额外加减的数值均是考虑到图片多余部分
self.y = 512 - 37 # 鸟坐标位置设为屏幕底部
self.down_speed += 1 # 下落距离加一,即下落速度越来越快
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN: # 按下键盘或点击鼠标
self.status = 0 # 鸟状态设为上升状态
self.y -= self.up_speed # y坐标减小,鸟上升
if self.y < -12: # 如果鸟上升后位置超出屏幕顶部
self.y = -12 # 鸟坐标位置设为屏幕顶部
self.down_speed = 1 # 下降速度重置
else: # 如果鸟死亡
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 如果点击get_ready按钮
if event.pos[0] in range(40, 40+190) and event.pos[1] in range(256, 256+50) and event.button == 1:
self.death = False # 鸟复活重新开始
global Score
Score = 0 # 重置分数
bird.__init__() # 重置鸟
pipe.__init__() # 重置管子
class Pipe(object): # 管子类
def __init__(self):
self.pic = [pygame.image.load('materials/pipes/pipe1_down.png'),
pygame.image.load('materials/pipes/pipe1_up.png')] # 载入上下管子
self.x = 288 - 52 # 管子y坐标不变,只改变x坐标
self.y0 = -170 # 上管子y坐标
self.y1 = 150 + 212 # 下管子y坐标
self.speed = 4
def move(self):
global Score
if not bird.death:
self.x -= self.speed # 管子移动速度,向左匀速移动
if self.x + 52 == 60: # 判断得分
Score += 1
# 如果管子移出屏幕左边,随机生成新的管子
if self.x < -52:
self.x = 288
h2 = random.randint(80, 200) # 两管口之间距离
h1 = random.randint(512-320-h2, 320) # 上管口距离顶部的距离
# 新管子位置
self.y0 = h1 - 320
self.y1 = h1 + h2
def update(): # 填充各种元素,更新屏幕画面
screen.blit(bgp[0], (0, 0)) # 背景, 0为白天,1为夜晚
screen.blit(pipe.pic[0], (pipe.x, pipe.y0)) # 上管
screen.blit(pipe.pic[1], (pipe.x, pipe.y1)) # 下管
screen.blit(font.render('Score: ' + str(Score), True, (234, 28, 39)), (60, 50)) # 得分
screen.blit(bird.pic[bird.status], (bird.x, bird.y)) # 鸟
if bird.death: # 如果鸟死亡,显示游戏结束画面
screen.blit(GameOver, (40, 150))
screen.blit(get_ready, (40, 256))
pygame.display.update() # 刷新屏幕
clock.tick(fps) # 帧率控制
if __name__ == '__main__':
pygame.init()
size = width, height = 288, 512
screen = pygame.display.set_mode(size) # 设置窗口大小
pygame.display.set_caption('Fly Bird') # 设置窗口标题
clock = pygame.time.Clock() # 用于控制刷新频率
fps = 24 # 帧率控制
bgp = [pygame.image.load('materials/bgp/白天.png'), pygame.image.load('materials/bgp/夜晚.png')] # 载入背景图片
bird = Bird() # 实例化鸟对象
pipe = Pipe() # 实例化管子对象
Score = 0 # 得分
pygame.font.init() # 字体初始化
font = pygame.font.SysFont('arial', 50) # 创建字体对象
GameOver = pygame.image.load('materials/buttons/game_over.png') # 载入GameOver图片
get_ready = pygame.image.load('materials/buttons/get_ready.png') # 载入Get Ready图片
while True: # 游戏主循环
if bird.y == -12 or bird.y == 512-37: # 撞到屏幕上下边
bird.death = True # 死亡
pipe.move() # 管子移动
bird.move() # 鸟移动
# 撞管子,鸟左边缘<管子右边缘 and 鸟右边缘>管子左边缘 and (鸟上边缘<管子上边缘 or 鸟下边缘>管子下边缘)
if bird.x+5 < pipe.x+52 and bird.x+40 > pipe.x and (bird.y+12 < pipe.y0+320 or bird.y+37 > pipe.y1):
bird.death = True
# 过管子期间撞管子
if bird.y < pipe.y0+320 and bird.x+40 > pipe.x+pipe.speed: # 鸟上边缘<管子上边缘 and 鸟右边缘>管子左边缘,即撞上管子
bird.y = pipe.y0+320-12 # 鸟y坐标设为刚好撞上管子的位置
elif bird.y+37 > pipe.y1 and bird.x+40 > pipe.x+pipe.speed: # 鸟下边缘>管子下边缘 and 鸟右边缘>管子左边缘,即撞下管子
bird.y = pipe.y1-37 # 鸟y坐标设为刚好撞下管子的位置
update() # 刷新屏幕
评论0