diff options
author | 2021-09-03 00:31:12 -0700 | |
---|---|---|
committer | 2021-09-03 00:31:12 -0700 | |
commit | ea47bc617e558929bcee39e6008a57d6dd814aa1 (patch) | |
tree | c40e2f23e55119fb33f83271d227103cb9be7c6f /bot/exts/evergreen/snakes/_utils.py | |
parent | Improved consistency for codeblocks to end with a newline (diff) | |
parent | Merge pull request #802 from python-discord/decorator-factory/typehints-fix (diff) |
Merge branch 'main' into android-codeblock-fix
Diffstat (limited to 'bot/exts/evergreen/snakes/_utils.py')
-rw-r--r-- | bot/exts/evergreen/snakes/_utils.py | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/bot/exts/evergreen/snakes/_utils.py b/bot/exts/evergreen/snakes/_utils.py index a3ff3d12..b5f13c53 100644 --- a/bot/exts/evergreen/snakes/_utils.py +++ b/bot/exts/evergreen/snakes/_utils.py @@ -6,7 +6,6 @@ import math import random from itertools import product from pathlib import Path -from typing import List, Tuple from PIL import Image from PIL.ImageDraw import ImageDraw @@ -99,25 +98,25 @@ BOARD = { 16: 6 } -DEFAULT_SNAKE_COLOR: int = 0x15c7ea -DEFAULT_BACKGROUND_COLOR: int = 0 -DEFAULT_IMAGE_DIMENSIONS: Tuple[int] = (200, 200) -DEFAULT_SNAKE_LENGTH: int = 22 -DEFAULT_SNAKE_WIDTH: int = 8 -DEFAULT_SEGMENT_LENGTH_RANGE: Tuple[int] = (7, 10) -DEFAULT_IMAGE_MARGINS: Tuple[int] = (50, 50) -DEFAULT_TEXT: str = "snek\nit\nup" -DEFAULT_TEXT_POSITION: Tuple[int] = ( +DEFAULT_SNAKE_COLOR = 0x15c7ea +DEFAULT_BACKGROUND_COLOR = 0 +DEFAULT_IMAGE_DIMENSIONS = (200, 200) +DEFAULT_SNAKE_LENGTH = 22 +DEFAULT_SNAKE_WIDTH = 8 +DEFAULT_SEGMENT_LENGTH_RANGE = (7, 10) +DEFAULT_IMAGE_MARGINS = (50, 50) +DEFAULT_TEXT = "snek\nit\nup" +DEFAULT_TEXT_POSITION = ( 10, 10 ) -DEFAULT_TEXT_COLOR: int = 0xf2ea15 +DEFAULT_TEXT_COLOR = 0xf2ea15 X = 0 Y = 1 ANGLE_RANGE = math.pi * 2 -def get_resource(file: str) -> List[dict]: +def get_resource(file: str) -> list[dict]: """Load Snake resources JSON.""" return json.loads((SNAKE_RESOURCES / f"{file}.json").read_text("utf-8")) @@ -144,7 +143,7 @@ class PerlinNoiseFactory(object): Licensed under ISC """ - def __init__(self, dimension: int, octaves: int = 1, tile: Tuple[int] = (), unbias: bool = False): + def __init__(self, dimension: int, octaves: int = 1, tile: tuple[int, ...] = (), unbias: bool = False): """ Create a new Perlin noise factory in the given number of dimensions. @@ -172,7 +171,7 @@ class PerlinNoiseFactory(object): self.gradient = {} - def _generate_gradient(self) -> Tuple[float, ...]: + def _generate_gradient(self) -> tuple[float, ...]: """ Generate a random unit vector at each grid point. @@ -282,13 +281,14 @@ class PerlinNoiseFactory(object): def create_snek_frame( perlin_factory: PerlinNoiseFactory, perlin_lookup_vertical_shift: float = 0, - image_dimensions: Tuple[int] = DEFAULT_IMAGE_DIMENSIONS, image_margins: Tuple[int] = DEFAULT_IMAGE_MARGINS, + image_dimensions: tuple[int, int] = DEFAULT_IMAGE_DIMENSIONS, + image_margins: tuple[int, int] = DEFAULT_IMAGE_MARGINS, snake_length: int = DEFAULT_SNAKE_LENGTH, snake_color: int = DEFAULT_SNAKE_COLOR, bg_color: int = DEFAULT_BACKGROUND_COLOR, - segment_length_range: Tuple[int] = DEFAULT_SEGMENT_LENGTH_RANGE, snake_width: int = DEFAULT_SNAKE_WIDTH, - text: str = DEFAULT_TEXT, text_position: Tuple[int] = DEFAULT_TEXT_POSITION, - text_color: Tuple[int] = DEFAULT_TEXT_COLOR -) -> Image: + segment_length_range: tuple[int, int] = DEFAULT_SEGMENT_LENGTH_RANGE, snake_width: int = DEFAULT_SNAKE_WIDTH, + text: str = DEFAULT_TEXT, text_position: tuple[float, float] = DEFAULT_TEXT_POSITION, + text_color: int = DEFAULT_TEXT_COLOR +) -> Image.Image: """ Creates a single random snek frame using Perlin noise. @@ -297,7 +297,7 @@ def create_snek_frame( """ start_x = random.randint(image_margins[X], image_dimensions[X] - image_margins[X]) start_y = random.randint(image_margins[Y], image_dimensions[Y] - image_margins[Y]) - points = [(start_x, start_y)] + points: list[tuple[float, float]] = [(start_x, start_y)] for index in range(0, snake_length): angle = perlin_factory.get_plain_noise( @@ -311,8 +311,8 @@ def create_snek_frame( )) # normalize bounds - min_dimensions = [start_x, start_y] - max_dimensions = [start_x, start_y] + min_dimensions: list[float] = [start_x, start_y] + max_dimensions: list[float] = [start_x, start_y] for point in points: min_dimensions[X] = min(point[X], min_dimensions[X]) min_dimensions[Y] = min(point[Y], min_dimensions[Y]) @@ -706,7 +706,7 @@ class SnakeAndLaddersGame: """Clean up the finished game object.""" del self.snakes.active_sal[self.channel] - def _board_coordinate_from_index(self, index: int) -> Tuple[int, int]: + def _board_coordinate_from_index(self, index: int) -> tuple[int, int]: """Convert the tile number to the x/y coordinates for graphical purposes.""" y_level = 9 - math.floor((index - 1) / 10) is_reversed = math.floor((index - 1) / 10) % 2 != 0 |