Build a GUI Mario game in python
- User
Build a GUI Mario game in python
- Assistant
Build a GUI Mario game in python
Building a full-scale Mario game is an exciting project, but it can be quite complex. To get you started, I'll guide you through creating a simple 2D platformer game in Python using the Pygame library. This game will feature a character that can move left and right and jump on platforms—core mechanics that resemble those in Mario games.
Prerequisites:
Basic understanding of Python programming.
Pygame library installed. You can install it using pip:
pip install pygame
Let's get started!
First, import the necessary modules and set up the game window.
import pygame
import sys
# Initialize Pygame
pygame.init()
# Window dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Create the game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Simple Platformer")
# Clock object to control the frame rate
clock = pygame.time.Clock()
We'll create a Player class to manage the player character.
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Create a simple surface as the player's image
self.image = pygame.Surface((50, 50))
self.image.fill((255, 0, 0)) # Red color
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 2
self.rect.y = SCREEN_HEIGHT - self.rect.height
# Player movement attributes
self.change_x = 0
self.change_y = 0
def update(self, platforms):
# Apply gravity
self.gravity()
# Move left/right
self.rect.x += self.change_x
# Check for horizontal collisions
self.collide(platforms, 'x')
# Move up/down
self.rect.y += self.change_y
# Check for vertical collisions
self.collide(platforms, 'y')
def gravity(self):
# Simulate gravity
self.change_y += 0.5 # Adjust gravity strength as needed
if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
self.change_y = 0
self.rect.y = SCREEN_HEIGHT - self.rect.height
def jump(self, platforms):
# Check if the player is on the ground or on a platform
self.rect.y += 2 # Move down a bit to check for collision
platform_hit_list = pygame.sprite.spritecollide(self, platforms, False)
self.rect.y -= 2 # Move back to original position
if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
self.change_y = -10 # Adjust jump strength as needed
def collide(self, platforms, direction):
if direction == 'x':
hits = pygame.sprite.spritecollide(self, platforms, False)
for platform in hits:
if self.change_x > 0:
self.rect.right = platform.rect.left
elif self.change_x < 0:
self.rect.left = platform.rect.right
elif direction == 'y':
hits = pygame.sprite.spritecollide(self, platforms, False)
for platform in hits:
if self.change_y > 0:
self.rect.bottom = platform.rect.top
self.change_y = 0
elif self.change_y < 0:
self.rect.top = platform.rect.bottom
self.change_y = 0
We'll create a Platform class for the player to stand on.
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
# Create a surface for the platform
self.image = pygame.Surface([width, height])
self.image.fill((0, 255, 0)) # Green color
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
Create sprite groups to manage updates and draws.
# Sprite groups
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
# Create the player instance
player = Player()
all_sprites.add(player)
# Create platforms
platform_list = [
Platform(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40), # Ground platform
Platform(200, 450, 100, 20),
Platform(400, 350, 100, 20),
Platform(600, 250, 100, 20),
]
for platform in platform_list:
platforms.add(platform)
all_sprites.add(platform)
Add event handling for player movement.
def handle_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.change_x = -5
elif event.key == pygame.K_RIGHT:
player.change_x = 5
elif event.key == pygame.K_SPACE:
player.jump(platforms)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and player.change_x < 0:
player.change_x = 0
elif event.key == pygame.K_RIGHT and player.change_x > 0:
player.change_x = 0
Put it all together in the main game loop.
# Main game loop
while True:
handle_events()
# Update sprites
all_sprites.update(platforms)
# Draw everything
screen.fill((135, 206, 250)) # Light blue background
all_sprites.draw(screen)
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(60)
Here's the full code for reference:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Window dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Create the game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Simple Platformer")
# Clock object to control the frame rate
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Create a simple surface as the player's image
self.image = pygame.Surface((50, 50))
self.image.fill((255, 0, 0)) # Red color
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 2
self.rect.y = SCREEN_HEIGHT - self.rect.height
# Player movement attributes
self.change_x = 0
self.change_y = 0
def update(self, platforms):
# Apply gravity
self.gravity()
# Move left/right
self.rect.x += self.change_x
# Check for horizontal collisions
self.collide(platforms, 'x')
# Move up/down
self.rect.y += self.change_y
# Check for vertical collisions
self.collide(platforms, 'y')
def gravity(self):
# Simulate gravity
self.change_y += 0.5 # Adjust gravity strength as needed
if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
self.change_y = 0
self.rect.y = SCREEN_HEIGHT - self.rect.height
def jump(self, platforms):
# Check if the player is on the ground or on a platform
self.rect.y += 2 # Move down a bit to check for collision
platform_hit_list = pygame.sprite.spritecollide(self, platforms, False)
self.rect.y -= 2 # Move back to original position
if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
self.change_y = -10 # Adjust jump strength as needed
def collide(self, platforms, direction):
if direction == 'x':
hits = pygame.sprite.spritecollide(self, platforms, False)
for platform in hits:
if self.change_x > 0:
self.rect.right = platform.rect.left
elif self.change_x < 0:
self.rect.left = platform.rect.right
elif direction == 'y':
hits = pygame.sprite.spritecollide(self, platforms, False)
for platform in hits:
if self.change_y > 0:
self.rect.bottom = platform.rect.top
self.change_y = 0
elif self.change_y < 0:
self.rect.top = platform.rect.bottom
self.change_y = 0
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
# Create a surface for the platform
self.image = pygame.Surface([width, height])
self.image.fill((0, 255, 0)) # Green color
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# Sprite groups
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
# Create the player instance
player = Player()
all_sprites.add(player)
# Create platforms
platform_list = [
Platform(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40), # Ground platform
Platform(200, 450, 100, 20),
Platform(400, 350, 100, 20),
Platform(600, 250, 100, 20),
]
for platform in platform_list:
platforms.add(platform)
all_sprites.add(platform)
def handle_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.change_x = -5
elif event.key == pygame.K_RIGHT:
player.change_x = 5
elif event.key == pygame.K_SPACE:
player.jump(platforms)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and player.change_x < 0:
player.change_x = 0
elif event.key == pygame.K_RIGHT and player.change_x > 0:
player.change_x = 0
# Main game loop
while True:
handle_events()
# Update sprites
all_sprites.update(platforms)
# Draw everything
screen.fill((135, 206, 250)) # Light blue background
all_sprites.draw(screen)
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(60)
Save the code in a file named simple_platformer.py and run it:
python simple_platformer.py
You should see a window with a red square that represents the player. You can move the player using the left and right arrow keys and make it jump with the spacebar.
Now that you have a basic platformer, you can enhance it by:
Feel free to ask if you have any questions or need further assistance with specific features!
Not yet included in a published release.