Advertisement
Guest User

Untitled

a guest
May 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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. #print(matrice[i],matrice[i+1],matrice[i+4])
  38. print(matrice[i],i,t)
  39.  
  40.  
  41. c = None
  42. 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]
  43. while True:
  44. if c != matrice: #(!=) differente
  45. tv = [e for e,i in enumerate(matrice) if not i]
  46. if tv:
  47. matrice[choice(tv)] = choice((2,4))
  48. print (len(tv),tv)
  49. if len(tv)<2:
  50. a = not any(matrice[i] == matrice[i+1]for i in (0,1,2,4,5,6,8,9,10,12,13,14))
  51. b = not any(matrice[i] == matrice[i+4]for i in range(12))
  52. if a and b:# si a et b sont vrai,,
  53. redraw()
  54. return 0 #perdu
  55. redraw()
  56. ev = event.wait()
  57. c = matrice[:] #[0~15]
  58. if ev.type == KEYDOWN and ev.key in direction:
  59. MoveAndAdd(ev.key)
  60. if 2048 in matrice:
  61. redraw()
  62. return 1 #gagne
  63.  
  64. while True:
  65. event.clear()
  66. g = game()
  67. if g == 0:
  68. message = f2.render('PERDU',1,(200,100,100))
  69. scr.blit(message,message.get_rect(center=(250,250)))
  70. else:
  71. message = f2.render('GAGNE',1,(255,100,100))
  72. scr.blit(message,message.get_rect(center=(250,250)))
  73. display.flip()
  74. while event.wait().type != QUIT: pass
  75. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement