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
22 changes: 16 additions & 6 deletions coral.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ def random_position():

return x, y, xmov, ymov

## The score based on the length and number of collected oranges
def get_score(snake):
return len(snake.tail) + snake.oranges

## Get and save highscore from/in a file
def save_high_score(score):
if not os.path.exists("data/"):
Expand Down Expand Up @@ -476,6 +480,9 @@ def update(self):
if self.energy.get_energy() <= 0:
self.alive = False

# Number of collected oranges (for score purposes; they are worth 2 points).
self.oranges = int(50*(self.speed - 1))

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

Expand All @@ -485,7 +492,7 @@ def update(self):

# Tell the bad news

display_highscore(len(snake.tail))
display_highscore(get_score(self))

self.draw_head()
center_prompt("Game Over", "Press to restart")
Expand All @@ -500,6 +507,9 @@ def update(self):
# Respawn the initial tail
self.tail = []

# Reset speed
self.speed = 1

# Resurrection
game_over_sound.stop()
self.alive = True
Expand All @@ -513,7 +523,7 @@ def update(self):

# Move the snake.

# If head hasn't moved, tail shouldn't either (otherwise, self-byte).
# If head hasn't moved, tail shouldn't either (otherwise, self-bite).
if (self.xmov or self.ymov):

# Prepend a new segment to tail.
Expand Down Expand Up @@ -687,8 +697,7 @@ def update(self):

# Check if the orange is already dropped, if not then maybe drop it
if self.dropped == False:
#if random.randint(1, 100) >= 99:
if True:
if random.randint(1, 100) >= 99:
pygame.draw.circle(arena, ORANGE_COLOR, (self.rect.centerx, self.rect.centery), self.radius)
self.dropped = True

Expand Down Expand Up @@ -812,8 +821,8 @@ def draw_grid():
if game_on:
snake.energy.update()

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

# If the head pass over an apple, lengthen the snake and drop another apple
Expand All @@ -826,6 +835,7 @@ def draw_grid():
# If the head passes over an orange, lengthen the snake and drop another orange
if snake.head.x == orange.x and snake.head.y == orange.y:
#snake.tail.append(pygame.Rect(snake.head.x, snake.head.y, GRID_SIZE, GRID_SIZE))
snake.energy.increase_energy(APPLE_ENERGY)
snake.speed += 0.04
orange = Orange()
got_apple_sound.play()
Expand Down