Here's a simplified version of a Super Mario-like game using Pygame. This game includes a start menu, player movement, enemies, power-ups, and scoring.
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 800, 600
PLAYER_SIZE = 50
ENEMY_SIZE = 50
POWER_UP_SIZE = 20
GRAVITY = 1
JUMP_HEIGHT = 20
# Set up some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Set up the display
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
# Set up the font
FONT = pygame.font.Font(None, 36)
# Set up the clock
CLOCK = pygame.time.Clock()
class Player(pygame.Rect):
def __init__(self):
super().__init__(WIDTH / 2, HEIGHT / 2, PLAYER_SIZE, PLAYER_SIZE)
self.vy = 0
self.on_ground = False
self.score = 0
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x -= 5
if keys[pygame.K_RIGHT]:
self.x += 5
if keys[pygame.K_SPACE] and self.on_ground:
self.vy = -JUMP_HEIGHT
self.on_ground = False
self.vy += GRAVITY
self.y += self.vy
if self.y + self.height > HEIGHT:
self.y = HEIGHT - self.height
self.vy = 0
self.on_ground = True
class Enemy(pygame.Rect):
def __init__(self):
super().__init__(random.randint(0, WIDTH - ENEMY_SIZE), random.randint(0, HEIGHT - ENEMY_SIZE), ENEMY_SIZE, ENEMY_SIZE)
self.vy = random.randint(-5, 5)
def move(self):
self.y += self.vy
if self.y + self.height > HEIGHT or self.y < 0:
self.vy = -self.vy
class PowerUp(pygame.Rect):
def __init__(self):
super().__init__(random.randint(0, WIDTH - POWER_UP_SIZE), random.randint(0, HEIGHT - POWER_UP_SIZE), POWER_UP_SIZE, POWER_UP_SIZE)
def draw_text(text, x, y):
text_surface = FONT.render(text, True, WHITE)
SCREEN.blit(text_surface, (x, y))
def main():
player = Player()
enemies = [Enemy() for _ in range(10)]
power_ups = [PowerUp() for _ in range(5)]
start_menu = True
while start_menu:
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_SPACE:
start_menu = False
SCREEN.fill((0, 0, 0))
draw_text("Press Space to start", WIDTH / 2 - 100, HEIGHT / 2)
pygame.display.flip()
CLOCK.tick(60)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
SCREEN.fill((0, 0, 0))
player.move()
for enemy in enemies:
enemy.move()
if player.colliderect(enemy):
player.score -= 10
pygame.draw.rect(SCREEN, RED, enemy)
for power_up in power_ups:
if player.colliderect(power_up):
player.score += 10
power_ups.remove(power_up)
power_ups.append(PowerUp())
pygame.draw.rect(SCREEN, GREEN, power_up)
pygame.draw.rect(SCREEN, BLUE, player)
draw_text(f"Score: {player.score}", 10, 10)
pygame.display.flip()
CLOCK.tick(60)
if __name__ == "__main__":
main()
This code creates a window with a start menu. When you press Space, the game starts. You can move the player left and right with the arrow keys, and jump with the Space key. The player's score increases when they collect power-ups and decreases when they collide with enemies. The game continues until you close the window.
Note: This is a simplified version of a Super Mario-like game and does not include all the features of the original game. You can add more features, such as multiple levels, different types of enemies and power-ups, and a more complex scoring system, to make the game more interesting.