pythonGame-实现flappyBird

通过python简单复现flappybird游戏。

使用到的库函数:
import pygame
import random
import os
游戏源码:
import pygame
import random
import os

# 初始化pygame
pygame.init()

# 设置窗口大小
WIDTH = 288
HEIGHT = 512
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")

# 加载图片
def load_image(name):
    return pygame.image.load(os.path.join('assets', name)).convert_alpha()

background = load_image('background-day.png')
bird_images = [load_image(f'yellowbird-{name}flap.png') for name in ['down', 'mid', 'up']]
pipe_image = load_image('pipe-green.png')
base_image = load_image('base.png')

# 鸟的属性
bird_x = 50
bird_y = HEIGHT // 2
bird_velocity = 0
gravity = 0.25
jump_strength = -5
bird_index = 0
bird_rect = bird_images[0].get_rect(center=(bird_x, bird_y))

# 管道属性
pipe_width = pipe_image.get_width()
pipe_gap = 150
pipe_x = WIDTH
pipe_height = random.randint(200, 400)
pipe_list = []

# 地面属性
base_x = 0
  
# 游戏循环
clock = pygame.time.Clock()
running = True
score = 0

def create_pipe():
    random_pipe_pos = random.randint(200, 400)
    bottom_pipe = pipe_image.get_rect(midtop=(WIDTH + 50, random_pipe_pos))
    top_pipe = pipe_image.get_rect(midbottom=(WIDTH + 50, random_pipe_pos - pipe_gap))
    return bottom_pipe, top_pipe

def move_pipes(pipes):
    for pipe in pipes:
        pipe.centerx -= 2
    return [pipe for pipe in pipes if pipe.right > -50]

def draw_pipes(pipes):
    for pipe in pipes:
        if pipe.bottom >= HEIGHT:
            screen.blit(pipe_image, pipe)
        else:
            flip_pipe = pygame.transform.flip(pipe_image, False, True)
            screen.blit(flip_pipe, pipe)

def check_collision(pipes):
    for pipe in pipes:
        if bird_rect.colliderect(pipe):
            return False
    if bird_rect.top <= -50 or bird_rect.bottom >= HEIGHT - 70:
        return False
    return True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_velocity = jump_strength

    # 更新鸟的位置
    bird_velocity += gravity
    bird_rect.centery += bird_velocity

    # 移动和创建管道
    pipe_list = move_pipes(pipe_list)
    if len(pipe_list) == 0 or pipe_list[-1].centerx < WIDTH - 150:
        pipe_list.extend(create_pipe())
        score += 1

    # 移动地面
    base_x -= 1
    if base_x <= -WIDTH:
        base_x = 0

    # 碰撞检测
    if not check_collision(pipe_list):
        running = False

    # 绘制
    screen.blit(background, (0, 0))
    draw_pipes(pipe_list)
    screen.blit(base_image, (base_x, HEIGHT - 70))
    screen.blit(base_image, (base_x + WIDTH, HEIGHT - 70))
    bird_index = (bird_index + 1) % 3
    bird_surface = bird_images[bird_index]
    bird_rotated = pygame.transform.rotate(bird_surface, -bird_velocity * 3)
    screen.blit(bird_rotated, bird_rect)

    # 显示分数
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"Score: {score}", True, (255, 255, 255))
    screen.blit(score_text, (10, 10))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

# background-day.png(背景图)

# yellowbird-downflap.png, yellowbird-midflap.png, yellowbird-upflap.png(鸟的三种状态)

# pipe-green.png(管道图)

# base.png(地面图)

# 这些图片放在游戏脚本同目录下的 "assets" 文件夹中。

运行结果:

其中图片可以自定更换,图片资源已上传,结合代码使用,否则会报错。

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值