write the code for a game like super mario using pygame, the game should be fully…
- User
write the code for a game like super mario using pygame, the game should be fully playable with a start menu and controls, score, etc.
- Assistant
Creating a full Super Mario-like game is a complex task, but I can provide you with a simplified version using Pygame that includes a start menu, basic gameplay mechanics (jumping, moving, collecting coins), enemies, a score system, and a game over screen. This example will give you a solid foundation to build upon.
Prerequisites
- Install Pygame if you haven't already:
pip install pygame
Game Features
- Start menu with "Start" and "Quit" options.
- Player movement (left, right, jump).
- Collectible coins to increase the score.
- Basic enemies that move horizontally.
- Collision detection (player with coins and enemies).
- Game over screen when the player collides with an enemy.
- Score display during gameplay.
Code
Below is the complete code for the game:
import pygame import sys import random # Initialize Pygame pygame.init() # Constants SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 GRAVITY = 0.8 JUMP_FORCE = -15 PLAYER_SPEED = 5 ENEMY_SPEED = 3 COIN_SIZE = 30 ENEMY_SIZE = 40 PLAYER_SIZE = 50 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) # Screen setup screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Super Mario Clone") clock = pygame.time.Clock() # Font font = pygame.font.SysFont(None, 50) # Player class class Player(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((PLAYER_SIZE, PLAYER_SIZE)) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.velocity_y = 0 self.on_ground = False def update(self): # Apply gravity self.velocity_y += GRAVITY self.rect.y += self.velocity_y # Keep player above ground if self.rect.bottom > SCREEN_HEIGHT - 50: self.rect.bottom = SCREEN_HEIGHT - 50 self.velocity_y = 0 self.on_ground = True def jump(self): if self.on_ground: self.velocity_y = JUMP_FORCE self.on_ground = False def move(self, dx): self.rect.x += dx # Coin class class Coin(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((COIN_SIZE, COIN_SIZE)) self.image.fill(YELLOW) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Enemy class class Enemy(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((ENEMY_SIZE, ENEMY_SIZE)) self.image.fill(GREEN) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.direction = 1 def update(self): self.rect.x += self.direction * ENEMY_SPEED if self.rect.left < 0 or self.rect.right > SCREEN_WIDTH: self.direction *= -1 # Start menu def start_menu(): while True: screen.fill(BLACK) title_text = font.render("Super Mario Clone", True, WHITE) start_text = font.render("Press S to Start", True, WHITE) quit_text = font.render("Press Q to Quit", True, WHITE) screen.blit(title_text, (SCREEN_WIDTH // 2 - title_text.get_width() // 2, 200)) screen.blit(start_text, (SCREEN_WIDTH // 2 - start_text.get_width() // 2, 300)) screen.blit(quit_text, (SCREEN_WIDTH // 2 - quit_text.get_width() // 2, 400)) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_s: return if event.key == pygame.K_q: pygame.quit() sys.exit() # Game over screen def game_over(score): while True: screen.fill(BLACK) game_over_text = font.render("Game Over!", True, WHITE) score_text = font.render(f"Score: {score}", True, WHITE) restart_text = font.render("Press R to Restart", True, WHITE) quit_text = font.render("Press Q to Quit", True, WHITE) screen.blit(game_over_text, (SCREEN_WIDTH // 2 - game_over_text.get_width() // 2, 200)) screen.blit(score_text, (SCREEN_WIDTH // 2 - score_text.get_width() // 2, 300)) screen.blit(restart_text, (SCREEN_WIDTH // 2 - restart_text.get_width() // 2, 400)) screen.blit(quit_text, (SCREEN_WIDTH // 2 - quit_text.get_width() // 2, 500)) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: return True if event.key == pygame.K_q: pygame.quit() sys.exit() # Main game loop def main_game(): # Sprite groups all_sprites = pygame.sprite.Group() coins = pygame.sprite.Group() enemies = pygame.sprite.Group() # Create player player = Player(100, SCREEN_HEIGHT - 100) all_sprites.add(player) # Create coins for _ in range(5): coin = Coin(random.randint(0, SCREEN_WIDTH - COIN_SIZE), random.randint(100, SCREEN_HEIGHT - 150)) all_sprites.add(coin) coins.add(coin) # Create enemies for _ in range(3): enemy = Enemy(random.randint(0, SCREEN_WIDTH - ENEMY_SIZE), SCREEN_HEIGHT - 90) all_sprites.add(enemy) enemies.add(enemy) # Score score = 0 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Player controls keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.move(-PLAYER_SPEED) if keys[pygame.K_RIGHT]: player.move(PLAYER_SPEED) if keys[pygame.K_SPACE]: player.jump() # Update all_sprites.update() # Check for coin collection collected_coins = pygame.sprite.spritecollide(player, coins, True) score += len(collected_coins) * 10 # Check for enemy collision if pygame.sprite.spritecollide(player, enemies, False): if game_over(score): return True # Restart game # Draw screen.fill(BLACK) pygame.draw.rect(screen, WHITE, (0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)) # Ground all_sprites.draw(screen) # Draw score score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) pygame.display.flip() clock.tick(60) # Game loop while True: start_menu() if not main_game(): break pygame.quit() sys.exit()Explanation of the Code
-
Initialization:
- Pygame is initialized, and constants for the game (e.g., screen size, gravity, speeds) are defined.
- Colors, fonts, and the game window are set up.
-
Classes:
Player: Handles player movement, jumping, and gravity.Coin: Represents collectible coins.Enemy: Represents enemies that move horizontally and bounce off screen edges.
-
Start Menu:
- Displays a simple menu with "Start" and "Quit" options.
- Pressing "S" starts the game, and "Q" quits.
-
Main Game Loop:
- Handles player input (left, right, jump).
- Updates all sprites (player, coins, enemies).
- Checks for collisions (player with coins or enemies).
- Displays the score.
-
Game Over Screen:
- Shown when the player collides with an enemy.
- Displays the final score and options to restart or quit.
-
Game Mechanics:
- The player can move left/right and jump.
- Coins increase the score when collected.
- Enemies move back and forth, and colliding with them ends the game.
How to Run
- Save the code in a
.pyfile (e.g.,mario_clone.py). - Run the file using Python:
python mario_clone.py
Controls
- Left Arrow: Move left
- Right Arrow: Move right
- Spacebar: Jump
- S: Start game (from start menu)
- Q: Quit game (from start menu or game over screen)
- R: Restart game (from game over screen)
Enhancements
This is a basic version of a Mario-like game. To make it more like Super Mario, you could add:
- Levels with platforms, pipes, and blocks.
- Power-ups (e.g., mushrooms, fire flowers).
- Better graphics (use sprites instead of colored rectangles).
- Sound effects and background music.
- More complex enemy AI.
- Scrolling background for larger levels.
Let me know if you'd like help implementing any of these enhancements!
- Install Pygame if you haven't already:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.