-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
356 lines (292 loc) · 11.7 KB
/
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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import pygame
import random
from sklearn.linear_model import RidgeClassifierCV
from sklearn.linear_model import LogisticRegression
import numpy as np
import cx_Freeze
display_width = 800
display_height = 600
Red = (255, 15, 10)
Blue = (10, 10, 255)
Green = (50, 150, 80)
White = (255, 255, 255)
Grey = (210, 210, 210)
Black = (0, 0, 0)
Shape_rect = 'rect'
Shape_circle = 'circle'
random.seed(0)
pygame.init()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 16)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('GAME!')
clock = pygame.time.Clock()
# image1 = pygame.image.load('clipart11701.png')
# image1 = pygame.transform.scale(image1, (120, 100))
#
# image2 = pygame.image.load('Corruption_and_Energy.png')
# image2 = pygame.transform.scale(image2, (120, 100))
image_width = 120
to_draw_pos = (100, 260)
def the_object(image, x, y):
gameDisplay.blit(image, (x, y))
def things(thing_x, thing_y, thing_w, thing_h, thing_color):
pygame.draw.rect(gameDisplay, Red, [thing_x, thing_y, thing_w, thing_h])
class Circle:
def __init__(self, position):
self.position = position
def draw(self, position):
pygame.draw.ellipse(gameDisplay, Blue, [position[0], position[1], 20, 20])
class Rect:
def __init__(self, position):
self.position = position
def draw(self, position):
pygame.draw.rect(gameDisplay, Red, [position[0], position[1], 20, 20])
class Drop:
def __init__(self, position=[random.randrange(0, display_width), 600], vel=[0, -(random.randrange(30, 45))],
acc=[0, 0]):
self.position = position
self.vel = vel
self.acc = acc
self.particles = []
self.g = 3
def draw(self, particle = False):
if not particle:
pygame.draw.rect(gameDisplay, Red, [self.position[0], self.position[1], 20, 20])
else:
pygame.draw.rect(gameDisplay, Green, [self.position[0], self.position[1], 5, 5])
def update(self):
self.position[0] += self.vel[0]
self.position[1] += self.vel[1]
self.vel[0] += self.acc[0]
self.vel[1] += self.acc[1] + self.g
def explode(self):
for i in range(0, 100):
temp = Drop(position=self.position, vel=[(random.randrange(-5, 1)), (random.randrange(-5, 1))],
acc=[0, 0])
# temp = Drop(position=self.position, vel=[(random.randrange(-15, 5)), (random.randrange(-15, 5))],
# acc=[float(random.randrange(-20, 50)) / 10, float(random.randrange(-20, 50)) / 10])
self.particles.append(temp)
class Particles(Drop):
def __init__(self, position=[random.randrange(0, display_width), 600], vel=[0, -(random.randrange(30, 45))],
acc=[0, 0]):
self.position = position
self.vel = vel
self.acc = acc
self.particles = []
self.g = 3
def draw(self, particle = False):
if not particle:
pygame.draw.rect(gameDisplay, Red, [self.position[0], self.position[1], 20, 20])
else:
pygame.draw.rect(gameDisplay, Green, [self.position[0], self.position[1], 5, 5])
def update(self):
self.position[0] += self.vel[0]
self.position[1] += self.vel[1]
self.vel[0] += self.acc[0]
self.vel[1] += self.acc[1] + self.g
def explode(self):
for i in range(0, 100):
temp = Drop(position=self.position, vel=[(random.randrange(-5, 1)), (random.randrange(-5, 1))],
acc=[0, 0])
# temp = Drop(position=self.position, vel=[(random.randrange(-15, 5)), (random.randrange(-15, 5))],
# acc=[float(random.randrange(-20, 50)) / 10, float(random.randrange(-20, 50)) / 10])
self.particles.append(temp)
class window:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def draw(self):
pygame.draw.rect(gameDisplay, Red, [self.x, self.y, self.w, self.h])
def clicked(self, pos):
if self.x <= pos[0] <= self.x + self.w and self.y <= pos[1] <= self.y + self.h:
return True
else:
return False
def draw_the_thing(thing):
thing.draw(thing)
def draw_classifier_helper(weights):
points = []
# (-w[0] - w[1] * X_1) / w[2]
plot_helper = [i for i in range(-800, 600, 20)]
X_2_values = [(-weights[0] - weights[1] * i) / weights[2] for i in plot_helper]
for i in range(len(plot_helper)):
points.append((plot_helper[i] + 400, X_2_values[i] + 300))
return points
def Classify(Circles, Rects, model_name='RG'):
X = []
y = []
for i in Circles:
X.append(i.position)
y.append(0)
for i in Rects:
X.append(i.position)
y.append(1)
X = np.array(X)
y = np.array(y)
for i in X:
i[0] -= 400
i[1] -= 300
if model_name == 'RG':
model = RidgeClassifierCV()
if model_name == 'LR':
model = LogisticRegression()
model.fit(X, y)
coef = model.coef_
intercept = model.intercept_
print(model.score(X, y))
return draw_classifier_helper([intercept[0], coef[0][0], coef[0][1]])
def Main_Menu():
intro = True
Button_1 = window(300, 500, 80, 40)
Button_2 = window(400, 500, 80, 40)
while intro:
for i in pygame.event.get():
if i.type == pygame.QUIT:
intro = False
if i.type == pygame.MOUSEBUTTONDOWN:
if Button_1.clicked(i.pos):
Classify_Game()
gameDisplay.fill(White)
Button_1.draw()
Button_2.draw()
pygame.display.update()
clock.tick(60)
def Classify_Game():
Cirles = []
Rects = []
Dropping_Mode = False
Shape_to_draw = ''
Draw_clf_line = False
draw_particles = False
event = False
a = Drop()
list_of_drops = list()
list_of_particles = []
ck = False
newWindow = window(10, 10, 30, 40)
model_name = 'RG'
for x in range(1, 2):
list_of_drops.append(Drop(position=[random.randrange(0, display_width), 600]))
while not event:
for i in pygame.event.get():
#####
if i.type == pygame.MOUSEBUTTONDOWN and Dropping_Mode and Shape_to_draw == 'circle':
to_draw_pos = i.pos
shape_to_be_drawn = Circle(position=to_draw_pos)
Cirles.append(shape_to_be_drawn)
if i.type == pygame.MOUSEBUTTONDOWN and Dropping_Mode and Shape_to_draw == 'rect':
to_draw_pos = i.pos
shape_to_be_drawn = Rect(position=to_draw_pos)
Rects.append(shape_to_be_drawn)
#####
if i.type == pygame.MOUSEBUTTONDOWN:
ck = newWindow.clicked(i.pos)
if i.type == pygame.QUIT:
event = True
####
# Buttons:
if i.type == pygame.MOUSEBUTTONDOWN:
# left
if i.pos[0] < display_width * 0.52 and i.pos[1] > display_height * 0.92:
if i.pos[0] > display_width * 0.44 and i.pos[1] < display_height:
if Dropping_Mode and Shape_to_draw == 'circle':
if len(Cirles) != 0:
Cirles.pop()
# Dropping_Mode = False
if Dropping_Mode and Shape_to_draw == 'rect':
if len(Rects) != 0:
Rects.pop()
# Dropping_Mode = False
Shape_to_draw = 'rect'
Dropping_Mode = True
# right
if i.pos[0] > display_width * 0.52 and i.pos[1] > display_height * 0.92:
if i.pos[0] < display_width * 0.60 and i.pos[1] < display_height:
if Dropping_Mode and Shape_to_draw == 'rect':
if len(Rects) != 0:
Rects.pop()
# Dropping_Mode = False
if Dropping_Mode and Shape_to_draw == 'circle':
if len(Cirles) != 0:
Cirles.pop()
# Dropping_Mode = False
Dropping_Mode = True
Shape_to_draw = 'circle'
# Classify:
if i.pos[0] > display_width * 0.5 and i.pos[1] < display_height * 0 + 35:
if Dropping_Mode and Shape_to_draw == 'rect':
if len(Rects) != 0:
Rects.pop()
# Dropping_Mode = False
if Dropping_Mode and Shape_to_draw == 'circle':
if len(Cirles) != 0:
Cirles.pop()
if i.pos[0] < display_width * 0.5 + 60:
points = Classify(Cirles, Rects, )
Dropping_Mode = False
Draw_clf_line = True
#####
gameDisplay.fill(Grey)
if len(Cirles) is not 0:
for i in Cirles:
i.draw(i.position)
if len(Rects) is not 0:
for i in Rects:
i.draw(i.position)
if Draw_clf_line:
for i in range(len(points)):
if i < len(points) - 1:
pygame.draw.line(gameDisplay, Black, [points[i][0], points[i][1]],
[points[i + 1][0], points[i + 1][1]], 3)
############################
# Buttons:
pygame.draw.rect(gameDisplay, Red, [display_width * 0.46, display_height * 0.93, 35, 35])
pygame.draw.ellipse(gameDisplay, Blue, [display_width * 0.54, display_height * 0.93, 35, 35])
pygame.draw.line(gameDisplay, Black, [display_width * 0.44, display_height],
[display_width * 0.44, display_height * 0.92], 1)
pygame.draw.line(gameDisplay, Black, [display_width * 0.60, display_height],
[display_width * 0.60, display_height * 0.92], 1)
pygame.draw.line(gameDisplay, Black, [display_width * 0.44, display_height * 0.92],
[display_width * 0.60, display_height * 0.92], 1)
pygame.draw.line(gameDisplay, Black, [display_width * 0.52, display_height],
[display_width * 0.52, display_height * 0.92], 1)
# Classify
pygame.draw.rect(gameDisplay, Green, [display_width * 0.50, display_height * 0, 60, 35])
textsurface = myfont.render('Classify', False, (0, 0, 0))
gameDisplay.blit(textsurface, (display_width * 0.50, 0))
############################
##################
# rain
# for x in list_of_drops:
# if x.position[1] > 600:
# x.position[1] = 600
# x.vel = [-i * 0.7 for i in x.vel]
# x.update()
# x.draw()
newWindow.draw()
a.update()
a.draw()
if not draw_particles:
if a.vel[1] > 0:
a.explode()
list_of_particles = a.particles
draw_particles = True
if len(list_of_particles) != 0:
for i in list_of_particles:
i.update()
i.draw(particle=True)
if ck:
#print('go')
#newWindow.x, newWindow.y = random.randint(0, display_width - newWindow.w), random.randint(0, display_height - newWindow.h)
model_name = 'LR'
ck = False
###########
pygame.display.update()
clock.tick(60)
Main_Menu()
#game_loop()
pygame.quit()
quit()