nsi_kandinsky_entrainement.py

Created by kmaulet5

Created on April 19, 2024

2.65 KB


from ion import*
from kandinsky import fill_rect, draw_string

position = [10, 10]  # x, y
mvt = [10, 10]  # x, y
BORDER = (10, 205, 10, 300)  # x min, x max, y min, y max
STEP = 5

def initialize(state=0):
    try:
        get_keys()
        # Si la bibliothèque ion est disponible (sur calculatrice TI-Nspire), on choisit une couleur spécifique.
        os = (192,53,53)
    except:
        # Sinon, on utilise une autre couleur.
        os = (255,183,52)
    if state == 0:
        # Si l'état est 0, on remplit un rectangle en haut de l'écran avec la couleur choisie.
        fill_rect(1, 204, 320, 18, os)
        # On affiche le texte "nsi.xyz/cubito par Gab & Raf" dans le rectangle avec une autre couleur.
        draw_string("nsi.xyz/cubito par Gab & Raf ", 2, 204, (255,255,255), os)
    else:
        # Sinon, on remplit le rectangle avec la même couleur mais on n'affiche pas de texte.
        fill_rect(1, 204, 320, 18, os)

def personnage(aff=0):
    if aff == 0:
        # Si aff est 0, on dessine un carré blanc à la position actuelle du personnage.
        fill_rect(position[0], position[1], 10 , 10, (255,255,255))
    else:
        # Sinon, on dessine un carré gris à la position actuelle du personnage.
        fill_rect(position[0], position[1], 10 , 10, (128,128,128))

def collision():
    # Fonction de détection de collision (à implémenter).
    return True

def move():
    # Boucle de déplacement du personnage.
    while not keydown(5):
        is_key_pressed = False
        if keydown(1):
            # Si la touche haut est pressée, on déplace le personnage vers le haut.
            mvt[1] = max(position[1] - STEP, BORDER[0])
            is_key_pressed = True
        elif keydown(2):
            # Si la touche bas est pressée, on déplace le personnage vers le bas.
            mvt[1] = min(position[1] + STEP, BORDER[1])
            is_key_pressed = True
        elif keydown(3):
            # Si la touche droite est pressée, on déplace le personnage vers la droite.
            mvt[0] = min(position[0] + STEP, BORDER[3])
            is_key_pressed = True
        elif keydown(0):
            # Si la touche gauche est pressée, on déplace le personnage vers la gauche.
            mvt[0] = max(position[0] - STEP, BORDER[2])
            is_key_pressed = True
        if is_key_pressed and collision():
            # Si une touche a été pressée et il n'y a pas de collision, on effectue le déplacement.
            personnage(0)
            position[0] = mvt[0]
            position[1] = mvt[1]
            personnage(1)

# Appel de la fonction d'initialisation.
initialize()
# Appel de la fonction de déplacement.
move()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.