Flappy Bird小游戏
import pygame
import random
import sys
初始化 Pygame
pygame.init()
游戏常量
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
GRAVITY = 0.25
BIRD_JUMP = -5
PIPE_SPEED = 3
PIPE_GAP = 150
PIPE_FREQUENCY = 1500 # 毫秒
颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 128, 0)
SKY_BLUE = (135, 206, 235)
创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 30)
class Bird:
def init(self):
self.x = 100
self.y = SCREEN_HEIGHT // 2
self.velocity = 0
self.width = 30
self.height = 30
def jump(self):
self.velocity = BIRD_JUMP
def update(self):
# 应用重力
self.velocity += GRAVITY
self.y += self.velocity
# 防止鸟飞出屏幕顶部
if self.y < 0:
self.y = 0
self.velocity = 0
def draw(self):
pygame.draw.rect(screen, (255, 255, 0), (self.x, self.y, self.width, self.height))
# 画眼睛
pygame.draw.circle(screen, BLACK, (self.x + 22, self.y + 10), 4)
# 画嘴巴
pygame.draw.polygon(screen, (255, 165, 0), [(self.x + 30, self.y + 15),
(self.x + 40, self.y + 15),
(self.x + 30, self.y + 20)])
def get_mask(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Pipe:
def init(self):
self.x = SCREEN_WIDTH
self.height = random.randint(100, 400)
self.top_pipe = pygame.Rect(self.x, 0, 60, self.height)
self.bottom_pipe = pygame.Rect(self.x, self.height + PIPE_GAP, 60, SCREEN_HEIGHT - self.height - PIPE_GAP)
self.passed = False
self.color = GREEN
def update(self):
self.x -= PIPE_SPEED
self.top_pipe.x = self.x
self.bottom_pipe.x = self.x
def draw(self):
pygame.draw.rect(screen, self.color, self.top_pipe)
pygame.draw.rect(screen, self.color, self.bottom_pipe)
def collide(self, bird):
bird_rect = bird.get_mask()
return bird_rect.colliderect(self.top_pipe) or bird_rect.colliderect(self.bottom_pipe)
def draw_score(score):
score_text = font.render(f'Score: {score}', True, BLACK)
screen.blit(score_text, (10, 10))
def game_over_screen(score):
screen.fill(SKY_BLUE)
game_over_text = font.render('Game Over!', True, BLACK)
score_text = font.render(f'Final Score: {score}', True, BLACK)
restart_text = font.render('Press R to Restart', True, BLACK)
screen.blit(game_over_text, (SCREEN_WIDTH//2 - game_over_text.get_width()//2, SCREEN_HEIGHT//2 - 60))
screen.blit(score_text, (SCREEN_WIDTH//2 - score_text.get_width()//2, SCREEN_HEIGHT//2))
screen.blit(restart_text, (SCREEN_WIDTH//2 - restart_text.get_width()//2, SCREEN_HEIGHT//2 + 60))
pygame.display.update()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
waiting = False
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
def main():
bird = Bird()
pipes = []
score = 0
last_pipe = pygame.time.get_ticks()
game_active = True
running = True
while running:
clock.tick(60)
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird.jump()
if event.key == pygame.K_q:
running = False
# 游戏逻辑
if game_active:
# 更新鸟
bird.update()
# 生成新管道
time_now = pygame.time.get_ticks()
if time_now - last_pipe > PIPE_FREQUENCY:
pipes.append(Pipe())
last_pipe = time_now
# 更新管道
for pipe in pipes:
pipe.update()
# 检测碰撞
if pipe.collide(bird):
game_active = False
# 计分
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
score += 1
# 移除屏幕外的管道
if pipe.x < -60:
pipes.remove(pipe)
# 检查鸟是否掉到地面
if bird.y + bird.height >= SCREEN_HEIGHT:
game_active = False
# 绘制
screen.fill(SKY_BLUE)
if game_active:
bird.draw()
for pipe in pipes:
pipe.draw()
draw_score(score)
else:
game_over_screen(score)
# 重置游戏
bird = Bird()
pipes = []
score = 0
last_pipe = pygame.time.get_ticks()
game_active = True
pygame.display.update()
pygame.quit()
sys.exit()
if name == "main":
main()


浙公网安备 33010602011771号