forked from MDL29/LPH-cubito
parent
b4eab7c444
commit
77585a546b
|
@ -5,7 +5,7 @@ from itertools import count
|
|||
from time import sleep
|
||||
|
||||
import arcade
|
||||
# import cubito
|
||||
import cubito
|
||||
|
||||
# Screen title and size
|
||||
SCREEN_TITLE = "Cubito"
|
||||
|
@ -51,13 +51,11 @@ BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
|
|||
# Start from left side
|
||||
START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
|
||||
|
||||
# Mat pos
|
||||
X_MAT_POS = START_X + SCREEN_WIDTH / 2
|
||||
Y_MAT_POS = TOP_Y
|
||||
|
||||
class Token(arcade.Sprite):
|
||||
"""Token sprite"""
|
||||
|
||||
# Sprite from token type
|
||||
sprite = {
|
||||
"FORDWARD": "up",
|
||||
"RIGHT": "r",
|
||||
|
@ -127,18 +125,28 @@ class Cubito(arcade.Window):
|
|||
|
||||
self.mat_list = arcade.SpriteList()
|
||||
|
||||
for y in range(MAT_ROW):
|
||||
for j in range(MAT_ROW):
|
||||
# Create the top "play" piles
|
||||
for x in range(MAT_COLUMN):
|
||||
for i in range(MAT_COLUMN):
|
||||
mat = Mat(MAT_WIDTH, MAT_HEIGHT)
|
||||
mat.position = (
|
||||
X_MAT_POS + X_SPACING_MAT * x,
|
||||
Y_MAT_POS - Y_SPACING_MAT * y,
|
||||
START_X + i * X_SPACING_MAT + SCREEN_WIDTH / 2,
|
||||
TOP_Y - Y_SPACING_MAT * j,
|
||||
)
|
||||
self.mat_list.append(mat)
|
||||
|
||||
self.mat_function_list = arcade.SpriteList()
|
||||
|
||||
for j in range(MAT_ROW, MAT_ROW + MAT_FUNCTION_ROW):
|
||||
# Create the top "play" piles
|
||||
for i in range(MAT_COLUMN):
|
||||
mat = Mat(MAT_WIDTH, MAT_HEIGHT, arcade.color.COOL_BLACK)
|
||||
mat.position = (
|
||||
START_X + i * X_SPACING_MAT + SCREEN_WIDTH / 2,
|
||||
TOP_Y - Y_SPACING_MAT * j,
|
||||
)
|
||||
self.mat_function_list.append(mat)
|
||||
|
||||
# Sprite list with all the cards, no matter what pile they are in.
|
||||
self.token_list = arcade.SpriteList()
|
||||
|
||||
|
@ -167,6 +175,18 @@ class Cubito(arcade.Window):
|
|||
# Draw the tokens
|
||||
self.token_list.draw()
|
||||
|
||||
# Draw token id in bottom left mat side
|
||||
all_mat = list(self.mat_list) + list(self.mat_function_list)
|
||||
for mat in all_mat:
|
||||
size = 10
|
||||
arcade.draw_text(
|
||||
mat.id,
|
||||
mat.left + size / 3,
|
||||
mat.bottom + size / 3,
|
||||
arcade.color.CHARTREUSE,
|
||||
size,
|
||||
)
|
||||
|
||||
def on_mouse_press(self, x, y, button, key_modifiers):
|
||||
"""Called when the user presses a mouse button"""
|
||||
# Get list of token when click
|
||||
|
@ -185,10 +205,22 @@ class Cubito(arcade.Window):
|
|||
if self.held_token is None:
|
||||
return
|
||||
|
||||
# Release holdosert mat from held token
|
||||
# Find the closert mat from held token
|
||||
mat, distance = arcade.get_closest_sprite(self.held_token, self.mat_list)
|
||||
reset_position = True
|
||||
|
||||
# Check collision between held token et the closert mat
|
||||
if arcade.check_for_collision(self.held_token, mat):
|
||||
# Set held token position on mat
|
||||
self.held_token.position = mat.center_x, mat.center_y
|
||||
|
||||
reset_position = False
|
||||
|
||||
# Find the closert mat function from held token
|
||||
mat, distance = arcade.get_closest_sprite(
|
||||
self.held_token, self.mat_function_list
|
||||
)
|
||||
|
||||
# Check collision between held token et the closert mat
|
||||
if arcade.check_for_collision(self.held_token, mat):
|
||||
# Set held token position on mat
|
||||
|
@ -200,6 +232,7 @@ class Cubito(arcade.Window):
|
|||
if reset_position:
|
||||
self.held_token.position = self.held_token_original_position
|
||||
|
||||
# Release hold token
|
||||
self.held_token = None
|
||||
|
||||
def on_mouse_motion(self, x, y, dx, dy):
|
||||
|
@ -217,9 +250,16 @@ class Cubito(arcade.Window):
|
|||
# Press « R » key
|
||||
if symbol == arcade.key.R:
|
||||
print("Restart !")
|
||||
# Reset mat ids
|
||||
Mat.ids = count(0)
|
||||
# Relunch setup methode
|
||||
self.setup()
|
||||
|
||||
# Press « S » key
|
||||
if symbol == arcade.key.S:
|
||||
# Run cubito
|
||||
self.cubito()
|
||||
|
||||
# Press « Q » key
|
||||
if symbol == arcade.key.Q:
|
||||
print("Good bye !")
|
||||
|
@ -228,7 +268,35 @@ class Cubito(arcade.Window):
|
|||
|
||||
def cubito(self, function=False):
|
||||
"""Move cubito !"""
|
||||
...
|
||||
# Get list of mats
|
||||
if function:
|
||||
mats = self.mat_function_list
|
||||
else:
|
||||
mats = self.mat_list
|
||||
|
||||
for mat in mats:
|
||||
# Find the closert token from mat
|
||||
token, distance = arcade.get_closest_sprite(mat, self.token_list)
|
||||
# Check collision between the closert token & mat
|
||||
if arcade.check_for_collision(mat, token):
|
||||
msg = token.type
|
||||
print("> Move %s : %s" % (mat.id, msg))
|
||||
if msg == "FORDWARD":
|
||||
cubito.fordward()
|
||||
elif msg == "LEFT":
|
||||
cubito.left()
|
||||
elif msg == "RIGHT":
|
||||
cubito.right()
|
||||
elif msg == "FUNCTION" and not function:
|
||||
self.cubito(function=True)
|
||||
elif msg == "PAUSE":
|
||||
pass
|
||||
else:
|
||||
print("Unknow command")
|
||||
else:
|
||||
print("> Move %s :" % mat.id)
|
||||
sleep(0.1)
|
||||
|
||||
|
||||
def main():
|
||||
"""Main method"""
|
||||
|
|
Loading…
Reference in New Issue