File size: 13,779 Bytes
8bd26ad |
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 |
import sys
import heapq
import math
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel, QSpinBox,
QComboBox, QMessageBox, QFrame)
from PyQt5.QtCore import Qt, QTimer, pyqtSignal
from PyQt5.QtGui import QPainter, QColor, QPen, QFont
class Node:
def __init__(self, x, y, walkable=True):
self.x = x
self.y = y
self.walkable = walkable
self.g = 0 # Cost from start to this node
self.h = 0 # Heuristic cost to end
self.f = 0 # Total cost (g + h)
self.parent = None
def __lt__(self, other):
return self.f < other.f
class AStarGame(QMainWindow):
def __init__(self):
super().__init__()
self.grid_size = 20
self.cell_size = 30
self.grid = []
self.start_node = None
self.end_node = None
self.path = []
self.open_set = []
self.closed_set = set()
self.is_running = False
self.is_paused = False
self.speed = 100 # ms
self.timer = QTimer()
self.timer.timeout.connect(self.step_algorithm)
self.current_mode = "start" # start, end, obstacle, erase
self.init_ui()
self.init_grid()
def init_ui(self):
self.setWindowTitle("A* Pathfinding Algorithm Game")
self.setFixedSize(self.grid_size * self.cell_size + 250,
self.grid_size * self.cell_size + 50)
# Central widget
central_widget = QWidget()
self.setCentralWidget(central_widget)
# Main layout
main_layout = QHBoxLayout()
central_widget.setLayout(main_layout)
# Grid widget
self.grid_widget = GridWidget(self)
main_layout.addWidget(self.grid_widget)
# Control panel
control_panel = QFrame()
control_panel.setFrameStyle(QFrame.Box)
control_panel.setFixedWidth(200)
control_layout = QVBoxLayout()
control_panel.setLayout(control_layout)
# Mode selection
mode_label = QLabel("Mode:")
mode_label.setFont(QFont("Arial", 12, QFont.Bold))
control_layout.addWidget(mode_label)
self.mode_combo = QComboBox()
self.mode_combo.addItems(["Start Point", "End Point", "Obstacles", "Erase"])
self.mode_combo.currentIndexChanged.connect(self.change_mode)
control_layout.addWidget(self.mode_combo)
# Algorithm controls
control_layout.addSpacing(20)
algo_label = QLabel("Algorithm Controls:")
algo_label.setFont(QFont("Arial", 12, QFont.Bold))
control_layout.addWidget(algo_label)
self.start_button = QPushButton("Start")
self.start_button.clicked.connect(self.start_algorithm)
control_layout.addWidget(self.start_button)
self.pause_button = QPushButton("Pause")
self.pause_button.clicked.connect(self.pause_algorithm)
self.pause_button.setEnabled(False)
control_layout.addWidget(self.pause_button)
self.reset_button = QPushButton("Reset")
self.reset_button.clicked.connect(self.reset_grid)
control_layout.addWidget(self.reset_button)
self.clear_button = QPushButton("Clear All")
self.clear_button.clicked.connect(self.clear_all)
control_layout.addWidget(self.clear_button)
# Speed control
control_layout.addSpacing(20)
speed_label = QLabel("Speed:")
speed_label.setFont(QFont("Arial", 12, QFont.Bold))
control_layout.addWidget(speed_label)
speed_layout = QHBoxLayout()
self.speed_spin = QSpinBox()
self.speed_spin.setRange(10, 500)
self.speed_spin.setValue(self.speed)
self.speed_spin.valueChanged.connect(self.change_speed)
speed_layout.addWidget(self.speed_spin)
speed_layout.addWidget(QLabel("ms"))
control_layout.addLayout(speed_layout)
# Instructions
control_layout.addSpacing(20)
instructions_label = QLabel("Instructions:")
instructions_label.setFont(QFont("Arial", 12, QFont.Bold))
control_layout.addWidget(instructions_label)
instructions = QLabel(
"1. Set start and end points\n"
"2. Add obstacles\n"
"3. Click Start to find path\n"
"4. Watch the algorithm work!"
)
instructions.setWordWrap(True)
control_layout.addWidget(instructions)
# Status
control_layout.addSpacing(20)
self.status_label = QLabel("Status: Ready")
self.status_label.setFont(QFont("Arial", 10))
control_layout.addWidget(self.status_label)
control_layout.addStretch()
main_layout.addWidget(control_panel)
def init_grid(self):
self.grid = []
for y in range(self.grid_size):
row = []
for x in range(self.grid_size):
row.append(Node(x, y))
self.grid.append(row)
# Set default start and end
self.start_node = self.grid[2][2]
self.end_node = self.grid[self.grid_size-3][self.grid_size-3]
self.update()
def change_mode(self, index):
modes = ["start", "end", "obstacle", "erase"]
self.current_mode = modes[index]
def change_speed(self, value):
self.speed = value
if self.timer.isActive():
self.timer.setInterval(self.speed)
def start_algorithm(self):
if not self.start_node or not self.end_node:
QMessageBox.warning(self, "Warning", "Please set both start and end points.")
return
self.is_running = True
self.is_paused = False
self.path = []
self.open_set = []
self.closed_set = set()
# Initialize start node
self.start_node.g = 0
self.start_node.h = self.heuristic(self.start_node, self.end_node)
self.start_node.f = self.start_node.g + self.start_node.h
heapq.heappush(self.open_set, (self.start_node.f, self.start_node))
self.start_button.setEnabled(False)
self.pause_button.setEnabled(True)
self.reset_button.setEnabled(False)
self.status_label.setText("Status: Running")
self.timer.start(self.speed)
def pause_algorithm(self):
if self.is_running:
if self.is_paused:
self.timer.start(self.speed)
self.pause_button.setText("Pause")
self.status_label.setText("Status: Running")
else:
self.timer.stop()
self.pause_button.setText("Resume")
self.status_label.setText("Status: Paused")
self.is_paused = not self.is_paused
def reset_grid(self):
self.timer.stop()
self.is_running = False
self.is_paused = False
for row in self.grid:
for node in row:
node.g = 0
node.h = 0
node.f = 0
node.parent = None
self.path = []
self.open_set = []
self.closed_set = set()
self.start_button.setEnabled(True)
self.pause_button.setEnabled(False)
self.reset_button.setEnabled(True)
self.pause_button.setText("Pause")
self.status_label.setText("Status: Ready")
self.update()
def clear_all(self):
self.reset_grid()
self.init_grid()
def step_algorithm(self):
if not self.open_set:
self.timer.stop()
self.is_running = False
self.status_label.setText("Status: No path found!")
self.start_button.setEnabled(True)
self.pause_button.setEnabled(False)
self.reset_button.setEnabled(True)
return
# Get node with lowest f score
current = heapq.heappop(self.open_set)[1]
# Check if we reached the end
if current == self.end_node:
self.timer.stop()
self.is_running = False
self.reconstruct_path(current)
self.status_label.setText("Status: Path found!")
self.start_button.setEnabled(True)
self.pause_button.setEnabled(False)
self.reset_button.setEnabled(True)
return
self.closed_set.add(current)
# Check neighbors
for neighbor in self.get_neighbors(current):
if neighbor in self.closed_set or not neighbor.walkable:
continue
tentative_g = current.g + self.distance(current, neighbor)
if tentative_g < neighbor.g or neighbor not in [n[1] for n in self.open_set]:
neighbor.parent = current
neighbor.g = tentative_g
neighbor.h = self.heuristic(neighbor, self.end_node)
neighbor.f = neighbor.g + neighbor.h
if neighbor not in [n[1] for n in self.open_set]:
heapq.heappush(self.open_set, (neighbor.f, neighbor))
self.update()
def get_neighbors(self, node):
neighbors = []
for dx, dy in [(0, -1), (1, 0), (0, 1), (-1, 0), (-1, -1), (1, -1), (-1, 1), (1, 1)]:
x, y = node.x + dx, node.y + dy
if 0 <= x < self.grid_size and 0 <= y < self.grid_size:
neighbors.append(self.grid[y][x])
return neighbors
def distance(self, node_a, node_b):
# Euclidean distance
dx = node_a.x - node_b.x
dy = node_a.y - node_b.y
return math.sqrt(dx*dx + dy*dy)
def heuristic(self, node_a, node_b):
# Manhattan distance
return abs(node_a.x - node_b.x) + abs(node_a.y - node_b.y)
def reconstruct_path(self, current):
self.path = []
while current:
self.path.append(current)
current = current.parent
self.path.reverse()
def handle_click(self, x, y):
if self.is_running:
return
node = self.grid[y][x]
if self.current_mode == "start":
if node != self.end_node and node.walkable:
self.start_node = node
elif self.current_mode == "end":
if node != self.start_node and node.walkable:
self.end_node = node
elif self.current_mode == "obstacle":
if node != self.start_node and node != self.end_node:
node.walkable = False
elif self.current_mode == "erase":
node.walkable = True
if node == self.start_node:
self.start_node = None
elif node == self.end_node:
self.end_node = None
self.update()
class GridWidget(QWidget):
def __init__(self, game):
super().__init__()
self.game = game
self.setMouseTracking(True)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
x = event.x() // self.game.cell_size
y = event.y() // self.game.cell_size
if 0 <= x < self.game.grid_size and 0 <= y < self.game.grid_size:
self.game.handle_click(x, y)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# Draw grid
for y in range(self.game.grid_size):
for x in range(self.game.grid_size):
node = self.game.grid[y][x]
rect = (x * self.game.cell_size, y * self.game.cell_size,
self.game.cell_size, self.game.cell_size)
# Background
if node == self.game.start_node:
painter.fillRect(*rect, QColor(0, 200, 0)) # Green for start
elif node == self.game.end_node:
painter.fillRect(*rect, QColor(200, 0, 0)) # Red for end
elif not node.walkable:
painter.fillRect(*rect, QColor(100, 100, 100)) # Gray for obstacles
elif node in self.game.path:
painter.fillRect(*rect, QColor(0, 0, 200)) # Blue for path
elif node in [n[1] for n in self.game.open_set]:
painter.fillRect(*rect, QColor(200, 200, 100)) # Yellow for open set
elif node in self.game.closed_set:
painter.fillRect(*rect, QColor(200, 150, 150)) # Light red for closed set
else:
painter.fillRect(*rect, QColor(255, 255, 255)) # White for empty
# Grid lines
painter.setPen(QPen(QColor(200, 200, 200), 1))
painter.drawRect(*rect)
# Display cost values if algorithm is running
if self.game.is_running and node.g > 0:
painter.setPen(QPen(QColor(0, 0, 0), 1))
painter.setFont(QFont("Arial", 8))
painter.drawText(rect[0] + 2, rect[1] + 12, f"g:{node.g:.1f}")
painter.drawText(rect[0] + 2, rect[1] + 24, f"h:{node.h:.1f}")
def main():
app = QApplication(sys.argv)
game = AStarGame()
game.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main() |