-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
82 lines (66 loc) · 2.56 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
# Imports
import pygame, random as rd
class Snake:
def __init__(self: object) -> None:
self.__speed: int = 5
self.__location: list = [rd.randint(0, 750), rd.randint(0, 490), 25, 25]
self.__control_location: list = [self.__speed, 0]
self.__body: list = []
self.__head: list = [self.__location[0], self.__location[1]]
self.__length: int = 5
@property
def location(self: object) -> list:
return self.__location
@property
def body(self: object) -> list:
return self.__body
def move_right(self: object) -> None:
if self.__control_location[0] == -self.__speed:
pass
else:
self.__control_location[0] = self.__speed
self.__control_location[1] = 0
def move_left(self: object) -> None:
if self.__control_location[0] == self.__speed:
pass
else:
self.__control_location[0] = -self.__speed
self.__control_location[1] = 0
def move_up(self: object) -> None:
if self.__control_location[1] == self.__speed:
pass
else:
self.__control_location[0] = 0
self.__control_location[1] = -self.__speed
def move_down(self: object) -> None:
if self.__control_location[1] == -self.__speed:
pass
else:
self.__control_location[0] = 0
self.__control_location[1] = self.__speed
def collision(self: object) -> bool:
if self.__body.count(self.__head) > 1:
return True
else:
return False
def update_location(self: object) -> None:
self.__location[0] += self.__control_location[0]
self.__location[1] += self.__control_location[1]
def update_length(self: object) -> None:
self.__length += 5
def update_body_head(self: object) -> None:
self.__head = [self.__location[0], self.__location[1]]
self.__body.append(self.__head)
def update(self: object, window_size: tuple, game_screen: pygame.Surface) -> None:
if self.__location[0] > window_size[0]:
self.__location[0] = 0
if self.__location[0] < 0:
self.__location[0] = window_size[0]
if self.__location[1] < 0:
self.__location[1] = window_size[1]
if self.__location[1] > window_size[1]:
self.__location[1] = 0
if len(self.__body) > self.__length:
del self.__body[0]
for position in self.__body:
pygame.draw.rect(game_screen, (0, 0, 255), (position[0], position[1], 30, 30))