add function restart

This commit is contained in:
doto 2024-02-27 19:26:46 +01:00
parent 71e499bb03
commit 5b722becea

View File

@ -4,7 +4,7 @@ ressources : https://api.arcade.academy/en/latest/resources.html
"""
import arcade
# import cubito
import cubito
# Screen title and size
SCREEN_TITLE = "Cubito"
@ -65,199 +65,273 @@ START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
class Start_mat(arcade.SpriteSolidColor):
def __init__(self, height, width, color=arcade.color.AMBER):
super().__init__(width, height, color)
class Mat(arcade.SpriteSolidColor):
def __init__(self, height, width, color=arcade.color.AMBER):
super().__init__(width, height, color)
def __init__(self, height, width, color=arcade.color.AMBER):
super().__init__(width, height, color)
class Mat_function(arcade.SpriteSolidColor):
def __init__(self, height, width, color=arcade.color.AMBER):
super().__init__(width, height, color)
class Token_sprite(arcade.Sprite):
""" Card sprite """
""" Card sprite """
def __init__(self, token_type, scale=1):
""" Card constructor """
def __init__(self, token_type, scale=1):
""" Card constructor """
# Attributes for token type
self.token_type = token_type
# 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")
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"""
"""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)
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
# List of tokens
self.token_list = None
# Hold token
self.held_token = None
# Hold token
self.held_token = None
# Origin pos for hold token
self.held_token_original_position = None
# Origin pos for hold token
self.held_token_original_position = None
# List of mats
self.mat_list = None
#List of start mats
self.start_mat_list = None
# List of mats function
self.mat_function_list = None
# List of mats
self.mat_list = None
def setup(self):
"""Set up the game"""
# 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 = []
# 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 = []
# 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.token_list = arcade.SpriteList()
self.mat_list = arcade.SpriteList()
self.start_mat_list = arcade.SpriteList()
self.mat_function_list = arcade.SpriteList()
self.mat_list = arcade.SpriteList()
for y in range(TOKEN_ROW):
x = X_TOKEN_START
for token_type in TOKEN_TYPES:
#placer les tokens
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)
token.token_type
for y in range(TOKEN_ROW):
x = X_TOKEN_START
for token_type in TOKEN_TYPES:
#placer les tokens
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)
#placer des cases sous les tokens
start_mat = Start_mat(MAT_HEIGHT, MAT_WIDTH, color=arcade.color.AMAZON)
start_mat.position = x - X_SPACING_TOKEN, Y_TOKEN_START - Y_SPACING_TOKEN * y
self.start_mat_list.append(start_mat)
#placer des cases sous les tokens
mat = Mat(MAT_HEIGHT, MAT_WIDTH, color=arcade.color.AMAZON)
mat.position = x - X_SPACING_TOKEN, Y_TOKEN_START - Y_SPACING_TOKEN * y
self.mat_list.append(mat)
#placer les cases principals
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)
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)
#placer les cases fonctions
Y_MAT_FUNCTION_START = Y_MAT_START - Y_SPACING_MAT*MAT_ROW
print(Y_MAT_FUNCTION_START)
for y in range(MAT_FUNCTION_ROW):
for x in range(MAT_COLUMN):
mat = Mat_function(MAT_HEIGHT, MAT_WIDTH, arcade.color.BABY_BLUE)
mat.position = X_MAT_START + X_SPACING_MAT * x, Y_MAT_FUNCTION_START - Y_SPACING_MAT * y
self.mat_function_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()
def on_draw(self):
"""Render the screen"""
# Clear the screen
self.clear()
# Draw the token
for token in self.token_list:
token.draw()
#Draw the mat
for mat in self.mat_list:
mat.draw()
#Draw the start mat
for start_mat in self.start_mat_list:
start_mat.draw()
#Draw the mat function
for mat_function in self.mat_function_list:
mat_function.draw()
# Draw the token
for token in self.token_list:
token.draw()
#arcade.start_render()
#arcade.start_render()
def pull_to_top(self, token: arcade.Sprite):
""" Pull token to top of rendering order (last to render, looks on-top) """
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)
# 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"""
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)
# 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:
# Have we clicked on a card?
if len(tokens) > 0:
# Might be a stack of cards, get the top one
primary_token = tokens[-1]
# 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])
# 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"""
def on_mouse_release(self, x, y, button, modifiers):
"""Called when the user presses a mouse button"""
def collition(reset_position, list_mat):
# Find the closest mat, in case we are in contact with more than one
mat, distance = arcade.get_closest_sprite(self.held_tokens[0], list_mat)
# If we don't have any tokens, who cares
if len(self.held_tokens) == 0:
return
# See if we are in contact with the closest mat
if arcade.check_for_collision(self.held_tokens[0], mat):
# 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
# 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
# See if we are in contact with the closest mat
if arcade.check_for_collision(self.held_tokens[0], mat):
# Success, don't reset position of tokens
reset_position = False
return reset_position
# 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 = []
# If we don't have any tokens, who cares
if len(self.held_tokens) == 0:
return
def on_mouse_motion(self, x, y, dx, dy):
"""Called when the user moves the mouse"""
...
reset_position = True
reset_position = collition(reset_position, self.mat_list)
reset_position = collition(reset_position, self.start_mat_list)
reset_position = collition(reset_position, self.mat_function_list)
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
""" User moves mouse """
if reset_position:
# Where-ever we were dropped, it wasn't valid. Reset the each token's position
# to its original spot.
for mat_index, token in enumerate(self.held_tokens):
token.position = self.held_token_original_position[mat_index]
# If we are holding cards, move them with the mouse
for token in self.held_tokens:
token.center_x += dx
token.center_y += dy
# 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"""
if symbol == arcade.key.R:
self.setup()
cubito.reset()
print("Restart !")
if symbol == arcade.key.S:
# for mat in self.mat_list:
# token, distance = arcade.get_closest_sprite(mat, self.token_list)
# if arcade.check_for_collision(token, mat):
# instruction = str(token.token_type).upper()
# if instruction == "UP":
# instruction = "FORWORD"
# cubito.mouvement(instruction)
# print(instruction)
for mat in self.mat_list:
token, distance = arcade.get_closest_sprite(mat, self.token_list)
if arcade.check_for_collision(token, mat):
token_type = str(token.token_type)
if token_type == "up":
cubito.fordward()
if token_type == "left":
cubito.left()
if token_type == "right":
cubito.right()
if token_type == "hamburger":
for mat_function in self.mat_function_list:
token_function, distance = arcade.get_closest_sprite(mat_function, self.token_list)
if arcade.check_for_collision(token_function, mat_function):
token_type = str(token_function.token_type)
if token_type == "up":
cubito.fordward()
if token_type == "left":
cubito.left()
if token_type == "right":
cubito.right()
def cubito(self, function=False):
"""Move cubito !"""
...
def on_key_press(self, symbol, modifiers):
"""Called when the user presses key"""
if symbol == arcade.key.R:
self.setup()
print("Restart !")
def cubito(self, function=False):
"""Move cubito !"""
...
def main():
"""Main method"""
window = Cubito()
window.setup()
arcade.run()
"""Main method"""
window = Cubito()
window.setup()
arcade.run()
if __name__ == "__main__":
main()
main()