import pygame import random from event import TickEvent, EnvironmentBuiltEvent, AgentPlacedEvent, AgentMovedEvent, ViewSlideRequest, AgentKillEvent from model import DIRECTION_RIGHT, DIRECTION_LEFT, DIRECTION_UP, DIRECTION_DOWN class SectorSprite(pygame.sprite.Sprite): def __init__(self, sector, group=None, zoom=1): pygame.sprite.Sprite.__init__(self, group) self.image = pygame.Surface((10*zoom,10*zoom)) self.zoom = zoom self.sector = sector self.draw() self.rect = (sector.pos[1]*(10*zoom)+sector.pos[1], sector.pos[0]*(10*zoom)+sector.pos[0]) def draw(self): self.image.fill((0,255,128)) def update(self, shift): self.rect = (self.sector.pos[1]*(10*self.zoom)+self.sector.pos[1]+shift[0], self.sector.pos[0]*(10*self.zoom)+self.sector.pos[0]+shift[1]) class AgentSprite(pygame.sprite.Sprite): def __init__(self, agent, group=None, zoom=1): pygame.sprite.Sprite.__init__(self, group) self.image = pygame.Surface((10*zoom,10*zoom)) self.agent = agent self.zoom = zoom self.draw() self.rect = (agent.sector.pos[1]*(10*zoom)+agent.sector.pos[1], agent.sector.pos[0]*(10*zoom)+agent.sector.pos[0]) def draw(self): self.image.fill((0, 255, 128)) pygame.draw.circle(self.image, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), (5*self.zoom, 5*self.zoom), 4*self.zoom, 0) def update(self, shift): self.rect = (self.agent.sector.pos[1]*(10*self.zoom)+self.agent.sector.pos[1]+shift[0], self.agent.sector.pos[0]*(10*self.zoom)+self.agent.sector.pos[0]+shift[1]) class SDLView: def __init__(self, disp, w, h, zoom=1): pygame.init() pygame.event.set_allowed(None) self.screen = pygame.display.set_mode((w, h)) self.zoom = zoom disp.register(self) self.disp = disp self.env_sprites = pygame.sprite.RenderUpdates() self.agent_sprites = pygame.sprite.RenderUpdates() self.view_shift = [0, 0] def show_env(self, env): for line in env.sectors: for sector in line: s = SectorSprite(sector, self.env_sprites, self.zoom) def show_agent(self, agent): AgentSprite(agent, self.agent_sprites, self.zoom) def agent_to_sprite(self, a): for s in self.agent_sprites: if s.agent == a: return s return None def update_sectors(self): for s in self.env_sprites: s.update(self.view_shift) def update_agent(self, agent): #print "Update agent visual" for sprite in self.agent_sprites: if sprite.agent == agent: sprite.update(self.view_shift) break def update_agents(self): for sprite in self.agent_sprites: sprite.update(self.view_shift) def update_view(self, dir): if dir == DIRECTION_RIGHT: self.view_shift[0] -= 10 elif dir == DIRECTION_LEFT: self.view_shift[0] += 10 elif dir == DIRECTION_UP: self.view_shift[1] += 10 elif dir == DIRECTION_DOWN: self.view_shift[1] -= 10 self.update_sectors() self.update_agents() def render(self): self.screen.fill((255, 255, 255)) self.env_sprites.draw(self.screen) self.agent_sprites.draw(self.screen) pygame.display.flip() def notify(self, e): if isinstance(e, TickEvent): self.render() print "Sprites : %d"%len(self.agent_sprites) elif isinstance(e, AgentMovedEvent): self.update_agent(e.agent) elif isinstance(e, EnvironmentBuiltEvent): print "Showing env" self.show_env(e.env) elif isinstance(e, AgentPlacedEvent): #print "Showing agent", e.agent self.show_agent(e.agent) elif isinstance(e, ViewSlideRequest): self.update_view(e.dir) elif isinstance(e, AgentKillEvent): for a in self.agent_sprites: if e.agent == a.agent: a.kill() a.agent = None break