z_deplace.py

Created by cent20

Created on April 19, 2021

1.71 KB

Je déplace un carré sur l’écran


from math import *
from kandinsky import *
from ion import *
from random import *


position = [120, 120] # x, y 
mvt = [120, 120] # x, y 

def initialisation(etat=0):
    try:
        get_keys()
        os = (192,53,53)
    except:
        os = (255,183,52)
    if etat == 0:
        fill_rect(1,204,320,18,os)
        draw_string("nsi.xyz/cubito par Gab & Raf ",2,204,(255,255,255),os)
    else:
      fill_rect(1,204,320,18,os)
      # Afficher le score par ex

initialisation()

def personnage(aff=0):

    if aff == 0:
        fill_rect(position[0], position[1], 10 , 10, (255,255,255))
        # On peut d'ailleurs faire une vrai dessin
    else:
        fill_rect(position[0], position[1], 10 , 10, (128,128,128))
        # On peut d'ailleurs faire une vrai dessin

        


def collision():

    '''on vérifie les colisions, si colisions, on retourne False, sinon True
    On teste mvt[0] et mvt[1] 
    ''' 
    return True


def move():   

    bordure = (10, 10, 150, 150 ) #x min, x max, y min, y max

    while not keydown(5):

        is_key_pressed = False

        if keydown(1): #haut
            mvt[1] = max(position[1] - 5, bordure[1])
            is_key_pressed = True

        elif keydown(2): #bas
            mvt[1] = min(position[1] + 5, bordure[2])
            is_key_pressed = True

        elif keydown(3): #Droite
            mvt[0] = min(position[0] + 5, bordure[3])
            is_key_pressed = True

        elif keydown(0): #Gauche
            mvt[0] = max(position[0] - 5, bordure[0])
            is_key_pressed = True
            
        if is_key_pressed and collision():
            personnage(0)
            position[0] = mvt[0]
            position[1] = mvt[1]
            personnage(1)


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.