import pygame
import sys
import random
import numpy as np
class Bird(object):
"""定义一个鸟类"""
def __init__(self):
"""定义初始化方法"""
self.birdRect = pygame.Rect(65, 50, 50, 50)
self.birdStatus = [pygame.image.load("sources/1.png"),
pygame.image.load("sources/2.png"),
pygame.image.load("sources/dead.png")]
self.status = 0
self.birdX = 120
self.birdY = 250
self.jump = False
self.jumpSpeed = 10
self.gravity = 5
self.dead = False
def bird_update(self):
if self.jump:
self.jumpSpeed -= 1
self.birdY -= self.jumpSpeed
else:
self.gravity += 0.1
self.birdY += self.gravity
self.birdRect[1] = self.birdY
class Pipeline(object):
"""定义一个管道类"""
def __init__(self):
"""定义初始化方法"""
self.wallx = 400
self.pineUp = pygame.image.load("sources/top.png")
self.pineDown = pygame.image.load("sources/bottom.png")
self.y = 0
def update_pipeline(self):
"""管道水平移动方法"""
self.wallx -= 5
if self.wallx < -80:
global score
score += 1
self.wallx = 400
self.y = random.choice(ls) * 60
def create_map():
"""定义创建地图的方法"""
screen.fill((255, 255, 255))
screen.blit(background, (0, 0))
screen.blit(Pipeline.pineUp, (Pipeline.wallx, -200 + Pipeline.y))
screen.blit(Pipeline.pineDown, (Pipeline.wallx, 400 + Pipeline.y))
Pipeline.update_pipeline()
if Bird.dead:
Bird.status = 2
elif Bird.jump:
Bird.status = 1
screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))
Bird.bird_update()
screen.blit(font.render("score: " + str(score), -1, (255, 255, 255)), (100, 50))
pygame.display.update()
def check_dead():
up_rect = pygame.Rect(Pipeline.wallx, -200 + Pipeline.y, Pipeline.pineUp.get_width() - 10,
Pipeline.pineUp.get_height())
down_rect = pygame.Rect(Pipeline.wallx, 400 + Pipeline.y, Pipeline.pineDown.get_width() - 10,
Pipeline.pineDown.get_height())
if up_rect.colliderect(Bird.birdRect) or down_rect.colliderect(Bird.birdRect):
Bird.dead = True
if not 0 < Bird.birdRect[1] < height:
Bird.dead = True
return True
else:
return False
def get_result():
final_text1 = "Game over"
final_text2 = "Your final score is: " + str(score)
ft1_font = pygame.font.SysFont("Arial", 70)
ft1_surf = ft1_font.render(final_text1, 1, (242, 3, 36))
ft2_font = pygame.font.SysFont("Arial", 50)
ft2_surf = ft2_font.render(final_text2, 1, (253, 177, 6))
screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100])
screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200])
pygame.display.flip()
if __name__ == "__main__":
"""主程序"""
pygame.init()
pygame.font.init()
font = pygame.font.SysFont('ziti.ttf', 50)
size = width, height = 400, 650
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
Pipeline = Pipeline()
Bird = Bird()
score = 0
ls = np.arange(-3, 3.5, 0.5)
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
Bird.jump = True
Bird.gravity = 5
Bird.jumpSpeed = 10
background = pygame.image.load("sources/background.png")
if check_dead():
get_result()
if pygame.event.get():
break
else:
create_map()
pygame.quit()

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】