-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
164 lines (126 loc) · 3.84 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
import pygame
import random
class Point:
row = 0
col = 0
def __init__(self, row, col):
self.row = row
self.col = col
def __copy__(self):
return Point(row=self.row, col=self.col)
# init env
pygame.init()
W = 800
H = 600
COL = 40
ROW = 30
size = (W, H)
window = pygame.display.set_mode(size)
pygame.display.set_caption('Eating Snake Game_GG') # Window's name
# Define Coordinate
head = Point(row=int(ROW / 2), col=int(COL / 2)) # Snake head in the middle of window
snake = [Point(row=head.row, col=head.col + 1),
Point(row=head.row, col=head.col + 2),
Point(row=head.row, col=head.col + 3),
Point(row=head.row, col=head.col + 4)]
bg_color = (255, 255, 255) # white
head_color = (0, 128, 128)
food_color = (255, 255, 0)
snake_color = (128, 128, 128) # gray
direct = 'left'
# Generate food
def gen_food():
while 1:
pos = Point(row=random.randint(0, ROW - 1), col=random.randint(0, COL - 1))
is_coll = False
if head.row == pos.row and head.col == pos.col:
is_coll = True
for _rectSnake in snake:
if _rectSnake.row == pos.row and _rectSnake.col == pos.col:
is_coll = True
break
if not is_coll:
break
return pos
food = gen_food()
# Draw with coordinates
def _rect(point, color):
"""draw snake in the window"""
cell_width = W / COL
cell_height = H / ROW
left = point.col * cell_width
top = point.row * cell_height
pygame.draw.rect(
window,
color,
(left, top, cell_width, cell_height))
pass
# ending game
quit = False
# timing control
clock = pygame.time.Clock()
while not quit:
# Handling Events
for event in pygame.event.get():
if event.type == pygame.QUIT: # quit game
quit = True
elif event.type == pygame.KEYDOWN:
if event.key == 1073741906 or event.key == 119:
if direct == 'left' or direct == 'right':
direct = 'up'
elif event.key == 1073741905 or event.key == 115:
if direct == 'left' or direct == 'right':
direct = 'down'
elif event.key == 1073741904 or event.key == 97:
if direct == 'up' or direct == 'down':
direct = 'left'
elif event.key == 1073741903 or event.key == 100:
if direct == 'up' or direct == 'down':
direct = 'right'
# Snake Eating
eat = (head.row == food.row and head.col == food.col)
# Refresh food
if eat:
food = Point(row=random.randint(0, ROW - 1), col=random.randint(0, COL - 1))
# Snake Move : 1. insert a rect at snake head
snake.insert(0, head.__copy__()) # insert new head in the first place of snake by copy the old head
# 2. Delete the last rect
if not eat:
snake.pop()
# Snake Move
if direct == 'left':
head.col -= 1
elif direct == 'right':
head.col += 1
elif direct == 'up':
head.row -= 1
elif direct == 'down':
head.row += 1
# Verify 1. Meeting wall
dead = False
for rectSnake in snake:
if rectSnake.row == head.row and rectSnake.col == head.col:
dead = True
break
# Verify 1. Meeting wall
if head.row < 0 or head.row >= ROW or head.col < 0 or head.col >= COL:
dead = True
if dead:
print("GAME OVER")
quit = True
# Background
pygame.draw.rect(
window,
bg_color,
(0, 0, W, H)) # Whole window white (position, color, size)
# Draw Snake body
for rectSnake in snake:
_rect(rectSnake, snake_color)
# Snake head
_rect(head, head_color)
# Food
_rect(food, food_color)
pygame.display.flip()
# setting frame
clock.tick(10) # sleep (60/1000)
pass