-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_game.py
86 lines (74 loc) · 3.56 KB
/
snake_game.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
from init import *
from init import np, cv2, random, sys
from init import SNAKE_LENGTH, UNIT_BLOCK, INC_LEVEL, screen_height, screen_width, EXIT_KEY, AI_MODE, UP, RIGHT, LEFT, DOWN
class snake_game:
def __init__(self):
self.snake_points = np.zeros((SNAKE_LENGTH, 2), dtype=int)
# Space Available for Snake Head and Food
self.space_x = screen_width - UNIT_BLOCK - 1
self.space_y = screen_height - UNIT_BLOCK - 1
self.direction = -1
self.score = 0
if self.space_x <= 0 or self.space_y <= 0:
print("Inconsistent Initialisation Values")
for i in range(SNAKE_LENGTH):
self.snake_points[i][0] = (int(self.space_x/2) // UNIT_BLOCK + i) * UNIT_BLOCK
self.snake_points[i][1] = (int(self.space_y/2) // UNIT_BLOCK) * UNIT_BLOCK
# Random Food Location
def generate_food(self):
self.food_x = (random.randint(0, self.space_x) // UNIT_BLOCK) * UNIT_BLOCK
self.food_y = (random.randint(0, self.space_y) // UNIT_BLOCK) * UNIT_BLOCK
self.food = np.array([self.food_x, self.food_y])
if self.snake_points[0][0] == self.food_x and self.snake_points[0][1] == self.food_y:
self.generate_food()
print("Here")
# Draw Snake and Food
def refresh_screen(self, image):
for i in range(SNAKE_LENGTH):
image = cv2.rectangle(image, (int(self.snake_points[i][0]),int(self.snake_points[i][1])), (int(self.snake_points[i][0]+UNIT_BLOCK),int(self.snake_points[i][1]+UNIT_BLOCK)), GREEN, -1)
image = cv2.rectangle(image, (self.food_x, self.food_y), (self.food_x + UNIT_BLOCK, self.food_y + UNIT_BLOCK), RED, -1)
# End Game
def end_game(self, image):
d_message = " Your Score " + str(self.score)
cv2.putText(image, d_message, (int(self.space_x/3), int(self.space_y/3)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow("Python_Snake_Game", image)
key = cv2.waitKey(0)
sys.exit(d_message)
# Run Snake
def run_snake(self, image, key):
snake_x = self.snake_points[0, 0]
snake_y = self.snake_points[0, 1]
is_hit = 0
# Check Key Press Event; Validate if it does not reverse direction
if (key == ord('a') or key == ord('j') or key == ord('A') or key == ord('J')) and (AI_MODE or self.direction != RIGHT):
self.direction = LEFT
elif (key == ord('s') or key == ord('k') or key == ord('S') or key == ord('K')) and (AI_MODE or self.direction != UP):
self.direction = DOWN
elif (key == ord('d') or key == ord('l') or key == ord('D') or key == ord('L')) and (AI_MODE or self.direction != LEFT):
self.direction = RIGHT
elif (key == ord('w') or key == ord('i') or key == ord('W') or key == ord('I')) and (AI_MODE or self.direction != DOWN):
self.direction = UP
if self.direction == LEFT:
snake_x = snake_x - UNIT_BLOCK
elif self.direction == DOWN:
snake_y = snake_y + UNIT_BLOCK
elif self.direction == RIGHT:
snake_x = snake_x + UNIT_BLOCK
elif self.direction == UP:
snake_y = snake_y - UNIT_BLOCK
for i in range(SNAKE_LENGTH):
if not AI_MODE and self.snake_points[i, 0] == snake_x and self.snake_points[i, 1] == snake_y:
is_hit = 1
is_boundary_hit = (snake_x < 0 or snake_y < 0 or snake_x >= screen_width or snake_y >= screen_height)
is_exit = (key == EXIT_KEY)
# Reached Boundaries or Pressed Exit Key or Hit self
if is_boundary_hit or is_exit or is_hit:
self.end_game(image)
# Reached Food
elif snake_x == self.food_x and snake_y == self.food_y:
self.generate_food()
self.score = self.score + 1
self.snake_points[1:SNAKE_LENGTH, :] = self.snake_points[0:SNAKE_LENGTH - 1, :]
self.snake_points[0, 0] = snake_x
self.snake_points[0, 1] = snake_y
return self.score