Add high scores and game restart
This commit is contained in:
parent
7142c86211
commit
6c12408417
2 changed files with 80 additions and 55 deletions
|
@ -8,3 +8,5 @@ TILE_SIZE = 20
|
||||||
RESOLUTION = MAP_SIZE[0] * TILE_SIZE, MAP_SIZE[1] * TILE_SIZE
|
RESOLUTION = MAP_SIZE[0] * TILE_SIZE, MAP_SIZE[1] * TILE_SIZE
|
||||||
|
|
||||||
INITIAL_SNAKE_SIZE = 3
|
INITIAL_SNAKE_SIZE = 3
|
||||||
|
|
||||||
|
SCORES_FILE = 'scores.snake'
|
||||||
|
|
133
main.py
133
main.py
|
@ -1,13 +1,15 @@
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import pickle
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
from pygame import locals as pglocals
|
from pygame import locals as pglocals
|
||||||
|
|
||||||
from config import RESOLUTION, SNAKE_COLOR, BACKGROUND_COLOR, FONT_COLOR, INITIAL_SNAKE_SIZE
|
from config import RESOLUTION, SNAKE_COLOR, BACKGROUND_COLOR, FONT_COLOR, INITIAL_SNAKE_SIZE, SCORES_FILE
|
||||||
from objects import Snake, Apple
|
from objects import Snake, Apple
|
||||||
from utils import get_score_text, Direction
|
from utils import get_score_text, Direction
|
||||||
|
|
||||||
logging.basicConfig(level=logging.WARNING)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
@ -24,74 +26,95 @@ def main():
|
||||||
|
|
||||||
screen = pygame.display.set_mode(RESOLUTION) # type: pygame.Surface
|
screen = pygame.display.set_mode(RESOLUTION) # type: pygame.Surface
|
||||||
pygame.display.set_caption('Snake')
|
pygame.display.set_caption('Snake')
|
||||||
|
scores = []
|
||||||
|
if os.path.isfile(SCORES_FILE):
|
||||||
|
with open(SCORES_FILE, 'rb') as scores_file:
|
||||||
|
scores = pickle.load(scores_file)
|
||||||
|
|
||||||
snake = Snake()
|
logger.info(f'High scores : {scores}')
|
||||||
screen.fill(SNAKE_COLOR, snake.head)
|
|
||||||
apple = Apple()
|
|
||||||
apple.display(screen)
|
|
||||||
|
|
||||||
score = 0
|
|
||||||
score_text, score_rect = get_score_text(score)
|
|
||||||
screen.blit(score_text, score_rect)
|
|
||||||
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
screen.fill(BACKGROUND_COLOR)
|
||||||
if event.type == pglocals.QUIT:
|
snake = Snake()
|
||||||
logger.warning('Received QUIT event')
|
screen.fill(SNAKE_COLOR, snake.head)
|
||||||
return
|
apple = Apple()
|
||||||
if event.type == pglocals.KEYDOWN:
|
apple.display(screen)
|
||||||
if event.key == pglocals.K_DOWN:
|
|
||||||
snake.direction = Direction.DOWN
|
|
||||||
elif event.key == pglocals.K_LEFT:
|
|
||||||
snake.direction = Direction.LEFT
|
|
||||||
elif event.key == pglocals.K_UP:
|
|
||||||
snake.direction = Direction.UP
|
|
||||||
elif event.key == pglocals.K_RIGHT:
|
|
||||||
snake.direction = Direction.RIGHT
|
|
||||||
|
|
||||||
dirty_rects = snake.move(screen, apple)
|
score = 0
|
||||||
if snake.dead:
|
score_text, score_rect = get_score_text(score)
|
||||||
logger.info(f'Vous avez perdu ! Score : {score}')
|
screen.blit(score_text, score_rect)
|
||||||
break
|
|
||||||
|
|
||||||
if snake.head.colliderect(apple.rect):
|
pygame.display.flip()
|
||||||
apple.renew()
|
|
||||||
score += apple.score
|
|
||||||
logger.info(f'Apple eaten, new score : {score}')
|
|
||||||
|
|
||||||
screen.fill(BACKGROUND_COLOR, score_rect)
|
while True:
|
||||||
old_score_rect = score_rect
|
for event in pygame.event.get():
|
||||||
|
if event.type == pglocals.QUIT:
|
||||||
|
logger.warning('Received QUIT event')
|
||||||
|
return
|
||||||
|
if event.type == pglocals.KEYDOWN:
|
||||||
|
if event.key == pglocals.K_DOWN:
|
||||||
|
snake.direction = Direction.DOWN
|
||||||
|
elif event.key == pglocals.K_LEFT:
|
||||||
|
snake.direction = Direction.LEFT
|
||||||
|
elif event.key == pglocals.K_UP:
|
||||||
|
snake.direction = Direction.UP
|
||||||
|
elif event.key == pglocals.K_RIGHT:
|
||||||
|
snake.direction = Direction.RIGHT
|
||||||
|
|
||||||
score_text, score_rect = get_score_text(score)
|
dirty_rects = snake.move(screen, apple)
|
||||||
screen.blit(score_text, score_rect)
|
if snake.dead:
|
||||||
dirty_rects.append(score_rect.union(old_score_rect))
|
logger.info(f'Vous avez perdu ! Score : {score}')
|
||||||
|
scores.append(score)
|
||||||
|
scores = sorted(scores, reverse=True)[:3]
|
||||||
|
logger.info(f'High scores : {scores}')
|
||||||
|
with open(SCORES_FILE, 'wb') as scores_file:
|
||||||
|
pickle.dump(scores, scores_file)
|
||||||
|
break
|
||||||
|
|
||||||
dirty_rects.append(apple.display(screen))
|
if snake.head.colliderect(apple.rect):
|
||||||
|
apple.renew()
|
||||||
|
score += apple.score
|
||||||
|
logger.info(f'Apple eaten, new score : {score}')
|
||||||
|
|
||||||
pygame.display.update(dirty_rects)
|
screen.fill(BACKGROUND_COLOR, score_rect)
|
||||||
|
old_score_rect = score_rect
|
||||||
|
|
||||||
# Run faster as snake grows
|
score_text, score_rect = get_score_text(score)
|
||||||
clock.tick(10 - INITIAL_SNAKE_SIZE + len(snake.slots))
|
screen.blit(score_text, score_rect)
|
||||||
|
dirty_rects.append(score_rect.union(old_score_rect))
|
||||||
|
|
||||||
screen.fill(BACKGROUND_COLOR)
|
dirty_rects.append(apple.display(screen))
|
||||||
|
|
||||||
font = pygame.font.Font(None, 60)
|
pygame.display.update(dirty_rects)
|
||||||
text = font.render(f"PERDU ! Score : {score}", 1, FONT_COLOR)
|
|
||||||
text_rect = text.get_rect() # type: pygame.Rect
|
|
||||||
text_rect.center = screen.get_rect().center
|
|
||||||
|
|
||||||
screen.blit(text, text_rect)
|
# Run faster as snake grows
|
||||||
pygame.display.flip()
|
clock.tick(10 - INITIAL_SNAKE_SIZE + len(snake.slots))
|
||||||
|
|
||||||
while True:
|
screen.fill(BACKGROUND_COLOR)
|
||||||
for event in pygame.event.get():
|
|
||||||
if event.type == pglocals.QUIT:
|
|
||||||
logger.info('Received QUIT event')
|
|
||||||
return
|
|
||||||
|
|
||||||
clock.tick(5)
|
font = pygame.font.Font(None, 60)
|
||||||
|
text = font.render(f"PERDU ! Score : {score}", 1, FONT_COLOR)
|
||||||
|
text_rect = text.get_rect() # type: pygame.Rect
|
||||||
|
text_rect.center = screen.get_rect().center
|
||||||
|
|
||||||
|
screen.blit(text, text_rect)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
restart = False
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pglocals.QUIT:
|
||||||
|
logger.info('Received QUIT event')
|
||||||
|
logger.info(f'High scores : {scores}')
|
||||||
|
return
|
||||||
|
if event.type == pglocals.KEYDOWN:
|
||||||
|
if event.key == pglocals.K_r:
|
||||||
|
logger.debug('Restarting game')
|
||||||
|
restart = True
|
||||||
|
if restart:
|
||||||
|
break
|
||||||
|
|
||||||
|
clock.tick(5)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue