Python Super Mario Example
Criado em: 10 de janeiro de 2025
Criado em: 10 de janeiro de 2025
Edging into Pygame setup
Piecing together a Python script for a minimal Super Mario game prototype, focusing on setting up Pygame, creating a Mario sprite, and handling basic movements with a background.
Crafting a minimal side-scrolling game
Mapping out a brief Python script using Pygame, focusing on a side-scrolling platformer. It will handle sprite animation, character movement, and collisions, while clearly stating this is an educational prototype.
Thought about Python Super Mario code for 7 seconds
Below is a simple example of how you might start coding a basic “Super Mario–style” platformer in Python using the Pygame library. This code is not an official reproduction of Super Mario Brothers and does not contain Nintendo assets; it’s simply a small sample project that demonstrates character movement, gravity, jumping, and collisions with the ground. You can expand on this to add more features such as enemies, level design, scoring, etc.
pip install pygame
).You will need a few image assets to test this properly:
background.png
) – or you can use a solid color.mario.png
) with transparent background.your_project/
main.py
background.png (optional)
mario.png (required)
(Feel free to replace mario.png
with any character placeholder image if you’d like.)
import pygame import sys # Initialize Pygame pygame.init() # --- Game Constants --- SCREEN_WIDTH = 800 SCREEN_HEIGHT = 450 FPS = 60 # Colors (if you don't want to load a background image) BLUE = (135, 206, 235) # Sky blue # Set up the display screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Super Mario-Style Demo") clock = pygame.time.Clock() # --- Load Assets --- try: background = pygame.image.load("background.png").convert() except: # If no background image is found, we will fill the screen with a solid color background = None # Load the player image player_image = pygame.image.load("mario.png").convert_alpha() # --- Player Settings --- player_x = 50 player_y = SCREEN_HEIGHT - 80 # Roughly on the "ground" player_speed = 5 player_width = player_image.get_width() player_height = player_image.get_height() # For jumping and gravity gravity = 0.5 player_velocity_y = 0 jump_force = -10 on_ground = False # Ground level (simple one-line ground for demonstration) GROUND_LEVEL = SCREEN_HEIGHT - 40 # Main game loop running = True while running: clock.tick(FPS) # --- Event Handling --- for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() # --- Key Presses --- keys = pygame.key.get_pressed() # Left-Right movement if keys[pygame.K_LEFT]: player_x -= player_speed if keys[pygame.K_RIGHT]: player_x += player_speed # Jumping if keys[pygame.K_SPACE] and on_ground: player_velocity_y = jump_force on_ground = False # --- Gravity & Vertical Movement --- player_velocity_y += gravity player_y += player_velocity_y # --- Collision with Ground --- if player_y + player_height > GROUND_LEVEL: player_y = GROUND_LEVEL - player_height player_velocity_y = 0 on_ground = True # --- Drawing --- # Draw background if background: screen.blit(background, (0, 0)) else: screen.fill(BLUE) # Sky blue as a fallback # Draw a simple "ground" pygame.draw.rect(screen, (0, 128, 0), (0, GROUND_LEVEL, SCREEN_WIDTH, SCREEN_HEIGHT - GROUND_LEVEL)) # Draw the player screen.blit(player_image, (player_x, player_y)) # Update the display pygame.display.update() # Clean up pygame.quit()
Initialization:
Loading Assets:
background.png
). If it fails (file not found), we fill the screen with a solid sky-blue color.mario.png
).Player Variables:
player_x
and player_y
control the character’s position.player_speed
is how fast the character moves horizontally.player_velocity_y
is used for vertical movement and jumping.Game Loop:
Drawing:
Remember, this is just a skeleton code to get you started. Building a full-fledged platformer takes additional work in sprite management, collision detection, animations, level design, and so on. Have fun experimenting!