-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathversion7.py
More file actions
475 lines (402 loc) · 17 KB
/
version7.py
File metadata and controls
475 lines (402 loc) · 17 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import pygame
import random
import numpy
import algorithms
import heapq
import sys
def step(surface):
global f, r, number_of_steps
number_of_steps += 1
f.move()
if f.position == r.position:
winner = "fox"
return
r.move()
class scroll(object):
def __init__(self, position_scroll, position_button, color_scroll, color_button):
self.position_scroll = position_scroll
self.position_button = position_button
self.color_scroll = color_scroll
self.color_button = color_button
self.width_scroll = 100
self.width_button = 10
self.height_scroll = 15
self.height_button = 15
def draw(self, surface):
pygame.draw.rect(surface, self.color_scroll,
(self.position_scroll[0], self.position_scroll[1], self.width_scroll, self.height_scroll))
pygame.draw.rect(surface, self.color_button,
(self.position_button[0], self.position_button[1], self.width_button, self.height_button))
mouse_pos = pygame.mouse.get_pos()
mouse_click = pygame.mouse.get_pressed()
if (self.position_scroll[0] <= mouse_pos[0] <= self.position_scroll[0] + self.width_scroll - 8 and
self.position_scroll[1] <= mouse_pos[1] <= self.position_scroll[1] + self.height_scroll and
mouse_click[0] == 1):
(x, y) = (mouse_pos[0], self.position_button[1])
self.position_button = (x, y)
def speed(self):
return (1000 - (self.position_button[0] - self.position_scroll[0]) * 10 + 5) / 2
class button(object):
def __init__(self, position, activeColor, inactiveColor, text):
self.position = position
self.activeColor = activeColor
self.inactiveColor = inactiveColor
self.text = text
self.width = 55
self.height = 25
self.state = False
def pressed(self):
click = pygame.mouse.get_pressed()
mouse_position = pygame.mouse.get_pos()
if click[0] == True and self.state == False:
if self.position[0] < mouse_position[0] < self.position[0] + self.width and self.position[1] < \
mouse_position[1] < self.position[1] + self.height:
self.state = True
return True
return False
def unpress(self):
self.state = False
def draw(self, surface):
cur = pygame.mouse.get_pos()
if self.position[0] < cur[0] < self.position[0] + self.width and self.position[1] < cur[1] < self.position[
1] + self.height:
pygame.draw.rect(surface, self.inactiveColor, (self.position[0], self.position[1], self.width, self.height))
else:
pygame.draw.rect(surface, self.activeColor, (self.position[0], self.position[1], self.width, self.height))
textSurface, textRect = text_objects(self.text, (0, 0, 0))
textRect.center = (self.position[0] + self.width // 2, self.position[1] + self.height // 2)
surface.blit(textSurface, textRect)
class bush(object):
rows = 25
width = 500
def __init__(self, position, color=(0, 255, 0)):
self.position = position
self.color = color
def draw(self, surface):
dis = width // rows
i = self.position[0]
j = self.position[1]
pygame.draw.rect(surface, self.color, (i * dis + 1, j * dis + 1, dis - 2, dis - 2))
def text_objects(text, color, size="small"):
if size == "small":
font = pygame.font.SysFont("comicsansms", 18)
elif size == "medium":
font = pygame.font.SysFont("comicsansms", 23)
else:
font = pygame.font.SysFont("comicsansms", 33)
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
def isCollision(coord):
global position_matrix
if position_matrix[coord[0]][coord[1]] == 1:
return True
return False
def outOffBoundaries(coord):
if coord[0] < 0 or coord[0] > 24 or coord[1] < 0 or coord[1] > 24:
return True
return False
def number_of_bad_neighbours(position):
global position_matrix
sol = 0
for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
curr_position = (position[0] + dx, position[1] + dy)
if outOffBoundaries(curr_position) or isCollision(curr_position):
sol += 1
return sol
class fox(object):
rows = 25
width = 500
rabbit_last_seen = None
def __init__(self, start, color):
self.position = start
self.prev_steps = []
self.color = color
def draw(self, surface, eyes=False):
dis = self.width // self.rows
i = self.position[0]
j = self.position[1]
pygame.draw.polygon(surface, self.color, [(i * dis + 1, j * dis + 1), (i * dis + 1 + dis, j * dis + 1),
(i * dis + 1 + dis // 2, j * dis + 1 + dis)])
if eyes:
center = dis // 2
radius = 3
circleMiddle = (i * dis + center - radius, j * dis + 8)
circleMiddle2 = (i * dis + dis - radius * 2, j * dis + 8)
pygame.draw.circle(surface, (0, 204, 0), circleMiddle, radius)
pygame.draw.circle(surface, (0, 204, 0), circleMiddle2, radius)
def move(self):
self.prev_steps.append(self.position)
p = random.randrange(0, 1000)
if number_of_steps > 10 and self.position == self.prev_steps[-9]:
p = 0
if p > 10 and algorithms.see_each_other(self.position, r.position, position_matrix):
path = algorithms.minimal_distance(self.position, r.position, position_matrix)
self.position = path[-1]
self.rabbit_last_seen = r.position
elif p > 10 and self.rabbit_last_seen != None and self.rabbit_last_seen != self.position:
path = algorithms.minimal_distance(self.position, r.position, position_matrix)
self.position = path[-1]
else:
options = []
for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
curr_position = (self.position[0] + dx, self.position[1] + dy)
if outOffBoundaries(curr_position) or isCollision(curr_position):
continue
if len(self.prev_steps) > 1 and curr_position == self.prev_steps[-2]:
continue
options.append(curr_position)
self.position = random.choice(options)
class rabbit(object):
rows = 25
width = 500
turn = 0
fox_last_seen = []
prev_steps = []
def __init__(self, start, color):
self.position = start
self.color = color
def draw(self, surface, eyes=False):
dis = self.width // self.rows
i = self.position[0]
j = self.position[1]
pygame.draw.rect(surface, self.color, (i * dis + 1, j * dis + 1, dis - 2, dis - 2))
if eyes:
center = dis // 2
radius = 3
circleMiddle = (i * dis + center - radius, j * dis + 8)
circleMiddle2 = (i * dis + dis - radius * 2, j * dis + 8)
pygame.draw.circle(surface, (0, 0, 0), circleMiddle, radius)
pygame.draw.circle(surface, (0, 0, 0), circleMiddle2, radius)
def move(self):
self.prev_steps.append(self.position)
if algorithms.see_each_other(self.position, f.position, position_matrix):
self.fox_last_seen.append(f.position)
else:
if len(self.fox_last_seen) > 0:
self.fox_last_seen.append(self.fox_last_seen[-1])
else:
self.fox_last_seen.append(None)
if self.fox_last_seen[-1] != None:
options = []
for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
curr_position = (self.position[0] + dx, self.position[1] + dy)
if outOffBoundaries(curr_position) or isCollision(curr_position):
continue
if len(self.prev_steps) > 1 and curr_position == self.prev_steps[-2]:
continue
if algorithms.distance(f.position, curr_position) == 1:
continue
if number_of_bad_neighbours(curr_position) == 3:
continue
dist_from_fox = abs(curr_position[0] - self.fox_last_seen[-1][0]) + abs(
curr_position[1] - self.fox_last_seen[-1][1])
curr_cost = algorithms.f(12 - dist_from_fox)
curr_cost += 5 * algorithms.f(abs(curr_position[0] - 12)) + algorithms.f(abs(curr_position[1] - 12))
curr_cost -= 10 * self.number_of_good_bushes()
if self.fox_last_seen[-1] != None:
if not algorithms.see_each_other(curr_position, self.fox_last_seen[-1], position_matrix):
curr_cost -= 100
heapq.heappush(options, (curr_cost, curr_position))
if options == []:
self.position = self.prev_steps[-2]
else:
self.position = heapq.heappop(options)[1]
else:
options = []
for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
curr_position = (self.position[0] + dx, self.position[1] + dy)
if outOffBoundaries(curr_position) or isCollision(curr_position):
continue
if number_of_bad_neighbours(curr_position) == 3:
continue
if curr_position == self.position:
continue
options.append(curr_position)
self.position = random.choice(options)
self.turn += 1
def number_of_good_bushes(self):
sol = 0
for bush in good_bushes:
if algorithms.manhattan_distance(bush, self.position) + 1 < algorithms.manhattan_distance(bush,self.fox_last_seen[-1]):
sol += 1
return sol
def drawGrid(width, rows, surface):
sizeBtwn = width // rows
x = 0
y = 0
for l in range(rows):
x = x + sizeBtwn
y = y + sizeBtwn
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, width))
pygame.draw.line(surface, (255, 255, 255), (0, y), (width, y))
pygame.draw.rect(surface, (255, 255, 255), (0, 500, 500, 200))
def set_bushes(density):
global position_matrix, good_bushes, bush_coords, r, f
bush_coords = []
path = algorithms.path(r.position[0], r.position[1], f.position[0], f.position[1])
while density > 0:
x = random.randrange(0, 25)
y = random.randrange(0, 25)
if position_matrix[x][y] == 0 and (x, y) not in [f.position, r.position] and (x, y) not in path:
bush_coords.append((x, y))
position_matrix[x][y] = 1
density -= 1
bad_bushes = [[0 for i in range(25)] for i in range(25)]
itr = [(0, i) for i in range(25)] + [(24, i) for i in range(25)]
itr += [(i, 0) for i in range(25)] + [(i, 24) for i in range(25)]
for (x, y) in itr:
if position_matrix[x][y] == 1:
bad_bushes[x][y] = 1
for d in range(1, 12):
itr = [(d, i) for i in range(d, 25 - d)] + [(24 - d, i) for i in range(d, 25 - d)]
itr += [(i, d) for i in range(d, 25 - d)] + [(i, 24 - d) for i in range(d, 25 - d)]
for (x, y) in itr:
if position_matrix[x][y] == 0:
continue
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if bad_bushes[x + dx][y + dy] == 1:
bad_bushes[x][y] = 1
good_bushes = []
for bush in bush_coords:
(x, y) = bush
if bad_bushes[x][y] == 0:
good_bushes.append(bush)
def draw_bushes(surface, set_bushes):
global bush
for coord in bush_coords:
b = bush(coord, (0, 255, 0))
b.draw(surface)
def refreshWindow(surface, bush_coords):
global s, rows, width, f, r, bReplay, bRun, bStop, bStep, bReset, number_of_steps
surface.fill((0, 0, 0))
drawGrid(width, rows, surface)
draw_bushes(surface, bush_coords)
f.draw(surface, True)
r.draw(surface, True)
s.draw(surface)
textSurface, textRect = text_objects("Speed", (0, 0, 0))
textRect.center = (30, 533)
surface.blit(textSurface, textRect)
textSurface, textRect = text_objects("Step: " + str(number_of_steps), (0, 0, 0), "medium")
textRect.center = (445, 565)
surface.blit(textSurface, textRect)
for b in buttons:
b.draw(surface)
pygame.display.update()
def message_to_screen(surface, surface_size, text, color, y_displace=0, size="small"):
textSurface, textRect = text_objects(text, color, size)
textRect.center = (surface_size[0] // 2, surface_size[1] // 2 + y_displace)
surface.blit(textSurface, textRect)
def set_table():
global f, r, bush_coords, good_bushes
f = fox((random.randrange(3, 20), random.randrange(3, 20)), (255, 51, 0))
(f_x, f_y) = f.position
options = [(f_x + i, f_y + 7 - i) for i in range(8)]
options += [(f_x - i, f_y + 7 - i) for i in range(8)]
r_pos = random.choice(options)
while outOffBoundaries(r_pos):
r_pos = random.choice(options)
r = rabbit(r_pos, (255, 255, 255))
set_bushes(60)
def check_gameover(gameOver):
if (f.position == r.position):
winner = "fox"
gameOver = True
if number_of_steps == 200:
winner = "rabbit"
gameOver = True
while gameOver:
field.fill((0, 0, 0))
if winner == "fox":
message_to_screen(field, (500, 560), "GAME OVER", (255, 51, 0), -25, "large")
message_to_screen(field, (500, 560), "Fox won", (255, 51, 0))
message_to_screen(field, (500, 560), "Press r to restart or q to quit", (255, 51, 0), 25)
elif winner == "rabbit":
message_to_screen(field, (500, 560), "GAME OVER", (0, 255, 0), -25, "large")
message_to_screen(field, (500, 560), "Rabbit won", (0, 255, 0))
message_to_screen(field, (500, 560), "Press r to restart or q to quit", (0, 255, 0), 25)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
main()
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
return gameOver
def main():
global field, winner, good_bushes, s, bush_coords, rows, width, f, r, position_matrix, bReplay, bRun, bStop, bStep, bReplay, buttons, number_of_steps
rows = 25
width = 500
position_matrix = numpy.zeros((rows + 1, rows + 1))
field = pygame.display.set_mode((width, 580))
pygame.display.set_caption('Rabbit hunt')
game = True
bRun = button((200, 520), (210, 210, 210), (160, 160, 160), "Run")
bStop = button((260, 520), (210, 210, 210), (160, 160, 160), "Stop")
bStep = button((320, 520), (210, 210, 210), (160, 160, 160), "Step")
bReset = button((380, 520), (210, 210, 210), (160, 160, 160), "Reset")
bReplay = button((440, 520), (210, 210, 210), (160, 160, 160), "Replay")
buttons = [bRun, bStop, bStep, bReset, bReplay]
s = scroll((65, 525), (155, 525), (180, 180, 180), (130, 130, 130))
clock = pygame.time.Clock()
FPS = 120
set_table()
running = False
stepping = False
replay = False
number_of_steps = 0
winner = None
gameOver = False
while game:
dt = clock.tick(FPS)
gameOver = check_gameover(gameOver)
miliseconds_passed = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
for b in buttons:
b.unpress()
if bStop.pressed():
running = False
stepping = False
if bStep.pressed():
stepping = True
running = False
if bRun.pressed():
running = True
stepping = False
if bReset.pressed():
main()
if bReplay.pressed() and number_of_steps >= 1:
replay = True
refreshWindow(field, bush_coords)
miliseconds_passed += clock.tick(FPS)
if miliseconds_passed > s.speed():
break
if replay:
running = False
stepping = False
number_of_steps -= 1
f.position = f.prev_steps[-1]
f.prev_steps = f.prev_steps[:-1]
r.position = r.prev_steps[-1]
r.prev_steps = r.prev_steps[:-1]
r.fox_last_seen = r.fox_last_seen[:-1]
replay = False
if running:
step(field)
if stepping:
step(field)
running = False
stepping = False
refreshWindow(field, bush_coords)
pygame.init()
main()