Ejemplo n.º 1
0
 def __init__(self, screen, squareSize=30, mapSize=20, drawerParams={}):
     self.screen = screen
     self.players = [
         Player(Objects.Player2, 0, 0),
         Player(Objects.Player1, mapSize - 1, mapSize - 1)
     ]
     self.map = MapModel(mapSize, self.players[0], self.players[1])
     self.players[0].setMap(self.map)
     self.players[1].setMap(self.map)
     self.drawer = MapDrawer(self.map, screen, squareSize, mapSize,
                             **drawerParams)
Ejemplo n.º 2
0
class Game:
    def __init__(self, screen, squareSize=30, mapSize=20, drawerParams={}):
        self.screen = screen
        self.players = [
            Player(Objects.Player2, 0, 0),
            Player(Objects.Player1, mapSize - 1, mapSize - 1)
        ]
        self.map = MapModel(mapSize, self.players[0], self.players[1])
        self.players[0].setMap(self.map)
        self.players[1].setMap(self.map)
        self.drawer = MapDrawer(self.map, screen, squareSize, mapSize,
                                **drawerParams)

    def redraw(self, update=True):
        self.drawer.redraw(update)

    def processEvents(self, t):
        """ Processes events, taking into account t seconds have passed since last """
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            #if event.type == KEYDOWN and event.key == K_t:
            #    print("CHEAT")
            #    Objects.BombBonus.collectBy(self.player[0])
            #    Objects.Pepper.collectBy(self.player[0])
        self.handleKeys(self.players[0], t, P1_KEYS)
        self.handleKeys(self.players[1], t, P2_KEYS)

    def handleKeys(self, player, t, playerKeys):
        keys = pygame.key.get_pressed()
        pressed = 0
        newDirection = (0, 0)
        for key, direction in zip(playerKeys[:-1], DIRECTIONS):
            if keys[key]:
                pressed += 1
                newDirection = direction
        if keys[playerKeys[-1]]:
            self.map.placeBomb(player)
        if pressed != 1:
            player.setMovement((0, 0))
        else:
            player.setMovement(newDirection)
            player.setDirection(newDirection)

    def update(self, t, update=True):
        " Updates the map and redraws it. Returns True if something on the map was added/removed "
        result = self.map.update(t)
        self.redraw(update)
        return result

    def winners(self):
        return list(filter(lambda p: p.lives == 0, self.players))
Ejemplo n.º 3
0
class Game:
    def __init__(self, screen, squareSize = 30, mapSize = 20, drawerParams={}):
        self.screen = screen
        self.players = [Player(Objects.Player2, 0, 0), 
                        Player(Objects.Player1, mapSize-1, mapSize-1)]
        self.map = MapModel(mapSize, self.players[0], self.players[1])
        self.players[0].setMap(self.map)
        self.players[1].setMap(self.map)
        self.drawer = MapDrawer(self.map, screen, squareSize, mapSize, **drawerParams)
    
    def redraw(self, update = True):
        self.drawer.redraw(update)
    
    def processEvents(self, t):
        """ Processes events, taking into account t seconds have passed since last """
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            #if event.type == KEYDOWN and event.key == K_t:
            #    print("CHEAT")
            #    Objects.BombBonus.collectBy(self.player[0])
            #    Objects.Pepper.collectBy(self.player[0])
        self.handleKeys(self.players[0], t, P1_KEYS)
        self.handleKeys(self.players[1], t, P2_KEYS)
            
    def handleKeys(self, player, t, playerKeys):
        keys = pygame.key.get_pressed()
        pressed = 0
        newDirection = (0,0)
        for key, direction in zip(playerKeys[:-1], DIRECTIONS):
            if keys[key]:
                pressed += 1
                newDirection = direction
        if keys[playerKeys[-1]]:
            self.map.placeBomb(player)
        if pressed != 1:
            player.setMovement((0,0))
        else:
            player.setMovement(newDirection)
            player.setDirection(newDirection)
    
    def update(self, t, update=True):
        " Updates the map and redraws it. Returns True if something on the map was added/removed "
        result = self.map.update(t)
        self.redraw(update)      
        return result
        
    def winners(self):
        return list(filter(lambda p: p.lives == 0, self.players))
Ejemplo n.º 4
0
 def __init__(self, screen, squareSize = 30, mapSize = 20, drawerParams={}):
     self.screen = screen
     self.players = [Player(Objects.Player2, 0, 0), 
                     Player(Objects.Player1, mapSize-1, mapSize-1)]
     self.map = MapModel(mapSize, self.players[0], self.players[1])
     self.players[0].setMap(self.map)
     self.players[1].setMap(self.map)
     self.drawer = MapDrawer(self.map, screen, squareSize, mapSize, **drawerParams)
Ejemplo n.º 5
0
        """Adjusts the row and column to map to coordinates on screen"""
        dw = 0 if w is None else self.tileSize - w
        dh = 0 if h is None else self.tileSize - h
        return (row * self.tileSize + self.start_x + dw / 2,
                col * self.tileSize + self.start_y + dh / 2)

    def redraw(self, update=True):
        """ Redraws the map onto the screen. If update is True, 
            pygame.display.flip() will be called. """
        self.screen.blit(Objects.Background(self.tileSize * self.mapSize),
                         (self.start_x, self.start_y))
        for (row, col), tile in self.model.items():
            icon = tile(self.tileSize)
            w, h = icon.get_size()
            self.screen.blit(icon, self.adjusted(row, col, w, h))
        if update:
            pygame.display.flip()


if __name__ == "__main__":
    import sys
    sys.path.append(".")
    from models.MapModel import MapModel
    n = 20
    model = MapModel(n)
    pygame.init()
    size = 25
    screen = pygame.display.set_mode((size * n, size * n))
    drawer = MapDrawer(model, screen, size)
    drawer.redraw()
    pygame.time.delay(3000)