import pygame import random import core.view from core.event import MouseClickEvent from model import Marble from event import MarbleMoveRequest, MarbleSpawnRequest class MarbleSprite(core.view.AgentSprite): def __init__(self, agent, group=None, zoom=1): self.hilighted = False core.view.AgentSprite.__init__(self, agent, group, zoom) def draw(self): if self.hilighted == True: self.image.fill((255, 0, 0)) else: self.image.fill((0, 255, 128)) # Pick the corresponding color color = {Marble.TYPE1 : (0, 0, 255), Marble.TYPE2 : (200, 255, 100), Marble.TYPE3 : (0, 255, 255), Marble.TYPE4 : (255, 0, 0), Marble.TYPE5 : (255, 0, 255), Marble.TYPE6 : (255, 255, 0), Marble.TYPE7 : (0, 0, 0)}[self.agent.type] pygame.draw.circle(self.image, color, (5*self.zoom, 5*self.zoom), 4*self.zoom, 0) def hilight(self, h): self.hilighted = h self.draw() class CGView(core.view.SDLView): def show_agent(self, agent): MarbleSprite(agent, self.agent_sprites, self.zoom) self.selected = None def click_to_sector(self, x, y): for s in self.env_sprites: r = pygame.Rect(s.rect, s.image.get_size()) if r.collidepoint(x, y): return s.sector return None def notify(self, e): core.view.SDLView.notify(self, e) if isinstance(e, MouseClickEvent): # Right button : next round ! if e.button == 3: if self.selected: sprite = self.agent_to_sprite(self.selected.occupant) self.selected = None sprite.hilight(False) self.disp.post(MarbleSpawnRequest(3)) # Left button: select/move marble else: sect = self.click_to_sector(e.pos[0], e.pos[1]) # A sector has been clicked if sect: # We already have a marble selected if self.selected: # We have clicked the same sector, unselect it if self.selected == sect: sprite = self.agent_to_sprite(sect.occupant) self.selected = None sprite.hilight(False) # Try to move ! else: old_sector = self.selected old_occupant = self.selected.occupant # Send request! self.disp.post(MarbleMoveRequest(self.selected.occupant, sect)) sprite = self.agent_to_sprite(old_occupant) if sprite: sprite.hilight(False) self.selected = None # First selection else: if sect.occupant: sprite = self.agent_to_sprite(sect.occupant) self.selected = sect sprite.hilight(True)