movement_planning #2

Merged
s473585 merged 13 commits from movement_planning into master 2023-05-24 14:28:17 +02:00
2 changed files with 35 additions and 28 deletions
Showing only changes of commit a9e8d1b83d - Show all commits

8
bfs.py
View File

@ -1,21 +1,23 @@
from succ import succ as successors
from queue import PriorityQueue
def bfs(istate, goalx, goaly, passedFields):
fringe = [istate]
explored = []
steps = []
while(fringe):
while fringe:
state = fringe.pop(0)
if state.xpos == goalx and state.ypos == goaly:
steps.insert(0, state)
while(state.parent != None):
while (state.parent != None):
state = state.parent
steps.insert(0, state)
return steps
element = successors(state, passedFields)
explored.append((state.xpos, state.ypos, state.orientation))
for value in element :
for value in element:
val = (value.xpos, value.ypos, value.orientation)
if val not in explored and value not in fringe:
fringe.append(value)

45
main.py
View File

@ -28,49 +28,55 @@ class Agent:
self.rect = rect
self.direction = direction
def randomize_map(): # tworzenie mapy z losowymi polami
fields_list = [DIRT, GRASS, SAND, COBBLE]
field_array_1 = []
field_array_2 = []
field_priority = []
for i in range(16):
temp_priority = []
for j in range(16):
randomChoiceOfBlock = random.choice(fields_list)
priorityOfBlock = fields_list.index(randomChoiceOfBlock)
field_array_2.append([randomChoiceOfBlock,priorityOfBlock])
if i in (0, 1) and j in (0, 1):
field_array_2.append(GRASS)
temp_priority.append(1)
else:
prob = random.uniform(0, 100)
if 0 <= prob <= 10:
field_array_2.append(COBBLE)
temp_priority.append(3)
if 10 < prob <= 20:
field_array_2.append(SAND)
temp_priority.append(2)
else:
field_array_2.append(GRASS)
temp_priority.append(1)
field_array_1.append(field_array_2)
field_array_2 = []
return field_array_1
field_priority.append(temp_priority)
return field_array_1, field_priority
def draw_window(agent, fields):
for i in range(16):
for j in range(16):
window.blit(fields[i][j][0], (i * 50, j * 50))
window.blit(fields[i][j], (i * 50, j * 50))
window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta
pygame.display.update()
#def agent_movement(keys_pressed, agent): # sterowanie
# if keys_pressed[pygame.K_LEFT] and agent.x > 0:
# agent.x -= 50
# if keys_pressed[pygame.K_RIGHT] and agent.x < 750:
# agent.x += 50
# if keys_pressed[pygame.K_UP] and agent.y > 0:
# agent.y -= 50
# if keys_pressed[pygame.K_DOWN] and agent.y < 750:
# agent.y += 50
def main():
clock = pygame.time.Clock()
run = True
agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
fields = randomize_map()
fields, priority_array = randomize_map()
print(priority_array)
print(priority_array[4][8])
while run:
clock.tick(FPS)
for event in pygame.event.get(): # przechwycanie zamknięcia okna
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#keys_pressed = pygame.key.get_pressed()
# keys_pressed = pygame.key.get_pressed()
draw_window(agent, fields)
steps = bfs(State(None, None, 0, 0, 'E'), 100, 50, fields)
for interm in steps:
@ -89,6 +95,5 @@ def main():
pygame.quit()
if __name__ == "__main__":
main()