Untitled

 avatar
unknown
plain_text
a month ago
5.4 kB
1
Indexable
import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
GROUND_HEIGHT = 50
GRAVITY = 0.8
JUMP_STRENGTH = -15
MOVE_SPEED = 5
COIN_RADIUS = 10
GOOMBA_WIDTH = 40
GOOMBA_HEIGHT = 40
BOWSER_WIDTH = 80
BOWSER_HEIGHT = 100
SECRET_TUNNEL_COLOR = (255, 255, 0)  # Yellow for secret tunnels

# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Super Mario-like Game')

# Player setup
player_width = 50
player_height = 60
player_x = SCREEN_WIDTH // 2 - player_width // 2
player_y = SCREEN_HEIGHT - GROUND_HEIGHT - player_height
player_velocity_y = 0
player_velocity_x = 0
on_ground = False
player_health = 3
score = 0

# Coins setup
coins = []
num_coins = 5
coin_speed = 2

# Goombas setup
goombas = []
num_goombas = 3

# Bowser setup
bowser_x = SCREEN_WIDTH - 200
bowser_y = SCREEN_HEIGHT - GROUND_HEIGHT - BOWSER_HEIGHT
bowser_velocity = -3

# Tunnels (Secret Levels)
tunnels = [(200, SCREEN_HEIGHT - GROUND_HEIGHT - player_height, 50, 50)]
secret_level = False

# Clock for controlling frame rate
clock = pygame.time.Clock()

# Font for score and health
font = pygame.font.SysFont('Arial', 24)

# Game loop
while True:
    screen.fill(WHITE)  # Background color

    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Keyboard input for player movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_velocity_x = -MOVE_SPEED
    elif keys[pygame.K_RIGHT]:
        player_velocity_x = MOVE_SPEED
    else:
        player_velocity_x = 0

    if keys[pygame.K_SPACE] and on_ground:
        player_velocity_y = JUMP_STRENGTH
        on_ground = False

    # Update player position
    player_x += player_velocity_x
    player_y += player_velocity_y

    # Simple gravity effect
    if player_y < SCREEN_HEIGHT - GROUND_HEIGHT - player_height:
        player_velocity_y += GRAVITY
        on_ground = False
    else:
        player_y = SCREEN_HEIGHT - GROUND_HEIGHT - player_height
        player_velocity_y = 0
        on_ground = True

    # Check for coin collision
    for coin in coins[:]:
        if player_x < coin[0] + COIN_RADIUS and player_x + player_width > coin[0] and \
           player_y < coin[1] + COIN_RADIUS and player_y + player_height > coin[1]:
            coins.remove(coin)
            score += 10

    # Check for Goomba collision
    for goomba in goombas[:]:
        if player_x < goomba[0] + GOOMBA_WIDTH and player_x + player_width > goomba[0] and \
           player_y < goomba[1] + GOOMBA_HEIGHT and player_y + player_height > goomba[1]:
            player_health -= 1
            goombas.remove(goomba)
            if player_health <= 0:
                pygame.quit()
                sys.exit()

    # Bowser collision (player must defeat him)
    if player_x < bowser_x + BOWSER_WIDTH and player_x + player_width > bowser_x and \
       player_y < bowser_y + BOWSER_HEIGHT and player_y + player_height > bowser_y:
        if not secret_level:
            player_health -= 1
            if player_health <= 0:
                pygame.quit()
                sys.exit()

    # Tunnels logic
    for tunnel in tunnels:
        if player_x < tunnel[0] + tunnel[2] and player_x + player_width > tunnel[0] and \
           player_y < tunnel[1] + tunnel[3] and player_y + player_height > tunnel[1]:
            secret_level = True  # Enter secret level
            player_x = SCREEN_WIDTH // 2 - player_width // 2
            player_y = SCREEN_HEIGHT // 2 - player_height // 2

    # Draw ground
    pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - GROUND_HEIGHT, SCREEN_WIDTH, GROUND_HEIGHT))

    # Draw coins
    for coin in coins:
        pygame.draw.circle(screen, YELLOW, (coin[0], coin[1]), COIN_RADIUS)

    # Draw Goombas
    for goomba in goombas:
        pygame.draw.rect(screen, RED, (goomba[0], goomba[1], GOOMBA_WIDTH, GOOMBA_HEIGHT))

    # Draw Bowser
    pygame.draw.rect(screen, (139, 69, 19), (bowser_x, bowser_y, BOWSER_WIDTH, BOWSER_HEIGHT))  # Brown for Bowser

    # Draw Tunnels
    for tunnel in tunnels:
        pygame.draw.rect(screen, SECRET_TUNNEL_COLOR, tunnel)

    # Draw player (Mario character)
    pygame.draw.rect(screen, BLUE, (player_x, player_y, player_width, player_height))

    # Update the display
    pygame.display.update()

    # Draw score and health
    score_text = font.render(f"Score: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))
    health_text = font.render(f"Health: {player_health}", True, BLACK)
    screen.blit(health_text, (10, 40))

    # Generate new coins and goombas if needed
    if len(coins) < num_coins:
        coins.append((random.randint(50, SCREEN_WIDTH - 50), random.randint(100, SCREEN_HEIGHT - GROUND_HEIGHT - 100)))

    if len(goombas) < num_goombas:
        goombas.append((random.randint(50, SCREEN_WIDTH - 50), SCREEN_HEIGHT - GROUND_HEIGHT - GOOMBA_HEIGHT))

    # Bowser movement logic
    bowser_x += bowser_velocity
    if bowser_x <= 100 or bowser_x >= SCREEN_WIDTH - 200:
        bowser_velocity *= -1

    # Control the frame rate
    clock.tick(60)
Leave a Comment