From 243eaed295780bdef26e79e89ac51b3b3f7ac355 Mon Sep 17 00:00:00 2001 From: s475275 Date: Sat, 18 Mar 2023 23:27:12 +0100 Subject: [PATCH] =?UTF-8?q?Symulacja,=20losowo=20ruszaj=C4=85cy=20si=C4=99?= =?UTF-8?q?=20agent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 1 + res/tiles.png | Bin 0 -> 1363 bytes src/main.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 requirements.txt create mode 100644 res/tiles.png create mode 100644 src/main.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..28e7d16 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pygame == 2.3.0 \ No newline at end of file diff --git a/res/tiles.png b/res/tiles.png new file mode 100644 index 0000000000000000000000000000000000000000..474cc23c10338f33abc346f937bd7839fac4a273 GIT binary patch literal 1363 zcmV-Z1+4msP)EX>4Tx04R}tkv&MmKpe$iTSbwopczCQGE{M}AS&XhRVYG*P%E_RU~=gfG-*gu zTpR`0f`cE6RRx%5XT~xAVGwJ3W_MfMwC{a6bnh(kNNlqUB5&wg4YKP8|AXJ%TDi#yFDV=Y+Fu;!V;Jbz1?pAD`95}>`Uwzv2Cnp`zgz=mK1r`O zwa5|ByA51iH#KDsxZD8-o($QPT`5E}n9Bk0XY@@Op#K)=TJw5q?BnzSNKsdb8{ps& z7|B!iy3e~iTYLNWOryUaUk7rZgiDh=00006VoOIv02KhO004mFZN2~i010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=K~TJ8V)rZY(M}202y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00S#YL_t(|+U?!Jk)tpWg<;s*6!z|%CI<)Q95yHJ6U6Sx zRW8N`1T7(n@2~O&yKIcqPp?}-jKeVe8?Mbb02k$#O8^i600aQQmH6!voWt#Q`%!zD zrb#^l9KaQWf9W`oi*YauFxC+K+yDqb#{#ftbSDCk0(5No0S=&J2M`hXTmU$L21op@ z!GFKsxd#w{E86{M@Dl+@0UA#M_5}W%$KxR#00(e|QC~U$4xj@E5S8n50w4el2|$j( zCj!{h|BmzpDL{q6FC9Qi+aKu*96+K2*mALt1b$6U;LqQfzVZFl=cX%Bqi{t3+xz~?hLt5E=I5`kU>Knw>jmpGl@z63xg!G$3J9R?B+z*g)Dm>59QG>z}x&r$tz=9$W^?B42gso4%5`aR?3wnYcTgWXi1fX!> zzaD=Tfl{jn0X2Y=gFi(i00ICUq~z+xDBB*r{TBfMKmY&`fK&2)^u<;?{(NNo{FUqX z2tY&ZStJylZ{nPqz^C_0wX12s%0=KTzQ2ai_Ta~Ye(OS^=V#A2PXe&h_FZA*omb8Y zLfpu|BmyP-76Axs*w;Eh^oV|TjbSbLiA6p2h7>#+Y{%olYa9Jb*vAwBNEM3+;aK|p z5^DgW`ot%9nX8RC092iJ>=A~Vw=`R``K1RJvj9M+u8jb6-gvd0i$0O3t#Y=p6d#A02&t$0zd$OU6=+02mk;A0Du4hAOHXe00062fB*pKhyUDp V#Y9B8SB(Gw002ovPDHLkV1mcgNq+zU literal 0 HcmV?d00001 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..593d0d7 --- /dev/null +++ b/src/main.py @@ -0,0 +1,99 @@ +import pygame as pg +import random + +class SimulationState: + def __init__(self): + self.agent_pos = pg.Vector2(0, 0) + self.world_limits = pg.Vector2(25, 13) + + def updateAgent(self, move_agent): + self.agent_pos += move_agent + + 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 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 + + +class Interface: + def __init__(self): + pg.init() + + # stan symulacji + self.simulation_state = SimulationState() + self.move_agent = pg.Vector2(0, 0) + + # tilesy + self.cell_size = pg.Vector2(64, 64) + self.textures = pg.image.load("../res/tiles.png") + + # okno + pg.display.set_caption("Inteligentna śmieciarka") + self.window = pg.display.set_mode((1600, 832)) + self.simulation_state.world_limits.elementwise() * self.cell_size + + # dla pętli + self.clock = pg.time.Clock() + self.run_simulation = True + + def processUserInput(self): + self.move_agent = pg.Vector2(0, 0) + + for event in pg.event.get(): + if event.type == pg.QUIT: + self.run_simulation = False + break + elif event.type == pg.KEYDOWN: + if event.key == pg.K_ESCAPE: + self.run_simulation = False + break + elif event.key == pg.K_RIGHT: + self.move_agent.x += 1 + elif event.key == pg.K_LEFT: + self.move_agent.x -= 1 + elif event.key == pg.K_DOWN: + self.move_agent.y += 1 + elif 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) + + self.move_agent = pg.Vector2(move_x, move_y) + + def update(self): + self.simulation_state.updateAgent(self.move_agent) + + def render(self): + self.window.fill((8, 68, 0)) + + 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() + + def loop(self): + while self.run_simulation: + self.processUserInput() + self.processSimulationInput() + self.update() + self.render() + self.clock.tick(6) + pg.quit() + + +simulation = Interface() +simulation.loop()