-
Notifications
You must be signed in to change notification settings - Fork 18
Maze #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
A-DBN
wants to merge
2
commits into
MathisHammel:main
Choose a base branch
from
A-DBN:Maze
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Maze #15
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import sys | ||
| import pygame | ||
| import threading | ||
| from pharmacontroller import SCREEN_SIZE, PharmaScreen | ||
| from textwriter import String | ||
|
|
||
| # Constants for the game | ||
| TRAIL_TIME = 5 # Time in seconds before trail disappears | ||
|
|
||
| game_started = False | ||
| game_over = False | ||
|
|
||
| # Directions (example level input) | ||
| level_directions = [ | ||
| "right", "right", "right", "right", "right", "right", "up", "right", "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "left", "up", "up", "right", "right", | ||
| "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "up", "right", "right", "right", "up", "up", "left", "up", "left", "left", "left", "left", "left", "left", "left", | ||
| "up", "up", "up", "up", "up", "up", "up", "up", "left", "left", "left", "up", "up", "up", "up", "up", "up", "up", "up" | ||
| ] | ||
| # To create level, copy the array above with the same name and replace the directions with the correct ones | ||
|
|
||
| # Colors | ||
| TRAIL_COLOR = 1.0 # Using 1.0 value for 'on' pixels | ||
| EMPTY_COLOR = 0.0 # Using 0.0 value for 'off' pixels | ||
|
|
||
| pygame.init() | ||
|
|
||
| # Define movement directions as x, y deltas | ||
| DIRECTIONS = { | ||
| "up": (0, -1), | ||
| "down": (0, 1), | ||
| "left": (-1, 0), | ||
| "right": (1, 0), | ||
| } | ||
|
|
||
| class Player: | ||
| def __init__(self, start_x, start_y): | ||
| self.start_x = start_x | ||
| self.start_y = start_y | ||
| self.x = start_x | ||
| self.y = start_y | ||
| self.trail = [(self.x, self.y)] # Start trail with initial position | ||
| self.trail_directions = [] | ||
|
|
||
| def move(self, direction, maze=None): | ||
| if direction in DIRECTIONS: | ||
| dx, dy = DIRECTIONS[direction] | ||
| self.trail_directions.append(direction) | ||
| new_x = self.x + dx | ||
| new_y = self.y + dy | ||
| # Make sure the player stays within the bounds | ||
| if 0 <= new_x < SCREEN_SIZE and 0 <= new_y < SCREEN_SIZE: | ||
| self.x = new_x | ||
| self.y = new_y | ||
| self.trail.append((self.x, self.y)) | ||
|
|
||
| def reset(self): | ||
| self.x = self.start_x | ||
| self.y = self.start_y | ||
| self.trail = [(self.x, self.y)] # Reset trail to the initial position | ||
| self.trail_directions = [] | ||
|
|
||
| def draw_trail(screen, player, show_trail=True): | ||
| # Fill screen with blank (off pixels) | ||
| screen.pixel_buffer = [[EMPTY_COLOR for _ in range(SCREEN_SIZE)] for _ in range(SCREEN_SIZE)] | ||
|
|
||
| # Draw player trail | ||
| if show_trail: | ||
| for pos in player.trail: | ||
| draw_cell(screen, pos[1], pos[0], TRAIL_COLOR) | ||
|
|
||
| # Draw player | ||
| draw_cell(screen, player.y, player.x, TRAIL_COLOR) | ||
| screen.set_image(screen.pixel_buffer) | ||
|
|
||
|
|
||
| def draw_cell(screen, row, col, color): | ||
| if screen.is_drawable(row, col): | ||
| screen.pixel_buffer[row][col] = color | ||
|
|
||
| def check_trail(player): | ||
| image = [[0.0 for c in range(SCREEN_SIZE)] for r in range(SCREEN_SIZE)] | ||
|
|
||
| loosing_string = String(image, (1, 18), 45, "Y o u L o s t") | ||
| winning_string = String(image, (1, 18), 45, "L e v e l C l e a r e d", cooldown=0.03, timeout=0) | ||
|
|
||
| current_length = len(player.trail_directions) | ||
| if player.trail_directions != level_directions[:current_length]: | ||
| return loosing_string, True # Return "You Lost" and set game_over as True | ||
| elif player.trail_directions == level_directions: | ||
| return winning_string, True # Return "Level Cleared" and set game_over as True | ||
| return None, False # No game over yet | ||
|
|
||
| # Function to hide trail after TRAIL_TIME and reset the player's position | ||
| def hide_trail(player): | ||
| player.reset() # Reset the player's position and trail | ||
| global game_started | ||
| game_started = True | ||
|
|
||
| def display_message_until_escape(string_object, screen): | ||
| """Display the string message until the player presses ESC to exit.""" | ||
| image = [[0.0 for c in range(SCREEN_SIZE)] for r in range(SCREEN_SIZE)] | ||
| string_object.image = image | ||
|
|
||
| running = True | ||
| while running: | ||
| # Keep showing the message until ESC is pressed | ||
| string_object.scroll(inverted=False) | ||
| screen.set_image(image) | ||
|
|
||
| for event in pygame.event.get(): | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| sys.exit() | ||
| elif event.type == pygame.KEYDOWN: | ||
| if event.key == pygame.K_ESCAPE: | ||
| running = False | ||
|
|
||
| pygame.time.Clock().tick(30) | ||
|
|
||
| def game_loop(): | ||
| # Define starting point at the very bottom middle of the cross | ||
| start_x = SCREEN_SIZE // 2 # Middle of the screen horizontally | ||
| start_y = SCREEN_SIZE - 1 # Very bottom of the screen | ||
| player = Player(start_x, start_y) | ||
|
|
||
| screen = PharmaScreen(True) | ||
|
|
||
| # Start a timer to hide the trail after TRAIL_TIME and reset the player | ||
| threading.Timer(TRAIL_TIME, lambda: hide_trail(player)).start() | ||
|
|
||
| for direction in level_directions: | ||
| player.move(direction) | ||
|
|
||
| global game_over | ||
|
|
||
| running = True | ||
| while running: | ||
| for event in pygame.event.get(): | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| sys.exit() | ||
| elif event.type == pygame.KEYDOWN: | ||
| if event.key == pygame.K_LEFT: | ||
|
||
| player.move("left") | ||
| elif event.key == pygame.K_RIGHT: | ||
| player.move("right") | ||
| elif event.key == pygame.K_UP: | ||
| player.move("up") | ||
| elif event.key == pygame.K_DOWN: | ||
| player.move("down") | ||
|
|
||
| draw_trail(screen, player, show_trail=True) | ||
|
|
||
| if game_started and not game_over: | ||
| string_object, game_over = check_trail(player) | ||
| if game_over: | ||
| display_message_until_escape(string_object, screen) | ||
|
|
||
| pygame.time.Clock().tick(30) | ||
|
|
||
| if __name__ == "__main__": | ||
| game_loop() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a dataclass with a post_init function to initialize values that depend on others would be better I think
It would look like this