forked from MDL29/LPH-cubito
add my code
This commit is contained in:
parent
82e02bbe9f
commit
564a434554
271
gui-arcade.py
Normal file
271
gui-arcade.py
Normal file
|
@ -0,0 +1,271 @@
|
||||||
|
"""
|
||||||
|
Cubito
|
||||||
|
ressources : https://api.arcade.academy/en/latest/resources.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
import arcade
|
||||||
|
# import cubito
|
||||||
|
|
||||||
|
# Screen title and size
|
||||||
|
SCREEN_TITLE = "Cubito"
|
||||||
|
SCREEN_WIDTH = 500*2
|
||||||
|
SCREEN_HEIGHT = 500*2
|
||||||
|
|
||||||
|
# Margin between mat & screen side
|
||||||
|
VERTICAL_MARGIN_PERCENT = 0.10
|
||||||
|
HORIZONTAL_MARGIN_PERCENT = 0.10
|
||||||
|
|
||||||
|
#Token row
|
||||||
|
TOKEN_ROW = 10
|
||||||
|
|
||||||
|
#Token start
|
||||||
|
X_TOKEN_START = 50*2
|
||||||
|
Y_TOKEN_START = 450*2
|
||||||
|
|
||||||
|
# Space between tokens
|
||||||
|
X_SPACING_TOKEN = 90
|
||||||
|
Y_SPACING_TOKEN = 90
|
||||||
|
|
||||||
|
# Token size
|
||||||
|
TOKEN_SCALE = 0.5*2
|
||||||
|
TOKEN_HEIGHT = 75 * TOKEN_SCALE
|
||||||
|
TOKEN_WIDTH = 75 * TOKEN_SCALE
|
||||||
|
|
||||||
|
# List of token types
|
||||||
|
TOKEN_TYPES = ["up", "left", "right", "hamburger"]#, "PAUSE", "FUNCTION"]
|
||||||
|
|
||||||
|
#Mat start
|
||||||
|
X_MAT_START = 250*2
|
||||||
|
Y_MAT_START = 400*2
|
||||||
|
|
||||||
|
# Mat size
|
||||||
|
MAT_PERCENT_OVERSIZE = 1.25
|
||||||
|
MAT_HEIGHT = int(TOKEN_HEIGHT * MAT_PERCENT_OVERSIZE)
|
||||||
|
MAT_WIDTH = int(TOKEN_WIDTH * MAT_PERCENT_OVERSIZE)
|
||||||
|
|
||||||
|
# Number of column & row mat
|
||||||
|
MAT_COLUMN = 4
|
||||||
|
MAT_ROW = 4
|
||||||
|
MAT_FUNCTION_ROW = 2
|
||||||
|
|
||||||
|
# Space between mats
|
||||||
|
X_SPACING_MAT = MAT_WIDTH + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
|
||||||
|
Y_SPACING_MAT = MAT_HEIGHT + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
|
||||||
|
|
||||||
|
# Top for mats
|
||||||
|
TOP_Y = SCREEN_HEIGHT - MAT_HEIGHT - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
|
||||||
|
|
||||||
|
# Bottom for tokens
|
||||||
|
BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
|
||||||
|
|
||||||
|
# Start from left side
|
||||||
|
START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Mat(arcade.SpriteSolidColor):
|
||||||
|
def __init__(self, height, width, color=arcade.color.AMBER):
|
||||||
|
super().__init__(width, height, color)
|
||||||
|
|
||||||
|
|
||||||
|
class Token_sprite(arcade.Sprite):
|
||||||
|
""" Card sprite """
|
||||||
|
|
||||||
|
def __init__(self, token_type, scale=1):
|
||||||
|
""" Card constructor """
|
||||||
|
|
||||||
|
# Attributes for token type
|
||||||
|
self.token_type = token_type
|
||||||
|
|
||||||
|
self.image_file_name = f":resources:onscreen_controls/shaded_dark/{self.token_type}.png"
|
||||||
|
# Call the parent
|
||||||
|
super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Cubito(arcade.Window):
|
||||||
|
"""Main application class"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
# Init parent class
|
||||||
|
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
|
||||||
|
# Set background color
|
||||||
|
arcade.set_background_color(arcade.color.AMAZON)
|
||||||
|
|
||||||
|
# List of tokens
|
||||||
|
self.token_list = None
|
||||||
|
|
||||||
|
# Hold token
|
||||||
|
self.held_token = None
|
||||||
|
|
||||||
|
# Origin pos for hold token
|
||||||
|
self.held_token_original_position = None
|
||||||
|
|
||||||
|
# List of mats
|
||||||
|
self.mat_list = None
|
||||||
|
|
||||||
|
# List of mats function
|
||||||
|
self.mat_function_list = None
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
"""Set up the game"""
|
||||||
|
|
||||||
|
|
||||||
|
# List of tokens we are dragging with the mouse
|
||||||
|
self.held_tokens = []
|
||||||
|
|
||||||
|
# Original location of tokens we are dragging with the mouse in case
|
||||||
|
# they have to go back.
|
||||||
|
self.held_token_original_position = []
|
||||||
|
|
||||||
|
self.token_list = arcade.SpriteList()
|
||||||
|
|
||||||
|
self.mat_list = arcade.SpriteList()
|
||||||
|
|
||||||
|
|
||||||
|
for y in range(TOKEN_ROW):
|
||||||
|
x = X_TOKEN_START
|
||||||
|
for token_type in TOKEN_TYPES:
|
||||||
|
token = Token_sprite(token_type, TOKEN_SCALE)
|
||||||
|
token.position = x, Y_TOKEN_START - Y_SPACING_TOKEN * y
|
||||||
|
x+=X_SPACING_TOKEN
|
||||||
|
self.token_list.append(token)
|
||||||
|
|
||||||
|
mat = Mat(MAT_HEIGHT, MAT_WIDTH)
|
||||||
|
mat.position = x, Y_TOKEN_START - Y_SPACING_TOKEN * y
|
||||||
|
self.mat_list.append(mat)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for y in range(MAT_ROW):
|
||||||
|
for x in range(MAT_COLUMN):
|
||||||
|
mat = Mat(MAT_HEIGHT, MAT_WIDTH)
|
||||||
|
mat.position = X_MAT_START + X_SPACING_MAT * x, Y_MAT_START - Y_SPACING_MAT * y
|
||||||
|
self.mat_list.append(mat)
|
||||||
|
|
||||||
|
|
||||||
|
def on_draw(self):
|
||||||
|
"""Render the screen"""
|
||||||
|
# Clear the screen
|
||||||
|
self.clear()
|
||||||
|
|
||||||
|
#Draw the mat
|
||||||
|
for mat in self.mat_list:
|
||||||
|
mat.draw()
|
||||||
|
|
||||||
|
# Draw the token
|
||||||
|
for token in self.token_list:
|
||||||
|
token.draw()
|
||||||
|
|
||||||
|
|
||||||
|
#arcade.start_render()
|
||||||
|
|
||||||
|
|
||||||
|
def pull_to_top(self, token: arcade.Sprite):
|
||||||
|
""" Pull token to top of rendering order (last to render, looks on-top) """
|
||||||
|
|
||||||
|
# Remove, and append to the end
|
||||||
|
self.token_list.remove(token)
|
||||||
|
self.token_list.append(token)
|
||||||
|
|
||||||
|
def on_mouse_press(self, x, y, button, key_modifiers):
|
||||||
|
"""Called when the user presses a mouse button"""
|
||||||
|
|
||||||
|
# Get list of tokens we've clicked on
|
||||||
|
tokens = arcade.get_sprites_at_point((x, y), self.token_list)
|
||||||
|
|
||||||
|
|
||||||
|
# Have we clicked on a card?
|
||||||
|
if len(tokens) > 0:
|
||||||
|
|
||||||
|
# Might be a stack of cards, get the top one
|
||||||
|
primary_token = tokens[-1]
|
||||||
|
|
||||||
|
# All other cases, grab the face-up card we are clicking on
|
||||||
|
self.held_tokens = [primary_token]
|
||||||
|
# Save the position
|
||||||
|
self.held_token_original_position = [self.held_tokens[0].position]
|
||||||
|
# Put on top in drawing order
|
||||||
|
self.pull_to_top(self.held_tokens[0])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def on_mouse_release(self, x, y, button, modifiers):
|
||||||
|
"""Called when the user presses a mouse button"""
|
||||||
|
|
||||||
|
# If we don't have any tokens, who cares
|
||||||
|
if len(self.held_tokens) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Find the closest pile, in case we are in contact with more than one
|
||||||
|
mat, distance = arcade.get_closest_sprite(self.held_tokens[0], self.mat_list)
|
||||||
|
reset_position = True
|
||||||
|
|
||||||
|
# See if we are in contact with the closest mat
|
||||||
|
if arcade.check_for_collision(self.held_tokens[0], mat):
|
||||||
|
|
||||||
|
# For each held token, move it to the mat we dropped on
|
||||||
|
for i, dropped_token in enumerate(self.held_tokens):
|
||||||
|
# Move cards to proper position
|
||||||
|
dropped_token.position = mat.center_x, mat.center_y
|
||||||
|
|
||||||
|
# Success, don't reset position of cards
|
||||||
|
reset_position = False
|
||||||
|
|
||||||
|
|
||||||
|
# Release on top play pile? And only one card held?
|
||||||
|
if reset_position:
|
||||||
|
# Where-ever we were dropped, it wasn't valid. Reset the each card's position
|
||||||
|
# to its original spot.
|
||||||
|
for mat_index, token in enumerate(self.held_tokens):
|
||||||
|
token.position = self.held_token_original_position[mat_index]
|
||||||
|
|
||||||
|
# We are no longer holding tokens
|
||||||
|
self.held_tokens = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def on_mouse_motion(self, x, y, dx, dy):
|
||||||
|
"""Called when the user moves the mouse"""
|
||||||
|
|
||||||
|
|
||||||
|
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
|
||||||
|
""" User moves mouse """
|
||||||
|
|
||||||
|
# If we are holding cards, move them with the mouse
|
||||||
|
for token in self.held_tokens:
|
||||||
|
token.center_x += dx
|
||||||
|
token.center_y += dy
|
||||||
|
|
||||||
|
def on_key_press(self, symbol, modifiers):
|
||||||
|
"""Called when the user presses key"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def cubito(self, function=False):
|
||||||
|
"""Move cubito !"""
|
||||||
|
...
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main method"""
|
||||||
|
window = Cubito()
|
||||||
|
window.setup()
|
||||||
|
arcade.run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
51
gui-simple.py
Normal file
51
gui-simple.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import PySimpleGUI as sg
|
||||||
|
import cubito
|
||||||
|
#python arcad
|
||||||
|
|
||||||
|
sg.theme('Dark') # Add a touch of color
|
||||||
|
# All the stuff inside your window.
|
||||||
|
choice = ["FORWORD", "RIGHT", "LEFT", "FONCTION"]
|
||||||
|
|
||||||
|
#fonctions = [sg.Combo(choice, key="fonction1"), sg.Combo(choice, key="fonction2"), sg.Combo(choice, key="fonction3"), sg.Combo(choice, key="fonction4")]
|
||||||
|
fonctions = [sg.Combo(choice, key=f"fonction{i}") for i in range(4)]
|
||||||
|
|
||||||
|
#combo_box_line = [sg.Combo(choice) for _ in range(4)]
|
||||||
|
|
||||||
|
layout = [[sg.Button("run")],
|
||||||
|
[sg.Combo(choice), sg.Combo(choice), sg.Combo(choice), sg.Combo(choice)],
|
||||||
|
[sg.Combo(choice), sg.Combo(choice), sg.Combo(choice), sg.Combo(choice)],
|
||||||
|
[sg.Combo(choice), sg.Combo(choice), sg.Combo(choice), sg.Combo(choice)],
|
||||||
|
[sg.Combo(choice), sg.Combo(choice), sg.Combo(choice), sg.Combo(choice)],
|
||||||
|
[sg.Text("Fonction")],
|
||||||
|
fonctions
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# Create the Window
|
||||||
|
window = sg.Window('Window Title', layout)
|
||||||
|
# Event Loop to process "events" and get the "values" of the inputs
|
||||||
|
|
||||||
|
#layout = []
|
||||||
|
|
||||||
|
def mouvement(value):
|
||||||
|
if value == "FORWORD":
|
||||||
|
cubito.fordward()
|
||||||
|
if value == "LEFT":
|
||||||
|
cubito.left()
|
||||||
|
if value == "RIGHT":
|
||||||
|
cubito.right()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
event, values = window.read()
|
||||||
|
if event == sg.WIN_CLOSED:
|
||||||
|
break
|
||||||
|
if event == "run":
|
||||||
|
for key, value in values.items():
|
||||||
|
if "fonction" not in str(key):
|
||||||
|
mouvement(value)
|
||||||
|
if value == "FONCTION":
|
||||||
|
for key_f, value_f in values.items():
|
||||||
|
if "fonction" in str(key_f):
|
||||||
|
mouvement(value_f)
|
||||||
|
|
||||||
|
window.close()
|
Loading…
Reference in New Issue
Block a user