-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsnake.py
1127 lines (936 loc) · 36.4 KB
/
snake.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""snake-on-pygame: A simple and fun snake game, playable by Human and AI.
This module is the implementation of the snake game on pygame, focusing on speed
and simplicity. It's playable by both humans and AI agents and it uses most of
pygame's optimizations to deliver a smooth experience in testing/playing.
Usage for human players
----------
To play as a human, you only need to run this file, given you have the
needed dependencies.
$ python snake.py
Usage for AI agents
----------
To use with AI agents, you need to integrate the game with the AI agent. An
example usage is:
>>> from snake-on-pygame import Game
>>> game = Game(player = "ROBOT",
board_size = board_size,
local_state = local_state,
relative_pos = RELATIVE_POS)
Useful properties:
>>> print(game.nb_actions)
5 # number of actions.
>>> print(game.food_pos)
(6, 5) # current position of food.
>>> print(game.steps)
10 # current number of steps in a given episode.
>>> print(game.snake.length)
4 # current length of the snake in a given episode.
Possible methods:
>>> state = game.reset()
Reset the game and returns the game state right after resetting.
>>> state = game.state()
Get the current game state.
>>> game.food_pos = game.generate_food()
Update the food position.
>>> state, reward, done, info = game.step(numerical_action)
Play a numerical_action, obtaining state, reward, over and info.
>>> game.render()
Render the game in a pygame window.
TO DO
----------
- Publish to pip.
"""
import sys # To close the window when the game is over
from array import array # Efficient numeric arrays
from os import environ, path # To center the game window the best possible
import random # Random numbers used for the food
import logging # Logging function for movements and errors
import json # For file handling (leaderboards)
from itertools import tee # For the color gradient on snake
import pygame # This is the engine used in the game
import numpy as np # Used in calculations and math
import pandas as pd # Used to manage the leaderboards data
from utilities.text_block import TextBlock, InputBox # Textblocks for pygame
__author__ = "Victor Neves"
__license__ = "MIT"
__maintainer__ = "Victor Neves"
__email__ = "victorneves478@gmail.com"
__status__ = "Production"
# Actions, options and forbidden moves
OPTIONS = {
"QUIT": 0,
"PLAY": 1,
"BENCHMARK": 2,
"LEADERBOARDS": 3,
"MENU": 4,
"ADD_TO_LEADERBOARDS": 5,
}
RELATIVE_ACTIONS = {"LEFT": 0, "FORWARD": 1, "RIGHT": 2}
ABSOLUTE_ACTIONS = {"LEFT": 0, "RIGHT": 1, "UP": 2, "DOWN": 3, "IDLE": 4}
FORBIDDEN_MOVES = [(0, 1), (1, 0), (2, 3), (3, 2)]
# Possible rewards in the game
REWARDS = {"MOVE": -0.005, "GAME_OVER": -1, "SCORED": 1}
# Types of point in the board
POINT_TYPE = {"EMPTY": 0, "FOOD": 1, "BODY": 2, "HEAD": 3, "DANGEROUS": 4}
# Speed levels possible to human players
LEVELS = [" EASY ", " MEDIUM ", " HARD "]
SPEEDS = {"EASY": 80, "MEDIUM": 60, "HARD": 40}
# Set the constant FPS limit for the game. Smoothness depend on this.
GAME_FPS = 100
class GlobalVariables:
"""Global variables to be used while drawing and moving the snake game.
Attributes
----------
board_size: int, optional, default = 30
The size of the board.
block_size: int, optional, default = 20
The size in pixels of a block.
head_color: tuple of 3 * int, optional, default = (42, 42, 42)
Color of the head. Start of the body color gradient.
tail_color: tuple of 3 * int, optional, default = (152, 152, 152)
Color of the tail. End of the body color gradient.
food_color: tuple of 3 * int, optional, default = (200, 0, 0)
Color of the food.
game_speed: int, optional, default = 10
Speed in ticks of the game. The higher the faster.
benchmark: int, optional, default = 10
Ammount of matches to benchmark and possibly go to leaderboards.
"""
def __init__(
self,
board_size=30,
block_size=20,
head_color=(42, 42, 42),
tail_color=(152, 152, 152),
food_color=(200, 0, 0),
game_speed=80,
benchmark=1,
):
"""Initialize all global variables. Updated with argument_handler."""
self.board_size = board_size
self.block_size = block_size
self.head_color = head_color
self.tail_color = tail_color
self.food_color = food_color
self.game_speed = game_speed
self.benchmark = benchmark
if self.board_size > 50: # Warn the user about performance
LOGGER.warning("WARNING: BOARD IS TOO BIG, IT MAY RUN SLOWER.")
@property
def canvas_size(self):
"""Canvas size is updated with board_size and block_size."""
return self.board_size * self.block_size
class Snake:
"""Player (snake) class which initializes head, body and board.
The body attribute represents a list of positions of the body, which are in-
cremented when moving/eating on the position [0]. The orientation represents
where the snake is looking at (head) and collisions happen when any element
is superposed with the head.
Attributes
----------
head: list of 2 * int, default = [board_size / 4, board_size / 4]
The head of the snake, located according to the board size.
body: list of lists of 2 * int
Starts with 3 parts and grows when food is eaten.
prev_action: int, default = 1
Last action which the snake took.
length: int, default = 3
Variable length of the snake, can increase when food is eaten.
"""
def __init__(self):
"""Inits Snake with 3 body parts (one is the head) and pointing right"""
self.head = [int(VAR.board_size / 4), int(VAR.board_size / 4)]
self.body = [
[self.head[0], self.head[1]],
[self.head[0] - 1, self.head[1]],
[self.head[0] - 2, self.head[1]],
]
self.prev_action = 1
self.length = 3
self.movement_mapping = {
ABSOLUTE_ACTIONS["LEFT"]: (-1, 0),
ABSOLUTE_ACTIONS["RIGHT"]: (1, 0),
ABSOLUTE_ACTIONS["UP"]: (0, -1),
ABSOLUTE_ACTIONS["DOWN"]: (0, 1),
}
def is_move_invalid(self, action):
"""Check if the movement is invalid, according to FORBIDDEN_MOVES."""
return (
action,
self.prev_action,
) in FORBIDDEN_MOVES or action == ABSOLUTE_ACTIONS["IDLE"]
def move(self, action, food_pos):
"""Move 1 block according to orientation.
If the head is not positioned on food, pop a body part.
Else, return without popping.
Return
----------
ate_food: boolean
Flag which represents whether the snake ate or not food.
"""
if self.is_move_invalid(action):
action = self.prev_action
else:
self.prev_action = action
# Use the dictionary to update the head position
movement = self.movement_mapping.get(action, (0, 0))
self.head[0] += movement[0]
self.head[1] += movement[1]
self.body.insert(0, list(self.head))
ate_food = self.head == food_pos
if not ate_food:
self.body.pop()
else:
LOGGER.info("EVENT: FOOD EATEN")
self.length = len(self.body)
return ate_food
class FoodGenerator:
"""Generate and keep track of food.
Attributes
----------
pos:
Current position of food.
is_food_on_screen:
Flag for existence of food.
"""
def __init__(self, body):
"""Initialize a food piece and set existence flag."""
self.is_food_on_screen = False
self.pos = self.generate_food(body)
def generate_food(self, body):
"""Generate food and verify if it's on a valid place.
Return
----------
pos: tuple of 2 * int
Position of the food that was generated. It can't be in the body.
"""
if not self.is_food_on_screen:
while True:
food = [
random.randint(0, VAR.board_size - 1),
random.randint(0, VAR.board_size - 1),
]
if food not in body:
self.pos = food
break
LOGGER.info("EVENT: FOOD APPEARED")
self.is_food_on_screen = True
return self.pos
class Game:
"""Hold the game window and functions.
Attributes
----------
window: pygame display
Pygame window to show the game.
fps: pygame time clock
Define Clock and ticks in which the game will be displayed.
snake: object
The actual snake who is going to be played.
food_generator: object
Generator of food which responds to the snake.
food_pos: tuple of 2 * int
Position of the food on the board.
game_over: boolean
Flag for game_over.
player: string
Define if human or robots are playing the game.
board_size: int, optional, default = 30
The size of the board.
local_state: boolean, optional, default = False
Whether to use or not game expertise (used mostly by robots players).
relative_pos: boolean, optional, default = False
Whether to use or not relative position of the snake head. Instead of
actions, use relative_actions.
screen_rect: tuple of 2 * int
The screen rectangle, used to draw relatively positioned blocks.
"""
def __init__(
self, player="HUMAN", board_size=30, local_state=False, relative_pos=False
):
"""Initialize window, fps and score. Change nb_actions if relative_pos"""
VAR.board_size = board_size
self.local_state = local_state
self.relative_pos = relative_pos
self.player = player
if player == "ROBOT":
self.nb_actions = 3 if self.relative_pos else 5
self.action_space = self.nb_actions
self.observation_space = np.empty(shape=(board_size**2,))
self.reset()
self.font_path = self.resource_path("resources/fonts/product_sans_bold.ttf")
self.logo_path = self.resource_path("resources/images/ingame_snake_logo.png")
def reset(self):
"""Reset the game environment."""
self.steps = 0
self.snake = Snake()
self.food_generator = FoodGenerator(self.snake.body)
self.food_pos = self.food_generator.pos
self.scored = False
self.game_over = False
return self.state()
def create_window(self):
"""Create a pygame display with board_size * block_size dimension."""
pygame.init()
flags = pygame.DOUBLEBUF | pygame.HWSURFACE
self.window = pygame.display.set_mode((VAR.canvas_size, VAR.canvas_size), flags)
self.window.set_alpha(None)
self.screen_rect = self.window.get_rect()
self.fps = pygame.time.Clock()
def cycle_menu(
self,
menu_options,
list_menu,
dictionary,
img=None,
img_rect=None,
leaderboards=False,
):
"""Cycle through a given menu, waiting for an option to be clicked."""
selected = False
selected_option = None
while not selected:
pygame.event.pump()
events = pygame.event.get()
self.window.fill(pygame.Color(225, 225, 225))
for i, option in enumerate(menu_options):
if option is not None:
option.draw()
option.hovered = False
if (
option.rect.collidepoint(pygame.mouse.get_pos())
and option.block_type != "text"
):
option.hovered = True
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
if leaderboards:
return self._extracted_from_cycle_menu_(
list_menu, i, dictionary
)
else:
selected_option = dictionary[list_menu[i]]
if selected_option is not None:
selected = True
if img is not None:
self.window.blit(img, img_rect.bottomleft)
pygame.display.update()
return selected_option
# TODO Rename this here and in `cycle_menu`
def _extracted_from_cycle_menu_(self, list_menu, i, dictionary):
opt = list_menu[i]
if opt == "MENU":
return dictionary[opt], None
pages = len(opt.rstrip("0123456789"))
page = int(opt[pages:])
selected_option = dictionary[opt[:pages]]
return selected_option, page
def cycle_matches(self, n_matches):
"""Cycle through matches until the end."""
score = array("i")
step = array("i")
for _ in range(n_matches):
self.reset()
self.start_match(wait=3)
current_score, current_step = self.single_player()
score.append(current_score)
step.append(current_step)
return score, step
def menu(self):
"""Main menu of the game.
Return
----------
selected_option: int
The selected option in the main loop.
"""
pygame.display.set_caption("snake-on-pygame | PLAY NOW!")
logo = pygame.image.load(self.logo_path).convert()
logo = pygame.transform.scale(logo, (VAR.canvas_size, int(VAR.canvas_size / 3)))
logo_rect = logo.get_rect()
logo_rect.center = self.screen_rect.center
options = ["PLAY", "BENCHMARK", "LEADERBOARDS", "QUIT"]
blocks = [
TextBlock(
text=f" {option.upper()} ",
pos=(
self.screen_rect.centerx,
(options.index(option) * 2 + 4) * self.screen_rect.centery / 10,
),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 12),
block_type="menu",
)
for option in options
]
return self.cycle_menu(blocks, options, OPTIONS, logo, logo_rect)
def start_match(self, wait):
"""Create some wait time before the actual drawing of the game."""
for i in range(wait):
self.window.fill(pygame.Color(225, 225, 225))
count_down = " {:d} ".format(wait - i)
text_blocks = [
TextBlock(
text=" Game starts in ",
pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 12),
block_type="text",
),
TextBlock(
text=count_down,
pos=(self.screen_rect.centerx, 12 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 1.5),
block_type="text",
),
]
for tb in text_blocks:
tb.draw()
pygame.display.update()
pygame.display.set_caption(
f"snake-on-pygame | Game starts in {count_down} second(s) ..."
)
pygame.time.wait(1000)
LOGGER.info("EVENT: GAME START")
def start(self):
"""Use menu to select the option/game mode."""
opt = self.menu()
while True:
page = 1
if opt == OPTIONS["QUIT"]:
pygame.quit()
sys.exit()
elif opt == OPTIONS["PLAY"]:
VAR.game_speed = self.select_speed()
score, _ = self.cycle_matches(n_matches=1)
opt = self.over(score, None)
elif opt == OPTIONS["BENCHMARK"]:
VAR.game_speed = self.select_speed()
score, steps = self.cycle_matches(n_matches=VAR.benchmark)
opt = self.over(score, steps)
elif opt == OPTIONS["LEADERBOARDS"]:
while page is not None:
opt, page = self.view_leaderboards(page)
elif opt == OPTIONS["MENU"]:
opt = self.menu()
if opt == OPTIONS["ADD_TO_LEADERBOARDS"]:
self.add_to_leaderboards(int(np.mean(score)), int(np.mean(steps)))
opt, page = self.view_leaderboards()
def over(self, score, step):
"""If collision with wall or body, end the game and open options.
Return
----------
selected_option: int
The selected option in the main loop.
"""
score_option = None
if len(score) == VAR.benchmark:
score_option = TextBlock(
text=" ADD TO LEADERBOARDS ",
pos=(self.screen_rect.centerx, 8 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 15),
block_type="menu",
)
text_score = f"SCORE: {int(np.mean(score))}"
list_menu = ["PLAY", "MENU", "ADD_TO_LEADERBOARDS", "QUIT"]
menu_options = [
TextBlock(
text=" PLAY AGAIN ",
pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 15),
block_type="menu",
),
TextBlock(
text=" GO TO MENU ",
pos=(self.screen_rect.centerx, 6 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 15),
block_type="menu",
),
score_option,
TextBlock(
text=" QUIT ",
pos=(self.screen_rect.centerx, 10 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 15),
block_type="menu",
),
TextBlock(
text=text_score,
pos=(self.screen_rect.centerx, 15 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 10),
block_type="text",
),
]
pygame.display.set_caption(
"snake-on-pygame | " + text_score + " | GAME OVER..."
)
LOGGER.info("EVENT: GAME OVER | FINAL %s", text_score)
return self.cycle_menu(menu_options, list_menu, OPTIONS)
def select_speed(self):
"""Speed menu, right before calling start_match.
Return
----------
speed: int
The selected speed in the main loop.
"""
list_menu = ["EASY", "MEDIUM", "HARD"]
menu_options = [
TextBlock(
text=LEVELS[i],
pos=(
self.screen_rect.centerx,
4 * (i + 1) * self.screen_rect.centery / 10,
),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 10),
block_type="menu",
)
for i in range(len(list_menu))
]
speed = self.cycle_menu(menu_options, list_menu, SPEEDS)
return speed
def single_player(self):
"""Play single player game.
Return
----------
score: int
The final score for the match (discounted of initial length).
"""
init_len, curr_len = self.snake.length, self.snake.length
color_list = self.gradient([VAR.head_color, VAR.tail_color], init_len)
elapsed_time, move_wait, last_key = 0, VAR.game_speed, self.snake.prev_action
while not self.game_over:
elapsed_time += self.fps.get_time()
key_input = self.handle_input()
if key_input == "Q":
break
if key_input is not None:
last_key = key_input
if elapsed_time >= move_wait:
elapsed_time = 0
self.play(last_key)
curr_len = self.snake.length
if curr_len > init_len:
color_list = self.gradient(
[VAR.head_color, VAR.tail_color], curr_len
)
init_len = curr_len
self.draw(color_list)
pygame.display.update()
self.fps.tick(GAME_FPS)
return curr_len - 3, self.steps
def check_collision(self):
"""Check wether any collisions happened with the wall or body.
Return
----------
collided: boolean
Whether the snake collided or not.
"""
collided = False
wall_size = VAR.board_size - 1
if any(
[
self.snake.head[0] >= wall_size,
self.snake.head[0] < 0,
self.snake.head[1] >= wall_size,
self.snake.head[1] < 0,
self.snake.head in self.snake.body[1:],
]
):
LOGGER.info("EVENT: COLLISION")
collided = True
return collided
def is_won(self):
"""Verify if the score is greater than 0.
Return
----------
won: boolean
Whether the score is greater than 0.
"""
return self.snake.length > 3
def generate_food(self):
"""Generate new food if needed.
Return
----------
food_pos: tuple of 2 * int
Current position of the food.
"""
return self.food_generator.generate_food(self.snake.body)
def handle_input(self):
"""After getting current pressed keys, handle important cases.
Return
----------
action: int
Handle human input to assess the next action.
"""
pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN])
keys = pygame.key.get_pressed()
pygame.event.pump()
action = None
if keys[pygame.K_ESCAPE] or keys[pygame.K_q]:
LOGGER.info("ACTION: KEY PRESSED: ESCAPE or Q")
action = "Q"
elif keys[pygame.K_LEFT]:
LOGGER.info("ACTION: KEY PRESSED: LEFT")
action = ABSOLUTE_ACTIONS["LEFT"]
elif keys[pygame.K_RIGHT]:
LOGGER.info("ACTION: KEY PRESSED: RIGHT")
action = ABSOLUTE_ACTIONS["RIGHT"]
elif keys[pygame.K_UP]:
LOGGER.info("ACTION: KEY PRESSED: UP")
action = ABSOLUTE_ACTIONS["UP"]
elif keys[pygame.K_DOWN]:
LOGGER.info("ACTION: KEY PRESSED: DOWN")
action = ABSOLUTE_ACTIONS["DOWN"]
return action
def state(self):
"""Create a matrix of the current state of the game.
Return
----------
canvas: np.array of size board_size**2
Return the current state of the game in a matrix.
"""
canvas = np.zeros((VAR.board_size, VAR.board_size))
if not self.game_over:
body = self.snake.body
canvas[body[0][0], body[0][1]] = POINT_TYPE["HEAD"]
for part in body:
canvas[part[0], part[1]] = POINT_TYPE["BODY"]
if self.local_state:
canvas = self.eval_local_safety(canvas, body)
canvas[self.food_pos[0], self.food_pos[1]] = POINT_TYPE["FOOD"]
return canvas
def relative_to_absolute(self, action):
"""Translate relative actions to absolute.
Return
----------
action: int
Translated action from relative to absolute.
"""
if action == RELATIVE_ACTIONS["FORWARD"]:
action = self.snake.prev_action
elif action == RELATIVE_ACTIONS["LEFT"]:
if self.snake.prev_action == ABSOLUTE_ACTIONS["LEFT"]:
action = ABSOLUTE_ACTIONS["DOWN"]
elif self.snake.prev_action == ABSOLUTE_ACTIONS["RIGHT"]:
action = ABSOLUTE_ACTIONS["UP"]
elif self.snake.prev_action == ABSOLUTE_ACTIONS["UP"]:
action = ABSOLUTE_ACTIONS["LEFT"]
else:
action = ABSOLUTE_ACTIONS["RIGHT"]
elif self.snake.prev_action == ABSOLUTE_ACTIONS["LEFT"]:
action = ABSOLUTE_ACTIONS["UP"]
elif self.snake.prev_action == ABSOLUTE_ACTIONS["RIGHT"]:
action = ABSOLUTE_ACTIONS["DOWN"]
elif self.snake.prev_action == ABSOLUTE_ACTIONS["UP"]:
action = ABSOLUTE_ACTIONS["RIGHT"]
else:
action = ABSOLUTE_ACTIONS["LEFT"]
return action
def play(self, action):
"""Move the snake to the direction, eat and check collision."""
self.scored = False
self.steps += 1
self.food_pos = self.generate_food()
if self.relative_pos:
action = self.relative_to_absolute(action)
if self.snake.move(action, self.food_pos):
self.scored = True
self.food_generator.is_food_on_screen = False
if self.player == "HUMAN":
if self.check_collision():
self.game_over = True
elif self.check_collision() or self.steps > 50 * self.snake.length:
self.game_over = True
def get_reward(self):
"""Return the current reward. Can be used as the reward function.
Return
----------
reward: float
Current reward of the game.
"""
if self.game_over or self.scored:
return REWARDS["GAME_OVER"] if self.game_over else self.snake.length
else:
return REWARDS["MOVE"]
def draw(self, color_list):
"""Draw the game, the snake and the food using pygame."""
self.window.fill(pygame.Color(225, 225, 225))
for part, color in zip(self.snake.body, color_list):
pygame.draw.rect(
self.window,
color,
pygame.Rect(
(part[0] * VAR.block_size),
part[1] * VAR.block_size,
VAR.block_size,
VAR.block_size,
),
)
pygame.draw.rect(
self.window,
VAR.food_color,
pygame.Rect(
self.food_pos[0] * VAR.block_size,
self.food_pos[1] * VAR.block_size,
VAR.block_size,
VAR.block_size,
),
)
pygame.display.set_caption(
f"snake-on-pygame | Score: {str(self.snake.length - 3)}"
)
def step(self, action):
"""Play the action and returns state, reward and if over."""
self.play(action)
return self.state(), self.get_reward(), self.game_over, None
def render(self):
if not hasattr(self, "window"):
self.create_window()
size = self.snake.length # Size of the snake
color_list = self.gradient([VAR.head_color, VAR.tail_color], size)
self.draw(color_list)
pygame.display.update()
self.fps.tick(60) # Limit FPS to 100
def get_name(self):
"""See test.py in my desktop, for a textinput_box input in pygame"""
done = False
input_box = InputBox(
x=200,
y=300,
w=140,
h=32,
window=self.window,
font_path=self.resource_path("resources/fonts/product_sans_bold.ttf"),
)
text_block = TextBlock(
text=" YOUR NAME ",
pos=(self.screen_rect.centerx, 0.9 * self.screen_rect.centery),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 24),
block_type="text",
)
while not done:
pygame.event.pump()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
done = True
text = input_box.handle_event(event)
if text is not None:
done = True
input_box.update()
self.window.fill(pygame.Color(225, 225, 225))
input_box.draw()
text_block.draw()
pygame.display.update()
return text
def add_to_leaderboards(self, score, step):
file_path = self.resource_path("resources/scores.json")
name = self.get_name()
new_score = {"name": str(name), "ranking_data": {"score": score, "step": step}}
if not path.isfile(file_path):
data = [new_score]
else:
with open(file_path) as leaderboards_file:
data = json.load(leaderboards_file)
data.append(new_score)
data.sort(key=lambda e: e["ranking_data"]["score"], reverse=True)
with open(file_path, mode="w") as leaderboards_file:
json.dump(data, leaderboards_file, indent=4)
def view_leaderboards(self, page=1):
file_path = self.resource_path("resources/scores.json")
with open(file_path, "r") as leaderboards_file:
scores_data = json.loads(leaderboards_file.read())
dataframe = pd.DataFrame.from_dict(scores_data)
dataframe = pd.concat(
[
dataframe.drop(["ranking_data"], axis=1),
dataframe["ranking_data"].apply(pd.Series),
],
axis=1,
) # Separate 'ranking_data' into 2 cols
ammount_of_players = len(dataframe.index)
players_per_page = 5
number_of_pages = -(-ammount_of_players // players_per_page)
score_page = []
score_header = " POS NAME SCORE STEP "
menu_options = [
TextBlock(
text=" LEADERBOARDS ",
pos=(self.screen_rect.centerx, 2 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 12),
block_type="text",
)
]
list_menu = ["LEADERBOARDS", "HEADER"]
menu_options.append(
TextBlock(
text=score_header,
pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 24),
block_type="text",
background_color=(152, 152, 152),
)
)
# Adding pages to the loop
for i in range(1, number_of_pages + 1):
score_page.append(
dataframe.loc[dataframe.index.intersection(range(5 * (i - 1), 5 * i))]
)
list_menu.append(("LEADERBOARDS{:d}".format(i)))
menu_options.append(
TextBlock(
text=(" {:d} ".format(i)),
pos=(
(2 * self.screen_rect.centerx / (number_of_pages + 1) * i),
(13 * self.screen_rect.centery / 10),
),
canvas_size=VAR.canvas_size,
font_path=self.font_path,
window=self.window,
scale=(1 / 18),
block_type="menu",
)
)
for i, row in score_page[page - 1].iterrows():
list_menu.append(("RANK{:d}".format(i)))
pos = "{0: <5} ".format(1 + i)
name = "{0: <25} ".format(row["name"])
score = "{0: <5} ".format(row["score"])