Files
wicked-quest/wicked_quest.py

107 lines
3.6 KiB
Python

#!/usr/bin/env python3
import json
import os
from libstormgames import *
from src.level import Level
from src.object import Object
from src.player import Player
class WickedQuest:
def __init__(self):
self.sounds = initialize_gui("Wicked Quest")
self.currentLevel = None
def load_level(self, levelNumber):
levelFile = f"levels/{levelNumber}.json"
try:
with open(levelFile, 'r') as f:
levelData = json.load(f)
self.currentLevel = Level(levelData, self.sounds)
speak(f"Level {levelNumber} loaded")
except FileNotFoundError:
speak("Level not found")
return False
return True
def handle_input(self):
keys = pygame.key.get_pressed()
player = self.currentLevel.player
currentTime = pygame.time.get_ticks()
# Calculate current speed based on jumping state
currentSpeed = player.moveSpeed * 1.5 if player.isJumping else player.moveSpeed
# Track movement distance for this frame
movementDistance = 0
# Horizontal movement
if keys[pygame.K_a]: # Left
movementDistance = currentSpeed
player.xPos -= currentSpeed
player.facingRight = False
elif keys[pygame.K_d]: # Right
movementDistance = currentSpeed
player.xPos += currentSpeed
player.facingRight = True
# Handle attack with either CTRL key
if (keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL]) and player.start_attack(currentTime):
self.sounds[player.currentWeapon.attackSound].play()
# Play footstep sounds if moving and not jumping
if movementDistance > 0 and not player.isJumping:
player.distanceSinceLastStep += movementDistance
if player.distanceSinceLastStep >= player.stepDistance:
self.sounds['footstep'].play()
player.distanceSinceLastStep = 0
# Handle jumping
if keys[pygame.K_w] and not player.isJumping:
player.isJumping = True
player.jumpStartTime = currentTime
self.sounds['jump'].play()
# Check if jump should end
if player.isJumping and currentTime - player.jumpStartTime >= player.jumpDuration:
player.isJumping = False
# Reset step distance tracking after landing
player.distanceSinceLastStep = 0
def game_loop(self):
clock = pygame.time.Clock()
while self.currentLevel.player.get_health() > 0 and self.currentLevel.player.get_lives() > 0:
currentTime = pygame.time.get_ticks()
if check_for_exit():
return
self.handle_input()
# Update audio positioning and handle collisions
self.currentLevel.update_audio()
self.currentLevel.handle_collisions()
# Handle combat interactions
self.currentLevel.handle_combat(currentTime)
clock.tick(60) # 60 FPS
# Player died or ran out of lives
speak("Game Over")
def run(self):
while True:
choice = game_menu(self.sounds, "play", "instructions", "learn_sounds", "credits", "donate", "exit")
if choice == "exit":
exit_game()
elif choice == "play":
if self.load_level(1):
self.game_loop()
if __name__ == "__main__":
game = WickedQuest()
game.run()