Advertisement
xX-AAAAAAAAAA-Xx

tetrominoes.py

Mar 3rd, 2025 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import enum
  2. from pygame import Vector2
  3. from math import fmod
  4. import random
  5.  
  6. class tetromino(enum.Enum):
  7.     O = 0
  8.     I = 1
  9.     S = 2
  10.     Z = 3
  11.     L = 4
  12.     J = 5
  13.     T = 6
  14.  
  15. shapes = [
  16. """
  17. XX
  18. XO
  19. """,
  20.  
  21. """
  22. X
  23. X
  24. O
  25. X
  26. """,
  27.  
  28. """
  29. XX
  30. XO
  31. """,
  32.  
  33. """
  34. XX
  35. OX
  36. """,
  37.  
  38. """
  39. X
  40. X
  41. OX
  42. """,
  43.  
  44. """
  45. X
  46. X
  47. XO
  48. """,
  49.  
  50. """
  51. XOX
  52. X
  53. """
  54. ]
  55.  
  56. # no error handling lol
  57. def get_shape(tet):
  58.     shape = shapes[tet.value]
  59.     compiled = []
  60.     x_o, y_o, x, y = 0, 0, 0, 0
  61.     for i in shape:
  62.         if i == "\n":
  63.             y-=1
  64.             x=0
  65.         else:
  66.             x+=1
  67.         if i == "X":
  68.             compiled.append(Vector2(x, y))
  69.         elif i == "O":
  70.             x_o = x
  71.             y_o = y
  72.             compiled.append(Vector2(x, y))
  73.     for i in compiled:
  74.         i.x-=x_o
  75.         i.y-=y_o
  76.     return compiled
  77.  
  78. def rotate(shape, degrees):
  79.     normalized = fmod(round(degrees / 90), 4)
  80.     compiled = []
  81.     if normalized == 0:
  82.         compiled = shape
  83.     elif normalized == 1:
  84.         for i in shape:
  85.             compiled.append(Vector2(i.y, i.x))
  86.     elif normalized == 2:
  87.         for i in shape:
  88.             compiled.append(Vector2(-i.x, -i.y))
  89.     elif normalized == 3:
  90.         for i in shape:
  91.             compiled.append(Vector2(-i.y, -i.x))
  92.     return compiled
  93.  
  94. def random_tetromino():
  95.     return random.choice([
  96.         tetromino.I,
  97.         tetromino.J,
  98.         tetromino.L,
  99.         tetromino.O,
  100.         tetromino.S,
  101.         tetromino.T,
  102.         tetromino.Z
  103.         ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement