movement_planning #2
14
bfs.py
14
bfs.py
@ -1,15 +1,21 @@
|
|||||||
from succ import succ as successors
|
from succ import succ as successors
|
||||||
|
|
||||||
def bfs(istate, goalstate):
|
def bfs(istate, goalx, goaly):
|
||||||
fringe = [istate]
|
fringe = [istate]
|
||||||
explored = []
|
explored = []
|
||||||
|
steps = []
|
||||||
while(fringe):
|
while(fringe):
|
||||||
state = fringe.pop(0)
|
state = fringe.pop(0)
|
||||||
if state == goalstate :
|
if state.xpos == goalx and state.ypos == goaly:
|
||||||
return
|
steps.insert(0, state)
|
||||||
|
while(state.parent != None):
|
||||||
|
state = state.parent
|
||||||
|
steps.insert(0, state)
|
||||||
|
return steps
|
||||||
|
|
||||||
element = successors(state)
|
element = successors(state)
|
||||||
explored.append(state)
|
explored.append(state)
|
||||||
for value in element :
|
for value in element :
|
||||||
if value not in explored :
|
if value not in explored and value not in fringe:
|
||||||
fringe.append(value)
|
fringe.append(value)
|
||||||
return False
|
return False
|
||||||
|
53
main.py
53
main.py
@ -1,5 +1,8 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
|
from bfs import bfs
|
||||||
|
from state import State
|
||||||
|
import time
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
WIDTH, HEIGHT = 800, 800
|
WIDTH, HEIGHT = 800, 800
|
||||||
@ -17,6 +20,10 @@ COBBLE_IMG = pygame.image.load("cobble.jpeg")
|
|||||||
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50))
|
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50))
|
||||||
FPS = 10
|
FPS = 10
|
||||||
|
|
||||||
|
class Agent:
|
||||||
|
def __init__(self, rect, direction):
|
||||||
|
self.rect = rect
|
||||||
|
self.direction = direction
|
||||||
|
|
||||||
def randomize_map(): # tworzenie mapy z losowymi polami
|
def randomize_map(): # tworzenie mapy z losowymi polami
|
||||||
fields_list = [DIRT, GRASS, SAND, COBBLE]
|
fields_list = [DIRT, GRASS, SAND, COBBLE]
|
||||||
@ -34,37 +41,57 @@ def draw_window(agent, fields):
|
|||||||
for i in range(16):
|
for i in range(16):
|
||||||
for j in range(16):
|
for j in range(16):
|
||||||
window.blit(fields[i][j], (i * 50, j * 50))
|
window.blit(fields[i][j], (i * 50, j * 50))
|
||||||
window.blit(AGENT, (agent.x, agent.y)) # wyswietlanie agenta
|
window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
def agent_movement(keys_pressed, agent): # sterowanie
|
#def agent_movement(keys_pressed, agent): # sterowanie
|
||||||
if keys_pressed[pygame.K_LEFT] and agent.x > 0:
|
# if keys_pressed[pygame.K_LEFT] and agent.x > 0:
|
||||||
agent.x -= 50
|
# agent.x -= 50
|
||||||
if keys_pressed[pygame.K_RIGHT] and agent.x < 750:
|
# if keys_pressed[pygame.K_RIGHT] and agent.x < 750:
|
||||||
agent.x += 50
|
# agent.x += 50
|
||||||
if keys_pressed[pygame.K_UP] and agent.y > 0:
|
# if keys_pressed[pygame.K_UP] and agent.y > 0:
|
||||||
agent.y -= 50
|
# agent.y -= 50
|
||||||
if keys_pressed[pygame.K_DOWN] and agent.y < 750:
|
# if keys_pressed[pygame.K_DOWN] and agent.y < 750:
|
||||||
agent.y += 50
|
# agent.y += 50
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
run = True
|
run = True
|
||||||
agent = pygame.Rect(0, 0, 50, 50) # tworzenie pola dla agenta
|
agent = Agent(pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
|
||||||
fields = randomize_map()
|
fields = randomize_map()
|
||||||
while run:
|
while run:
|
||||||
clock.tick(FPS)
|
clock.tick(FPS)
|
||||||
for event in pygame.event.get(): # przechwycanie zamknięcia okna
|
for event in pygame.event.get(): # przechwycanie zamknięcia okna
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
run = False
|
run = False
|
||||||
keys_pressed = pygame.key.get_pressed()
|
#keys_pressed = pygame.key.get_pressed()
|
||||||
|
|
||||||
|
steps = bfs(State(None, None, 0, 0, 'E'), 450, 100)
|
||||||
|
for interm in steps:
|
||||||
|
if interm.action == 'LEFT':
|
||||||
|
agent.direction = (agent.direction - 1) % 4
|
||||||
|
if interm.action == 'RIGHT':
|
||||||
|
agent.direction = (agent.direction + 1) % 4
|
||||||
|
if interm.action == 'FORWARD':
|
||||||
|
if agent.direction == 0:
|
||||||
|
agent.rect.x += 50
|
||||||
|
elif agent.direction == 1:
|
||||||
|
agent.rect.y += 50
|
||||||
|
elif agent.direction == 2:
|
||||||
|
agent.rect.x -= 50
|
||||||
|
else:
|
||||||
|
agent.rect.y -= 50
|
||||||
draw_window(agent, fields)
|
draw_window(agent, fields)
|
||||||
agent_movement(keys_pressed, agent)
|
time.sleep(1)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
1
succ.py
1
succ.py
@ -26,5 +26,4 @@ def succ(st: State):
|
|||||||
successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S'))
|
successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S'))
|
||||||
if st.xpos < 750:
|
if st.xpos < 750:
|
||||||
successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E'))
|
successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E'))
|
||||||
|
|
||||||
return successors
|
return successors
|
||||||
|
Loading…
Reference in New Issue
Block a user