Adaptation du jeu Bagh Chal (Tactique du tigre ou Tigres et chèvres).
Un des joueurs dirige les 4 tigres et doit manger 5 chèvres pour gagner. Il perd si les 4 tigres sont bloqués. L’autre joueur place puis déplace 20 chèvres et doit bloquer les 4 tigres pour gagner. Il perd si 5 chèvres sont mangées.
On joue avec les flèches et la touche OK (pour sélectionner un pion ou pour valider un déplacement).
from kandinsky import * from ion import * from time import * Plateau=[[0]*5 for k in range(5)] Plateau[0][0]=2 Plateau[0][4]=2 Plateau[4][0]=2 Plateau[4][4]=2 clr_plateau=(220,180,50) gris=(210,210,210) def dessine_figure(x,y,nom): dX=int(70+40*x+1) dY=int(20+40*y+1) if nom=="tigre": motif=("WWWWWWOOOKOOOWWWWWW","WWWWOOKKKOKKKOOWWWW","WWWOOOOOOKOOOOOOWWW","WWOOOKKKKOKKKKOOOWW","WOKOKKWOOKOOWKKOKOW","WOKOWWWWKOKWWWWOKOW","KOKKKKKWOOOWKKKKKOK","KOKOVKVKOWOKVKVOKOK","KOKOVVVKOWOKVVVOKOK","KOKKOOOKOWOKOOOKKOK","KOOKKOOKOWOKOOKKOOK","KKOOKKKOOOOOKKKOOKK","OKKOOKOOKKKOOKOOKKO","WOKKKWWKPPPKWWKKKOW","WKOOKWWWKPKWWWKOOKW","WWKKKKWWWKWWWKKKKWW","WWWWWWKKKWKKKWWWWWW","WWWWWWKWWWWWKWWWWWW","WWWWWWWKKKKKWWWWWWW") elif nom=="chevre": motif=("W"*19,"WWWWKKKWWWWWKKKWWWW","WWWKGGGKWWWKGGGKWWW","WWKGKKGKWWWKGKKGKWW","WWKKWKGKWWWKGKWKKWW","WWWWWKGKKKKKGKWWWWW","WWKKKKKKGGGKKKKKKWW","WKLLLLLGGGGGLLLLLKW","KLLKKLKLGGGLKLKKLLK","WKKWKLKLGGGLKLKWKKW","WWWWKLLLGGGLLLKWWWW","WWWWKLLLLGLLLLKWWWW","WWWWKLLLLLLLLLKWWWW","WWWWWKLLKKKLLKWWWWW","WWWWWKLLLKLLLKWWWWW","WWWWWKLLLLLLLKWWWWW","WWWWWKLLKKKLLKWWWWW","WWWWWWKLLLLLKWWWWWW","WWWWWWWKKKKKWWWWWWW") else: motif=("WWWWWWwwwwwwwWWWWWW","WWWWwwwwwwwwwwwWWWW","WWWwwwwwwwwwwwwwWWW","WWwwwwwwwwwwwwwwwWW","WwwwwwwwwwwwwwwwwwW","WwwwwwwwwwwwwwwwwwW","w"*19,"w"*19,"w"*19,"w"*19,"w"*19,"w"*19,"w"*19,"WwwwwwwwwwwwwwwwwwW","WwwwwwwwwwwwwwwwwwW","WWwwwwwwwwwwwwwwwWW","WWWwwwwwwwwwwwwwWWW","WWWWwwwwwwwwwwwWWWW","WWWWWwwwwwwwwwWWWWW") for ligne in range(19): for colonne in range(19): clr=("w",(250,200,0),"k","g","pink","gray",(200,200,200),"white")[("W","O","K","V","P","G","L","w").index(motif[ligne][colonne])] if clr != "w": set_pixel(dX+colonne,dY+ligne,clr) def dessine_curseur(x,y,clr,figure=0): dX=int(70+40*x) dY=int(20+40*y) #draw_string(" CT"[Plateau[y][x]],dX+5,dY+2,(clr_plateau,gris,"orange")[Plateau[y][x]]) if figure: dessine_figure(x,y,("","chevre","tigre")[Plateau[y][x]]) fill_rect(dX+6,dY,9,1,clr) fill_rect(dX+4,dY+1,3,1,clr) fill_rect(dX+14,dY+1,3,1,clr) fill_rect(dX+3,dY+2,2,1,clr) fill_rect(dX+16,dY+2,2,1,clr) fill_rect(dX+2,dY+3,2,1,clr) fill_rect(dX+17,dY+3,2,1,clr) fill_rect(dX+1,dY+4,2,1,clr) fill_rect(dX+18,dY+4,2,1,clr) fill_rect(dX+1,dY+5,1,2,clr) fill_rect(dX+19,dY+5,1,2,clr) fill_rect(dX,dY+6,1,9,clr) fill_rect(dX+20,dY+6,1,9,clr) fill_rect(dX+19,dY+14,1,2,clr) fill_rect(dX+1,dY+14,1,2,clr) fill_rect(dX+18,dY+16,2,1,clr) fill_rect(dX+1,dY+16,2,1,clr) fill_rect(dX+17,dY+17,2,1,clr) fill_rect(dX+2,dY+17,2,1,clr) fill_rect(dX+16,dY+18,2,1,clr) fill_rect(dX+3,dY+18,2,1,clr) fill_rect(dX+14,dY+19,3,1,clr) fill_rect(dX+4,dY+19,3,1,clr) fill_rect(dX+6,dY+20,9,1,clr) def dessine_plateau(): for ligne in range(5): for colonne in range(5): #fill_rect(70+40*colonne,20+40*ligne,20,20,clr_plateau) dessine_curseur(colonne,ligne,(clr_plateau,gris,"orange")[Plateau[ligne][colonne]],1) # mode figure if colonne<4: fill_rect(70+40*colonne+21,20+40*ligne+10,19,1,clr_plateau) if ligne<4: fill_rect(70+40*colonne+10,20+40*ligne+21,1,19,clr_plateau) for (colonne,ligne) in ((0,0),(2,0),(1,1),(3,1),(0,2),(2,2),(1,3),(3,3)): for k in range(25): set_pixel(70+40*colonne+18+k,20+40*ligne+18+k,clr_plateau) for (colonne,ligne) in ((2,0),(4,0),(1,1),(3,1),(2,2),(4,2),(1,3),(3,3)): for k in range(25): set_pixel(70+40*colonne+2-k,20+40*ligne+18+k,clr_plateau) def deplacement(mode_tigre): global Xa,Ya,Xc,Yc,coup_valide,chevre_mangee U=keydown(KEY_UP) D=keydown(KEY_DOWN) L=keydown(KEY_LEFT) R=keydown(KEY_RIGHT) if L and U: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Xc>0 and Yc>0 and Xc%2==Yc%2 and Plateau[Yc-1][Xc-1]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc-1,Yc-1 dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Xc>1 and Yc>1 and Xc%2==Yc%2 and Plateau[Yc-1][Xc-1]==1 and Plateau[Yc-2][Xc-2]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc-2,Yc-2 dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif L and D: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Xc>0 and Yc<4 and Xc%2==Yc%2 and Plateau[Yc+1][Xc-1]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc-1,Yc+1 dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Xc>1 and Yc<3 and Xc%2==Yc%2 and Plateau[Yc+1][Xc-1]==1 and Plateau[Yc+2][Xc-2]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc-2,Yc+2 dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif R and U: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Xc<4 and Yc>0 and Xc%2==Yc%2 and Plateau[Yc-1][Xc+1]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc+1,Yc-1 dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Xc<3 and Yc>1 and Xc%2==Yc%2 and Plateau[Yc-1][Xc+1]==1 and Plateau[Yc-2][Xc+2]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc+2,Yc-2 dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif R and D: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Xc<4 and Yc<4 and Xc%2==Yc%2 and Plateau[Yc+1][Xc+1]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc+1,Yc+1 dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Xc<3 and Yc<3 and Xc%2==Yc%2 and Plateau[Yc+1][Xc+1]==1 and Plateau[Yc+2][Xc+2]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc+2,Yc+2 dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif L: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Xc>0 and Plateau[Yc][Xc-1]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc-1,Yc dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Xc>1 and Plateau[Yc][Xc-1]==1 and Plateau[Yc][Xc-2]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc-2,Yc dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif R: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Xc<4 and Plateau[Yc][Xc+1]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc+1,Yc dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Xc<3 and Plateau[Yc][Xc+1]==1 and Plateau[Yc][Xc+2]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc+2,Yc dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif D: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Yc<4 and Plateau[Yc+1][Xc]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc,Yc+1 dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Yc<3 and Plateau[Yc+1][Xc]==1 and Plateau[Yc+2][Xc]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc,Yc+2 dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 elif U: coup_valide=0 chevre_mangee=0 dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) if Yc>0 and Plateau[Yc-1][Xc]==0: Xa,Ya=Xc,Yc-1 dessine_curseur(Xa,Ya,"g") coup_valide=1 elif mode_tigre and Yc>1 and Plateau[Yc-1][Xc]==1 and Plateau[Yc-2][Xc]==0: #dessine_curseur(Xa,Ya,((clr_plateau,gris,"orange")[Plateau[Ya][Xa]],"purple")[(Xa,Ya)==(Xc,Yc)]) Xa,Ya=Xc,Yc-2 dessine_curseur(Xa,Ya,"g") coup_valide=1 chevre_mangee=1 def mouvement_possible_tigre(x,y): return (x>0 and y>0 and x%2==y%2 and Plateau[y-1][x-1]==0) or (x>1 and y>1 and x%2==y%2 and Plateau[y-1][x-1]==1 and Plateau[y-2][x-2]==0) or\ (x>0 and y<4 and x%2==y%2 and Plateau[y+1][x-1]==0) or (x>1 and y<3 and x%2==y%2 and Plateau[y+1][x-1]==1 and Plateau[y+2][x-2]==0) or\ (x<4 and y<4 and x%2==y%2 and Plateau[y+1][x+1]==0) or (x<3 and y<3 and x%2==y%2 and Plateau[y+1][x+1]==1 and Plateau[y+2][x+2]==0) or\ (x<4 and y>0 and x%2==y%2 and Plateau[y-1][x+1]==0) or (x<3 and y>1 and x%2==y%2 and Plateau[y-1][x+1]==1 and Plateau[y-2][x+2]==0) or\ (x>0 and Plateau[y][x-1]==0) or (x>1 and Plateau[y][x-1]==1 and Plateau[y][x-2]==0) or\ (x<4 and Plateau[y][x+1]==0) or (x<3 and Plateau[y][x+1]==1 and Plateau[y][x+2]==0) or\ (y>0 and Plateau[y-1][x]==0) or (y>1 and Plateau[y-1][x]==1 and Plateau[y-2][x]==0) or\ (y<4 and Plateau[y+1][x]==0) or (y<3 and Plateau[y+1][x]==1 and Plateau[y+2][x]==0) def mouvement_possible_chevre(x,y): return (x>0 and y>0 and x%2==y%2 and Plateau[y-1][x-1]==0) or\ (x>0 and y<4 and x%2==y%2 and Plateau[y+1][x-1]==0) or\ (x<4 and y<4 and x%2==y%2 and Plateau[y+1][x+1]==0) or\ (x<4 and y>0 and x%2==y%2 and Plateau[y-1][x+1]==0) or\ (x>0 and Plateau[y][x-1]==0) or\ (x<4 and Plateau[y][x+1]==0) or\ (y>0 and Plateau[y-1][x]==0) or\ (y<4 and Plateau[y+1][x]==0) def tigres_bloques(): for x in range(5): for y in range(5): if Plateau[y][x]==2 and mouvement_possible_tigre(x,y): return False return True draw_string("Bagh Chal",115,70,"purple") draw_string("Tigres et chèvres",75,95,"purple") draw_string("Valider avec OK",85,135,"purple") for k in range(2): fill_rect(60,50+k*122,200,1,"g") for k in range(16): dessine_figure(-1.5+(k%8),-0.2+4.5*(k//8),list(["tigre"]+["chevre"]*6+["tigre"])[k%8]) if k in (0,7,8,15): dessine_curseur(-1.5+(k%8),-0.2+4.5*(k//8),"orange") for k in range(8): dessine_figure(-1.5+7*(k//4),0.7+0.9*(k%4),"chevre") while not (keydown(KEY_OK) or keydown(KEY_EXE)):1 while keydown(KEY_OK) or keydown(KEY_EXE):1 fill_rect(0,0,320,222,"w") draw_string("Présentation",100,10,(80,150,50)) Regles=("Un joueur déplace les tigres","et tente de manger 5 chèvres","pour gagner. Il perd si les","4 tigres sont bloqués.","","L'autre joueur place puis","déplace 20 chèvres et tente","de bloquer les tigres pour","gagner. Il perd si 5 chèvres","sont mangées.") for k in range(len(Regles)): draw_string(Regles[k],20,33+18*k,"b") while not (keydown(KEY_OK) or keydown(KEY_EXE)):1 while keydown(KEY_OK) or keydown(KEY_EXE):1 fill_rect(0,0,320,222,"w") dessine_plateau() # phase de placement des chèvres (et déplacement des tigres) for k in range(20): # dessin des 20 chèvres à placer dessine_figure(-1.5+0.5*(k//10),0.45*(k%10),"chevre") #draw_string("C",5+15*(k//10),25+18*(k%10),gris) for k in range(1,5+1): # dessin des 5 emplacements de capture des chèvres dessine_curseur(5,0.7*k,"r") chevres_mangees=0 tour=-1 while chevres_mangees<5 and not tigres_bloques(): #if tour<19: # for tour in range(20): # 20 chèvres à placer tour+=1 # placement d'une chèvre draw_string((" Placer une chèvre "," Déplacer une chèvre ")[tour>19],45,2,"b") Xc,Yc=2,3 chgX,chgY=0,-1 while not ((keydown(KEY_OK) or keydown(KEY_EXE)) and Plateau[Yc][Xc]==(tour>19) and (tour<20 or mouvement_possible_chevre(Xc,Yc))): if chgX or chgY: dessine_curseur(Xc,Yc,(clr_plateau,gris,"orange")[Plateau[Yc][Xc]]) Xc=(Xc+chgX)%5 Yc=(Yc+chgY)%5 dessine_curseur(Xc,Yc,("r","purple")[Plateau[Yc][Xc]==(tour>19) and (tour<20 or mouvement_possible_chevre(Xc,Yc))]) sleep(0.2) chgX=keydown(KEY_RIGHT)-keydown(KEY_LEFT) chgY=keydown(KEY_DOWN)-keydown(KEY_UP) Plateau[Yc][Xc]=1 if tour<20: dessine_curseur(Xc,Yc,gris,1) fill_rect(int(70+40*(-1.5+0.5*((19-tour)//10))+1),int(20+40*(0.45*((19-tour)%10))+1+1),19,18,"w") #dessine_figure(-1.5+0.5*((19-tour)//10),0.45*((19-tour)%10),"") #draw_string("C",5+15*((19-tour)//10),25+18*((19-tour)%10),"w") while keydown(KEY_OK) or keydown(KEY_EXE):1 if tour>19: # déplacement d'une chèvre draw_string("Choisir la destination",50,2,"b") # choix de la nouvelle position de la chèvre déplacée coup_valide=0 Xa,Ya=Xc,Yc while not ((keydown(KEY_EXE) or keydown(KEY_OK)) and coup_valide): deplacement(0) # mode chèvre Plateau[Yc][Xc]=0 dessine_curseur(Xc,Yc,clr_plateau,1) Plateau[Ya][Xa]=1 dessine_curseur(Xa,Ya,gris,1) while keydown(KEY_OK) or keydown(KEY_EXE):1 # déplacement d'un tigre if tigres_bloques(): break draw_string(" Déplacer un tigre ",45,2,"r") Xc,Yc=2,3 chgX,chgY=0,-1 while not ((keydown(KEY_OK) or keydown(KEY_EXE)) and Plateau[Yc][Xc]==2 and mouvement_possible_tigre(Xc,Yc)): # on choisit un tigre if chgX or chgY: dessine_curseur(Xc,Yc,(clr_plateau,gris,"orange")[Plateau[Yc][Xc]]) Xc=(Xc+chgX)%5 Yc=(Yc+chgY)%5 dessine_curseur(Xc,Yc,("r","purple")[Plateau[Yc][Xc]==2 and mouvement_possible_tigre(Xc,Yc)]) sleep(0.2) chgX=keydown(KEY_RIGHT)-keydown(KEY_LEFT) chgY=keydown(KEY_DOWN)-keydown(KEY_UP) while keydown(KEY_OK) or keydown(KEY_EXE):1 draw_string("Choisir la destination",50,2,"r") # choix de la nouvelle position du tigre déplacé coup_valide=0 Xa,Ya=Xc,Yc while not ((keydown(KEY_EXE) or keydown(KEY_OK)) and coup_valide): deplacement(1) # mode tigre Plateau[Yc][Xc]=0 dessine_curseur(Xc,Yc,clr_plateau,1) if chevre_mangee: Xm,Ym=(Xc+Xa)//2,(Ya+Yc)//2 Plateau[Ym][Xm]=0 dessine_curseur(Xm,Ym,clr_plateau,1) chevres_mangees+=1 dessine_figure(5,0.7*chevres_mangees,"chevre") #draw_string("C",290,25-18+18*chevres_mangees,gris) Plateau[Ya][Xa]=2 dessine_curseur(Xa,Ya,"orange",1) while keydown(KEY_OK) or keydown(KEY_EXE):1 if chevres_mangees>4: draw_string(" 5 chèvres capturées ! ",45,2,"r") draw_string(" Les tigres ont gagné ! ",40,202,"r") else: draw_string("Les tigres sont bloqués !",35,202,"r") draw_string("Les chèvres ont gagné !",45,202,"b")