From 0e3f07938ccd23b425fb6d1985c038fc3017703e Mon Sep 17 00:00:00 2001 From: nejdetckenobi Date: Sun, 11 Oct 2026 18:21:07 +0300 Subject: [PATCH] Handles mouse input (prototype) --- assets/goods.txt | 7 +++++++ helpers.py | 17 ++++++++++++++--- settings.py | 30 ++++++++++++++++++++++++++++++ test.py | 7 +++++++ ui.py | 21 ++++++++++++++++++--- 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 assets/goods.txt create mode 100644 test.py diff --git a/assets/goods.txt b/assets/goods.txt new file mode 100644 index 0000000..16cce75 --- /dev/null +++ b/assets/goods.txt @@ -0,0 +1,7 @@ +red_spice +yellow_spice +orange_spice +pink_spice +blue_spice +green_spice +purple_spice diff --git a/helpers.py b/helpers.py index 865f4d8..b0f0f9e 100644 --- a/helpers.py +++ b/helpers.py @@ -8,10 +8,11 @@ from components import ( SpaceShipMarker, Target, ) -from random import randint, sample +from random import choice, randint, sample from settings import ( MIN_PLANET_RADIUS, MAX_PLANET_RADIUS, + PLANET_COLORS, SCREEN_WIDTH, SCREEN_HEIGHT, SPACESHIP_COLOR, @@ -58,8 +59,8 @@ def generate_random_planet(world: World) -> int: radius: int = randint(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS) x: int = randint(radius, SCREEN_WIDTH - radius) y: int = randint(radius, SCREEN_HEIGHT - radius) - color = (randint(0, 255), randint(0, 255), randint(0, 255), 255) - + # color = (randint(0, 255), randint(0, 255), randint(0, 255), 255) + color = choice(PLANET_COLORS) + (255,) return create_planet(world, x, y, radius, color) @@ -94,3 +95,13 @@ def generate_random_spaceship(world: World) -> int: _planet_sample = sample(planets, 2) return create_travelling_spaceship(world, *_planet_sample) + + +def load_items(path: str) -> list[str]: + with open(path) as f: + data = [l.rstrip("\n") for l in f.readlines()] + return data + + +def normalize_name(name: str) -> str: + return name.replace("_", " ").title() diff --git a/settings.py b/settings.py index c77b2a1..32c4d1a 100644 --- a/settings.py +++ b/settings.py @@ -9,3 +9,33 @@ SPACESHIP_RADIUS = 5 SPACESHIP_COLOR = (255, 255, 255, 255) SPACESHIP_PATH_COLOR = (127, 127, 127, 127) SPACESHIP_SPEED = 500 + +PLANET_COLORS = ( + # (0, 0, 0), # NO FULL BLACK PLANET ON BLACK BACKGROUND + (0, 0, 127), + (0, 0, 255), + (0, 127, 0), + (0, 127, 127), + (0, 127, 255), + (0, 255, 0), + (0, 255, 127), + (0, 255, 255), + (127, 0, 0), + (127, 0, 127), + (127, 0, 255), + (127, 127, 0), + (127, 127, 127), + (127, 127, 255), + (127, 255, 0), + (127, 255, 127), + (127, 255, 255), + (255, 0, 0), + (255, 0, 127), + (255, 0, 255), + (255, 127, 0), + (255, 127, 127), + (255, 127, 255), + (255, 255, 0), + (255, 255, 127), + (255, 255, 255), +) diff --git a/test.py b/test.py new file mode 100644 index 0000000..f4bcd54 --- /dev/null +++ b/test.py @@ -0,0 +1,7 @@ +from helpers import normalize_name, load_items + +if __name__ == "__main__": + items = load_items("assets/goods.txt") + + for item in items: + print(normalize_name(item)) diff --git a/ui.py b/ui.py index cb52d8e..568b14f 100644 --- a/ui.py +++ b/ui.py @@ -1,8 +1,9 @@ from random import randint import pyglet +from pyglet.window.mouse import LEFT from pyglet.shapes import Circle, Line -from components import PlanetMarker, SpaceShipMarker +from components import PlanetMarker, Position, Radius, SpaceShipMarker, Vector2D from helpers import generate_random_planet, generate_random_spaceship from libs.ecs.world import World from libs.ecs.event import EventBus @@ -42,11 +43,25 @@ def on_draw(): for _, component_storage in world.query(SpaceShipMarker): component_storage[Line].draw() + for _, component_storage in world.query(SpaceShipMarker): + component_storage[Circle].draw() + for _, component_storage in world.query(PlanetMarker): component_storage[Circle].draw() - for _, component_storage in world.query(SpaceShipMarker): - component_storage[Circle].draw() + +@window.event +def on_mouse_release(x, y, button, modifiers): + if button == LEFT: + closest_entity, component_storage = min( + world.query(PlanetMarker), + key=lambda p: abs(p[1][Position] - Vector2D(x, y)), + ) + pos = component_storage[Position] + if component_storage[Radius].value < abs(pos - Vector2D(x, y)): + print("NO PLANET") + else: + print("PLANET") def on_update(dt):