代码积木

代码小计

用以校验代码问题,防止翻车
完整功能代码移步下一随笔

main.py

from screen import Game

if __name__ == '__main__':
    game = Game()
    game.run()


screen.py

import pygame
import sys
import tool
from sprite import Bird, Pipe
import random

class Game():
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((tool.WIDTH, tool.HEIGHT))
        pygame.display.set_caption("Flappy Bird")
        self.clock = pygame.time.Clock()
        self.running = True

        self.all_sprite = pygame.sprite.Group()
        self.pipe_group = pygame.sprite.Group()

        self.bird = Bird()
        self.all_sprite.add(self.bird)

        self.spawn_timer = 0
    
    def spawn_pipe(self):
        gap_y = random.randint(80, tool.HEIGHT - tool.PIPE_GAP - 80)
        top = Pipe(tool.WIDTH, 0, tool.PIPE_WIDTH, gap_y, is_top=True)
        bottom = Pipe(tool.WIDTH, gap_y + tool.PIPE_GAP, tool.PIPE_WIDTH, tool.HEIGHT - (gap_y + tool.PIPE_GAP))
        self.all_sprite.add(top, bottom)
        self.pipe_group.add(top, bottom)

    def update(self):
        self.spawn_timer += 1
        if self.spawn_timer > tool.PIPE_SPAWN_INTERVAL:
            self.spawn_timer = 0
            self.spawn_pipe()
        
        self.all_sprite.update()

        for pipe in list(self.pipe_group):
            if pipe.off_screen():
                pipe.kill()

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.bird.jump()
    
    def draw(self):
        self.screen.fill(tool.SKY_COLOR)
        self.all_sprite.draw(self.screen)
        pygame.display.flip()
    
    def run(self):
        while self.running:
            self.handle_events()
            self.update()
            self.draw()
            self.clock.tick(tool.FPS)
        pygame.quit()
        sys.exit()

sprite.py

import pygame
import tool

class Bird(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super().__init__(*groups)
        self.image = pygame.Surface((tool.BIRD_WIDTH, tool.BIRD_HEIGHT))
        self.image.fill(tool.BIRD_COLOR)
        self.rect = self.image.get_rect()
        self.rect.x = tool.BIRD_X
        self.rect.y = tool.HEIGHT // 2

        self.velocity = 0
    
    def jump(self):
        self.velocity = tool.JUMP_STRENGTH
    
    def update(self):
        self.velocity += tool.GRAVITY
        self.rect.y += self.velocity
        if self.rect.top <0:
            self.rect.top = 0
            self.velocity = 0
        if self.rect.bottom > tool.HEIGHT:
            self.rect.bottom = tool.HEIGHT
            return True
        return False
    
    def reset(self):
        self.rect.y = tool.HEIGHT // 2
        self.velocity = 0

class Pipe(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, is_top=False):
        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill(tool.PIPE_COLOR)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.is_top = is_top
        self.scored = False
    
    def update(self, *args, **kwargs):
        self.rect.x -= tool.PIPE_SPEED
    
    def off_screen(self):
        return self.rect.right < 0



tool.py

WIDTH = 360
HEIGHT = 640
FPS = 60
SKY_COLOR = (135, 206, 235)

BIRD_X = 80
BIRD_WIDTH = 30
BIRD_HEIGHT = 30
BIRD_COLOR = (255, 255, 0)

GRAVITY = 0.5
JUMP_STRENGTH = -8

PIPE_WIDTH = 70
PIPE_GAP = 180
PIPE_SPEED = 4
PIPE_SPAWN_INTERVAL = 90
PIPE_COLOR = (0, 200, 0)
posted on 2026-09-14 02:17  凌晨码字  阅读(11)  评论(0)    收藏  举报