Drogi na mapie, ograniczenia poruszania się agenta, generacja mapy z pliku txt

This commit is contained in:
s475275 2023-03-19 01:42:59 +01:00
parent 060531d208
commit 7467996cc6
3 changed files with 88 additions and 28 deletions

13
res/map.txt Normal file
View File

@ -0,0 +1,13 @@
LRRRRRRRRRRRRRRRRR---R---
---R------R------R---RRR-
---R------R------R---R---
RRRR------RRRRRRRRRRRR---
R--R-------------R-------
R--RRRRRRRRRRRRRRR-------
R--R---R---R-----R-------
R--R---R---R-----RRRRRRR-
R--R-------------R-------
R--R-----R----R--R-------
R--R-----R----R--R-------
RRRRRRRRRRRRRRRRRRRRRRR--
-------------------------

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -1,25 +1,45 @@
from pathlib import Path
import pygame as pg
import random
from random import randint
class SimulationState:
def __init__(self):
self.agent_pos = pg.Vector2(0, 0)
self.landfill_pos = pg.Vector2(0, 0)
self.roads_pos = []
self.houses_pos = []
map_path = Path("../res/map.txt")
with open(map_path, "r") as map_file:
map_data = map_file.read().replace('\n', '')
for y in range(0, 13):
for x in range(0, 25):
pos = x + 25*y
tile = map_data[pos]
if tile == "L":
self.landfill_pos = pg.Vector2(x, y)
if tile == "R":
self.roads_pos.append(pg.Vector2(x, y))
if tile == "H":
self.houses_pos.append(pg.Vector2(x, y))
self.agent_pos = self.landfill_pos
self.world_limits = pg.Vector2(25, 13)
def updateAgent(self, move_agent):
self.agent_pos += move_agent
proposed_pos = self.agent_pos + move_agent
move_valid = True
if self.agent_pos.x < 0:
self.agent_pos.x = 0
elif self.agent_pos.x >= self.world_limits.x:
self.agent_pos.x = self.world_limits.x - 1
if proposed_pos != self.landfill_pos and proposed_pos not in self.roads_pos:
move_valid = False
if proposed_pos.x < 0 or proposed_pos.x >= self.world_limits.x:
move_valid = False
if proposed_pos.y < 0 or proposed_pos.y >= self.world_limits.y:
move_valid = False
if self.agent_pos.y < 0:
self.agent_pos.y = 0
elif self.agent_pos.y >= self.world_limits.y:
self.agent_pos.y = self.world_limits.y - 1
if move_valid:
self.agent_pos = proposed_pos
class Interface:
@ -37,7 +57,8 @@ class Interface:
# okno
pg.display.set_caption("Inteligentna śmieciarka")
self.window = pg.display.set_mode((1600, 832))
window_size = self.simulation_state.world_limits.elementwise() * self.cell_size
self.window = pg.display.set_mode((int(window_size.x), int(window_size.y)))
self.simulation_state.world_limits.elementwise() * self.cell_size
# dla pętli
@ -57,26 +78,23 @@ class Interface:
if event.key == pg.K_ESCAPE:
self.run_simulation = False
break
elif event.key == pg.K_BACKQUOTE:
if not self.debug_mode:
self.debug_mode = True
else:
self.debug_mode = False
elif self.debug_mode:
if event.key == pg.K_BACKQUOTE:
self.debug_mode = not self.debug_mode
if self.debug_mode:
if event.key == pg.K_RIGHT:
self.move_agent.x = 1
elif event.key == pg.K_LEFT:
if event.key == pg.K_LEFT:
self.move_agent.x = -1
elif event.key == pg.K_DOWN:
if event.key == pg.K_DOWN:
self.move_agent.y = 1
elif event.key == pg.K_UP:
if event.key == pg.K_UP:
self.move_agent.y = -1
def processSimulationInput(self):
move_x = random.randint(-1, 1)
move_y = random.randint(-1, 1)
moves = [pg.Vector2(-1, 0), pg.Vector2(1, 0), pg.Vector2(0, -1), pg.Vector2(0, 1)]
self.move_agent = pg.Vector2(move_x, move_y)
self.move_agent = moves[randint(0,3)]
def update(self):
self.simulation_state.updateAgent(self.move_agent)
@ -86,14 +104,43 @@ class Interface:
self.window.fill((8, 68, 0))
else:
self.window.fill((68, 8, 0))
agent_pos = self.simulation_state.agent_pos.elementwise() * self.cell_size
# render wysypiska
landfill_pos = self.simulation_state.landfill_pos.elementwise() * self.cell_size
landfill_texture_pos = pg.Vector2(3, 0).elementwise() * self.cell_size
landfill_texture = pg.Rect(int(landfill_texture_pos.x),
int(landfill_texture_pos.y),
int(self.cell_size.x),
int(self.cell_size.y))
self.window.blit(self.textures, landfill_pos, landfill_texture)
# render dróg
for road_pos in self.simulation_state.roads_pos:
adjusted_road_pos = road_pos.elementwise() * self.cell_size
road_texture_pos = pg.Vector2(0, 0).elementwise() * self.cell_size
road_texture = pg.Rect(int(road_texture_pos.x),
int(road_texture_pos.y),
int(self.cell_size.x),
int(self.cell_size.y))
self.window.blit(self.textures, adjusted_road_pos, road_texture)
# render domów
for house_pos in self.simulation_state.houses_pos:
adjusted_house_pos = house_pos.elementwise() * self.cell_size
house_texture_pos = pg.Vector2(1, 0).elementwise() * self.cell_size
house_texture = pg.Rect(int(house_texture_pos.x),
int(house_texture_pos.y),
int(self.cell_size.x),
int(self.cell_size.y))
self.window.blit(self.textures, adjusted_house_pos, house_texture)
# render agenta
agent_pos = self.simulation_state.agent_pos.elementwise() * self.cell_size
agent_texture_pos = pg.Vector2(2, 0).elementwise() * self.cell_size
agent_texture = pg.Rect(int(agent_texture_pos.x),
int(agent_texture_pos.y),
int(self.cell_size.x),
int(self.cell_size.y))
self.window.blit(self.textures, agent_pos, agent_texture)
pg.display.update()
@ -105,7 +152,7 @@ class Interface:
self.processSimulationInput()
self.update()
self.render()
self.clock.tick(6)
self.clock.tick(24)
pg.quit()