course_voiture.py

Created by julien-bernon

Created on June 20, 2020

2.71 KB

Vous conduisez un petit pixel rouge sur un circuit à l’aide de la croix directionnelle, Il vous faut boucler le circuit le plus vite possible, votre performance est chronométrée.


from kandinsky import *
from time import *
import ion #gere les touches

 
circuit=[
        [17,16,86,35],
        [17,40,35,85],
        [68,16,35,74],
        [99,55,66,35]]
depart=[160,55,10,42]
checkpoint=[160,150,10,35]
checkpointed=False
arrivee=False


for i in range(4):
        circuit.append([320-circuit[i][0]-circuit[i][2],circuit[i][1],circuit[i][2],circuit[i][3]])
        circuit.append([circuit[i][0],240-circuit[i][1]-circuit[i][3],circuit[i][2],circuit[i][3]])
        circuit.append([320-circuit[i][0]-circuit[i][2],240-circuit[i][1]-circuit[i][3],circuit[i][2],circuit[i][3]])

fill_rect(0,0,320,240,(255,255,255))

def touches():
    down=[]
    for i in range(4):
        if ion.keydown(i):
            down.append(i)
    return down

def move(old_pos):
    down=touches()
    new_pos=[old_pos[0],old_pos[1]]
    for touche in down:
        if touche==0:
            new_pos[0]-=5
            if new_pos[0]<0:
                new_pos[0]+=320
        if touche==3:
            new_pos[0]+=5
            new_pos[0]%=320
        if touche==1:
            new_pos[1]-=5
            if new_pos[1]<0:
                new_pos[1]+=240
        if touche==2:
            new_pos[1]+=5
            new_pos[1]%=240
    
    route=False
    global checkpointed,arrivee
    for i in circuit:
        if new_pos[0]>=checkpoint[0] and new_pos[0]<=checkpoint[0]+checkpoint[2] and new_pos[1]>=checkpoint[1] and new_pos[1]<=checkpoint[1]+checkpoint[3] :
            checkpointed=True
        if new_pos[0]-2>=depart[0] and new_pos[0]+2<=depart[0]+depart[2] and new_pos[1]-2>=depart[1] and new_pos[1]+2<=depart[1]+depart[3] and checkpointed :
            arrivee=True
        if new_pos[0]-2>=i[0] and new_pos[0]+2<=i[0]+i[2] and new_pos[1]-2>=i[1] and new_pos[1]+2<=i[1]+i[3] :
            route=True
            break
    
    if route:
        return new_pos
    else :
        return old_pos

        

def dessine_terrain():
    
    fill_rect(0,0,320,220,(178,109,48))
 
    for i in circuit:
        fill_rect(i[0],i[1],i[2],i[3],(255,167,70))


def trace(position,couleur):
    fill_rect(160,150,10,35,(0,255,0))
    fill_rect(160,55,10,42,(0,0,255))
    fill_rect(position[0]-5,position[1]-5,10,10,couleur)
    
    if arrivee:
        fill_rect(0,0,10,10,(0,255,0))
    else:
        fill_rect(0,0,10,10,(255,0,0))
    if checkpointed:
        fill_rect(10,0,10,10,(0,255,0))
    else:
        fill_rect(10,0,10,10,(255,0,0))
        
def main(position):
    t=0
    while not arrivee :
        trace(position,(255,167,70))
        position=move(position)        
        trace(position,(255,0,0))
        sleep(0.033)
        t+=1
    draw_string("Votre chrono :",100,80)
    draw_string(str(t),120,100)
dessine_terrain()

main([160,72])