import pygame
# Initialize the game
pygame.init()
# Set up the screen
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Super Mario Bros")
# Mario properties
mario_width, mario_height = 60, 95
mario_x, mario_y = 50, screen_height - mario_height - 10
mario_speed = 5
# Load Mario image
mario_img = pygame.image.load('mario.png')
def mario(x, y):
# Draw Mario on the screen
screen.blit(mario_img, (x, y))
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle key events
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and mario_x > mario_speed:
mario_x -= mario_speed
if keys[pygame.K_RIGHT] and mario_x < screen_width - mario_width - mario_speed:
mario_x += mario_speed
# Update the screen
screen.fill((0, 0, 0))
mario(mario_x, mario_y)
pygame.display.flip()
# Quit the game
pygame.quit()
Make sure to save an image named 'mario.png' in the same directory as the Python script file. You can replace this image with your own Mario sprite to customize the character's appearance. Note that this code only handles Mario's movement to the left and right. To create a full game, you would need to implement additional features such as enemies, power-ups, levels, and collision detection.