import pygame from pygame.locals import * from event import TickEvent, QuitEvent, AgentMoveRequest from event import SimulationStartedEvent, ViewSlideRequest, MouseClickEvent from model import DIRECTION_RIGHT, DIRECTION_LEFT, DIRECTION_UP, DIRECTION_DOWN class CPUIdler: def __init__(self, disp): self.disp = disp disp.register(self) self.run = 1 def idle(self, wt=0, cycles=0): if cycles != 0: start = pygame.time.get_ticks() for i in range(cycles): self.disp.post(TickEvent()) pygame.time.wait(wt) end = pygame.time.get_ticks() return end - start else: while self.run: print "New round" self.disp.post(TickEvent()) pygame.time.wait(wt) def notify(self, e): if isinstance(e, QuitEvent): self.run = 0 class KeyboardController: def __init__(self, disp): self.disp = disp disp.register(self) pygame.key.set_repeat(1, 10) pygame.event.set_allowed(KEYDOWN) def notify(self, e): if isinstance(e, TickEvent): for event in pygame.event.get(KEYDOWN): if event.key == K_ESCAPE: self.disp.post(QuitEvent()) elif event.key == K_RIGHT: self.disp.post(ViewSlideRequest(DIRECTION_RIGHT)) elif event.key == K_LEFT: self.disp.post(ViewSlideRequest(DIRECTION_LEFT)) elif event.key == K_UP: self.disp.post(ViewSlideRequest(DIRECTION_UP)) elif event.key == K_DOWN: self.disp.post(ViewSlideRequest(DIRECTION_DOWN)) class MouseController: def __init__(self, disp): self.disp = disp disp.register(self) pygame.event.set_allowed(MOUSEBUTTONUP) def notify(self, e): if isinstance(e, TickEvent): for event in pygame.event.get(MOUSEBUTTONUP): self.disp.post(MouseClickEvent(event.pos, event.button)) class AgentController: def __init__(self, disp): self.disp = disp disp.register(self) self.do = 0 def notify(self, e): if isinstance(e, SimulationStartedEvent): self.do = 1 if isinstance(e, TickEvent): if self.do: self.disp.post(AgentMoveRequest())