game_2048.py

Created by caucaucybu

Created on January 09, 2024

6.27 KB

2048 codé en python par moi-même.


09/01/2024 : ajout du print du score à la fin de la partie


Vous pouvez trouvez d’autres jeux comme un jeu de farming sur mon compte NumWorks : Caucaucybu.


from kandinsky import fill_rect as draw, draw_string as drawtxt
from ion import keydown
from random import randint, choice

i = 1
COLS = {0:(255,255,255)}
while i < 8192:
  COLS[i] = (randint(100,255),randint(100,255),randint(100,255))
  i *= 2

possible = [1] * 1024 + [2] * 512 + [4] * 256 + [8] * 128 + [16] * 64 + [32] * 32 + [64] * 16 + [128] * 8 + [256] * 4 + [512] * 2 + [1024]

def display(scren, score):
  notgood = True
  while notgood:
    i, j, numb = randint(0,4), randint(0,4), choice(possible)
    if scren[i][j][0]:
      continue
    else:
      scren[i][j][0] = numb
      notgood = False
  drawtxt("Score : "+str(score), 59, -2)
  draw(57, 15, 206, 206, (0,0,0)) 
  for i in range(5):
    for j in range(5):
      value = scren[i][j][0]
      draw(58 + 41*j, 16 +41*i, 40, 40, COLS[value])
      if value:
        drawtxt(str(value), 78 + 41*j - len(str(value)) * 5, 28 + 41*i, (0,0,0), COLS[value])

def left(screen, score):
  modif = False
  for i in range(0, 5):
    for j in range(1, 5):
      val1, val2 = screen[i][j-1:j+1]
      if not val2[0]:
        continue
      if val1[0]:
        if val1[0] == val2[0] and not (val1[1] or val2[1]):
          screen[i][j-1], screen[i][j] = [2*val1[0],1], [0,0]
          score += 2*val1[0]
          modif = True
        else:
          continue
      else:
        screen[i][j-1], screen[i][j] = val2.copy(), [0,0]
        modif = True
      j -= 1
      while j != 0:
        val1, val2 = screen[i][j-1:j+1]
        if not val1[0]:
          screen[i][j-1], screen[i][j] = val2.copy(), [0,0]
          modif = True
        elif val1[0] == val2[0] and not (val1[1] or val2[1]):
          modif = True
          screen[i][j-1], screen[i][j] = [val2[0]*2,1], [0,0]
          score += 2*val2[0]
        else:
          break
        j -= 1
  for i in range(5):
    for j in range(5):
      screen[i][j][1] = 0
  return screen, score, modif

def right(screen, score):
  modif = False
  for i in range(0, 5):
    for j in range(1, 5):
      j = 4 - j
      val1, val2 = screen[i][j:j+2]
      if not val1[0]:
        continue
      if val2[0]:
        if val1[0] == val2[0] and not (val1[1] or val2[1]):
          screen[i][j+1], screen[i][j] = [2*val1[0],1], [0,0]
          modif = True
          score += 2*val1[0]
        else:
          continue
      else:
        screen[i][j+1], screen[i][j] = val1.copy(), [0,0]
        modif = True
      j += 1
      while j != 4:
        val1, val2 = screen[i][j:j+2]
        if not val2[0]:
          screen[i][j+1], screen[i][j] = val1.copy(), [0,0]
          modif = True
        elif val1[0] == val2[0] and not (val1[1] or val2[1]):
          screen[i][j+1], screen[i][j] = [val1[0]*2,1], [0,0]
          modif = True
          score += 2*val1[0]
        else:
          break
        j += 1
  for i in range(5):
    for j in range(5):
      screen[i][j][1] = 0
  return screen, score, modif

def down(screen, score):
  modif = False
  for i in range(0, 5):
    for j in range(1, 5):
      j = 4 - j
      val1, val2 = screen[j][i], screen[j+1][i]
      if not val1[0]:
        continue
      if val2[0]:
        if val1[0] == val2[0] and not (val1[1] or val2[1]):
          screen[j+1][i], screen[j][i] = [2*val1[0],1], [0,0]
          modif = True
          score += 2*val1[0]
        else:
          continue
      else:
        screen[j+1][i], screen[j][i] = val1.copy(), [0,0]
        modif = True
      j += 1
      while j != 4:
        val1, val2 = screen[j][i], screen[j+1][i]
        if not val2[0]:
          screen[j+1][i], screen[j][i] = val1.copy(), [0,0]
          modif = True
        elif val1[0] == val2[0] and not (val1[1] or val2[1]):
          screen[j+1][i], screen[j][i] = [val1[0]*2,1], [0,0]
          modif = True
          score += 2*val1[0]
        else:
          break
        j += 1
  for i in range(5):
    for j in range(5):
      screen[i][j][1] = 0
  return screen, score, modif

def up(screen, score):
  modif = False
  for i in range(0, 5):
    for j in range(1, 5):
      val1, val2 = screen[j-1][i], screen[j][i]
      if not val2[0]:
        continue
      if val1[0]:
        if val1[0] == val2[0] and not (val1[1] or val2[1]):
          screen[j-1][i], screen[j][i] = [2*val1[0],1], [0,0]
          modif = True
          score += 2*val1[0]
        else:
          continue
      else:
        screen[j-1][i], screen[j][i] = val2.copy(), [0,0]
        modif = True
      j -= 1
      while j != 0:
        val1, val2 = screen[j-1][i], screen[j][i]
        if not val1[0]:
          screen[j-1][i], screen[j][i] = val2.copy(), [0,0]
          modif = True
        elif val1[0] == val2[0] and not (val1[1] or val2[1]):
          modif = True
          screen[j-1][i], screen[j][i] = [val1[0]*2,1], [0,0]
          score += 2*val1[0]
        else:
          break
        j -= 1
  for i in range(5):
    for j in range(5):
      screen[i][j][1] = 0
  return screen, score, modif
def game(scren=None, score=0):
  try:
    try:
      for i in range(5):
        for j in range(5):
          scren[i][j]
    except (IndexError, TypeError):
      scren = [[[0 for i in range(2)] for i in range(5)] for i in range(5)]
      display(scren, score)
    win = True
    while win:
      test = False
      if keydown(0):
        test = True
        scren, score, modif = left(scren, score)
        if modif:
          display(scren, score)
      if keydown(3):
        test = True
        scren, score, modif = right(scren, score)
        if modif:
          display(scren, score)
      if keydown(2):
        test = True
        scren, score, modif = down(scren, score)
        if modif:
          display(scren, score)
      if keydown(1):
        test = True
        scren, score, modif = up(scren, score)
        if modif:
          display(scren, score)
      if test:
        while keydown(0) or keydown(1) or keydown(2) or keydown(3):pass
        win = False
        for i in range(5):
          for j in range(5):
            val = scren[i][j][0]
            if (
                (not val) or
                (i > 0 and val == scren[i-1][j][0]) or
                (i < 4 and val == scren[i+1][j][0]) or
                (j > 0 and val == scren[i][j-1][0]) or
                (j < 4 and val == scren[i][j+1][0])
              ):
                win = True
                break
  except KeyboardInterrupt:
    pass
  return score
print("Score :", game())