"""Sky Hopper - a Pygame.net game project (a one-button flying game). Tap to flap and steer the bird through the gaps between the pillars. Every pillar you pass is a point; touching a pillar or the ground ends the run. Run it: pip install pygame-ce (or: pip install pygame) python flappy_style.py Controls: Space, Up or a mouse click to flap. Same key to restart. License: MIT - https://pygame.net/licenses/ """ import asyncio import random import pygame WIDTH, HEIGHT = 640, 480 FPS = 60 GROUND = HEIGHT - 50 GRAVITY = 1500 # pixels per second squared FLAP = -440 # upward speed after a flap PIPE_W, GAP = 70, 150 PIPE_SPEED = 170 PIPE_EVERY = 1.45 # seconds between pillars SKY = (186, 214, 247) PIPE = (72, 160, 100) PIPE_DARK = (48, 120, 72) GROUND_C = (222, 196, 130) BIRD = (244, 197, 49) INK = (21, 35, 63) class Bird: def __init__(self): self.x = 150 self.y = HEIGHT / 2 self.vy = 0.0 self.radius = 14 @property def rect(self): r = self.radius - 2 # forgiving hit box return pygame.Rect(self.x - r, self.y - r, r * 2, r * 2) def flap(self): self.vy = FLAP def update(self, dt): self.vy = min(self.vy + GRAVITY * dt, 700) self.y += self.vy * dt class Game: def __init__(self, seed=None): self.rng = random.Random(seed) self.font = pygame.font.Font(None, 72) self.small = pygame.font.Font(None, 30) self.best = 0 self.clouds = [(self.rng.randrange(WIDTH), self.rng.randrange(40, 220)) for _ in range(5)] self.reset() def reset(self): self.bird = Bird() self.pipes = [] # list of [x, gap_top, scored] self.timer = 0.0 self.score = 0 self.state = "ready" # ready, playing, over def add_pipe(self): gap_top = self.rng.randint(70, GROUND - GAP - 50) self.pipes.append([float(WIDTH), gap_top, False]) def pipe_rects(self, pipe): x, gap_top, _ = pipe top = pygame.Rect(round(x), 0, PIPE_W, gap_top) bottom = pygame.Rect(round(x), gap_top + GAP, PIPE_W, GROUND - gap_top - GAP) return top, bottom def press(self): if self.state == "ready": self.state = "playing" self.bird.flap() elif self.state == "playing": self.bird.flap() elif self.state == "over" and self.bird.y >= GROUND - self.bird.radius: self.reset() def handle_event(self, event): if event.type == pygame.KEYDOWN and event.key in (pygame.K_SPACE, pygame.K_UP, pygame.K_w): self.press() elif event.type == pygame.MOUSEBUTTONDOWN: self.press() def update(self, dt): if self.state == "ready": return self.bird.update(dt) if self.bird.y >= GROUND - self.bird.radius: self.bird.y = GROUND - self.bird.radius self.bird.vy = 0 self.end() if self.state != "playing": return self.timer += dt if self.timer >= PIPE_EVERY: self.timer -= PIPE_EVERY self.add_pipe() for pipe in self.pipes: pipe[0] -= PIPE_SPEED * dt if not pipe[2] and pipe[0] + PIPE_W < self.bird.x: pipe[2] = True self.score += 1 if any(self.bird.rect.colliderect(r) for r in self.pipe_rects(pipe)): self.end() self.pipes = [p for p in self.pipes if p[0] > -PIPE_W] if self.bird.y < -40: self.end() def end(self): if self.state == "playing": self.state = "over" self.best = max(self.best, self.score) def draw(self, surface): surface.fill(SKY) for cx, cy in self.clouds: pygame.draw.ellipse(surface, (232, 241, 252), (cx, cy, 90, 30)) pygame.draw.ellipse(surface, (232, 241, 252), (cx + 25, cy - 14, 60, 34)) for pipe in self.pipes: for r in self.pipe_rects(pipe): pygame.draw.rect(surface, PIPE, r) pygame.draw.rect(surface, PIPE_DARK, r, 3) top, bottom = self.pipe_rects(pipe) pygame.draw.rect(surface, PIPE_DARK, (top.x - 6, top.bottom - 22, PIPE_W + 12, 22)) pygame.draw.rect(surface, PIPE_DARK, (bottom.x - 6, bottom.top, PIPE_W + 12, 22)) pygame.draw.rect(surface, GROUND_C, (0, GROUND, WIDTH, HEIGHT - GROUND)) pygame.draw.line(surface, (160, 130, 70), (0, GROUND), (WIDTH, GROUND), 3) b = self.bird tilt = max(-25, min(70, b.vy / 8)) body = pygame.Surface((40, 32), pygame.SRCALPHA) pygame.draw.ellipse(body, BIRD, (4, 4, 30, 24)) pygame.draw.ellipse(body, (230, 160, 30), (6, 14, 16, 10)) # wing pygame.draw.circle(body, (255, 255, 255), (26, 12), 6) pygame.draw.circle(body, INK, (28, 12), 3) pygame.draw.polygon(body, (240, 120, 50), [(32, 16), (40, 19), (32, 22)]) rotated = pygame.transform.rotate(body, -tilt) surface.blit(rotated, rotated.get_rect(center=(b.x, round(b.y)))) score = self.font.render(str(self.score), True, (255, 255, 255)) surface.blit(score, score.get_rect(midtop=(WIDTH // 2, 24))) if self.state == "ready": t = self.small.render("Press Space or click to flap", True, INK) surface.blit(t, t.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 70))) elif self.state == "over": t = self.small.render(f"Score {self.score} Best {self.best} - Space to retry", True, INK) surface.blit(t, t.get_rect(center=(WIDTH // 2, HEIGHT // 2))) async def main(): pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Sky Hopper - Pygame.net") clock = pygame.time.Clock() game = Game() running = True while running: dt = min(clock.tick(FPS) / 1000, 0.03) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False else: game.handle_event(event) game.update(dt) game.draw(screen) pygame.display.flip() await asyncio.sleep(0) pygame.quit() if __name__ == "__main__": asyncio.run(main())