crossy_road.py

Created by caucaucybu

Created on January 09, 2024

15.4 KB

Le principe est simple. Il faut faire avancer le perso jusqu’en haut de l’écran. Les voitures et les camions, l’eau et le feu sont mortels. Les arbres empêchent le passage. Vous pouvez modifier la difficulté dans le menu.


09/01/2024 : fixation de bug de touches et de rapidité


Vous pouvez avoir d’autres jeux comme des echecs sur mon compte NumWorks : Caucaucybu


from ion import keydown
from random import randrange as rand
from time import sleep, monotonic
from kandinsky import fill_rect as draw,draw_string as txt,get_pixel as get
def bn(x,y,w,h,c):draw(x,y,w,h,(0,0,0));draw(x+1,y+1,w-2,h-2,c)
mort=[0,11,1];level=0;route=(156,158,140);terre=(140,190,0);feu=(181,40,0);feuillage=(8,109,0);eau=(0,130,231);bois=(247,113,41);pos=[160,11];options={"k":("route","eau","feu","terre"),"route":[1,48],"eau":[1,103],"feu":[1,178],"terre":[1,233],"arbres":1,"quantite":3,"rapidite":3}
def start():
  global lignes,pos,mort,level,compt,go,pos1,pos0,lel,options
  go=0;level+=1;lignes=[[0]];compt=0;pos[1]=11;pos1=pos.copy();mort[0]=229;mort[1]-=1
  if mort[1]==2:mort[1]=10;mort[2]+=1
  draw(0,0,325,225,(255,255,255));txt("Level : "+str(level),110,90);sleep(1)
  if level>=10:lel=" "
  else:lel=""
  while len(lignes)<11:
    lignes.append([rand(-2,6)])
    if lignes[-1][0]>3:
      if not options["route"][0]:lignes.pop()
    elif lignes[-1][0]>1:
      if not options["eau"][0]:lignes.pop()
    else:
      if not options["terre"][0]:lignes.pop()
  lignes.reverse()
  for i in range(len(lignes)):
    if lignes[i][0]>3:
      draw(0,i*20,325,20,eau);lignes[i].insert(1,rand(1,3+options["rapidite"]))
      for j in range(-30,321,35):lignes[i].append([rand(-2-int((options["quantite"]+0.5)/2),0),j])
    elif lignes[i][0]>1:
      draw(-1,i*20,325,20,(255,255,255));draw(0,i*20+1,323,18,route);lignes[i].insert(1,rand(1,3+options["rapidite"]))
      for j in range(-30,356,50):lignes[i].append([rand(0,7-options["quantite"]),j])
    else:
      draw(0,i*20,325,20,terre)
      if options["arbres"]:
        for j in range(20,pos[0]-16,20):
          if rand(0,5-options["quantite"])==0:draw(j+8,i*20+12,4,7,(165,77,8));draw(j+4,i*20+1,12,12,feuillage)
        for j in range(pos[0]+15,280,20):
          if rand(0,5-options["quantite"])==0:draw(j+8,i*20+12,4,7,(165,77,8));draw(j+4,i*20+1,12,12,feuillage)
    if lignes[i][0]==2 or lignes[i][0]==4:lignes[i][1]*=-1
def jeu():
  global compt,pos0,pos1,go,cd,level,mort,pos;start()
  while 1:
    try:
      tim=monotonic();compt+=1
      if keydown(1):
        if get(pos[0],pos[1]*20-35)!=feuillage and get(pos[0]+10,pos[1]*20-35)!=feuillage and avant:pos[1]-=1
        avant=0
      else:avant=1
      if keydown(2):
        if get(pos[0],pos[1]*20+5)!=feuillage and get(pos[0]+10,pos[1]*20+5)!=feuillage and arriere:pos[1]+=1
        arriere=0
      else:arriere=1
      if get(pos[0]-1,pos[1]*20-15)!=feuillage and keydown(0):
        for i in range(6):
          if get(pos[0]-1-i,pos[1]*20-15)==feuillage:break
        pos[0]-=i
      if get(pos[0]+11,pos[1]*20-15)!=feuillage and keydown(3):
        for i in range(6):
          if get(pos[0]+11+i,pos[1]*20-15)==feuillage:break
        pos[0]+=i
      for i in range(len(lignes)):
        if lignes[i][0]>1:
          for j in range(2,len(lignes[i])):
            tipe=lignes[i][j][0];x=lignes[i][j][1];y=i*20+1;c=lignes[i][0]
            if tipe==0:
              if c==3:bn(x,y,30,15,(134,98,180));bn(x+30,y+3,10,12,(78,105,204));draw(x+8,y+15,5,4,(0,0,0));draw(x+10,y+16,1,1,(189,182,165));draw(x+29,y+15,5,4,(0,0,0));draw(x+31,y+16,1,1,(189,182,165))
              if c==2:bn(x+10,y,30,15,(135,89,213));bn(x,y+3,10,12,(204,105,75));draw(x+6,y+15,5,4,(0,0,0));draw(x+8,y+16,1,1,(189,182,165));draw(x+27,y+15,5,4,(0,0,0));draw(x+29,y+16,1,1,(189,182,165))
            if tipe==1:
              if c==3:
                bn(x,y+10,25,5,(231,75,56));draw(x+7,y+5,5,1,(0,0,0))
                for k in range(5):draw(x+3+k,y+9-k,2,1,(0,0,0));draw(x+12+k,y+5+k,2,1,(0,0,0))
                draw(x+3,y+15,5,4,(0,0,0));draw(x+5,y+16,1,1,(189,182,165));draw(x+16,y+15,5,4,(0,0,0));draw(x+18,y+16,1,1,(189,182,165))
              if c==2:
                bn(x,y+10,25,5,(132,56,251));draw(x+13,y+5,5,1,(0,0,0))
                for k in range(5):draw(x+7+k,y+9-k,2,1,(0,0,0));draw(x+16+k,y+5+k,2,1,(0,0,0))
                draw(x+3,y+15,5,4,(0,0,0));draw(x+5,y+16,1,1,(189,182,165));draw(x+16,y+15,5,4,(0,0,0));draw(x+18,y+16,1,1,(189,182,165))
            if tipe==-1:draw(x,y,35,18,bois)
            if tipe<-1:draw(x,y,35,18,eau)
      p1=get(pos[0]-1,pos[1]*20-9);p2=get(pos[0]+11,pos[1]*20-9)
      if p1==bois and p2==bois:pos[0]+=lignes[pos[1]-1][1]
      if pos[1]==9:go=1
      if pos[0]<1:pos[0]=1
      if pos[0]>308:pos[0]=308
      if pos[1]==0:start()
      if pos1!=pos:
        if lignes[pos1[1]-1][0]>3:cd=bois
        elif lignes[pos1[1]-1][0]>1:cd=route
        else:cd=terre
        draw(pos1[0],pos1[1]*20-19,11,18,cd);pos1=pos.copy()
      if compt%mort[1]==0 and go and options["feu"][0]:
        draw(0,mort[0],325,325-mort[0],feu);mort[0]-=mort[2]
        if (mort[0]+5)%20==0:lignes.pop()
      p1=get(pos[0]-1,pos[1]*20-9);p2=get(pos[0]+11,pos[1]*20-9);x=pos[0];y=pos[1]*20-20;draw(x+4,y+3,3,3,(0,0,0));bn(x+1,y+6,9,10,(255,255,255));draw(x+3,y+16,1,3,(0,0,0));draw(x+7,y+16,1,3,(0,0,0));draw(x,y+9,1,3,(0,0,0));draw(x+10,y+9,1,3,(0,0,0))
      if (p1 not in (bois,route,terre,feuillage)) or (p2 not in (bois,route,terre,feuillage)) or get(pos[0]+5,pos[1]*20-4)==feu:print(options,"\nLevel :",level);break
      for i in range(len(lignes)):
        if lignes[i][0]>1:
          for j in range(2,len(lignes[i])):
            sens=lignes[i][0];tipe=lignes[i][j][0];x=lignes[i][j][1];y=i*20+1;vitesse=lignes[i][1]
            if sens==3 and tipe==0:draw(x,y,vitesse,18,route);draw(x+8,y+15,vitesse,3,route);draw(x+8,y+18,vitesse,1,(255,255,255));draw(x+29,y+18,vitesse,1,(255,255,255));draw(x+29,y+15,vitesse,3,route)
            if sens==2 and tipe==0:draw(x+40,y,vitesse,18,route);draw(x+11,y+15,vitesse,3,route);draw(x+11,y+18,vitesse,1,(255,255,255));draw(x+32,y+15,vitesse,3,route);draw(x+32,y+18,vitesse,1,(255,255,255))
            if sens==3 and tipe==1:
              for k in range(5):draw(x+3+k,y+9-k,2,1,route);draw(x+12+k,y+5+k,2,1,route)
              draw(x,y+10,vitesse,5,route);draw(x+3,y+15,vitesse,3,route);draw(x+3,y+18,vitesse,1,(255,255,255));draw(x+16,y+15,vitesse,3,route);draw(x+16,y+18,vitesse,1,(255,255,255));draw(x+8,y+5,vitesse,1,route)
            if sens==2 and tipe==1:
              for k in range(5):draw(x+7+k,y+9-k,2,1,route);draw(x+16+k,y+5+k,2,1,route)
              draw(x+25,y+10,vitesse,5,route);draw(x+8,y+15,vitesse,3,route);draw(x+8,y+18,vitesse,1,(255,255,255));draw(x+21,y+15,vitesse,3,route);draw(x+21,y+18,vitesse,1,(255,255,255));draw(x+16+vitesse,y+5,-vitesse,1,route)
            lignes[i][j][1]+=vitesse
            if lignes[i][j][1]<-45:
              del lignes[i][j]
              if lignes[i][0]>3:lignes[i].append([rand(-2-int((options["quantite"]+0.5)/2),0),340])
              elif lignes[i][0]>1:lignes[i].append([rand(0,4+options["quantite"]),340])
            elif lignes[i][j][1]>355:
              del lignes[i][j]
              if lignes[i][0]>3:lignes[i].insert(2,[rand(-2-int((options["quantite"]+0.5)/2),0),-45+lignes[i][1]])
              elif lignes[i][0]>1:lignes[i].insert(2,[rand(0,4+options["quantite"]),-45])
      while sum([len(i) for i in lignes])/1000 > monotonic() - tim:pass
    except:pass
  while 1:
    try:
      for i in range(3):bn(pos[0]+1,pos[1]*20-14,9,10,(255,255,255));sleep(0.2);bn(pos[0]+1,pos[1]*20-14,9,10,(255,52,0));sleep(0.2)
      break
    except KeyboardInterrupt:pass
  txt("Game over"+lel,110,90,cd,(255,52,0));txt("Level : "+str(level),110,108,cd,(255,52,0))
  try:
    t=1
    while t:
      for i in range(55):
        if keydown(i):
          while keydown(i):pass
          t=0;break
  except KeyboardInterrupt:fin()
  mort=[0,11,1];level=0;pos=[160,11]
men_opt=(("Menu > Options :",30),(" Terrains :",50,("eau","route","feu","terre"),((0,0),(55,1),(130,2),(185,3))),(" Rapidite des vehicules :",100),(" Proportion d'obstacles :",145));men=(("Options",70,"Personnaliser\n\t\t\t\t ses parties"),("Instructions",110," Tout savoir\n\t\t\t\t sur comment\n\t\t\t\t    jouer","Le principe est simple.\n\tIl faut faire avancer le\n\t perso jusqu'en haut de\n\tl'ecran. Les voitures et\n\tles camions, l'eau et le\n\t feu sont mortels. Les\n\t  arbres empechent le\n\t       passage."),("Credits",150," Tout savoir\n\t\t\t\t sur l'auteur\n\t\t\t\t   du jeu"," Ce jeu a ete cree par\n\t    Hipster Whale et\n\tdeveloppe sur Python par\n\tCaucaucybu. Vous pouvez\n\t trouver d'autres jeux\n\t comme des echecs ou un\n\t morpion sur mon compte\n\t numworks : Caucaucybu."))
def menfond():draw(20,20,280,182,(206,105,8))
def cachtxt():draw(157,87,136,66,(255,255,255))
def meng(rang,c):
  if rang!=-1:txt(men[rang][0],30,men[rang][1],c,(206,105,8));txt(men[rang][2],160,93)
  else:txt("CROSSY ROAD",100,40,c,(206,105,8));draw(98,57,113,1,c);txt(" Pour jouer,\n\t\t\t\t appuyez sur\n\t\t\t\t    [OK]",160,93)
def menop(rg,c):
  txt(men_opt[rg][0],30,men_opt[rg][1],c,(206,105,8))
  if rg==0:draw(28,45,163,1,c)
def menog(rng,c):
  if rng<4:
    draw(45+men_opt[1][3][rng][0],73,10,10,c);draw(47+men_opt[1][3][rng][0],75,6,6,(206,105,8));txt(men_opt[1][2][men_opt[1][3][rng][1]],58+men_opt[1][3][rng][0],69,c,(206,105,8))
    if options[options["k"][rng]][0]:draw(options[options["k"][rng]][1],76,4,4,c)
  else:
    draw(110,90,10,10,c);draw(112,92,6,6,(206,105,8));txt("arbres",123,86,c,(206,105,8))
    if options["arbres"]:draw(113,93,4,4,c)
def barc(r,c,h):draw(70+int(r*(166/3)),h-8,5,20,c);draw(71+int(r*(166/3)),h-9,3,22,c)
def debar(r,h):draw(70+int(r*(166/3)),h-9,5,22,(206,105,8));draw(70+int(r*(166/3)),h+1,5,3,(255,0,0))
def fin():
  try:
    while keydown(5):pass
  except KeyboardInterrupt:fin()
if 1:
  jeu()
  while 1:
    while keydown(4):pass
    rang=-1;menfond();txt("CROSSY ROAD",100,40,(0,0,0),(206,105,8));draw(98,57,113,1,(0,0,0));cachtxt()
    for i in men:txt(i[0],30,i[1],(0,0,0),(206,105,8))
    go=1
    try:
      while not keydown(4):
        if go:
          if rang==3:rang=-1
          if rang==-2:rang=2
          cachtxt();go=0;meng(rang,(255,0,0))
        if keydown(0) or keydown(1):
          while keydown(0) or keydown(1):pass
          meng(rang,(0,0,0));rang-=1;go=1
        if keydown(2) or keydown(3):
          while keydown(2) or keydown(3):pass
          meng(rang,(0,0,0));rang+=1;go=1
    except KeyboardInterrupt:return
    while keydown(4):pass
    if rang==-1:
      jeu()
      continue
    if rang==1 or rang==2:
      menfond();draw(37,37,246,148,(255,255,255));txt(men[rang][3],40,40)
      try:
        while not keydown(4):pass
        while keydown(4):pass
      except KeyboardInterrupt:fin()
    if rang==0:
      rg=1
      while 1:
        try:
          menfond();go=1
          for i in men_opt:txt(i[0],30,i[1],(0,0,0),(206,105,8))
          draw(28,45,163,1,(0,0,0))
          for i in men_opt[1][3]:draw(45+i[0],73,10,10,(0,0,0));draw(47+i[0],75,6,6,(206,105,8));txt(men_opt[1][2][i[1]],58+i[0],69,(0,0,0),(206,105,8))
          for i in options["k"]:
            if options[i][0]:draw(options[i][1],76,4,4,(0,0,0))
          if options["terre"][0]:
            draw(110,90,10,10,(0,0,0));draw(112,92,6,6,(206,105,8));txt("arbres",123,86,(0,0,0),(206,105,8))
            if options["arbres"]:draw(113,93,4,4,(0,0,0))
          for i in (130,options["rapidite"]),(180,options["quantite"]):
            draw(70,i[0]+1,170,3,(0,0,0));draw(45,i[0]+2,15,1,(0,0,0));draw(250,i[0]+2,15,1,(0,0,0));draw(257,i[0]-5,1,15,(0,0,0));barc(i[1],(0,0,0),i[0]);draw(70+int(i[1]*(166/3)),i[0]-8,5,20,(0,0,0));draw(71+int(i[1]*(166/3)),i[0]-9,3,22,(0,0,0))
          while not keydown(4):
            if go:
              if rg==-1:rg=3
              if rg==4:rg=0
              menop(rg,(255,0,0));go=0
            if keydown(0) or keydown(1):
              while keydown(0) or keydown(1):pass
              go=1;menop(rg,(0,0,0));rg-=1
            if keydown(2) or keydown(3):
              while keydown(2) or keydown(3):pass
              go=1;menop(rg,(0,0,0));rg+=1
          while keydown(4):pass
          if rg==0:break
          if rg==1:
            og=1;rng=0
            while 1:
              try:
                while not keydown(4):
                  if og:
                    if rng>3:
                      if options["terre"][0]:
                        if rng==5:rng=0
                      else:rng=0
                    if rng==-1:
                      if options["terre"][0]:rng=4
                      else:rng=3
                    og=0;menog(rng,(255,0,0))
                  if keydown(0):
                    while keydown(0):pass
                    og=1;menog(rng,(0,0,0));rng-=1
                  if keydown(3):
                    while keydown(3):pass
                    og=1;menog(rng,(0,0,0));rng+=1
                  if keydown(1):
                    while keydown(1):pass
                    og=1;menog(rng,(0,0,0))
                    if options["terre"][0]:
                      if -1<rng<4:rng=4
                      else:rng=3
                    else:rng-=1
                  if keydown(2):
                    while keydown(2):pass
                    og=1;menog(rng,(0,0,0))
                    if options["terre"][0]:
                      if -1<rng<4:rng=4
                      else:rng=0
                    else:rng+=1
                while keydown(4):pass
                if rng!=4:
                  kk=options["k"][rng]
                  if options[kk][0]:
                    tstk=[]
                    for i in ("terre","route","eau"):
                      if options[i][0]:tstk.append(1)
                    if rng==2 or len(tstk)>1:
                      draw(options[kk][1],76,4,4,(206,105,8));options[kk][0]=0
                      if rng==3:draw(110,86,75,15,(206,105,8))
                  else:
                    draw(options[kk][1],76,4,4,(255,0,0));options[kk][0]=1
                    if rng==3:
                      draw(110,90,10,10,(0,0,0));draw(112,92,6,6,(206,105,8));txt("arbres",123,86,(0,0,0),(206,105,8))
                      if options["arbres"]:draw(113,93,4,4,(0,0,0))
                else:
                  if options["arbres"]:draw(113,93,4,4,(206,105,8));options["arbres"]=0
                  else:draw(113,93,4,4,(255,0,0));options["arbres"]=1
              except KeyboardInterrupt:fin();break
          if rg==2:
            og=1;draw(70,131,170,3,(255,0,0));draw(45,132,15,1,(255,0,0));draw(250,132,15,1,(255,0,0));draw(257,125,1,15,(255,0,0))
            try:
              while not keydown(4):
                if og:barc(options["rapidite"],(255,0,0),130)
                if (keydown(0) or keydown(46)) and options["rapidite"]:
                  while keydown(0) or keydown(46):pass
                  debar(options["rapidite"],130);options["rapidite"]-=1;og=1
                if (keydown(3) or keydown(45)) and options["rapidite"]!=3:
                  while keydown(3) or keydown(45):pass
                  debar(options["rapidite"],130);options["rapidite"]+=1;og=1
              while keydown(4):pass
            except KeyboardInterrupt:fin()
          if rg==3:
            draw(70,181,170,3,(255,0,0));draw(45,182,15,1,(255,0,0));draw(250,182,15,1,(255,0,0));draw(257,175,1,15,(255,0,0))
            try:
              while not keydown(4):
                if og:barc(options["quantite"],(255,0,0),180)
                if (keydown(0) or keydown(46)) and options["quantite"]:
                  while keydown(0) or keydown(46):pass
                  debar(options["quantite"],180);options["quantite"]-=1;og=1
                if (keydown(3) or keydown(45)) and options["quantite"]!=3:
                  while keydown(3) or keydown(45):pass
                  debar(options["quantite"],180);options["quantite"]+=1;og=1
              while keydown(4):pass
            except KeyboardInterrupt:fin()
        except KeyboardInterrupt:fin();break