mario

Pasted Oct 22, 2026, 10:35 PM1 file238 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgsp9gmsqj7vqy1
untitled
import sys
import pygame

# Initialize Pygame
pygame.init()

# Window dimensions
WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simple 3-Level Mario Game")

clock = pygame.time.Clock()
FPS = 60

# Colors
SKY_BLUE = (107, 140, 255)
LAVA_RED = (200, 50, 50)
GREEN = (34, 139, 34)
ORANGE = (255, 140, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


class Player:

  def __init__(self):
    self.width = 30
    self.height = 40
    self.reset()

  def reset(self):
    self.x = 50
    self.y = HEIGHT - 100
    self.vel_x = 0
    self.vel_y = 0
    self.speed = 5
    self.jump_power = -12
    self.gravity = 0.6
    self.is_jumping = False

  def update(self, platforms):
    keys = pygame.key.get_pressed()
    self.vel_x = 0
    if keys[pygame.K_LEFT]:
      self.vel_x = -self.speed
    if keys[pygame.K_RIGHT]:
      self.vel_x = self.speed
    if keys[pygame.K_SPACE] and not self.is_jumping:
      self.vel_y = self.jump_power
      self.is_jumping = True

    self.vel_y += self.gravity
    self.x += self.vel_x
    self.y += self.vel_y

    # Simple platform collision
    self.is_jumping = True
    for p in platforms:
      if (
          self.x + self.width > p.x
          and self.x < p.x + p.width
          and self.y + self.height >= p.y
          and self.y + self.height - self.vel_y <= p.y + 10
      ):
        self.y = p.y - self.height
        self.vel_y = 0
        self.is_jumping = False

    # Keep inside screen bounds
    if self.x < 0:
      self.x = 0
    if self.y > HEIGHT:
      return True  # Fell off
    return False

  def draw(self, surface):
    pygame.draw.rect(
        surface, (255, 0, 0), (int(self.x), int(self.y), self.width, self.height)
    )


class Platform:

  def __init__(self, x, y, width, height, color=GREEN):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.color = color

  def draw(self, surface):
    pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))


class Bowser:

  def __init__(self):
    self.width = 50
    self.height = 60
    self.x = 650
    self.y = HEIGHT - 100 - self.height
    self.hp = 3
    self.timer = 0

  def update(self):
    # Moves back and forth slowly
    self.timer += 1
    self.x += 2 * (1 if (self.timer // 60) % 2 == 0 else -1)

  def draw(self, surface):
    pygame.draw.rect(
        surface, ORANGE, (int(self.x), int(self.y), self.width, self.height)
    )


def run_game():
  player = Player()
  current_level = 1

  # Level definitions: list of platforms and goal/boss info
  def get_level(level_num):
    platforms = [
        Platform(0, HEIGHT - 40, WIDTH, 40, GREEN)
    ]  # Ground floor
    goal_x = 750
    has_bowser = False

    if level_num == 1:
      platforms.append(Platform(200, HEIGHT - 120, 150, 20))
      platforms.append(Platform(420, HEIGHT - 200, 150, 20))
    elif level_num == 2:
      platforms.append(Platform(150, HEIGHT - 110, 100, 20))
      platforms.append(Platform(320, HEIGHT - 170, 100, 20))
      platforms.append(Platform(500, HEIGHT - 230, 100, 20))
    elif level_num == 3:
      # Bowser level (lava floor instead of green grass)
      platforms[0] = Platform(0, HEIGHT - 40, WIDTH, 40, LAVA_RED)
      platforms.append(Platform(250, HEIGHT - 140, 120, 20))
      platforms.append(Platform(450, HEIGHT - 200, 120, 20))
      has_bowser = True

    return platforms, goal_x, has_bowser

  platforms, goal_x, has_bowser = get_level(current_level)
  bowser = Bowser() if has_bowser else None

  font = pygame.font.SysFont(None, 36)
  game_over = False
  won = False

  running = True
  while running:
    clock.tick(FPS)
    screen.fill(SKY_BLUE)

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        running = False
      if game_over or won:
        if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
          # Restart
          current_level = 1
          platforms, goal_x, has_bowser = get_level(current_level)
          player.reset()
          bowser = Bowser() if has_bowser else None
          game_over = False
          won = False

    if not game_over and not won:
      fell = player.update(platforms)
      if fell:
        game_over = True

      # Check Bowser logic
      if bowser:
        bowser.update()
        # Check collision with Bowser
        player_rect = pygame.Rect(player.x, player.y, player.width, player.height)
        bowser_rect = pygame.Rect(bowser.x, bowser.y, bowser.width, bowser.height)
        if player_rect.colliderect(bowser_rect):
          # If player lands on top of Bowser, damage Bowser
          if player.vel_y > 0 and player.y + player.height - player.vel_y <= bowser.y + 15:
            bowser.hp -= 1
            player.vel_y = -8  # Bounce
            if bowser.hp <= 0:
              bowser = None
          else:
            game_over = True

      # Check level completion
      if player.x >= goal_x and (not bowser):
        if current_level < 3:
          current_level += 1
          platforms, goal_x, has_bowser = get_level(current_level)
          player.reset()
          bowser = Bowser() if has_bowser else None
        else:
          won = True

    # Draw everything
    for p in platforms:
      p.draw(screen)

    # Draw Goal Flag
    pygame.draw.rect(screen, WHITE, (goal_x, HEIGHT - 140, 10, 100))
    pygame.draw.polygon(
        screen, (255, 255, 0), [(goal_x + 10, HEIGHT - 140), (goal_x + 40, HEIGHT - 125), (goal_x + 10, HEIGHT - 110)]
    )

    player.draw(screen)
    if bowser:
      bowser.draw(screen)

    # UI Text
    lvl_text = font.render(f"Level: {current_level}/3", True, BLACK)
    screen.blit(lvl_text, (10, 10))

    if bowser:
      hp_text = font.render(f"Bowser HP: {bowser.hp}", True, BLACK)
      screen.blit(hp_text, (600, 10))

    if game_over:
      msg = font.render("GAME OVER! Press 'R' to Restart", True, (255, 0, 0))
      screen.blit(msg, (WIDTH // 2 - 180, HEIGHT // 2))

    if won:
      msg = font.render("YOU WIN! Princess Saved! Press 'R'", True, (0, 200, 0))
      screen.blit(msg, (WIDTH // 2 - 200, HEIGHT // 2))

    pygame.display.flip()

  pygame.quit()
  sys.exit()


if __name__ == "__main__":
  run_game()

6,230 chars · codepastes.com