JacoBot/jacovirt/jacovirt/pad/window.py

142 lines
4.2 KiB
Python

import arcade
import jacovirt.logger
import logging
from jacovirt.pad.token import Token, Token_generator, Token_trash
# Screen title and size
SCREEN_TITLE = "Jaco Pad"
SCREEN_MULTILPLIER = 1
SCREEN_WIDTH = int(1000* SCREEN_MULTILPLIER)
SCREEN_HEIGHT = int(1000* SCREEN_MULTILPLIER)
# Window color
BACKGROUND_COLOR = (133, 100, 100)
class Pad(arcade.Window):
"""Main application class"""
def __init__(self):
# Init parent class
super().__init__(int(SCREEN_WIDTH), int(SCREEN_HEIGHT), SCREEN_TITLE)
# Set background color
arcade.set_background_color(BACKGROUND_COLOR)
# List of token generator
self.tokens_generator = arcade.SpriteList()
# List of trash
self.tokens_trash = arcade.SpriteList()
# The held token
self.held_token = None
self.tokens = arcade.SpriteList()
def setup(self):
"""Set up the pad"""
logging.info("Set up the pad.")
# Create a token
token = Token("up", 1*SCREEN_MULTILPLIER)
token.position = 500, 500
self.tokens.append(token)
# Create a token generator
token_generator = Token_generator("up", 1*SCREEN_MULTILPLIER)
token_generator.position = 200, 500
self.tokens_generator.append(token_generator)
# Create a other token generator
token_generator = Token_generator("right", 1*SCREEN_MULTILPLIER)
token_generator.position = 800, 500
self.tokens_generator.append(token_generator)
# Create a trash for token
trash = Token_trash(200, 200)
trash.position = 500, 800
self.tokens_trash.append(trash)
def on_draw(self):
"""Render the screen"""
# Clear the screen
self.clear()
# Draw the token generator
self.tokens_generator.draw()
# Draw the trash
self.tokens_trash.draw()
# Draw the tokens
self.tokens.draw()
def on_key_press(self, symbol, modifiers):
"""Called when the user presses key"""
if symbol == arcade.key.R:
self.setup()
print("Restart !")
if symbol == arcade.key.Q:
arcade.exit()
def on_mouse_press(self, x, y, button, key_modifiers):
"""Called when the user presses a mouse button"""
logging.info(f"Mouse pressed at {x},{y}")
# Get list of tokens we've clicked on
tokens = arcade.get_sprites_at_point((x, y), self.tokens)
# Have we clicked on a token?
if len(tokens) > 0:
# Might be a stack of tokens, get the top one
primary_token = tokens[-1]
# All other cases, grab the token we are clicking on
self.held_token = primary_token
return
# Get the list of generator we've clicked on
generators = arcade.get_sprites_at_point((x, y), self.tokens_generator)
# Have we clicked on a generator ?
if len(generators) > 0:
generator = generators[-1]
logging.info(f"Cliced on the generator of {generator.type}")
# Create a token and take it
self.held_token = generator.create_token(x, y)
self.tokens.append(self.held_token)
def on_mouse_release(self, x, y, button, modifiers):
"""Called when the user presses a mouse button"""
logging.info(f"Mouse relase at {x},{y}")
if self.held_token is not None:
# Find the closest trash
trash, distance = arcade.get_closest_sprite(self.held_token, self.tokens_trash)
# See if we are in contact with the closest trash, if yes delete the held token
if arcade.check_for_collision(self.held_token, trash):
trash.remove_token(self.held_token)
# Remove the token from the hand
self.held_token = None
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
"""Called when the user moves the mouse"""
# If we are holding token, move it with the mouse
if self.held_token != None:
self.held_token.center_x += dx
self.held_token.center_y += dy
def main():
"""Main method"""
window = Pad()
window.setup()
arcade.run()