Advertisement
snowden_web

Untitled

Nov 29th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.70 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. BLACK    = (   0,   0,   0)
  5. WHITE    = ( 255, 255, 255)
  6. GREEN    = (   0, 255,   0)
  7. RED      = ( 255,   0,   0)
  8.  
  9. WIDTH = 10
  10. HEIGHT = 20
  11. EMPTY = 0
  12. FULL = 1
  13. CELL_SIZE = 30
  14. SPEED = 400
  15. O = [[[1, 1],
  16.      [1, 1]]]
  17.  
  18. I = [[[1, 1, 1, 1]],
  19.      [[0, 1],
  20.      [0, 1],
  21.      [0, 1],
  22.      [0, 1]]]
  23.  
  24. J = [[[1, 0, 0],
  25.      [1, 1, 1]],
  26.      [[0, 1],
  27.       [0, 1],
  28.       [1, 1]],
  29.      [[1, 1, 1],
  30.       [0, 0, 1]],
  31.      [[1, 1],
  32.       [1, 0],
  33.       [1, 0]]]
  34.  
  35. L = [[[0, 0, 1],
  36.      [1, 1, 1]],
  37.      [[1, 1],
  38.       [0, 1],
  39.       [0, 1]],
  40.      [[1, 1, 1],
  41.       [1, 0, 0]],
  42.      [[1, 0],
  43.       [1, 0],
  44.       [1, 1]]]
  45.      
  46. T = [[[0, 1, 0],
  47.      [1, 1, 1]],
  48.      [[0, 1],
  49.       [1, 1],
  50.       [0, 1]],
  51.      [[1, 1, 1],
  52.       [0, 1, 0]],
  53.      [[1, 0],
  54.       [1, 1],
  55.       [1, 0]]]
  56.      
  57. S = [[[0, 1, 1],
  58.      [1, 1, 0]],
  59.      [[1, 0],
  60.       [1, 1],
  61.       [0, 1]]]
  62.  
  63. Z = [[[1, 1, 0],
  64.      [0, 1, 1]],
  65.      [[0, 1],
  66.       [1, 1],
  67.       [1, 0]]]
  68.  
  69.  
  70.  
  71. FORMS = [O, I, J, L, T, S, Z]
  72.  
  73. class Field:
  74.     def __init__(self, width, height):
  75.         self.width = width
  76.         self.height = height
  77.         self.map = [([FULL] + [EMPTY] * self.width + [FULL])
  78.                     for i in range(self.height)] + [[FULL] * (self.width + 2)]
  79.  
  80.     def draw(self, screen):
  81.         screen.fill(BLACK)
  82.         for y in range(self.height):
  83.             for x in range(1, self.width + 1):
  84.                 if self.map[y][x] != EMPTY:
  85.                     pygame.draw.rect(screen, self.map[y][x],
  86.                                              [(x - 1) * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE])                    
  87.     def delete_rows(self):
  88.         for y in range(self.height):
  89.             if EMPTY not in self.map[y]:
  90.                 self.map = [[FULL] + [EMPTY] * self.width + [FULL]] + self.map[:y] + self.map[y + 1:]
  91.                 #0  [1, 0, 0,...,0,1]
  92.                 #1  0
  93.                 #2  1   y = 2
  94.                 #3  3
  95.                 #4  4
  96.  
  97.     def is_full(self):
  98.         # TODO проверять место под новую фигуру
  99.         return self.map[0][1:-1] != [0] * self.width                
  100.        
  101. class Figure:
  102.     def __init__(self, field, screen, forms, turn):
  103.         self.x = field.width // 2 - 1
  104.         self.y = 0
  105.         self.forms = forms
  106.         self.turn = turn
  107.         self.form = self.forms[self.turn]
  108.         self.color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
  109.         self.draw(screen, self.color)
  110.  
  111.     def draw(self, screen, color=WHITE):
  112.         for y in range(len(self.form)):
  113.             for x in range(len(self.form[y])):
  114.                 if self.form[y][x] == FULL:
  115.                     pygame.draw.rect(screen, color,
  116.                                      [(self.x + x) * CELL_SIZE, (self.y + y) * CELL_SIZE, CELL_SIZE, CELL_SIZE])
  117.  
  118.     def can_move_down(self, field):    
  119.         for y in range(len(self.form)):
  120.             for x in range(len(self.form[y])):
  121.                 if self.form[y][x] == FULL:
  122.                     if field.map[self.y + y + 1][self.x + x + 1] != EMPTY:
  123.                         return False
  124.         return True
  125.        
  126.     def can_move_right(self, field):    
  127.         for y in range(len(self.form)):
  128.             for x in range(len(self.form[y])):
  129.                 if self.form[y][x] == FULL:
  130.                     if field.map[self.y + y][self.x + x + 2] != EMPTY:
  131.                         return False
  132.         return True
  133.  
  134.     def can_move_left(self, field):    
  135.         for y in range(len(self.form)):
  136.             for x in range(len(self.form[y])):
  137.                 if self.form[y][x] == FULL:
  138.                     if field.map[self.y + y][self.x + x] != EMPTY:
  139.                         return False
  140.         return True
  141.  
  142.     def can_rotate(self, field):
  143.         # TODO проверять выход за границы стакана
  144.         next_form = self.forms[(self.turn + 1) % len(self.forms)]
  145.         for y in range(len(next_form)):
  146.             for x in range(len(next_form[y])):
  147.                 if next_form[y][x] == FULL:
  148.                     if field.map[self.y + y][self.x + x + 1] != EMPTY:
  149.                         return False
  150.         return True
  151.    
  152.     def move_down(self, field):
  153.         self.draw(screen, BLACK)
  154.         self.y += 1        
  155.         self.draw(screen, self.color)        
  156.  
  157.     def move_left(self, field):
  158.         self.draw(screen, BLACK)
  159.         self.x -= 1        
  160.         self.draw(screen, self.color)
  161.        
  162.     def move_right(self, field):
  163.         self.draw(screen, BLACK)
  164.         self.x += 1        
  165.         self.draw(screen, self.color)        
  166.        
  167.     def rotate(self):
  168.         # TODO сделать честный поворот
  169.         self.draw(screen, BLACK)
  170.         self.turn = (self.turn + 1) % len(self.forms)
  171.         self.form = self.forms[self.turn]
  172.         self.draw(screen, self.color)
  173.        
  174.  
  175.     def to_map(self):
  176.         for y in range(len(self.form)):
  177.             for x in range(len(self.form[y])):
  178.                 if self.form[y][x] == FULL:
  179.                     field.map[self.y + y][self.x + x + 1] = self.color
  180.  
  181.  
  182. pygame.init()
  183. size = [CELL_SIZE * WIDTH, CELL_SIZE * HEIGHT]
  184. screen = pygame.display.set_mode(size)
  185. field = Field(WIDTH, HEIGHT)
  186. figure = Figure(field, screen, random.choice(FORMS), 0)
  187. pygame.time.set_timer(pygame.KEYDOWN, SPEED)
  188. pygame.display.set_caption("Tetris")
  189. done = False
  190. # clock = pygame.time.Clock()
  191. while not done:
  192.     for event in pygame.event.get():
  193.         if event.type == pygame.QUIT:
  194.             screen.fill(BLACK)
  195.             done = True
  196.         if event.type == pygame.KEYDOWN:
  197.             if event.key == pygame.K_RIGHT:
  198.                 if figure.can_move_right(field):
  199.                     figure.move_right(field)
  200.             elif event.key == pygame.K_LEFT:
  201.                 if figure.can_move_left(field):
  202.                     figure.move_left(field)
  203.             elif event.key == pygame.K_UP:
  204.                 if figure.can_rotate(field):
  205.                     figure.rotate()
  206.             elif event.key == pygame.K_SPACE:
  207.                 pygame.time.set_timer(pygame.KEYDOWN, SPEED // 10)
  208.             else:
  209.                 if figure.can_move_down(field):
  210.                     figure.move_down(field)
  211.                 else:
  212.                     figure.to_map()
  213.                     field.delete_rows()
  214.                     field.draw(screen)
  215.                     if not field.is_full():
  216.                         pygame.time.set_timer(pygame.KEYDOWN, SPEED)
  217.                         figure = Figure(field, screen, random.choice(FORMS), 0)
  218.                     else:
  219.                         done = True
  220.     pygame.display.flip()
  221.  
  222. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement