Untitled
unknown
plain_text
6 months ago
2.3 kB
4
Indexable
import pygame import random # Initialize Pygame pygame.init() # Constants SCREEN_WIDTH = 400 SCREEN_HEIGHT = 600 GRAVITY = 0.25 FLAP_STRENGTH = -6.5 PIPE_WIDTH = 70 PIPE_GAP = 150 # Colors WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Create screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) class Bird: def __init__(self): self.x = 50 self.y = SCREEN_HEIGHT // 2 self.velocity = 0 self.image = pygame.Surface((30, 30)) self.image.fill(BLUE) def flap(self): self.velocity = FLAP_STRENGTH def update(self): self.velocity += GRAVITY self.y += self.velocity if self.y > SCREEN_HEIGHT: self.y = SCREEN_HEIGHT self.velocity = 0 if self.y < 0: self.y = 0 class Pipe: def __init__(self): self.x = SCREEN_WIDTH self.height = random.randint(100, 400) self.passed = False def update(self): self.x -= 3 def draw(self): pygame.draw.rect(screen, GREEN, (self.x, 0, PIPE_WIDTH, self.height)) pygame.draw.rect(screen, GREEN, (self.x, self.height + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT)) def main(): clock = pygame.time.Clock() bird = Bird() pipes = [Pipe()] score = 0 running = True while running: screen.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: bird.flap() bird.update() for pipe in pipes: pipe.update() if pipe.x < bird.x < pipe.x + PIPE_WIDTH and not pipe.passed: if bird.y < pipe.height or bird.y + 30 > pipe.height + PIPE_GAP: running = False if bird.x > pipe.x + PIPE_WIDTH: pipe.passed = True score += 1 if pipes[-1].x < SCREEN_WIDTH // 2: pipes.append(Pipe()) for pipe in pipes: pipe.draw() screen.blit(bird.image, (bird.x, bird.y)) pygame.display.flip() # Corrected here clock.tick(60) pygame.quit() if __name__ == "__main__": main()
Editor is loading...
Leave a Comment