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 0000000..474cc23 Binary files /dev/null and b/res/tiles.png differ 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()