-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_engine.py
157 lines (138 loc) · 5.81 KB
/
game_engine.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
#Author: Nicholas Massad
#Date: 28/02/2023
import pygame
import time
class GameEngine:
def __init__(self, width=600, height=600, grid_size=30, benchmark=False):
self.width = width
self.height = height
self.grid_size = grid_size
if not benchmark:
pygame.init()
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pathfinding Visualizer")
else:
self.window = None
self.obstacles = {}
# Draw a grid on the pygame window
def draw_grid(self):
if self.window is not None:
for i in range(0, self.width, self.grid_size):
pygame.draw.line(self.window, (255, 255, 255), (i, 0), (i, self.height))
for i in range(0, self.height, self.grid_size):
pygame.draw.line(self.window, (255, 255, 255), (0, i), (self.width, i))
pass
def create_boundary(self):
for i in range(0, self.width, self.grid_size):
self.add_obstacle(i, 0)
self.add_obstacle(i, self.height - self.grid_size)
for i in range(0, self.height, self.grid_size):
self.add_obstacle(0, i)
self.add_obstacle(self.width - self.grid_size, i)
# Add an obstacle to the pygame window
def add_obstacle(self, x, y):
x = x - (x % self.grid_size)
y = y - (y % self.grid_size)
if self.window is not None:
obstacle = pygame.Rect(x, y, self.grid_size, self.grid_size)
pygame.draw.rect(self.window, (255, 0, 0), obstacle)
else:
obstacle = Rect(x, y)
if (x, y) not in self.obstacles:
self.obstacles[(x, y)] = obstacle
# add a start point to the pygame window
def add_start_point(self, x, y):
x = x - (x % self.grid_size)
y = y - (y % self.grid_size)
if self.window is not None:
pygame.draw.rect(self.window, (0, 255, 0), pygame.Rect(x, y, self.grid_size, self.grid_size))
return x, y
# add an end point to the pygame window
def add_end_point(self, x, y):
x = x - (x % self.grid_size)
y = y - (y % self.grid_size)
if self.window is not None:
pygame.draw.rect(self.window, (0, 0, 255), pygame.Rect(x, y, self.grid_size, self.grid_size))
return x, y
# add a path to the pygame window
def add_path_centered(self, x, y, x2, y2, rgb=(0, 255, 0)):
x = x - (x % self.grid_size) + (self.grid_size / 2)
y = y - (y % self.grid_size) + (self.grid_size / 2)
x2 = x2 - (x2 % self.grid_size) + (self.grid_size / 2)
y2 = y2 - (y2 % self.grid_size) + (self.grid_size / 2)
if self.window is not None:
pygame.draw.line(self.window, rgb, (x, y), (x2, y2), 2)
pygame.display.update()
time.sleep(0.01)
def add_path(self, x, y, x2, y2, rgb=(0, 255, 0)):
if self.window is not None:
pygame.draw.line(self.window, rgb, (x, y), (x2, y2), 2)
pygame.display.update()
time.sleep(0.01)
pass
def remove_obstacle(self, x, y):
if x < self.grid_size or x > self.width - self.grid_size:
return
if y < self.grid_size or y > self.height - self.grid_size:
return
x = x - (x % self.grid_size)
y = y - (y % self.grid_size)
if self.window is not None:
pygame.draw.rect(self.window, (0, 0, 0), pygame.Rect(x, y, self.grid_size, self.grid_size))
if (x, y) in self.obstacles:
del self.obstacles[(x, y)]
def clear(self, keep_obstacles=False):
if self.window is not None:
self.window.fill((0, 0, 0))
self.draw_grid()
if not keep_obstacles:
self.obstacles = {}
self.create_boundary()
else:
for obstacle in self.obstacles.values():
if self.window is not None:
pygame.draw.rect(self.window, (255, 0, 0), obstacle)
pass
def draw_search_path(self, path, center=False):
if center:
for i in range(len(path)-1):
node_a = path[i]
node_b = path[i+1]
self.add_path_centered(node_a[0], node_a[1], node_b[0], node_b[1])
else:
for i in range(len(path)-1):
node_a = path[i]
node_b = path[i+1]
self.add_path(node_a[0], node_a[1], node_b[0], node_b[1])
if self.window is not None:
pygame.display.update()
def draw_ellipse(self, x, y, a, b, rgb=(255, 255, 0)):
if self.window is not None:
pygame.draw.ellipse(self.window, rgb, pygame.Rect(x-a/2, y-b/2, a, b), 2)
pygame.display.update()
time.sleep(0.01)
pass
def draw_rectangle(self, x, y):
if self.window is not None:
pygame.draw.rect(self.window, (255, 255, 0), pygame.Rect(x, y, 10, 10))
pygame.display.update()
time.sleep(0.01)
pass
def draw_small_rectangle(self, x, y):
if self.window is not None:
pygame.draw.rect(self.window, (255, 255, 0), pygame.Rect(x, y, 5, 5))
pygame.display.update()
pass
def draw_ellipse_angled(self, rect, angle, color=(255, 255, 0), width=2):
if self.window is not None:
surface = pygame.display.set_mode((rect[2], rect[3]))
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.ellipse(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center=target_rect.center))
pass
class Rect:
def __init__(self, x, y):
self.x = x
self.y = y