Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial code

## [1.0.1] - 2023-10-16

### Added

- CLI Parser

169 changes: 104 additions & 65 deletions sucury.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import pygame
import random
import argparse
import sys

##
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))

Copy link
Copy Markdown

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

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.