-
Notifications
You must be signed in to change notification settings - Fork 4
feat: parse window and grid size from CLI #94
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
LMFRomero
wants to merge
6
commits into
develop
Choose a base branch
from
feat/39/parse-constants-in-cli
base: develop
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
Changes from all commits
Commits
Show all changes
6 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
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 |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
|
|
||
| import pygame | ||
| import random | ||
| import argparse | ||
| import sys | ||
|
|
||
| ## | ||
|
|
@@ -56,22 +57,7 @@ | |
| DOWN = (0, 1) | ||
| LEFT = (-1, 0) | ||
|
|
||
| ## | ||
| ## Game implementation. | ||
| ## | ||
|
|
||
| pygame.init() | ||
|
|
||
| clock = pygame.time.Clock() | ||
|
|
||
| arena = pygame.display.set_mode((WIDTH, HEIGHT)) | ||
|
|
||
| BIG_FONT = pygame.font.Font("assets/font/Ramasuri.ttf", int(WIDTH/8)) | ||
| SMALL_FONT = pygame.font.Font("assets/font/Ramasuri.ttf", int(WIDTH/20)) | ||
|
|
||
| pygame.display.set_caption(WINDOW_TITLE) | ||
|
|
||
| game_on = 1 | ||
|
|
||
| ## This function is called when the snake dies. | ||
|
|
||
|
|
@@ -173,12 +159,13 @@ def __init__(self): | |
| # The snake should grow in the next update. | ||
| self.should_grow = False | ||
|
|
||
| def change_direction(self, direction: tuple[int, int]): | ||
| def change_direction(self, direction: "tuple[int, int]"): | ||
| # Remove 1-frame 180 turns that lead to death | ||
| if BLOCK_180_TURNS and (-self.last_velocity[0], -self.last_velocity[1]) == direction: | ||
| return | ||
|
|
||
| (self.xmov, self.ymov) = direction | ||
| self.xmov = direction[0] | ||
| self.ymov = direction[1] | ||
|
|
||
| # This function is called at each loop interation. | ||
| def update(self): | ||
|
|
@@ -279,71 +266,123 @@ def draw_grid(): | |
| rect = pygame.Rect(x, y, grid_size, grid_size) | ||
| pygame.draw.rect(arena, GRID_COLOR, rect, 1) | ||
|
|
||
| score = BIG_FONT.render("1", True, MESSAGE_COLOR) | ||
| score_rect = score.get_rect(center=(WIDTH/2, HEIGHT/20+HEIGHT/30)) | ||
| ### | ||
| ### Init parser to get args from CLI | ||
| ### | ||
| def init_cli_parser(): | ||
| parser = argparse.ArgumentParser(description="The classic snake game") | ||
|
|
||
| main_menu() | ||
| parser.add_argument('--width', default=WIDTH, | ||
| help="set window's width", | ||
| type=int) | ||
| parser.add_argument('--height', default=HEIGHT, | ||
| help="set window's height", | ||
| type=int) | ||
| parser.add_argument('-g', '--grid-size', default=grid_size, | ||
| help="set window's grid size", | ||
| type=int) | ||
|
|
||
| snake = Snake() # The snake | ||
| apple = Apple() # An apple | ||
| return parser | ||
|
|
||
| ## | ||
| ## Main loop | ||
| ## | ||
| ### | ||
| ### Set args gotten from CLI | ||
| ### | ||
| def set_cli_args(args): | ||
| global WIDTH | ||
| global HEIGHT | ||
| global grid_size | ||
|
|
||
| WIDTH = args['width'] | ||
| HEIGHT = args['height'] | ||
| grid_size = max(min(args['grid_size'], 100), 10) | ||
|
|
||
| while True: | ||
| if __name__ == "__main__": | ||
| parser = init_cli_parser() | ||
| args = vars(parser.parse_args()) | ||
| set_cli_args(args) | ||
|
|
||
| for event in pygame.event.get(): # Wait for events | ||
| ## | ||
| ## Game implementation. | ||
| ## | ||
|
|
||
| # App terminated | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| sys.exit() | ||
| pygame.init() | ||
|
|
||
| # Key pressed | ||
| if event.type == pygame.KEYDOWN: | ||
| if event.key == pygame.K_DOWN or event.key == pygame.K_s: # Down arrow or S: move down | ||
| snake.change_direction(DOWN) | ||
| elif event.key == pygame.K_UP or event.key == pygame.K_w: # Up arrow or W: move up | ||
| snake.change_direction(UP) | ||
| elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: # Right arrow or D: move right | ||
| snake.change_direction(RIGHT) | ||
| elif event.key == pygame.K_LEFT or event.key == pygame.K_a: # Left arrow or A: move left | ||
| snake.change_direction(LEFT) | ||
| elif event.key == pygame.K_q: # Q : quit game | ||
| clock = pygame.time.Clock() | ||
|
|
||
| arena = pygame.display.set_mode((WIDTH, HEIGHT)) | ||
|
|
||
| BIG_FONT = pygame.font.Font("assets/font/Ramasuri.ttf", int(WIDTH/8)) | ||
| SMALL_FONT = pygame.font.Font("assets/font/Ramasuri.ttf", int(WIDTH/20)) | ||
|
|
||
| pygame.display.set_caption(WINDOW_TITLE) | ||
|
|
||
| game_on = 1 | ||
|
|
||
| score = BIG_FONT.render("1", True, MESSAGE_COLOR) | ||
| score_rect = score.get_rect(center=(WIDTH/2, HEIGHT/20+HEIGHT/30)) | ||
|
|
||
| main_menu() | ||
|
|
||
| snake = Snake() # The snake | ||
| apple = Apple() # An apple | ||
|
|
||
| ## | ||
| ## Main loop | ||
| ## | ||
|
|
||
| while True: | ||
|
|
||
| for event in pygame.event.get(): # Wait for events | ||
|
|
||
| # App terminated | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| sys.exit() | ||
| elif event.key == pygame.K_p: # S : pause game | ||
| game_on = not game_on | ||
|
|
||
| ## Update the game | ||
| # Key pressed | ||
| if event.type == pygame.KEYDOWN: | ||
| if event.key == pygame.K_DOWN or event.key == pygame.K_s: # Down arrow or S: move down | ||
| snake.change_direction(DOWN) | ||
| elif event.key == pygame.K_UP or event.key == pygame.K_w: # Up arrow or W: move up | ||
| snake.change_direction(UP) | ||
| elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: # Right arrow or D: move right | ||
| snake.change_direction(RIGHT) | ||
| elif event.key == pygame.K_LEFT or event.key == pygame.K_a: # Left arrow or A: move left | ||
| snake.change_direction(LEFT) | ||
| elif event.key == pygame.K_q: # Q : quit game | ||
| pygame.quit() | ||
| sys.exit() | ||
| elif event.key == pygame.K_p: # S : pause game | ||
| game_on = not game_on | ||
|
|
||
| if game_on: | ||
| ## Update the game | ||
|
|
||
| snake.update() | ||
| if game_on: | ||
|
|
||
| arena.fill(ARENA_COLOR) | ||
| draw_grid() | ||
| snake.update() | ||
|
|
||
| apple.update() | ||
| arena.fill(ARENA_COLOR) | ||
| draw_grid() | ||
|
|
||
| # Draw the tail | ||
| for square in snake.tail: | ||
| pygame.draw.rect(arena, TAIL_COLOR, square) | ||
| apple.update() | ||
|
|
||
| # Draw head | ||
| pygame.draw.rect(arena, HEAD_COLOR, snake.head) | ||
| # Draw the tail | ||
| for square in snake.tail: | ||
| pygame.draw.rect(arena, TAIL_COLOR, square) | ||
|
|
||
| # Show score (snake length = head + tail) | ||
| score = BIG_FONT.render(f"{len(snake.tail)}", True, SCORE_COLOR) | ||
| arena.blit(score, score_rect) | ||
| # Draw head | ||
| pygame.draw.rect(arena, HEAD_COLOR, snake.head) | ||
|
|
||
| # If the head pass over an apple, lengthen the snake and drop another apple | ||
| if snake.head.x == apple.x and snake.head.y == apple.y: | ||
| snake.grow() | ||
| apple = Apple() | ||
| # Show score (snake length = head + tail) | ||
| score = BIG_FONT.render(f"{len(snake.tail)}", True, SCORE_COLOR) | ||
| arena.blit(score, score_rect) | ||
|
|
||
| # If the head pass over an apple, lengthen the snake and drop another apple | ||
| if snake.head.x == apple.x and snake.head.y == apple.y: | ||
| snake.grow() | ||
| apple = Apple() | ||
|
|
||
| # Update display and move clock. | ||
| pygame.display.update() | ||
| clock.tick(CLOCK_TICKS) | ||
|
|
||
| # Update display and move clock. | ||
| pygame.display.update() | ||
| clock.tick(CLOCK_TICKS) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a clock_tick scaling factor to the cli arguments? that would resolve the difficulty setting issue. |
||
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.
Maybe it's a good idea to standardize formatting with a linter so we can get consistent whitespaces