Files
WarioWare-Black-and-WAH/microgames/wario/light_it_up.py
T

103 lines
4.2 KiB
Python

# microgames/wario/light_it_up.py
import pygame
from engine.scene import Scene
from settings import *
from engine.utils import scale_to_fit
class LightItUp(Scene):
def __init__(self, manager):
super().__init__(manager)
self.prompt = "Light it up!"
self.light_bulb_rect = pygame.Rect(0, 0, 100, 100)
self.chain_rect = pygame.Rect(0, 0, 50, 200) # Adjusted width for better scaling
self.light_on = False
self.chain_dragging = False
self.initial_chain_y = 0
self.timer = 3.0 / self.manager.speed # Keep for GameManager time limit calculation
self.state = "playing"
self.end_timer = 2.0
# Load raw images
self.light_off_image_raw = None
self.light_on_image_raw = None
self.chain_image_raw = None
try:
self.light_off_image_raw = pygame.image.load(resource_path("Assets/images/wario/light_off.png")).convert_alpha()
self.light_on_image_raw = pygame.image.load(resource_path("Assets/images/wario/light_on.png")).convert_alpha()
self.chain_image_raw = pygame.image.load(resource_path("Assets/images/wario/chain.png")).convert_alpha()
if self.chain_image_raw:
original_width, original_height = self.chain_image_raw.get_size()
desired_height = 200
aspect_ratio = original_width / original_height
desired_width = int(desired_height * aspect_ratio)
self.chain_rect = pygame.Rect(0, 0, desired_width, desired_height)
else:
self.chain_rect = pygame.Rect(0, 0, 50, 200) # Fallback if image fails to load
except (pygame.error, FileNotFoundError) as e:
print(f"Error loading images for LightItUp: {e}")
def start(self):
self.timer = 3.0 / self.manager.speed
self.state = "playing"
self.end_timer = 2.0
self.light_on = False
self.light_bulb_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 4)
self.chain_rect.midtop = self.light_bulb_rect.midbottom
self.initial_chain_y = self.chain_rect.y
self.chain_dragging = False
def update(self, dt, time_left=None):
if self.state == "playing":
if time_left is not None and time_left <= 0:
self.end(False) # Failure due to timeout
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed[0]:
if self.chain_rect.collidepoint(mouse_pos):
self.chain_dragging = True
else:
self.chain_dragging = False
if self.chain_dragging:
self.chain_rect.y = mouse_pos[1]
if self.chain_rect.y > self.initial_chain_y + 100:
self.light_on = True
self.end(True) # Success
elif self.state == "success" or self.state == "failure":
self.end_timer -= dt
if self.end_timer <= 0:
self.manager.microgame_finished(self.state == "success", 'wario')
def draw(self, screen):
screen.fill(WARIO_BG_COLOR)
if self.state == "playing":
# Draw light bulb
if self.light_on and self.light_on_image_raw:
scaled_img, scaled_rect = scale_to_fit(self.light_on_image_raw, self.light_bulb_rect)
screen.blit(scaled_img, scaled_rect)
elif self.light_off_image_raw:
scaled_img, scaled_rect = scale_to_fit(self.light_off_image_raw, self.light_bulb_rect)
screen.blit(scaled_img, scaled_rect)
else:
color = YELLOW if self.light_on else (50, 50, 50)
pygame.draw.rect(screen, color, self.light_bulb_rect)
# Draw chain
if self.chain_image_raw:
scaled_img, scaled_rect = scale_to_fit(self.chain_image_raw, self.chain_rect)
screen.blit(scaled_img, scaled_rect)
else:
pygame.draw.rect(screen, (100, 100, 100), self.chain_rect)
# Timer is now drawn by GameManager
pass
def end(self, success):
if success:
self.state = "success"
else:
self.state = "failure"