From 7467996cc6ba981b22121d916bf09eaf43dc5b16 Mon Sep 17 00:00:00 2001 From: s475275 Date: Sun, 19 Mar 2023 01:42:59 +0100 Subject: [PATCH] =?UTF-8?q?Drogi=20na=20mapie,=20ograniczenia=20poruszania?= =?UTF-8?q?=20si=C4=99=20agenta,=20generacja=20mapy=20z=20pliku=20txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/map.txt | 13 +++++++ res/tiles.png | Bin 1363 -> 1413 bytes src/main.py | 103 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 88 insertions(+), 28 deletions(-) create mode 100644 res/map.txt diff --git a/res/map.txt b/res/map.txt new file mode 100644 index 0000000..bfa98fc --- /dev/null +++ b/res/map.txt @@ -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-- +------------------------- \ No newline at end of file diff --git a/res/tiles.png b/res/tiles.png index 474cc23c10338f33abc346f937bd7839fac4a273..bebcfc0011a6d07252ac243042f20fbb62a70aec 100644 GIT binary patch delta 908 zcmV;719SY-3WW=>#US00eNt;HQ5C zn*=}rjg0yL0T}g(0|d}o@LyhDNB{)TV!1ztp9HWMedk($ey`wP^ZNQa`*-#U#N%23 zR1toa08{`6tN^0s`YHhx00A6G06BtB0tB#>_g~-tegE|1w<$o4CI8y+n+{-U_;3BJ z0?1eaED7G0;MY_MJiNucH$I;0z3G3-mI@$h6y6g4tx!BX*2;F5C2immz-kC ze-!|lk^l%mkVpUlXaXh%Xc&h6*5f(4|CgjT7sqq7VaWVn1<>&5sTw%;96f*c|5gDc zuyi!NHCpGW5XSRqG32+JftCmfpvv%%$5So?P7%h`w4|l*&mAWRQ-DoD&pD5t6Gjd} zE#Zt|iC#V|we>gmIj8_KhiQ-VzN`i`0igmoDEF#>sj`0cHd1nTV^rB5eg8iK06+i${-OyOIN#T_YmcQ%fl=jr^hHQ2 zfC^Z%Nod3kKk8>r5L1tp+Fzgos9XYj@$uaCZV$dK^eNTBUY8dWzzTm_xdF#@=bRAY zM*cYol-ylN04taZ|K+-U^oYLqo`8GdCyO%uhG={?;EIn2uWj@%aovB408(WUA&$A{ zpL@Qjj|Yz$wWp>Hxuysz08~BgSR;m-Z)tX~%rAX$kyQZTB;`B-oT%hJ;<*TPp3vu% zg(*N_@JjUtY76~!$!&NEaao9KxSs$47>yT)JmteUQhz^3-{1(BcKcC#nWjlS0vx~{+!3-Asqk*aD`D{Isgu!0|yY5 z>vIAi01XL1j=(1Z*wg=x^aUwEg~2Z!KuX&m=?fe{q664+v5y3PO-|s?-guBqV3u%}D@G5P%l!c68^fQ2=TZfnEea4WNVI zk3P>@gCFtYPXIay{#MVIIGx|V1VAUjg&_bP1`-j#R_qCw7(mlBjql#iQT=~qZ6@H; zf3RRk{?7qic=Y53`ncA)zMBKcptkQu>l_Z@c|KPR@^{6+UKbom0U8hf=kb4(+kjI9 z<5OC4rQlyWP8_5FhXOrk9=#+OC3C^vgC%z=9$W^?B42gso4%5`aR? z3wnYcTgWXi1fX!>zaD=Tfl_~~2LUyJlY>7+Bme>c9Hivx#wgn!z5N#f06+i$5P(zi zee}gvJN|rR{QQ;c_Xt2k>{%ofop0itn!u;`O0}zLz{*A7EWW>n(e~iSf`02lq337M zI8Oqw)An6q k000O800IDj008KR|J-@SL`1n)jQ{`u07*qoM6N<$f?Lpg0ssI2 diff --git a/src/main.py b/src/main.py index 3e72348..0346b06 100644 --- a/src/main.py +++ b/src/main.py @@ -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()