forked from MDL29/LPH-cubito
Mise à jour de la séance
This commit is contained in:
parent
82e02bbe9f
commit
14eaf2aa88
|
@ -1,6 +1,8 @@
|
|||
"""
|
||||
Cubito
|
||||
"""
|
||||
from itertools import count
|
||||
from time import sleep
|
||||
|
||||
import arcade
|
||||
# import cubito
|
||||
|
@ -49,6 +51,42 @@ 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 = {}
|
||||
|
||||
def __init__(self, token_type, scale=1):
|
||||
"""Token constructor"""
|
||||
# Type of token
|
||||
self.type = None
|
||||
# Get sprite for type of token
|
||||
self.image_file_name = (
|
||||
":resources:onscreen_controls/shaded_dark/%s.png"
|
||||
% self.sprite.get(token_type, "unchecked")
|
||||
)
|
||||
|
||||
# Init parent class
|
||||
super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
|
||||
|
||||
|
||||
class Mat(arcade.SpriteSolidColor):
|
||||
"""Mat sprite"""
|
||||
|
||||
# Count instance mat classes
|
||||
ids = count(0)
|
||||
|
||||
def __init__(self, wigth, height, color=arcade.csscolor.DARK_OLIVE_GREEN):
|
||||
# Get id from number of instance classes
|
||||
self.id = next(self.ids)
|
||||
|
||||
# Init parent class
|
||||
super().__init__(wigth, height, color)
|
||||
|
||||
|
||||
class Cubito(arcade.Window):
|
||||
"""Main application class"""
|
||||
|
@ -76,28 +114,97 @@ class Cubito(arcade.Window):
|
|||
|
||||
def setup(self):
|
||||
"""Set up the game"""
|
||||
...
|
||||
# List of cards we are dragging with the mouse
|
||||
self.held_token = None
|
||||
|
||||
self.held_token_original_position = None
|
||||
|
||||
self.mat_list = arcade.SpriteList()
|
||||
|
||||
for y in range(MAT_ROW):
|
||||
# Create the top "play" piles
|
||||
for x 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,
|
||||
)
|
||||
self.mat_list.append(mat)
|
||||
|
||||
self.mat_function_list = arcade.SpriteList()
|
||||
|
||||
# Sprite list with all the cards, no matter what pile they are in.
|
||||
self.token_list = arcade.SpriteList()
|
||||
|
||||
# Create every token
|
||||
for j, t in enumerate(TOKEN_TYPES):
|
||||
for i in range(10):
|
||||
token = Token(t, TOKEN_SCALE)
|
||||
token.position = (
|
||||
START_X + X_SPACING_TOKEN * j,
|
||||
BOTTOM_Y + X_SPACING_TOKEN * i,
|
||||
)
|
||||
self.token_list.append(token)
|
||||
i += 50
|
||||
|
||||
def on_draw(self):
|
||||
"""Render the screen"""
|
||||
# Clear the screen
|
||||
arcade.start_render()
|
||||
|
||||
# Draw the mats
|
||||
self.mat_list.draw()
|
||||
|
||||
# Draw the mats function
|
||||
self.mat_function_list.draw()
|
||||
|
||||
# Draw the tokens
|
||||
self.token_list.draw()
|
||||
|
||||
def on_mouse_press(self, x, y, button, key_modifiers):
|
||||
"""Called when the user presses a mouse button"""
|
||||
...
|
||||
# Get list of token when click
|
||||
tokens = arcade.get_sprites_at_point((x, y), self.token_list)
|
||||
|
||||
# Check if grab a token
|
||||
if len(tokens) > 0:
|
||||
# Hold the token
|
||||
self.held_token = tokens[-1]
|
||||
# Set original pos for this token
|
||||
self.held_token_original_position = self.held_token.position
|
||||
|
||||
def on_mouse_release(self, x, y, button, modifiers):
|
||||
"""Called when the user presses a mouse button"""
|
||||
...
|
||||
# If no token hold, pass
|
||||
if self.held_token is None:
|
||||
return
|
||||
|
||||
# Release hold token
|
||||
self.held_token = None
|
||||
|
||||
def on_mouse_motion(self, x, y, dx, dy):
|
||||
"""Called when the user moves the mouse"""
|
||||
...
|
||||
# If no token hold, pass
|
||||
if self.held_token is None:
|
||||
return
|
||||
|
||||
self.held_token.center_x += dx
|
||||
self.held_token.center_y += dy
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
"""Called when the user presses key"""
|
||||
...
|
||||
|
||||
# Press « R » key
|
||||
if symbol == arcade.key.R:
|
||||
print("Restart !")
|
||||
# Relunch setup methode
|
||||
self.setup()
|
||||
|
||||
# Press « Q » key
|
||||
if symbol == arcade.key.Q:
|
||||
print("Good bye !")
|
||||
# Exit
|
||||
arcade.exit()
|
||||
|
||||
def cubito(self, function=False):
|
||||
"""Move cubito !"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user