jeu2048_2.py

Created by alexandre-merle

Created on November 13, 2023

3.96 KB


from time import sleep
from kandinsky import *
from math import log
from random import *
from ion import *
_1=[1,1,0,0,1,0,0,1,0,0,1,0,1,1,1]
_2=[1,1,1,0,0,1,1,1,1,1,0,0,1,1,1]
_3=[1,1,1,0,0,1,1,1,1,0,0,1,1,1,1]
_4=[1,0,1,1,0,1,1,1,1,0,0,1,0,0,1]
_5=[1,1,1,1,0,0,1,1,1,0,0,1,1,1,1]
_6=[1,1,1,1,0,0,1,1,1,1,0,1,1,1,1]
_7=[1,1,1,0,0,1,0,1,1,0,0,1,0,0,1]
_8=[1,1,1,1,0,1,1,1,1,1,0,1,1,1,1]
_9=[1,1,1,1,0,1,1,1,1,0,0,1,1,1,1]
_0=[1,1,1,1,0,1,1,0,1,1,0,1,1,1,1]
def draw(liste,x,y,t,color):
  k=0
  for i in range(y,y+(5*t),t):
    for j in range(x,x+(3*t),t):
      if liste[k]:fill_rect(j,i,t,t,color)
      k+=1
def draw_chiffre(n,x,y,t,color=(0,0,0),inutile=0):
  num,a=[_0,_1,_2,_3,_4,_5,_6,_7,_8,_9],0
  for i in n:draw(num[int(i)],x+a,y,t,color);a+=4*t
def t2048(tab):
  fill_rect(0,0,320,222,(255,255,255))
  for i in range(9):
    fill_rect(i*25+60,22,1,200,(0,0,0))
  for i in range(9):
    fill_rect(60,i*25+22,200,1,(0,0,0))
  for i in range(8):
    for k in range(8):
      x=tab[k][i]
      if x<10:dec=7;t=2
      elif x<100:dec=4;t=2
      elif x<1000:dec=1;t=2
      elif x<10000:dec=4;t=1
      else:dec=1;t=1
      if x>0:
        col=(max(0,255-51*(int(log(x,2))-1)),255,max(0,int(log(x,2)-1)*51-255))
        fill_rect(61+i*25,23+k*25,24,24,col)
        draw_chiffre(str(x),61+i*25+dec,29+((t%2)*3)+k*25,t,(0,0,0),col)
def chknum(n,tab):
  lst=[]
  for i in range(8):
    for k in range(8):
      if n==tab[i][k]:lst.append([i,k])
  return lst

def p2048(tab=[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]):
  score=0
  tab[randint(0,7)][randint(0,7)]=2
  canspawn=True
  while True:
    if canspawn or tr:
      new=choice(chknum(0,tab))
      tab[new[0]][new[1]]=2*randint(1,2)
    canmove=0
    act=[0,0]
    t2048(tab)
    for i in range(8):
      for k in range(8):
        if act[0]==tab[i][k] or act[1]==tab[k][i] or tab[i][k]==0:canmove=1
        act=[tab[i][k],tab[k][i]]
      act=[0,0]
    if not canmove:break
    col=(max(0,150-30*(int(log(score+0.01,2)*0.77))),150,max(0,int(log(score+0.01,2)*0.77)*30-150))
    draw_string(str(score),1,1,col)
    while not (keydown(KEY_OK) or keydown(0) or keydown(1) or keydown(2) or keydown(3)):1
    moved=[]
    tr=False
    if keydown(KEY_OK):
      while keydown(KEY_OK):pass
      a=str(input("Pause"))
    if keydown(1) or keydown(2):#up/down
      if keydown(1):
        key=1
        key2=0
      else:
        key=-1
        key2=-1
      for k in range(8):
        act=[0,-1]
        for i in range(0-7*key2,8+9*key2,key):
          if tab[i][k]==act[0] and not act[0]==0:
            tab[act[1]][k]=0
            tab[i][k]=act[0]*2
            score=score+act[0]*2
            tr=True
            act=[0,-1]
          else:
            if not tab[i][k]==0:act=[tab[i][k],i]
        for z in range(7):
          for i in range(7+7*key2,0-7*key2,-key):
            if tab[i-key][k]==0 and not tab[i][k]==0:
              tab[i-key][k]=tab[i][k]
              tab[i][k]=0
              moved.append([i,k])
    if keydown(0) or keydown(3):#left/right
      if keydown(0):
        key=1
        key2=0
      else:
        key=-1
        key2=-1
      for i in range(8):
        act=[0,-1]
        for k in range(0-7*key2,8+9*key2,key):
          if tab[i][k]==act[0] and not act[0]==0:
            tab[i][act[1]]=0
            tab[i][k]=act[0]*2
            score=score+act[0]*2
            tr=True
            act=[0,-1]
          else:
            if not tab[i][k]==0:act=[tab[i][k],k]
        for z in range(7):
          for k in range(7+7*key2,0-7*key2,-key):
            if tab[i][k-key]==0 and not tab[i][k]==0:
              tab[i][k-key]=tab[i][k]
              tab[i][k]=0
              moved.append([i,k])
    while keydown(KEY_OK) or keydown(0) or keydown(1) or keydown(2) or keydown(3):1
    if len(moved)==0:canspawn=False
    else:canspawn=True
  draw_string(str(score),1,1,(255,0,0))
  draw_string("Game over",114,1)
  print("Score:"+str(score))
p2048()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

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.