|
| 1 | +import pygame |
| 2 | +import random |
| 3 | + |
| 4 | +# Initialize Pygame |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +# Define constants |
| 8 | +WIDTH, HEIGHT = 800, 600 |
| 9 | +SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 10 | +pygame.display.set_caption("2D Platformer with Coins") |
| 11 | + |
| 12 | +WHITE = (255, 255, 255) |
| 13 | +BLACK = (0, 0, 0) |
| 14 | +GREEN = (0, 255, 0) |
| 15 | +BLUE = (0, 0, 255) |
| 16 | +YELLOW = (255, 255, 0) |
| 17 | + |
| 18 | +FPS = 60 |
| 19 | +GRAVITY = 1 |
| 20 | +PLATFORM_COUNT = 6 |
| 21 | + |
| 22 | +# Player class |
| 23 | +class Player(pygame.sprite.Sprite): |
| 24 | + def __init__(self): |
| 25 | + super().__init__() |
| 26 | + self.image = pygame.Surface((50, 50)) |
| 27 | + self.image.fill(BLUE) |
| 28 | + self.rect = self.image.get_rect() |
| 29 | + self.rect.center = (WIDTH // 2, HEIGHT - 100) |
| 30 | + self.velocity_y = 0 |
| 31 | + self.jumping = False |
| 32 | + |
| 33 | + def update(self): |
| 34 | + # Horizontal movement |
| 35 | + keys = pygame.key.get_pressed() |
| 36 | + if keys[pygame.K_LEFT]: |
| 37 | + self.rect.x -= 5 |
| 38 | + if keys[pygame.K_RIGHT]: |
| 39 | + self.rect.x += 5 |
| 40 | + |
| 41 | + # Gravity |
| 42 | + self.velocity_y += GRAVITY |
| 43 | + self.rect.y += self.velocity_y |
| 44 | + |
| 45 | + # Platform collision |
| 46 | + for platform in platforms: |
| 47 | + if self.rect.colliderect(platform.rect) and self.velocity_y > 0: |
| 48 | + self.rect.bottom = platform.rect.top |
| 49 | + self.velocity_y = 0 |
| 50 | + self.jumping = False |
| 51 | + |
| 52 | + # Keep player on screen |
| 53 | + if self.rect.right > WIDTH: |
| 54 | + self.rect.right = WIDTH |
| 55 | + if self.rect.left < 0: |
| 56 | + self.rect.left = 0 |
| 57 | + |
| 58 | + def jump(self): |
| 59 | + if not self.jumping: |
| 60 | + self.velocity_y = -15 |
| 61 | + self.jumping = True |
| 62 | + |
| 63 | +# Platform class |
| 64 | +class Platform(pygame.sprite.Sprite): |
| 65 | + def __init__(self, x, y, width, height): |
| 66 | + super().__init__() |
| 67 | + self.image = pygame.Surface((width, height)) |
| 68 | + self.image.fill(GREEN) |
| 69 | + self.rect = self.image.get_rect() |
| 70 | + self.rect.x = x |
| 71 | + self.rect.y = y |
| 72 | + |
| 73 | +# Coin class |
| 74 | +class Coin(pygame.sprite.Sprite): |
| 75 | + def __init__(self, platform): |
| 76 | + super().__init__() |
| 77 | + self.image = pygame.Surface((20, 20)) |
| 78 | + self.image.fill(YELLOW) |
| 79 | + self.rect = self.image.get_rect() |
| 80 | + self.rect.center = (platform.rect.x + platform.rect.width // 2, platform.rect.y - 10) |
| 81 | + |
| 82 | +# Score display |
| 83 | +def draw_text(text, size, color, x, y): |
| 84 | + font = pygame.font.Font(None, size) |
| 85 | + text_surface = font.render(text, True, color) |
| 86 | + SCREEN.blit(text_surface, (x, y)) |
| 87 | + |
| 88 | +# Main game loop |
| 89 | +def main(): |
| 90 | + running = True |
| 91 | + clock = pygame.time.Clock() |
| 92 | + score = 0 |
| 93 | + |
| 94 | + # Create player |
| 95 | + global player, platforms |
| 96 | + player = Player() |
| 97 | + |
| 98 | + # Create platforms and place coins |
| 99 | + platforms = pygame.sprite.Group() |
| 100 | + coins = pygame.sprite.Group() |
| 101 | + |
| 102 | + platform_height = HEIGHT - 50 |
| 103 | + for i in range(PLATFORM_COUNT): |
| 104 | + platform = Platform(random.randint(0, WIDTH - 150), platform_height, 150, 20) |
| 105 | + platforms.add(platform) |
| 106 | + coin = Coin(platform) |
| 107 | + coins.add(coin) |
| 108 | + platform_height -= random.randint(80, 120) |
| 109 | + |
| 110 | + all_sprites = pygame.sprite.Group() |
| 111 | + all_sprites.add(player) |
| 112 | + all_sprites.add(platforms) |
| 113 | + all_sprites.add(coins) |
| 114 | + |
| 115 | + while running: |
| 116 | + clock.tick(FPS) |
| 117 | + SCREEN.fill(WHITE) |
| 118 | + |
| 119 | + for event in pygame.event.get(): |
| 120 | + if event.type == pygame.QUIT: |
| 121 | + running = False |
| 122 | + |
| 123 | + if event.type == pygame.KEYDOWN: |
| 124 | + if event.key == pygame.K_SPACE: |
| 125 | + player.jump() |
| 126 | + |
| 127 | + # Update player movement |
| 128 | + all_sprites.update() |
| 129 | + |
| 130 | + # Coin collection logic |
| 131 | + coins_collected = pygame.sprite.spritecollide(player, coins, True) |
| 132 | + if coins_collected: |
| 133 | + score += len(coins_collected) |
| 134 | + |
| 135 | + # Draw everything |
| 136 | + all_sprites.draw(SCREEN) |
| 137 | + draw_text(f"Score: {score}", 36, BLACK, 10, 10) |
| 138 | + |
| 139 | + # Update the screen |
| 140 | + pygame.display.flip() |
| 141 | + |
| 142 | + pygame.quit() |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + main() |
0 commit comments