Initial commit.
This commit is contained in:
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
76
src/level.py
Normal file
76
src/level.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from libstormgames import *
|
||||
from src.player import Player
|
||||
from src.object import Object
|
||||
|
||||
class Level:
|
||||
def __init__(self, levelData, sounds):
|
||||
self.sounds = sounds
|
||||
self.objects = []
|
||||
self.player = Player(levelData["player_start"]["x"], levelData["player_start"]["y"])
|
||||
|
||||
# Load objects from level data
|
||||
for obj in levelData["objects"]:
|
||||
gameObject = Object(
|
||||
obj["x"],
|
||||
obj["y"],
|
||||
obj["sound"],
|
||||
isStatic=obj.get("static", True),
|
||||
isCollectible=obj.get("collectible", False),
|
||||
isHazard=obj.get("hazard", False)
|
||||
)
|
||||
self.objects.append(gameObject)
|
||||
|
||||
def update_audio(self):
|
||||
# Update all object sounds based on player position
|
||||
for obj in self.objects:
|
||||
if not obj.isActive:
|
||||
continue
|
||||
|
||||
# For non-static objects like goblins, ensure continuous sound
|
||||
if not obj.isStatic:
|
||||
# If channel doesn't exist or sound stopped playing, restart it
|
||||
if obj.channel is None or not obj.channel.get_busy():
|
||||
obj.channel = obj_play(self.sounds, obj.soundName, self.player.xPos, obj.xPos)
|
||||
else:
|
||||
# Original logic for static objects
|
||||
if obj.channel is None:
|
||||
obj.channel = obj_play(self.sounds, obj.soundName, self.player.xPos, obj.xPos)
|
||||
|
||||
# Update position-based audio for all objects
|
||||
if obj.channel is not None:
|
||||
obj.channel = obj_update(obj.channel, self.player.xPos, obj.xPos)
|
||||
|
||||
def handle_collisions(self):
|
||||
for obj in self.objects:
|
||||
if not obj.isActive:
|
||||
continue
|
||||
|
||||
# Single-tile collision detection (more precise)
|
||||
collision_threshold = 0.5 # Half a tile width from center
|
||||
distance = abs(self.player.xPos - obj.xPos)
|
||||
|
||||
if distance <= collision_threshold:
|
||||
if obj.isCollectible:
|
||||
# Coins must be collected while jumping
|
||||
if self.player.isJumping:
|
||||
self.sounds[f'get_{obj.soundName}'].play()
|
||||
speak(f"Collected {obj.soundName}")
|
||||
obj.isActive = False
|
||||
obj_stop(obj.channel)
|
||||
self.player.collectedItems.append(obj.soundName)
|
||||
elif obj.isHazard:
|
||||
# Only affect player if they're not jumping (for ground-based hazards)
|
||||
if not self.player.isJumping:
|
||||
if obj.soundName == "grave":
|
||||
self.sounds['grave'].play()
|
||||
speak("You fell into a pit!")
|
||||
elif obj.soundName == "goblin":
|
||||
self.sounds['goblin'].play()
|
||||
speak("A goblin got you!")
|
||||
else: # Other hazards
|
||||
self.sounds[obj.soundName].play()
|
||||
speak(f"A {obj.soundName} got you!")
|
||||
|
||||
# Apply knockback for any hazard hit
|
||||
knockback = 2
|
||||
self.player.xPos = self.player.xPos - knockback if self.player.facingRight else self.player.xPos + knockback
|
10
src/object.py
Normal file
10
src/object.py
Normal file
@@ -0,0 +1,10 @@
|
||||
class Object:
|
||||
def __init__(self, xPos, yPos, soundName, isStatic=True, isCollectible=False, isHazard=False):
|
||||
self.xPos = xPos
|
||||
self.yPos = yPos
|
||||
self.soundName = soundName
|
||||
self.isStatic = isStatic
|
||||
self.isCollectible = isCollectible
|
||||
self.isHazard = isHazard
|
||||
self.channel = None # For tracking the sound channel
|
||||
self.isActive = True
|
13
src/player.py
Normal file
13
src/player.py
Normal file
@@ -0,0 +1,13 @@
|
||||
class Player:
|
||||
def __init__(self, xPos, yPos):
|
||||
self.xPos = xPos
|
||||
self.yPos = yPos
|
||||
self.moveSpeed = 0.05
|
||||
self.jumpDuration = 1000 # Jump duration in milliseconds
|
||||
self.jumpStartTime = 0
|
||||
self.isJumping = False
|
||||
self.facingRight = True
|
||||
self.collectedItems = []
|
||||
# Track distance for footsteps
|
||||
self.distanceSinceLastStep = 0
|
||||
self.stepDistance = 0.5 # Play a step sound every 0.5 units moved
|
Reference in New Issue
Block a user