jeu_2048.py

Created by ph-moutou

Created on June 14, 2018

1.98 KB

Ce programme reproduit le fonctionnement du jeu 2048 inventé par Gabriele Cirulli. Utiliser les touches 2, 4, 6 et 8 pour aller vers le bas, la gauche, le haut ou la droite. Atteignez 2048 et allez au-delà!

```from random import *
from math import *
from kandinsky import *
from utilitaires import *

def carre(x,y,c,l):
for i in range(l):
for j in range(l):
set_pixel(x+i,y+j,c)

def plateau():
carre(60,10,color(150,150,150),200)
for i in range(16):
carre(64+(i%4)*49,14+(i//4)*49,color(200,200,200),45)
draw_string("score",265,20)
affiche(0)

def affiche(s):
draw_string(str(s),265,40)

def tuiles(k):
col=[color(200,200,200),color(238,228,218),color(237,224,200),
color(242,177,121),color(245,149,99),color(246,124,95),
color(246,94,59),color(237,207,114),color(237,204,97),
color(237,200,80),color(236,198,65),color(224,194,46),
color(255,61,61),color(255,30,32)]#jusqu'a 8196
for i in range(16):
c=k[i%4][i//4]
x,y=64+(i%4)*49,14+(i//4)*49
carre(x,y,col[c],45)
if c!=0:draw_string(str(2**c),x+22-5*len(str(2**c)),y+15)

def valeur():
if randint(1,7)==1:return 2
return 1

def entrer():
c=int(input())
if c==5 or c==2 or c==4 or c==6 or c==8 :return c
else : return entrer()

def j(s):
seed(s)
score=0
K=[4*[0],4*[0],4*[0],4*[0]]
case=randint(0,15)
K[case%4][case//4]=valeur()
case+=randint(1,15)
K[case%4][(case%16)//4]=valeur()
plateau()
tuiles(K)
sens=entrer()
while sens!=5:
T=[4*[0],4*[0],4*[0],4*[0]]
moved=False
for i in range(4):
for j in range(4):
if sens==6:T[i][j]=K[j][i]
if sens==4:T[i][j]=K[3-j][i]
if sens==8:T[i][j]=K[i][3-j]
if sens==2:T[i][j]=K[i][j]
moved=True
if moved==True:
for i in range(4):
for j in range(4):
if sens==6:K[i][j]=T[j][i]
if sens==4:K[i][j]=T[j][3-i]
if sens==8:K[i][j]=T[i][3-j]
if sens==2:K[i][j]=T[i][j]
affiche(score)
tuiles(K)
case=randint(0,15)
while K[case%4][case//4]!=0:
case=randint(0,15)
K[case%4][case//4]=valeur()
tuiles(K)
sens=entrer()```