#2: Add check collision for token and matrice: unit tests.

This commit is contained in:
Christian Jacolot 2024-02-24 16:54:34 +01:00
parent 82e02bbe9f
commit c3d6929a1c

View File

@ -2,6 +2,9 @@
Cubito
"""
from itertools import count
from time import sleep
import arcade
# import cubito
@ -25,6 +28,8 @@ Y_SPACING_TOKEN = TOKEN_HEIGHT + TOKEN_HEIGHT * VERTICAL_MARGIN_PERCENT
# List of token types
TOKEN_TYPES = ["FORDWARD", "RIGHT", "LEFT", "PAUSE", "FUNCTION"]
TOKEN_TYPES_DICT = {"FORDWARD":"up", "RIGHT":"right", "LEFT":"left", "PAUSE":"pause_square",
"FUNCTION":"star_round"}
# Mat size
MAT_PERCENT_OVERSIZE = 1.25
@ -49,6 +54,10 @@ 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 Cubito(arcade.Window):
"""Main application class"""
@ -76,33 +85,143 @@ class Cubito(arcade.Window):
def setup(self):
"""Set up the game"""
...
# --- Create the mats the cards go on.
# Sprite list with all the mats tha cards lay on.
self.mat_list: arcade.SpriteList = 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()
# List of cards we are dragging with the mouse
self.held_token = []
# Original location of cards we are dragging with the mouse in case
# they have to go back.
self.held_token_original_position = []
# Sprite list with all the cards, no matter what pile they are in.
self.token_list = arcade.SpriteList()
# Create every token type
i = 0
for token in TOKEN_TYPES:
one_token = Token(TOKEN_TYPES_DICT[token], TOKEN_SCALE)
one_token.position = START_X + i * X_SPACING_TOKEN, BOTTOM_Y
self.token_list.append(one_token)
i += 1
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 cards 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_token = [primary_token]
# Save the position
self.held_token_original_position = [self.held_token[0].position]
def on_mouse_release(self, x, y, button, modifiers):
"""Called when the user presses a mouse button"""
...
# If we don't have any cards, who cares
if len(self.held_token) == 0:
return
one_case_mat, distance = arcade.get_closest_sprite(self.held_token[0], self.mat_list)
if arcade.check_for_collision(self.held_token[0], one_case_mat):
self.held_token[0].position = one_case_mat.position
# We are no longer holding cards
self.held_token = []
def on_mouse_motion(self, x, y, dx, dy):
"""Called when the user moves the mouse"""
...
# If we are holding cards, move them with the mouse
for token in self.held_token:
token.center_x += dx
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 !"""
...
class Token(arcade.Sprite):
""" Token sprite """
def __init__(self, token_type, scale=1):
""" Token constructor """
# Attributes for token type
self.token_type = token_type
# Image to use for the sprite when face up
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 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)
def main():
"""Main method"""
window = Cubito()