Symulacja, losowo ruszający się agent

This commit is contained in:
s475275 2023-03-18 23:27:12 +01:00
parent c45d6b6a7a
commit 243eaed295
3 changed files with 100 additions and 0 deletions

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
pygame == 2.3.0

BIN
res/tiles.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

99
src/main.py Normal file
View File

@ -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()