importkandinskyaskimportmathimportrandomimportionimporttime# Dimensions de l'écran
SCREEN_WIDTH=320SCREEN_HEIGHT=240# Avion
PLANE_SIZE=10# Taille du triangle
plane_x=SCREEN_WIDTH//2plane_y=SCREEN_HEIGHT//2plane_angle=0# Angle en degrés
plane_speed=2# Vitesse constante
# Couleurs
BACKGROUND_COLOR=k.color(135,206,235)# Bleu ciel
PLANE_COLOR=k.color(0,128,255)# Bleu clair
MISSILE_COLOR=k.color(255,0,0)# Rouge pour les missiles
# Missiles
missiles=[]# Générer un missile aléatoire
defgenerate_missile():side=random.choice(["top","bottom","left","right"])ifside=="top":x,y=random.randint(0,SCREEN_WIDTH),0elifside=="bottom":x,y=random.randint(0,SCREEN_WIDTH),SCREEN_HEIGHTelifside=="left":x,y=0,random.randint(0,SCREEN_HEIGHT)else:x,y=SCREEN_WIDTH,random.randint(0,SCREEN_HEIGHT)target_x,target_y=plane_x,plane_yangle=math.atan2(target_y-y,target_x-x)speed=random.randint(2,4)return[x,y,angle,speed]# Dessiner l'avion
defdraw_plane(x,y,angle):# Points du triangle
p1=(x+PLANE_SIZE*math.cos(math.radians(angle)),y+PLANE_SIZE*math.sin(math.radians(angle)))p2=(x+PLANE_SIZE*0.5*math.cos(math.radians(angle+120)),y+PLANE_SIZE*0.5*math.sin(math.radians(angle+120)))p3=(x+PLANE_SIZE*0.5*math.cos(math.radians(angle-120)),y+PLANE_SIZE*0.5*math.sin(math.radians(angle-120)))k.fill_triangle(int(p1[0]),int(p1[1]),int(p2[0]),int(p2[1]),int(p3[0]),int(p3[1]),PLANE_COLOR)# Dessiner les missiles
defdraw_missiles():formissileinmissiles:x,y=missile[0],missile[1]k.fill_rect(int(x),int(y),10,5,MISSILE_COLOR)# Déplacer les missiles
defmove_missiles():formissileinmissiles:missile[0]+=missile[3]*math.cos(missile[2])missile[1]+=missile[3]*math.sin(missile[2])# Supprimer les missiles hors écran
missiles[:]=[mforminmissilesif0<=m[0]<=SCREEN_WIDTHand0<=m[1]<=SCREEN_HEIGHT]# Vérifier les collisions
defcheck_collisions():formissileinmissiles:ifmath.hypot(missile[0]-plane_x,missile[1]-plane_y)<PLANE_SIZE:returnTruereturnFalse# Fonction principale
defmain():globalplane_x,plane_y,plane_angle,missilesmissile_timer=0whileTrue:# Effacer l'écran
k.fill_rect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,BACKGROUND_COLOR)# Générer un missile toutes les 30 itérations
missile_timer+=1ifmissile_timer>=30:missile_timer=0missiles.append(generate_missile())# Déplacer les missiles
move_missiles()# Vérifier les collisions
ifcheck_collisions():k.draw_string("Game Over",100,100,k.color(255,255,255),BACKGROUND_COLOR)time.sleep(2)return# Gérer les contrôles pour tourner l'avion
ifion.keypad.is_pressed(ion.keypad.Key.LEFT):plane_angle=(plane_angle-5)%360ifion.keypad.is_pressed(ion.keypad.Key.RIGHT):plane_angle=(plane_angle+5)%360# Déplacer automatiquement l'avion dans la direction actuelle
plane_x+=plane_speed*math.cos(math.radians(plane_angle))plane_y+=plane_speed*math.sin(math.radians(plane_angle))# Empêcher l'avion de sortir de l'écran (rebond)
ifplane_x<0:plane_x=0plane_angle=(plane_angle+180)%360elifplane_x>SCREEN_WIDTH:plane_x=SCREEN_WIDTHplane_angle=(plane_angle+180)%360ifplane_y<0:plane_y=0plane_angle=(plane_angle+180)%360elifplane_y>SCREEN_HEIGHT:plane_y=SCREEN_HEIGHTplane_angle=(plane_angle+180)%360# Dessiner l'avion et les missiles
draw_plane(plane_x,plane_y,plane_angle)draw_missiles()# Pause pour ralentir le jeu
time.sleep(0.05)# Lancer le jeu
main()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.