Handles mouse input (prototype)
This commit is contained in:
1 parent
fe9fc14849
commit
0e3f07938c
5 files changed
+76
-6
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
red_spice
|
||||||
|
yellow_spice
|
||||||
|
orange_spice
|
||||||
|
pink_spice
|
||||||
|
blue_spice
|
||||||
|
green_spice
|
||||||
|
purple_spice
|
||||||
+14
-3
@@ -8,10 +8,11 @@ from components import (
|
|||||||
SpaceShipMarker,
|
SpaceShipMarker,
|
||||||
Target,
|
Target,
|
||||||
)
|
)
|
||||||
from random import randint, sample
|
from random import choice, randint, sample
|
||||||
from settings import (
|
from settings import (
|
||||||
MIN_PLANET_RADIUS,
|
MIN_PLANET_RADIUS,
|
||||||
MAX_PLANET_RADIUS,
|
MAX_PLANET_RADIUS,
|
||||||
|
PLANET_COLORS,
|
||||||
SCREEN_WIDTH,
|
SCREEN_WIDTH,
|
||||||
SCREEN_HEIGHT,
|
SCREEN_HEIGHT,
|
||||||
SPACESHIP_COLOR,
|
SPACESHIP_COLOR,
|
||||||
@@ -58,8 +59,8 @@ def generate_random_planet(world: World) -> int:
|
|||||||
radius: int = randint(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS)
|
radius: int = randint(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS)
|
||||||
x: int = randint(radius, SCREEN_WIDTH - radius)
|
x: int = randint(radius, SCREEN_WIDTH - radius)
|
||||||
y: int = randint(radius, SCREEN_HEIGHT - 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)
|
return create_planet(world, x, y, radius, color)
|
||||||
|
|
||||||
|
|
||||||
@@ -94,3 +95,13 @@ def generate_random_spaceship(world: World) -> int:
|
|||||||
_planet_sample = sample(planets, 2)
|
_planet_sample = sample(planets, 2)
|
||||||
|
|
||||||
return create_travelling_spaceship(world, *_planet_sample)
|
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()
|
||||||
+30
@@ -9,3 +9,33 @@ SPACESHIP_RADIUS = 5
|
|||||||
SPACESHIP_COLOR = (255, 255, 255, 255)
|
SPACESHIP_COLOR = (255, 255, 255, 255)
|
||||||
SPACESHIP_PATH_COLOR = (127, 127, 127, 127)
|
SPACESHIP_PATH_COLOR = (127, 127, 127, 127)
|
||||||
SPACESHIP_SPEED = 500
|
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),
|
||||||
|
)
|
||||||
@@ -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))
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
import pyglet
|
import pyglet
|
||||||
|
from pyglet.window.mouse import LEFT
|
||||||
from pyglet.shapes import Circle, Line
|
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 helpers import generate_random_planet, generate_random_spaceship
|
||||||
from libs.ecs.world import World
|
from libs.ecs.world import World
|
||||||
from libs.ecs.event import EventBus
|
from libs.ecs.event import EventBus
|
||||||
@@ -42,11 +43,25 @@ def on_draw():
|
|||||||
for _, component_storage in world.query(SpaceShipMarker):
|
for _, component_storage in world.query(SpaceShipMarker):
|
||||||
component_storage[Line].draw()
|
component_storage[Line].draw()
|
||||||
|
|
||||||
|
for _, component_storage in world.query(SpaceShipMarker):
|
||||||
|
component_storage[Circle].draw()
|
||||||
|
|
||||||
for _, component_storage in world.query(PlanetMarker):
|
for _, component_storage in world.query(PlanetMarker):
|
||||||
component_storage[Circle].draw()
|
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):
|
def on_update(dt):
|
||||||
|
|||||||
Reference in new issue
Block a user