Transfer in to JPNB
This commit is contained in:
94
P1/grid.py
94
P1/grid.py
@@ -40,8 +40,10 @@ class Field:
|
||||
color = GREEN
|
||||
elif self.state == "path":
|
||||
color = ORANGE
|
||||
elif self.state == "current":
|
||||
color = RED
|
||||
elif self.state == "visited":
|
||||
color = WHITE
|
||||
color = GREY
|
||||
|
||||
x_calc = (MARGIN + WIDTH) * self.x + MARGIN
|
||||
y_calc = (MARGIN + HEIGHT) * (grid_size - 1 - self.y) + MARGIN # flipping
|
||||
@@ -52,13 +54,15 @@ class Field:
|
||||
[x_calc, y_calc, WIDTH, HEIGHT]
|
||||
)
|
||||
|
||||
|
||||
'''
|
||||
# Render the heuristic value as text
|
||||
if self.state != "obstacale": # Don't display on obstacles
|
||||
# Create a font object
|
||||
font = pygame.font.Font(None, 16) # None means default font, 14 is the size
|
||||
|
||||
# Round the heuristic value to 1 decimal place for better display
|
||||
f_text = f"{self.f:.1f}"
|
||||
f_text = f"{self.g:.1f}"
|
||||
|
||||
# Render the text
|
||||
text = font.render(f_text, True, BLACK) # True for anti-aliasing, BLACK for text color
|
||||
@@ -68,6 +72,7 @@ class Field:
|
||||
|
||||
# Draw the text on the screen
|
||||
screen.blit(text, text_rect)
|
||||
'''
|
||||
|
||||
|
||||
class Grid:
|
||||
@@ -101,7 +106,7 @@ class Grid:
|
||||
return self.grid[x][y].state
|
||||
|
||||
def set_state(self, state, x, y):
|
||||
if state == "free" or state == "obstacale" or state == "start" or state == "target" or state == "path" or state == "visited":
|
||||
if state == "free" or state == "obstacale" or state == "start" or state == "target" or state == "path" or state == "current" or state == "visited":
|
||||
self.grid[x][y].state = state
|
||||
|
||||
def set_free(self, x, y):
|
||||
@@ -109,15 +114,17 @@ class Grid:
|
||||
|
||||
def set_obstacle(self, x, y):
|
||||
self.set_state("obstacale", x, y)
|
||||
|
||||
def set_current(self, x, y):
|
||||
self.set_state("current", x, y)
|
||||
|
||||
def set_visited(self, x,y):
|
||||
self.set_state("visited", x, y)
|
||||
|
||||
def set_path(self, x, y):
|
||||
if not (x == self.start[0] and y == self.start[1]) and not (x == self.target[0] and y == self.target[1]):
|
||||
self.set_state("path", x, y)
|
||||
|
||||
def set_visited(self, x, y):
|
||||
if not (x == self.start[0] and y == self.start[1]) and not (x == self.target[0] and y == self.target[1]):
|
||||
self.set_state("visited", x, y)
|
||||
|
||||
def set_start(self, x, y):
|
||||
# reset old start if it exits
|
||||
if self.start:
|
||||
@@ -127,7 +134,7 @@ class Grid:
|
||||
self.grid[x][y].parent = self.grid[x][y]
|
||||
self.grid[x][y].g = 0
|
||||
self.grid[x][y].h = self.heuristic(self.grid[x][y])
|
||||
|
||||
self.grid[x][y].f = self.grid[x][y].h + self.grid[x][y].g
|
||||
self.start = (x, y)
|
||||
|
||||
def set_target(self, x, y):
|
||||
@@ -144,7 +151,7 @@ class Grid:
|
||||
Initializing the Grid
|
||||
'''
|
||||
start = (0, 0)
|
||||
target = (19, 19)
|
||||
target = (19, 0)
|
||||
grid = Grid(grid_size, grid_size)
|
||||
|
||||
# check if start an target are valid
|
||||
@@ -163,7 +170,7 @@ for i in range(9, 20):
|
||||
grid.set_obstacle(16, i)
|
||||
|
||||
'''
|
||||
Initializing A* Comps
|
||||
Initializing A* Components
|
||||
'''
|
||||
|
||||
open = Queue('PRIO', 'f')
|
||||
@@ -172,9 +179,11 @@ closed = Queue('PRIO', 'f')
|
||||
neighbors = []
|
||||
path = []
|
||||
|
||||
pygame.init()
|
||||
'''
|
||||
Initializing Visuals
|
||||
'''
|
||||
|
||||
# window
|
||||
pygame.init()
|
||||
window_width = grid_size * (WIDTH + MARGIN) + MARGIN
|
||||
window_height = grid_size * (HEIGHT + MARGIN) + MARGIN
|
||||
size = (window_width, window_height) # made size variable
|
||||
@@ -185,74 +194,65 @@ pygame.display.set_caption("A* Algorithm")
|
||||
done = False
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
while not done:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
done = True
|
||||
|
||||
def a_star():
|
||||
neighbor = None
|
||||
while not open.empty():
|
||||
while not open.is_empty():
|
||||
current_field = open.pop()
|
||||
grid.set_current(current_field.x, current_field.y)
|
||||
screen.fill(BLACK)
|
||||
|
||||
grid.draw(screen)
|
||||
pygame.display.flip()
|
||||
clock.tick(15)
|
||||
|
||||
if current_field.x == grid.target[0] and current_field.y == grid.target[1]:
|
||||
if current_field.x == grid.target[0] and current_field.y == grid.target[1]:
|
||||
path.append(current_field)
|
||||
grid.set_path(current_field.x, current_field.y)
|
||||
while not (current_field.x == grid.start[0] and current_field.y == grid.start[1]):
|
||||
current_field = current_field.parent
|
||||
path.insert(0, current_field)
|
||||
grid.set_path(current_field.x, current_field.y)
|
||||
grid.draw(screen)
|
||||
pygame.display.flip()
|
||||
clock.tick(15)
|
||||
open.clear()
|
||||
|
||||
break
|
||||
|
||||
closed.push(current_field)
|
||||
grid.set_visited(current_field.x, current_field.y)
|
||||
|
||||
# Nachbarn finden
|
||||
|
||||
for dx, dy in [(0, -1), (-1, 0), (0, 1), (1, 0)]:
|
||||
nx = current_field.x + dx
|
||||
ny = current_field.y + dy
|
||||
|
||||
# Prüfen, ob der Nachbar gültig ist
|
||||
if 0 <= nx < grid.cols and 0 <= ny < grid.rows:
|
||||
neighbor = grid.grid[nx][ny]
|
||||
|
||||
# Hindernis oder bereits in geschlossener Liste -> überspringen
|
||||
if neighbor.state == "obstacale" or neighbor in [item for item in closed.items]:
|
||||
if neighbor.state == "obstacale" or closed.items.__contains__(neighbor):
|
||||
continue
|
||||
|
||||
# Neuen g-Wert berechnen
|
||||
tentative_g = current_field.g + 1 # Kosten für einen Schritt = 1
|
||||
tentative_g = current_field.g + 1
|
||||
|
||||
# Wenn Nachbar nicht in offener Liste oder neuer Pfad besser
|
||||
if neighbor not in [item for item in open.items] or tentative_g < neighbor.g:
|
||||
if tentative_g < neighbor.g:
|
||||
neighbor.parent = current_field
|
||||
neighbor.g = tentative_g
|
||||
neighbor.h = grid.heuristic(neighbor)
|
||||
neighbor.f = neighbor.g + neighbor.h
|
||||
|
||||
# Knoten zur offenen Liste hinzufügen oder aktualisieren
|
||||
if neighbor not in [item for item in open.items]:
|
||||
open.push(neighbor)
|
||||
else:
|
||||
# Queue aktualisieren
|
||||
if open.items.__contains__(neighbor):
|
||||
open.items.sort(key=lambda item: item.f)
|
||||
else:
|
||||
open.push(neighbor)
|
||||
|
||||
|
||||
def a_star_main():
|
||||
global done
|
||||
while not done:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
done = True
|
||||
|
||||
a_star()
|
||||
screen.fill(BLACK)
|
||||
grid.draw(screen)
|
||||
|
||||
# refresh display
|
||||
screen.fill(BLACK)
|
||||
grid.draw(screen)
|
||||
pygame.display.flip()
|
||||
|
||||
# refreshrate
|
||||
clock.tick(120)
|
||||
|
||||
|
||||
a_star_main()
|
||||
clock.tick(30)
|
||||
|
||||
pygame.quit()
|
||||
|
||||
Reference in New Issue
Block a user