Skip to content
Open
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
40 changes: 38 additions & 2 deletions sucury.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

GRID_SIZE = 50 # Square grid size.

MAXIMUM_SNAKE_SIZE = (WIDTH/GRID_SIZE)*(HEIGHT/GRID_SIZE) # Maximum snake size

HEAD_COLOR = "#00aa00" # Color of the snake's head.
WIN_HEAD_COLOR = "#4de3e8" # Color of the winning snake's head.
DEAD_HEAD_COLOR = "#4b0082" # Color of the dead snake's head.
TAIL_COLOR = "#00ff00" # Color of the snake's tail.
APPLE_COLOR = "#aa0000" # Color of the apple.
Expand Down Expand Up @@ -116,7 +119,10 @@ def __init__(self):

# and a tail (array of segments).
self.tail = []


# The snake has a size
self.size = 1 + len(self.tail)

# The snake is born.
self.alive = True

Expand All @@ -133,7 +139,33 @@ def update(self):
for square in self.tail:
if self.head.x == square.x and self.head.y == square.y:
self.alive = False

# In the event of winning, reset the game arena.
if self.size == MAXIMUM_SNAKE_SIZE:
# Tell the good news
pygame.draw.rect(arena, WIN_HEAD_COLOR, snake.head)
center_prompt("You Won", "Press to restart")

# Respan the head
self.x, self.y = GRID_SIZE, GRID_SIZE
self.head = pygame.Rect(self.x, self.y, GRID_SIZE, GRID_SIZE)

# Respan the initial tail
self.tail = []

# Reset size
self.size = 1

# Initial direction
self.xmov = 1 # Right
self.ymov = 0 # Still

# Resurrection
self.alive = True

# Drop and apple
apple = Apple()

# In the event of death, reset the game arena.
if not self.alive:

Expand All @@ -147,6 +179,8 @@ def update(self):

# Respan the initial tail
self.tail = []
# Reset size
self.size = 1

# Initial direction
self.xmov = 1 # Right
Expand Down Expand Up @@ -247,6 +281,8 @@ def draw_grid():
sys.exit()
elif event.key == pygame.K_p: # S : pause game
game_on = not game_on
elif event.key == pygame.K_z: # Z : win game (TEST)
snake.size = MAXIMUM_SNAKE_SIZE

## Update the game

Expand All @@ -267,7 +303,7 @@ def draw_grid():
pygame.draw.rect(arena, HEAD_COLOR, snake.head)

# Show score (snake length = head + tail)
score = BIG_FONT.render(f"{len(snake.tail)}", True, SCORE_COLOR)
score = BIG_FONT.render(f"{len(snake.tail) + 1}", True, SCORE_COLOR)
arena.blit(score, score_rect)

# If the head pass over an apple, lengthen the snake and drop another apple
Expand Down