Advertisement
Guest User

Untitled

a guest
May 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. from pygame import *
  2. font.init()
  3. f1 = font.Font(font.get_default_font(),40)
  4. f2 = font.Font(font.get_default_font(),80)
  5. from random import choice
  6. scr = display.set_mode((500,500))
  7. import pygame.event
  8. direction = {K_LEFT  : (  0,  4, 1, 4), K_RIGHT : (-13, -4,-1, 4),
  9.              K_UP    : (  0, 15, 4, 1), K_DOWN  : ( -4,-13,-4, 1)}
  10. matrice = [0,0,0,0,
  11.            0,0,0,0,
  12.            0,0,0,0,
  13.            0,0,0,0]
  14. def game():
  15.     def redraw():
  16.         for e,v in enumerate(matrice):
  17.             x = (e%4)*120+20
  18.             y = (e//4)*120+20
  19.             r = scr.fill(-1,(x,y,100,100))
  20.             if not v: v = ''
  21.             v = f1.render(str(v),1,(100,100,100))
  22.             scr.blit(v,v.get_rect(center=r.center))
  23.         display.flip()
  24.  
  25.     def MoveAndAdd(key):
  26.         n1,n2,s,p = direction[key]
  27.         for foo in 1,2,3,4:
  28.             t = [i for i in matrice[n1:n1+n2:s] if i]
  29.             i = 0
  30.             while i<len(t)-1:
  31.                 if t[i] == t[i+1]:
  32.                     t[i] += t[i] #t[i] = t[i]+t[i]
  33.                     del(t[i+1])# supprimer t[i+1]
  34.                 i += 1
  35.             matrice[n1:n1+n2:s] = (t+[0,0,0,0])[:4]# 0~4
  36.             n1 += p
  37.            
  38.  
  39.  
  40.     c  = None
  41.     matrice[choice([e for e,i in enumerate(matrice) if not i])] = choice((2,4))   #[e for e,i in enumerate(matrice) if not i]=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  42.     while True:
  43.         if c != matrice:  
  44.             tv = [e for e,i in enumerate(matrice) if not i]
  45.             if tv:
  46.                 matrice[choice(tv)] = choice((2,4))
  47.                 print (len(tv),tv)
  48.             if len(tv)<2:
  49.                 a = not any(matrice[i] == matrice[i+1]for i in (0,1,2,4,5,6,8,9,10,12,13,14))
  50.                 b = not any(matrice[i] == matrice[i+4]for i in range(12))#0~11
  51.                 if a and b:# si a et b sont vrai,,
  52.                          redraw()
  53.                          return 0 #perdu
  54.             redraw()
  55.         ev = event.wait()
  56.         c = matrice[:] #[0~15]
  57.         if ev.type == KEYDOWN and ev.key in direction:
  58.             MoveAndAdd(ev.key)
  59.         if 2048 in matrice:
  60.             redraw()
  61.             return 1 #gagne
  62.  
  63. while True:
  64.     event.clear()
  65.     g = game()
  66.     if g == 0:
  67.         message = f2.render('PERDU',1,(200,100,100))
  68.         scr.blit(message,message.get_rect(center=(250,250)))
  69.     else:
  70.         message = f2.render('GAGNE',1,(255,100,100))
  71.         scr.blit(message,message.get_rect(center=(250,250)))
  72.     display.flip()
  73.     while event.wait().type != QUIT: pass
  74.     break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement