-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.py
222 lines (193 loc) · 8.31 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
import pygame
import time
import random
import numpy as np
import sys
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
class SnakeGame:
def __init__(self, snake_block, block_count,seed=0):
pygame.init()
self.snake_list=[]
self.snake_block = snake_block
self.snake_speed = 10
self.block_count = block_count
self.dis_width = self.snake_block * self.block_count
self.dis_height = self.snake_block * self.block_count
self.snake_length=0
self.direction=None
self.game_over=True
# snake_list: 蛇身体的数组
#
# self.game_close=True
self.seed_value = seed
random.seed(seed)
self.reset(seed)
def reset(self,seed=0):
self.game_over = True
# self.game_close = False
x1= self.block_count // 2 * self.snake_block
y1 = self.block_count // 2 * self.snake_block
self.snake=[x1,y1]
self.snake_length=1
self.snake_list.append(self.snake)
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / self.snake_block) * self.snake_block
foody = round(random.randrange(0, self.dis_height - self.snake_block) / self.snake_block) * self.snake_block
while [foodx, foody] in self.snake_list:
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / self.snake_block) * self.snake_block
foody = round(random.randrange(0, self.dis_height - self.snake_block) / self.snake_block) * self.snake_block
self.food = [foodx,foody]
self.score = 0
def step(self, action):
x1, y1 = self.snake_list[0]
if len(self.snake_list) > self.snake_length:
del self.snake_list[self.snake_length-1]
if action == 0: #up
y1 -= self.snake_block
self.direction=0
elif action == 1: #down
y1 += self.snake_block
self.direction=1
elif action == 2: #left
x1 -= self.snake_block
self.direction=2
elif action == 3: #right
x1 += self.snake_block
self.direction=3
self.snake_list.insert(0,[x1,y1])
if(x1==self.food[0] and y1==self.food[1]):
self.score+=100
self.snake_length+=1
while [foodx, foody] in self.snake_list:
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / self.snake_block) * self.snake_block
foody = round(random.randrange(0, self.dis_height - self.snake_block) / self.snake_block) * self.snake_block
self.food = [foodx,foody]
if x1 >= self.dis_width or x1 < 0 or y1 >= self.dis_height or y1 < 0 or [x1,y1] in self.snake_list:
self.score-=100/self.snake_length
self.game_over = True
info ={
"snake_size": self.snake_length,
"snake_list": np.array(self.snake_list),
"food_pos": np.array(self.food),
"score": self.score
}
return info,self.game_over
def render(self):
pygame.display.set_mode((self.dis_width, self.dis_height))
pygame.display.set_caption('Snake Game')
pygame.draw.rect(self.dis, yellow, [self.food[0], self.food[1], self.snake_block, self.snake_block])
self.show_score(self.score)
for x in self.snake_list[::-1]:
cnt += 1
if cnt == 1:
pygame.draw.rect(self.dis, red, [x[0], x[1], self.snake_block, self.snake_block])
else:
pygame.draw.rect(self.dis, green, [x[0], x[1], self.snake_block, self.snake_block])
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def our_snake(self, snake_block, snake_list):
cnt = 0
for x in snake_list[::-1]:
cnt += 1
if cnt == 1:
pygame.draw.rect(self.dis, red, [x[0], x[1], snake_block, snake_block])
else:
pygame.draw.rect(self.dis, green, [x[0], x[1], snake_block, snake_block])
self.show_score(self.score)
def message(self, msg, color):
font_style = pygame.font.SysFont(None, 50)
mesg = font_style.render(msg, True, color)
self.dis.blit(mesg, [self.dis_width / 6, self.dis_height / 3])
def show_score(self, score):
score_font = pygame.font.SysFont(None, 35)
score_text = score_font.render("Score: " + str(score), True, white)
self.dis.blit(score_text, [10, 10])
def game_loop(self):
game_over = False
game_close = False
x1 = self.block_count // 2 * self.snake_block
y1 = self.block_count // 2 * self.snake_block
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / self.snake_block) * self.snake_block
foody = round(random.randrange(0, self.dis_height - self.snake_block) / self.snake_block) * self.snake_block
last_event = 0
self.dis = pygame.display.set_mode((self.dis_width, self.dis_height))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
while not game_over:
while game_close:
self.dis.fill(blue)
self.message("You lost! Press Q-Quit or C-Play Again", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
self.game_loop()
changed = 0
for event in pygame.event.get():
if changed == 1:
continue
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and last_event != 2:
x1_change = -self.snake_block
y1_change = 0
last_event = 1
changed = 1
elif event.key == pygame.K_RIGHT and last_event != 1:
x1_change = self.snake_block
y1_change = 0
last_event = 2
changed = 1
elif event.key == pygame.K_UP and last_event != 4:
y1_change = -self.snake_block
x1_change = 0
last_event = 3
changed = 1
elif event.key == pygame.K_DOWN and last_event != 3:
y1_change = self.snake_block
x1_change = 0
last_event = 4
changed = 1
if x1 >= self.dis_width or x1 < 0 or y1 >= self.dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
self.dis.fill(black)
pygame.draw.rect(self.dis, yellow, [foodx, foody, self.snake_block, self.snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
self.our_snake(self.snake_block, snake_List)
pygame.display.update()
if x1 == foodx and y1 == foody:
self.score += 100
while [foodx, foody] in snake_List:
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / self.snake_block) * self.snake_block
foody = round(random.randrange(0, self.dis_height - self.snake_block) / self.snake_block) * self.snake_block
Length_of_snake += 1
clock.tick(self.snake_speed)
pygame.quit()
# Create an instance of the SnakeGame class
game = SnakeGame(12,20)
game.game_loop()
# Start the game loop