飞扬的小鸟游戏代码(可粘贴)

话不多说,直接上代码


import sys
import random,time

import pygame

# FPS
FPS = 30
# 屏幕宽高
SCREEN_WIDTH = 288
SCREEN_HEIGHT = 512
# 管道宽高
PIPE_WIDTH = 50
PIPE_HEIGHT = 300
# 管道之间空隙
PIPE_GAP_SIZE = 100
# 小鸟
BIRD_WIDTH = 20
BIRD_HEIGHT = 20
# 地面高度
FLOOR_HEIGHT = 80
# 游戏有效高度
BASE_HEIGHT = SCREEN_HEIGHT - FLOOR_HEIGHT


class Bird(pygame.sprite.Sprite):
    def __init__(self, position):
        pygame.sprite.Sprite.__init__(self)
        self.rect = pygame.Rect(*position, BIRD_WIDTH, BIRD_HEIGHT)
        # 定义飞行变量
        self.is_flapped = False
        self.up_speed = 10
        self.down_speed = 0

        self.time_pass = FPS / 1000

    # 更新小鸟的位置
    def update(self):
        # 判断小鸟是上升还是下降
        if self.is_flapped:
            # 上升速度越来越小
            self.up_speed -= 60 * self.time_pass
            self.rect.top -= self.up_speed
            # 上升速度小于等于0, 改为下降状态
            if self.up_speed <= 0:
                self.down()
                self.up_speed = 10
                self.down_speed = 0
        else:
            # 下降速度越来越大
            self.down_speed += 30 * self.time_pass
            self.rect.bottom += self.down_speed

        # 判断小鸟是否撞到了边界死亡
        is_dead = False
        if self.rect.top <= 0:  # 上边界
            self.up_speed = 0
            self.rect.top = 0
            is_dead = True

        if self.rect.bottom >= BASE_HEIGHT:  # 下边界
            self.up_speed = 0
            self.down_speed = 0
            self.rect.bottom = BASE_HEIGHT
            is_dead = True

        return is_dead

    # 下落状态
    def down(self):
        self.is_flapped = False

    # 上升状态
    def up(self):
        if self.is_flapped:
            self.up_speed = max(12, self.up_speed + 1)
        else:
            self.is_flapped = True

    def draw(self, screen):
        pygame.draw.rect(screen, (255, 255, 255), self.rect, 1)
class Pipe(pygame.sprite.Sprite):
    def __init__(self, position):
        pygame.sprite.Sprite.__init__(self)
        left, top = position
        # 如果是下边的管道, 通过定义管道高度, 删除地面以下的管道
        pipe_height = PIPE_HEIGHT
        if top > 0:
            pipe_height = BASE_HEIGHT - top + 1
        self.rect = pygame.Rect(left, top, PIPE_WIDTH, pipe_height)
        # 用于计算分数
        self.used_for_score = False

    def draw(self, screen):
        pygame.draw.rect(screen, (255, 255, 255), self.rect, 1)

    @staticmethod
    def generate_pipe_position():
        # 生成上下两个管道的坐标
        top = int(BASE_HEIGHT * 0.2) + random.randrange(
            0, int(BASE_HEIGHT * 0.6 - PIPE_GAP_SIZE))
        return {
            'top': (SCREEN_WIDTH + 25, top - PIPE_HEIGHT),
            'bottom': (SCREEN_WIDTH + 25, top + PIPE_GAP_SIZE)
        }
# 初始化游戏
def init_game():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('Flappy Bird')
    return screen


# 初始化精灵
def init_sprite():
    # 小鸟类
    bird_position = [SCREEN_WIDTH * 0.2, (SCREEN_HEIGHT - BIRD_HEIGHT) / 3]
    bird = Bird(bird_position)
    # 管道类
    pipe_sprites = pygame.sprite.Group()
    for i in range(2):
        pipe_pos = Pipe.generate_pipe_position()
        # 添加上方的管道
        pipe_sprites.add(
            Pipe((SCREEN_WIDTH + i * SCREEN_WIDTH / 2,
                  pipe_pos.get('top')[-1])))
        # 添加下方的管道
        pipe_sprites.add(
            Pipe((SCREEN_WIDTH + i * SCREEN_WIDTH / 2,
                  pipe_pos.get('bottom')[-1])))
    return bird, pipe_sprites
# 精灵类碰撞检测和小鸟更新位置
def collision(bird, pipe_sprites):
    # 检测碰撞
    is_collision = False
    for pipe in pipe_sprites:
        if pygame.sprite.collide_rect(bird, pipe):
            is_collision = True

    # 更新小鸟
    is_dead = bird.update()
    if is_dead:
        is_collision = True

    return is_collision


# 移动pipe实现小鸟往前飞的效果
def move_pipe(bird, pipe_sprites, is_add_pipe, score):
    flag = False  # 下一次是否要增加新的pipe的标志位
    for pipe in pipe_sprites:
        pipe.rect.left -= 4
        # 小鸟飞过pipe 加分
        if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:
            pipe.used_for_score = True
            score += 0.5
        # 增加新的pipe
        if pipe.rect.left < 10 and pipe.rect.left > 0 and is_add_pipe:
            pipe_pos = Pipe.generate_pipe_position()
            pipe_sprites.add(Pipe(position=pipe_pos.get('top')))
            pipe_sprites.add(Pipe(position=pipe_pos.get('bottom')))
            is_add_pipe = False
        # 删除已不在屏幕的pipe, 更新标志位
        elif pipe.rect.right < 0:
            pipe_sprites.remove(pipe)
            flag = True
    if flag:
        is_add_pipe = True
    return is_add_pipe, score
# 画分数
def draw_score(screen, score):
    font_size = 32
    digits = len(str(int(score)))
    offset = (SCREEN_WIDTH - digits * font_size) / 2
    font = pygame.font.SysFont('Blod', font_size)
    screen.blit(font.render(str(int(score)), True, (255, 255, 255)),
                (offset, SCREEN_HEIGHT * 0.1))


# 画Game Over
def draw_game_over(screen, text):
    font_size = 24
    font = pygame.font.SysFont('arial', font_size)
    screen.blit(font.render(text, True, (255, 255, 255), (0, 0, 0)),
                (60, SCREEN_HEIGHT * 0.4))


# 按键
def press(is_game_running, bird):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 点击关闭按钮退出
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE or event.key == pygame.K_UP:  # 空格键或者up键小鸟上升
                if is_game_running:
                    bird.up()
            elif event.key == 13 and not is_game_running:  # 游戏结束时回车键继续
                return True
def main():
    screen = init_game()  # 初始化游戏
    bird, pipe_sprites = init_sprite()  # 初始化精灵
    clock = pygame.time.Clock()
    is_add_pipe = True  # 是否需要增加管道
    is_game_running = True  # 是否在游戏中
    score = 0  # 初始分数
    while True:
        restart = press(is_game_running, bird)  # 按键
        if restart:
            return
        screen.fill((0, 0, 0))  # 填充背景
        is_collision = collision(bird, pipe_sprites)  # 碰撞检测
        if is_collision:
            is_game_running = False  # 如果碰撞 游戏结束
        if is_game_running:
            is_add_pipe, score = move_pipe(bird, pipe_sprites, is_add_pipe,
                                           score)  # 不碰撞 移动管道
        else:
            draw_game_over(screen, 'Press Enter To Start!')  # 游戏结束
        bird.draw(screen)  # 画鸟
        draw_score(screen, score)  # 画分数
        # 画地面
        pygame.draw.line(screen, (255, 255, 255), (0, BASE_HEIGHT),
                         (SCREEN_WIDTH, BASE_HEIGHT))
        # 画管道
        for pipe in pipe_sprites:
            pipe.draw(screen)

        # 更新画布
        pygame.display.update()
        clock.tick(FPS)


if __name__ == "__main__":
    while True:
        main()            
            
 

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的飞扬小鸟游戏代码,使用了pgzero库: ```python import pgzrun import random WIDTH = 400 HEIGHT = 708 bird = Actor('bird1', (75, 350)) bird.dead = False bird.score = 0 bird.vy = 0 bird.gravity = 0.25 bird.flap = -4.6 pipe_gap = 140 pipe_frequency = 1200 last_pipe = pipe_frequency pipes = [] def update_pipes(): global pipes, last_pipe for pipe in pipes: pipe.centerx -= 2 if pipe.right < 0: pipes.remove(pipe) if last_pipe > pipe_frequency: size = random.randint(50, 220) bottom_pipe = Actor('bottom', (WIDTH, size + pipe_gap + 80)) top_pipe = Actor('top', (WIDTH, size - 80)) pipes.append(bottom_pipe) pipes.append(top_pipe) last_pipe = 0 last_pipe += 1 def update_bird(): global bird bird.vy += bird.gravity bird.y += bird.vy if bird.y > HEIGHT: bird.dead = True bird.y = HEIGHT def update(): global bird if not bird.dead: update_pipes() update_bird() def on_key_down(): global bird if not bird.dead: bird.vy = bird.flap def draw(): screen.blit('background', (0,0)) for pipe in pipes: if pipe.image == 'bottom': pipe.draw() else: pipe.draw() bird.draw() screen.draw.text( str(bird.score), color='white', midtop=(WIDTH//2, 10), fontsize=70 ) if bird.dead: screen.draw.text( 'Game Over', color='white', center=(WIDTH//2, HEIGHT//2), fontsize=50 ) def check_collisions(): global bird if not bird.dead: for pipe in pipes: if pipe.colliderect(bird): bird.dead = True return if bird.y > HEIGHT: bird.dead = True return if bird.top < 0: bird.top = 0 elif bird.bottom > HEIGHT: bird.bottom = HEIGHT bird.score = len([p for p in pipes if p.right < bird.x]) def game_loop(): while True: check_collisions() draw() if keyboard.RETURN: reset() def reset(): global bird, pipes, last_pipe bird = Actor('bird1', (75, 350)) bird.dead = False bird.score = 0 bird.vy = 0 bird.gravity = 0.25 bird.flap = -4.6 pipes = [] last_pipe = pipe_frequency game_loop() ``` 运行以上代码,即可开始游戏。玩家需要控制小鸟跳过障碍物,并在屏幕上方积累分数。当小鸟碰到障碍物或触底时,游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值