-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateDataset.py
197 lines (173 loc) · 7.54 KB
/
generateDataset.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
import random
import arcade
from scipy.spatial.distance import euclidean
import pandas as pd
WIN_HEIGHT = 600
WIN_WIDTH = 600
DEFAULT_FONT_SIZE = 5
class Dynamite(arcade.Sprite):
def __init__(self, w, h):
arcade.Sprite.__init__(self)
self.img = 'img/dynamite.png'
self.dynamite = arcade.Sprite(self.img, 0.02)
self.dynamite.center_x = random.randint(10, w - 10)
self.dynamite.center_y = random.randint(10, h - 10)
def draw(self):
self.dynamite.draw()
class Pear(arcade.Sprite):
def __init__(self, w, h):
arcade.Sprite.__init__(self)
self.img = 'img/pear.png'
self.pear = arcade.Sprite(self.img, 0.1)
self.pear.center_x = random.randint(10, w - 10)
self.pear.center_y = random.randint(10, h - 10)
def draw(self):
self.pear.draw()
class Apple(arcade.Sprite):
def __init__(self, w, h):
arcade.Sprite.__init__(self)
self.img = 'img/apple.png'
self.apple = arcade.Sprite(self.img, 0.03)
self.apple.center_x = random.randint(10, w - 10)
self.apple.center_y = random.randint(10, h - 10)
def draw(self):
self.apple.draw()
class Snake(arcade.Sprite):
def __init__(self, w, h):
arcade.Sprite.__init__(self)
self.color_manual = [arcade.color.GREEN, arcade.color.RED, arcade.color.BLACK, arcade.color.CYBER_YELLOW,
arcade.color.ORANGE, arcade.color.PINK, arcade.color.BROWN]
self.speed = 1
self.width = 16
self.height = 16
self.center_x = w // 2
self.center_y = h // 2
self.r = 8
self.change_x = 0
self.change_y = 0
self.score = 1
self.body = []
self.body.append([self.center_x, self.center_y])
def draw(self):
for index, item in enumerate(self.body):
arcade.draw_circle_filled(item[0], item[1], self.r, self.color_manual[index % (len(self.color_manual))])
def move(self):
for i in range(len(self.body) - 1, 0, -1):
self.body[i][0] = self.body[i - 1][0]
self.body[i][1] = self.body[i - 1][1]
self.center_x += self.speed * self.change_x
self.center_y += self.speed * self.change_y
if self.body:
self.body[0][0] += self.speed * self.change_x
self.body[0][1] += self.speed * self.change_y
def eat(self, food):
if food == 'apple':
self.score += 1
self.body.append([self.body[len(self.body) - 1][0] + 3000, self.body[len(self.body) - 1][1]])
elif food == 'pear':
self.score += 2
self.body.append([self.body[len(self.body) - 1][0] + 3000, self.body[len(self.body) - 1][1]])
self.body.append([self.body[len(self.body) - 1][0] + 3000, self.body[len(self.body) - 1][1]])
elif food == 'dynamite':
self.score -= 1
self.body.pop()
class Game(arcade.Window):
def __init__(self):
arcade.Window.__init__(self, WIN_WIDTH, WIN_HEIGHT, "Clever Snake by Benyamin Zojaji")
arcade.set_background_color(arcade.color.SAND)
self.snake = Snake(WIN_WIDTH, WIN_HEIGHT)
self.apple = Apple(WIN_WIDTH, WIN_HEIGHT)
self.pear = Pear(WIN_WIDTH, WIN_HEIGHT)
self.dynamite = Dynamite(WIN_WIDTH, WIN_HEIGHT)
self.Data = []
self.flag_getData = 0
def on_draw(self):
arcade.start_render()
self.snake.draw()
self.apple.draw()
self.pear.draw()
self.dynamite.draw()
start_x = 10
start_y = WIN_HEIGHT - 20
arcade.draw_text('Score: %i' % self.snake.score,
start_x, start_y,
arcade.color.BLACK, DEFAULT_FONT_SIZE * 2, width=WIN_WIDTH, align='left')
if self.snake.score <= 0 or self.snake.center_x < 0 or self.snake.center_x > WIN_WIDTH or self.snake.center_y < 0 or self.snake.center_y > WIN_HEIGHT:
arcade.draw_text('GAME OVER',
WIN_WIDTH // 2, WIN_HEIGHT // 2,
arcade.color.BLACK, DEFAULT_FONT_SIZE * 5, width=WIN_WIDTH, align='left')
arcade.exit()
elif self.snake.score == 30: # thats enough for now! :)
Data = pd.DataFrame(self.Data)
Data.columns=['x_snake', 'y_snake', 'x_apple', 'y_apple', 'x_sub', 'y_sub', 'direction']
Data.to_csv('data/snakeData.csv', index=False)
arcade.exit()
def on_update(self, delta_time: float):
if self.snake.center_x < self.apple.apple.center_x:
self.snake.change_x = 1
elif self.snake.center_x > self.apple.apple.center_x:
self.snake.change_x = -1
else:
self.snake.change_x = 0
if self.snake.center_y < self.apple.apple.center_y:
self.snake.change_y = 1
elif self.snake.center_y > self.apple.apple.center_y:
self.snake.change_y = -1
else:
self.snake.change_y = 0
self.data = [] # wu, wd, wl, wr, dis_snake-apple(euclidean), x-snake-apple(not abs), y-snake-apple(not abs), x-apple, y-apple, direction(1-8)
# 7 0 1
# \ | /
# \|/
# 6 <---|---> 2
# /|\
# / | \
# 5 4 3
#if self.flag_getData == 2:
self.data.append(self.snake.center_x)
self.data.append(self.snake.center_y)
self.data.append(self.apple.apple.center_x)
self.data.append(self.apple.apple.center_y)
self.data.append(self.snake.center_x - self.apple.apple.center_x)
self.data.append(self.snake.center_y - self.apple.apple.center_y)
#self.data.append(abs(WIN_HEIGHT - self.snake.center_y))
#self.data.append(abs(WIN_WIDTH - self.snake.center_x))
#self.data.append(round(euclidean([self.snake.center_x, self.snake.center_y], [self.apple.apple.center_x, self.apple.apple.center_y])))
if self.snake.change_x == 1:
if self.snake.change_y == 1:
self.data.append(1)
elif self.snake.change_y == -1:
self.data.append(3)
elif self.snake.change_y == 0:
self.data.append(2)
elif self.snake.change_x == -1:
if self.snake.change_y == 1:
self.data.append(7)
elif self.snake.change_y == -1:
self.data.append(5)
elif self.snake.change_y == 0:
self.data.append(6)
elif self.snake.change_x == 0:
if self.snake.change_y == 1:
self.data.append(0)
elif self.snake.change_y == -1:
self.data.append(4)
# elif self.snake.change_y == 0:
# pass
self.Data.append(self.data)
#self.flag_getData = 0
#else:
#self.flag_getData += 1
self.snake.move()
if arcade.check_for_collision(self.snake, self.apple.apple):
self.snake.eat('apple')
self.apple = Apple(WIN_WIDTH, WIN_HEIGHT)
elif arcade.check_for_collision(self.snake, self.pear.pear):
self.snake.eat('pear')
self.pear = Pear(WIN_WIDTH, WIN_HEIGHT)
elif arcade.check_for_collision(self.snake, self.dynamite.dynamite):
self.snake.eat('dynamite')
self.dynamite = Dynamite(WIN_WIDTH, WIN_HEIGHT)
if __name__ == "__main__":
game = Game()
arcade.run()