_2048.py

Created by marius-layan

Created on February 05, 2024

2.23 KB

Le but du jeu est de faire glisser des tuiles sur une grille, pour combiner les tuiles de mêmes valeurs et créer ainsi une tuile portant le nombre 2048.

Le joueur peut toutefois continuer à jouer après cet objectif atteint pour faire le meilleur score possible.

déplacement: flèches directionnelles

BON JEU !


from random import *
from kandinsky import *
from ion import *
from time import *
def affiche():
  cmt=0
  for j in range(5):
    for i in range(5):
      if game[cmt]!=0:fill_rect(i*44+53,j*44+3,41,41,(255,255-game[cmt]/2,255-game[cmt]/2));draw_string(str(game[cmt]),i*44-int(len(str(game[cmt]))/2*8)+70,j*44+15,'black',(255,255-game[cmt]/2,255-game[cmt]/2))
      else:fill_rect(i*44+53,j*44+3,41,41,(255,255-game[cmt]/2,255-game[cmt]/2))
      cmt+=1
l,game,fin=[2,4,8],[0 for i in range(25)],0
for i in range(6):fill_rect(50+i*44,0,3,222,'black')
for i in range(6):fill_rect(50,i*44,220,3,'black')
while True:
  a=[i for i in range(25)]
  while True:
    b=choice(a)
    for i in range(len(a)):
      if b==a[i]:c=i
    if game[b]==0:game[b]=choice(l);break
    else:del a[c]
    if a==[]:fin=1;break
  affiche()
  while True:
    if keydown(KEY_LEFT):
      for j in range(5):
        for i in range(1,25):
          if game[i-1]==0 and i-1 not in (-1,4,9,14,19) or game[i-1]==game[i]:
            if game[i-1]==game[i]:
              game[i]*=2
              if game[i]==2048:draw_string("win",0,0)
            game[i-1]=game[i]
            game[i]=0
      while keydown(KEY_LEFT):continue
      break
    if keydown(KEY_RIGHT):
      for j in range(5):
        for i in range(24):
          if game[i+1]==0 and i+1 not in (5,10,15,20) or game[i+1]==game[i]:
            if game[i+1]==game[i]:
              game[i]*=2
              if game[i]==2048:fin=2
            game[i+1]=game[i]
            game[i]=0
      while keydown(KEY_RIGHT):continue
      break
    if keydown(KEY_UP):
      for j in range(5):
        for i in range(5,25):
          if game[i-5]==0 or game[i-5]==game[i]:
            if game[i-5]==game[i]:
              game[i]*=2
              if game[i]==2048:fin=2
            game[i-5]=game[i]
            game[i]=0
      while keydown(KEY_UP):continue
      break
    if keydown(KEY_DOWN):
      for j in range(5):
        for i in range(20):
          if game[i+5]==0 or game[i+5]==game[i]:
            if game[i+5]==game[i]:
              game[i]*=2
              if game[i]==2048:fin=2
            game[i+5]=game[i]
            game[i]=0
      while keydown(KEY_DOWN):continue
      break
  if fin!=0:break
affiche()
draw_string("lose",0,0)

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.