Changed map generating
This commit is contained in:
parent
674393a4d9
commit
a9e8d1b83d
14
bfs.py
14
bfs.py
@ -1,21 +1,23 @@
|
|||||||
from succ import succ as successors
|
from succ import succ as successors
|
||||||
|
from queue import PriorityQueue
|
||||||
|
|
||||||
|
|
||||||
def bfs(istate, goalx, goaly, passedFields):
|
def bfs(istate, goalx, goaly, passedFields):
|
||||||
fringe = [istate]
|
fringe = [istate]
|
||||||
explored = []
|
explored = []
|
||||||
steps = []
|
steps = []
|
||||||
while(fringe):
|
while fringe:
|
||||||
state = fringe.pop(0)
|
state = fringe.pop(0)
|
||||||
if state.xpos == goalx and state.ypos == goaly:
|
if state.xpos == goalx and state.ypos == goaly:
|
||||||
|
steps.insert(0, state)
|
||||||
|
while (state.parent != None):
|
||||||
|
state = state.parent
|
||||||
steps.insert(0, state)
|
steps.insert(0, state)
|
||||||
while(state.parent != None):
|
return steps
|
||||||
state = state.parent
|
|
||||||
steps.insert(0, state)
|
|
||||||
return steps
|
|
||||||
|
|
||||||
element = successors(state, passedFields)
|
element = successors(state, passedFields)
|
||||||
explored.append((state.xpos, state.ypos, state.orientation))
|
explored.append((state.xpos, state.ypos, state.orientation))
|
||||||
for value in element :
|
for value in element:
|
||||||
val = (value.xpos, value.ypos, value.orientation)
|
val = (value.xpos, value.ypos, value.orientation)
|
||||||
if val not in explored and value not in fringe:
|
if val not in explored and value not in fringe:
|
||||||
fringe.append(value)
|
fringe.append(value)
|
||||||
|
45
main.py
45
main.py
@ -28,49 +28,55 @@ class Agent:
|
|||||||
self.rect = rect
|
self.rect = rect
|
||||||
self.direction = direction
|
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]
|
|
||||||
field_array_1 = []
|
field_array_1 = []
|
||||||
field_array_2 = []
|
field_array_2 = []
|
||||||
|
field_priority = []
|
||||||
for i in range(16):
|
for i in range(16):
|
||||||
|
temp_priority = []
|
||||||
for j in range(16):
|
for j in range(16):
|
||||||
randomChoiceOfBlock = random.choice(fields_list)
|
if i in (0, 1) and j in (0, 1):
|
||||||
priorityOfBlock = fields_list.index(randomChoiceOfBlock)
|
field_array_2.append(GRASS)
|
||||||
field_array_2.append([randomChoiceOfBlock,priorityOfBlock])
|
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_1.append(field_array_2)
|
||||||
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):
|
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][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
|
window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta
|
||||||
pygame.display.update()
|
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():
|
def main():
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
run = True
|
run = True
|
||||||
agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
|
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:
|
while run:
|
||||||
clock.tick(FPS)
|
clock.tick(FPS)
|
||||||
for event in pygame.event.get(): # przechwycanie zamknięcia okna
|
for event in pygame.event.get():
|
||||||
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()
|
||||||
draw_window(agent, fields)
|
draw_window(agent, fields)
|
||||||
steps = bfs(State(None, None, 0, 0, 'E'), 100, 50, fields)
|
steps = bfs(State(None, None, 0, 0, 'E'), 100, 50, fields)
|
||||||
for interm in steps:
|
for interm in steps:
|
||||||
@ -89,6 +95,5 @@ def main():
|
|||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user