Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Bertrand_DUCON | 3564095cb5 | |
Bertrand_DUCON | c5a580f104 |
Binary file not shown.
After Width: | Height: | Size: 3.5 KiB |
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 3.1 KiB |
Binary file not shown.
After Width: | Height: | Size: 3.5 KiB |
BIN
background.png
BIN
background.png
Binary file not shown.
Before Width: | Height: | Size: 412 KiB |
91
cubito.py
91
cubito.py
|
@ -1,38 +1,69 @@
|
|||
import turtle
|
||||
from turtle import *
|
||||
import serial
|
||||
|
||||
# Cell size
|
||||
distance = 90
|
||||
angle = 0
|
||||
|
||||
# Set background - ocean
|
||||
turtle.bgpic("background.png")
|
||||
def cubetto_init():
|
||||
|
||||
bgpic("fond.png")
|
||||
register_shape("Cubetto_forme_haut.gif")
|
||||
register_shape("Cubetto_forme_bas.gif")
|
||||
register_shape("Cubetto_forme_gauche.gif")
|
||||
register_shape("Cubetto_forme_droite.gif")
|
||||
shape("Cubetto_forme_droite.gif")
|
||||
shapesize(3,3,6)
|
||||
penup()
|
||||
goto(-210,194)
|
||||
|
||||
# Set turtle cusor
|
||||
turtle.pensize(10)
|
||||
turtle.pencolor("gray")
|
||||
turtle.color("red", "lightgreen")
|
||||
turtle.shape("turtle")
|
||||
turtle.shapesize(2)
|
||||
x_c = 0
|
||||
y_c = 0
|
||||
sh = "d"
|
||||
def cubetto_forme_init():
|
||||
global angle, sh
|
||||
sh = "d"
|
||||
if angle == 360 or angle == -360:
|
||||
angle=0
|
||||
sh = "d"
|
||||
if angle == 0:
|
||||
shape("Cubetto_forme_droite.gif")
|
||||
sh = "d"
|
||||
if angle == 90 or angle == -270:
|
||||
shape("Cubetto_forme_haut.gif")
|
||||
sh = "h"
|
||||
if angle == 180 or angle == -180:
|
||||
shape("Cubetto_forme_gauche.gif")
|
||||
sh = "g"
|
||||
if angle == 270 or angle == -90:
|
||||
shape("Cubetto_forme_bas.gif")
|
||||
sh = "b"
|
||||
|
||||
# Goto to first cell
|
||||
turtle.penup()
|
||||
turtle.goto(-225,225)
|
||||
turtle.pendown()
|
||||
def cubetto_forward():
|
||||
global sh, x_c, y_c
|
||||
forward(84)
|
||||
print(sh, x_c)
|
||||
if sh == "d" and x_c<=336 and x_c>=0:
|
||||
x_c+=84
|
||||
if sh == "h" and y_c<=(4*84) and y_c>=0:
|
||||
y_c-=84
|
||||
|
||||
def cubetto_left():
|
||||
global angle
|
||||
left(90)
|
||||
angle += 90
|
||||
cubetto_forme_init()
|
||||
|
||||
def cubetto_right():
|
||||
global angle
|
||||
right(90)
|
||||
angle -= 90
|
||||
cubetto_forme_init()
|
||||
|
||||
def cubetto_backward():
|
||||
right(180)
|
||||
forward(84)
|
||||
right(180)
|
||||
|
||||
|
||||
def fordward():
|
||||
"""
|
||||
Forward fonction
|
||||
"""
|
||||
turtle.forward(distance)
|
||||
cubetto_init()
|
||||
|
||||
def left():
|
||||
"""
|
||||
Turn left fonction
|
||||
"""
|
||||
turtle.left(90)
|
||||
|
||||
def right():
|
||||
"""
|
||||
Turn right fonction
|
||||
"""
|
||||
turtle.right(90)
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
"""
|
||||
Cubito
|
||||
"""
|
||||
from itertools import count
|
||||
from time import sleep
|
||||
from cubito import cubetto_forward, cubetto_left, cubetto_right, cubetto_backward
|
||||
|
||||
import arcade
|
||||
# import cubito
|
||||
|
@ -8,7 +11,7 @@ import arcade
|
|||
# Screen title and size
|
||||
SCREEN_TITLE = "Cubito"
|
||||
SCREEN_WIDTH = 500
|
||||
SCREEN_HEIGHT = 500
|
||||
SCREEN_HEIGHT = 430
|
||||
|
||||
# Margin between mat & screen side
|
||||
VERTICAL_MARGIN_PERCENT = 0.10
|
||||
|
@ -49,6 +52,60 @@ 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 = {
|
||||
"FORDWARD": "up",
|
||||
"RIGHT": "r",
|
||||
"LEFT": "l",
|
||||
"FUNCTION": "star_round",
|
||||
"PAUSE": "cancel"
|
||||
}
|
||||
|
||||
def __init__(self, token_type, scale=1):
|
||||
"""Token constructor"""
|
||||
# Type of token
|
||||
self.type = token_type
|
||||
# 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.color.BLEU_DE_FRANCE):
|
||||
# Get id from number of instance classes
|
||||
self.id = next(self.ids)
|
||||
|
||||
# Init parent class
|
||||
super().__init__(wigth, height, color)
|
||||
|
||||
class Mat_function(arcade.SpriteSolidColor):
|
||||
"""Mat sprite"""
|
||||
|
||||
# Count instance mat classes
|
||||
ids = count(0)
|
||||
|
||||
def __init__(self, wigth, height, color=arcade.color.BLEU_DE_FRANCE):
|
||||
# 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"""
|
||||
|
@ -57,7 +114,7 @@ class Cubito(arcade.Window):
|
|||
# Init parent class
|
||||
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
|
||||
# Set background color
|
||||
arcade.set_background_color(arcade.color.AMAZON)
|
||||
arcade.set_background_color(arcade.color.AIR_SUPERIORITY_BLUE)
|
||||
|
||||
# List of tokens
|
||||
self.token_list = None
|
||||
|
@ -76,32 +133,166 @@ 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()
|
||||
|
||||
self.mat_function_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 - 60
|
||||
)
|
||||
self.mat_list.append(mat)
|
||||
|
||||
self.mat_function_list = arcade.SpriteList()
|
||||
|
||||
for x in range(MAT_COLUMN):
|
||||
mat = Mat_function(MAT_WIDTH, MAT_HEIGHT)
|
||||
mat.position = (
|
||||
X_MAT_POS + X_SPACING_MAT * x,
|
||||
Y_MAT_POS - Y_SPACING_MAT * y - 150
|
||||
)
|
||||
self.mat_function_list.append(mat)
|
||||
|
||||
# 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
|
||||
token = arcade.get_sprites_at_point((x, y), self.token_list)
|
||||
|
||||
# Check if grab a token
|
||||
if len(token) > 0:
|
||||
# Hold the token
|
||||
self.held_token = token[-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 we don't have any tokens, who cares
|
||||
if self.held_token is None:
|
||||
return
|
||||
|
||||
# Find the closest pile, in case we are in contact with more than one
|
||||
mat, distance = arcade.get_closest_sprite(self.held_token, self.mat_list)
|
||||
mat_function, distance = arcade.get_closest_sprite(self.held_token, self.mat_function_list)
|
||||
reset_position = True
|
||||
|
||||
# See if we are in contact with the closest mat
|
||||
if arcade.check_for_collision(self.held_token, mat):
|
||||
|
||||
self.held_token.position = mat.center_x, mat.center_y
|
||||
|
||||
# Success, don't reset position of cards
|
||||
reset_position = False
|
||||
|
||||
if arcade.check_for_collision(self.held_token, mat_function):
|
||||
token, distance = arcade.get_closest_sprite(self.held_token, self.token_list)
|
||||
if token.type != "FUNCTION":
|
||||
self.held_token.position = mat_function.center_x, mat_function.center_y
|
||||
else:
|
||||
self.held_token.position = self.held_token_original_position
|
||||
|
||||
# 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.
|
||||
self.held_token.position = self.held_token_original_position
|
||||
|
||||
# We are no longer holding tokens
|
||||
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"""
|
||||
...
|
||||
|
||||
if symbol == arcade.key.L:
|
||||
|
||||
token_type_list = []
|
||||
|
||||
for i in self.mat_list:
|
||||
token, distance = arcade.get_closest_sprite(i, self.token_list)
|
||||
if arcade.check_for_collision(i, token):
|
||||
token_type_list.append(token.type)
|
||||
|
||||
for t in token_type_list:
|
||||
if t == "FORDWARD":
|
||||
cubetto_forward()
|
||||
elif t == "LEFT":
|
||||
cubetto_left()
|
||||
elif t == "RIGHT":
|
||||
cubetto_right()
|
||||
elif t == "FUNCTION":
|
||||
for i in self.mat_function_list:
|
||||
token, distance = arcade.get_closest_sprite(i, self.token_list)
|
||||
if arcade.check_for_collision(i, token):
|
||||
token_type_list.append(token.type)
|
||||
elif t == "PAUSE":
|
||||
sleep(1)
|
||||
|
||||
|
||||
# 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 !"""
|
||||
...
|
||||
|
||||
|
||||
def main():
|
||||
"""Main method"""
|
||||
|
@ -111,4 +302,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
Reference in New Issue