Initial commit.
This commit is contained in:
		
							
								
								
									
										94
									
								
								wicked_quest.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								wicked_quest.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,94 @@ | ||||
| #!/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 | ||||
|      | ||||
|         # Calculate current speed based on jumping state | ||||
|         currentSpeed = player.moveSpeed * 1.5 if player.isJumping else player.moveSpeed | ||||
|      | ||||
|         # Track movement distance for this frame | ||||
|         movement = 0 | ||||
|      | ||||
|         # Horizontal movement | ||||
|         if keys[pygame.K_a]:  # Left | ||||
|             movement = currentSpeed | ||||
|             player.xPos -= currentSpeed | ||||
|             player.facingRight = False | ||||
|         elif keys[pygame.K_d]:  # Right | ||||
|             movement = currentSpeed | ||||
|             player.xPos += currentSpeed | ||||
|             player.facingRight = True | ||||
|          | ||||
|         # Play footstep sounds if moving and not jumping | ||||
|         if movement > 0 and not player.isJumping: | ||||
|             player.distanceSinceLastStep += movement | ||||
|             if player.distanceSinceLastStep >= player.stepDistance: | ||||
|                 self.sounds['footstep'].play() | ||||
|                 player.distanceSinceLastStep = 0 | ||||
|  | ||||
|         # Handle jumping | ||||
|         currentTime = pygame.time.get_ticks() | ||||
|      | ||||
|         # Start jump | ||||
|         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 True: | ||||
|             if check_for_exit(): | ||||
|                 return | ||||
|                  | ||||
|             self.handle_input() | ||||
|              | ||||
|             # Update audio positioning and handle collisions | ||||
|             self.currentLevel.update_audio() | ||||
|             self.currentLevel.handle_collisions() | ||||
|              | ||||
|             clock.tick(60)  # 60 FPS | ||||
|  | ||||
|     def run(self): | ||||
|         while True: | ||||
|             choice = game_menu(self.sounds, "play", "instructions", "learn_sounds", "credits", "donate") | ||||
|              | ||||
|             if choice == "play": | ||||
|                 if self.load_level(1): | ||||
|                     self.game_loop() | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     game = WickedQuest() | ||||
|     game.run() | ||||
		Reference in New Issue
	
	Block a user