inteligentna_smieciarka/src/main.py

92 lines
2.6 KiB
Python
Raw Normal View History

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