-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpysnake.py
More file actions
78 lines (55 loc) · 2.42 KB
/
pysnake.py
File metadata and controls
78 lines (55 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import game
import pygame
from visualizer.printboard import BoardPrinter
from visualizer.pygame_drawer import BoardDrawer
from moves import Moves
from move_input.random_agent import RandomAgent
from move_input.basic_agent import BasicAgent
from move_input.better_agent import BetterAgent
from move_input.user_input import UserAgent
from high_scores import High_Scores
import time
class pySnake:
TIME_PER_FRAME = 0.1 # The time (in seconds) that one step should ideally take
def __init__(self):
print("Initializing game...")
isPlaying = True # Keep track of the game state (playing/game over)
board = game.Board() # Initalize the board
# Choose gameplay input
#agent = RandomAgent() # This agent selects a random possible move
#agent = BasicAgent() # This agent selects a move closer to the dot
#agent = BetterAgent() # This agent selects a move closer to the dot, but farther from its tail and the edge
agent = UserAgent() # This records and processes user input
# Choose a visualizer
#visualizer = BoardPrinter() # Select a visualiser (terminal)
visualizer = BoardDrawer() # Select a visualiser (view screen)
high_scores = High_Scores()
while (isPlaying):
start = time.time()
# Visualize this step
visualizer.drawBoard(board)
# Check if the game is over
isPlaying = not board.is_game_over()
# Get the next move
next_move = agent.get_direction(board)
# Perform the move
board.move( next_move )
if not isPlaying:
print("Ended with score:", board.score)
break
end = time.time()
frame_time = end - start
sleep_time = self.TIME_PER_FRAME - frame_time
if frame_time > 0 and sleep_time > 0:
time.sleep( sleep_time )
# Update high scores and save them
high_scores.add_highscore(board.score)
high_scores.write_highscores()
is_watching_score = True
while is_watching_score:
visualizer.show_end_screen(board)
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_watching_score = False
time.sleep( 0.05 )
pySnake()