예제 #1
0
    def make_image(
        self,
        image: Optional[Image] = None,
        font: Optional[Font] = None,
    ) -> Tuple[Image, Draw]:

        if image is not None:
            image = image.copy()
        else:
            image = make_new_image("L", (self.width, self.height), "black")

        draw = Draw(image)
        draw.font = (self.font or font)
        draw.ink = 255

        return (image, draw)
예제 #2
0
    def draw_gamestate(self, draw: ImageDraw.Draw):
        # food (cross)
        food_tile_center = to_canvas_coord(self.game.food_position)
        draw.ink = 4
        draw.point(food_tile_center + (-1, 0))
        draw.point(food_tile_center + (0, -1))
        draw.point(food_tile_center)
        draw.point(food_tile_center + (1, 0))
        draw.point(food_tile_center + (0, 1))

        # snake body
        draw.ink = 3
        draw.fill = 3
        snake_head_tile_center = to_canvas_coord(self.game.snake.head_position)
        tile_center = snake_head_tile_center
        is_first = True

        labs = partial(looparound_vector, board_size)
        tile_position = self.game.snake.head_position
        for move in reversed(self.game.snake.movements):
            start_offset = NULL_VECTOR
            end_offset = NULL_VECTOR

            if move.direction == UP:
                dir_vector = Vector(0, 1)
                start_offset = Vector(0, -1)
            elif move.direction == DOWN:
                dir_vector = Vector(0, -1)
                end_offset = Vector(0, 1)
            elif move.direction == LEFT:
                dir_vector = Vector(1, 0)
                start_offset = Vector(-1, 0)
            elif move.direction == RIGHT:
                dir_vector = Vector(-1, 0)
                end_offset = Vector(1, 0)

            for i in range(move.amount):
                tile_position = labs(tile_position + dir_vector)
                tile_center = to_canvas_coord(tile_position)
                if move.direction == UP and tile_position.y == board_size.h - 1:
                    end_offset += (0, 1)
                elif move.direction == LEFT and tile_position.x == board_size.w - 1:
                    end_offset += (1, 0)

                draw.rectangle([
                    tile_center + (-1, -1) + start_offset,
                    tile_center + (1, 1) + end_offset
                ])

            if is_first:
                # snake head
                end_offset = NULL_VECTOR
                if move.direction == UP and self.game.snake.head_position.y == board_size.h - 1:
                    end_offset += (0, 1)
                elif move.direction == LEFT and self.game.snake.head_position.x == board_size.w - 1:
                    end_offset += (1, 0)

                draw.rectangle([
                    snake_head_tile_center + (-1, -1),
                    snake_head_tile_center + (1, 1) + end_offset
                ])

            is_first = False

        # snake eyes
        last_movement_dir = self.game.snake.movements[-1].direction
        if last_movement_dir == UP or last_movement_dir == DOWN:
            draw.point(snake_head_tile_center + (-1, 0), 2)
            draw.point(snake_head_tile_center + (1, 0), 2)
        else:
            draw.point(snake_head_tile_center + (0, -1), 2)
            draw.point(snake_head_tile_center + (0, 1), 2)