mission.py

Created by schraf

Created on October 04, 2022

1.93 KB

Explications en vidéo


from ion import *
from time import sleep
from kandinsky import set_pixel,fill_rect, draw_string
from cmath import polar
from math import cos,sin,log10
from random import choice,random,randint

RO,NR,BL,VE = (255,0,0),(0,)*3,(255,)*3,(0,255,0)
niv = 6

def zx(i): return 75 + 75 * (i%3)
def zy(i): return 60 + 40 * (i//3)

def melange():
  t,m = list(range(1,niv+1)), []
  while len(t) > 0:
    m.append(choice(t))
    t.remove(m[-1])
  code = ''.join([str(n) for n in m])
  fill_rect(0,0,320,222,(160,220,240))
  aff_code(code,0)
  for i in range(niv):
    draw_string(str(i+1),zx(i), zy(i),NR,BL)
  return code

def aff_mess(x,y,txt,c):
 draw_string("= "*7 + txt + " ="*6,0,140,NR,c)
 for i in range(900):
   coul = (randint(10,255),randint(10,255),randint(10,255))
   [r, t] = [-30*log10(random()), 6.28*random()]
   set_pixel(int(x+r*cos(t)),int(y+r*sin(t)),coul)
   sleep(.002)
   
def aff_code(code,n):
  for i in range(niv):
    draw_string(code[i],10*i,0,NR,VE if i==n else RO)

def touche(jx,jy):
  for i in range(niv):
    dx = jx - zx(i)
    dy = jy - zy(i)
    if 0 <= dx < 10 and 0 <= dy < 18: return i+1
  return 0
  
def deplace(x,y,dx,dy,v,c):
  x = max(0, min(320, x + v * dx))
  y = max(0, min(220, y + v * dy))
  set_pixel(int(x),int(y),c)
  return [x,y]  

def go(diff = 2):
 jx,jy,mx,my = 0,220,320,0
 code = melange()
 rang, preced = 0, 0
 while True:
  sleep(.02)
  djx,djy = keydown(KEY_RIGHT)-keydown(KEY_LEFT), keydown(KEY_DOWN)-keydown(KEY_UP)
  if djx != 0 or djy!=0: [jx,jy] = deplace(jx,jy,djx,djy,2,NR)
  (r,t) = polar(jx - mx + (jy - my) * 1j) 
  if r > 2:
    [mx,my] = deplace(mx,my,cos(t),sin(t),diff,RO)
    nb = touche(jx,jy)
    if nb > 0 and nb != preced:
      if int(code[rang]) == nb:
        rang += 1
        preced = nb
        if rang == niv:
          aff_mess(mx,my,"GAGNE !",VE)
          return
      else:
       rang,preced = 0,0
      aff_code(code,rang)
  else:
    aff_mess(jx,jy,"BOOM !",RO)
    return
  
go()