Beispiel #1
0
    def update_position(self):
        if self.direction == game.LEFT:
            self.x -= 1
        elif self.direction == game.RIGHT:
            self.x += 1
        elif self.direction == game.UP:
            self.y -= 1
        elif self.direction == game.DOWN:
            self.y += 1

        # Check if player is out of bounds
        if self.x < 0:
            self.x = game.BOARD_WIDTH-1
        if self.x >= game.BOARD_WIDTH:
            self.x = 0
        if self.y < 0:
            self.y = game.BOARD_HEIGHT-1
        if self.y >= game.BOARD_HEIGHT:
            self.y = 0

        if self.is_invincible:
            if isinstance(game.board[self.x][self.y], Apple):
                self.is_invincible = False
            else:
                head = self.parts[0]
                head.x, head.y = self.x, self.y
                head.rect = pygame.Rect(head.x*head.width, head.y*head.height,
                        head.width, head.height)
                self._lock_set_direction = False
                return

        # Update game board
        try:
            head = SnakePart(self, self.x, self.y, self.color)
            self.parts.append(head)
        except game.CollisionError, ce:
            if isinstance(ce.collidee, Apple):
                game.apples.remove(ce.collidee)
                ce.collidee.remove_from_board()
                self.grow = True
                # if self.player_number == 0:
                #     self.grow = False
                head = SnakePart(self, self.x, self.y, self.color)
                self.parts.append(head)
                game.add_apple()
                game.log_screen.add("%s grew to %s blocks." % (self.name, len(self.parts)))
            else:
                self.kill(ce.collidee)
                if isinstance(ce.collidee, Missile):
                    ce.collidee.cleanup()
                    ce.collidee.remove_from_board()
                elif isinstance(ce.collidee, SnakePart):
                    player = ce.collidee.player
                    if (player.x, player.y) == (ce.collidee.x, ce.collidee.y):
                        player.kill(self.parts[0])
                return
Beispiel #2
0
 def parse_layout(self):
     layout = self.layout.split('\n')[1:]
     for y, row in enumerate(layout):
         for x, column in enumerate(row):
             if column == 'W':
                 game.walls.append(game_objects.Wall(x, y))
             if column == 'I':
                 game.walls.append(game_objects.IndestructableWall(x, y))
             elif column in ('1', '2', '3', '4'):
                 if int(column) <= game.num_players:
                     game.players.append(game_objects.Player('Player %s' % column, int(column)-1, x, y, self.player_directions[column], player_colors[column]))
     for i in range(self.num_apples):
         game.add_apple()