move_objet.py

Created by julien-bernon

Created on June 14, 2020

1.29 KB

deplace un objet en appuyant sur les touches


import kandinsky as k #module du graphisme

import time as t #gestion du temps

import ion #gestion des touches

 

position=[160,120] #l'objet commence au milieu de l'ecran

 

#on commence par effacer l'ecran

color=(255,255,255)

k.fill_rect(0,0,320,240,color)

 

def touches(): #fonction qui gere l'affichage des touches

    down=[]

    for i in range(5):

        if ion.keydown(i):

            down.append(i)

    return down

 

def move():

    down=touches()

    for touche in down:

        if touche==0:

            position[0]-=5

            if position[0]<0:

                position[0]+=320

        if touche==3:

            position[0]+=5

            position[0]%=320

        if touche==1:

            position[1]-=5

            if position[1]<0:

                position[1]+=240

        if touche==2:

            position[1]+=5

            position[1]%=240

   

def trace_before():

    color=(255,255,255)

    k.fill_rect(0,0,320,240,color)

    color=(64,64,64)

    k.fill_rect(position[0]-5,position[1]+5,10,10,color)

 

def trace_after():

    color=(255,0,0)

    k.fill_rect(position[0]-5,position[1]+5,10,10,color)

 

def main():

    while True :

        trace_before()

        move()        

        trace_after()

        t.sleep(0.033)

 

main()