以下是一个简单的笨鸟先飞游戏代码示例:

import pygame
import random

# 初始化 Pygame 引擎
pygame.init()

# 设置游戏屏幕的大小
screen_width = 500
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("笨鸟先飞")

# 加载游戏背景图片
background_image = pygame.image.load("background.png").convert()

# 加载鸟的图片
bird_image = pygame.image.load("bird.png").convert_alpha()

# 加载管道的图片
pipe_image = pygame.image.load("pipe.png").convert_alpha()

# 定义鸟的初始位置和速度
bird_x = 50
bird_y = 250
bird_speed = 0

# 定义管道的初始位置和速度
pipe_x = screen_width
pipe_gap = 150
pipe_width = 50
pipe_speed = 5
pipe_height = random.randint(100, 400)

# 定义得分和字体
score = 0
font = pygame.font.SysFont(None, 30)

# 游戏主循环
while True:
    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_speed = -10

    # 更新鸟的位置和速度
    bird_speed += 1
    bird_y += bird_speed

    # 更新管道的位置
    pipe_x -= pipe_speed
    if pipe_x < -pipe_width:
        pipe_x = screen_width
        pipe_height = random.randint(100, 400)
        score += 1

    # 检测鸟是否碰到管道或撞到屏幕边缘
    if bird_y < 0 or bird_y > screen_height - bird_image.get_height():
        pygame.quit()
        exit()

    if bird_x + bird_image.get_width() > pipe_x and bird_x < pipe_x + pipe_width:
        if bird_y < pipe_height or bird_y + bird_image.get_height() > pipe_height + pipe_gap:
            pygame.quit()
            exit()

    # 绘制游戏元素
    screen.blit(background_image, (0, 0))
    screen.blit(bird_image, (bird_x, bird_y))
    screen.blit(pipe_image, (pipe_x, 0 - (screen_height - pipe_height - pipe_gap)))
    screen.blit(pipe_image, (pipe_x, pipe_height + pipe_gap))
    score_text = font.render("得分:" + str(score), True, (255, 255, 255))
    screen.blit(score_text, (10, 10))

    # 更新屏幕显示
    pygame.display.update()

运行该代码,即可开始玩笨鸟先飞游戏。玩家需要控制鸟的飞行高度,避免撞到管道或撞到屏幕边缘,同时尽可能地通过更多的管道,获得更高的得分

写一个笨鸟先飞的游戏代码

原文地址: https://www.cveoy.top/t/topic/doGB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录