Advertisement
Guest User

Tetris Code

a guest
Aug 13th, 2022
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3. from inspect import currentframe, getframeinfo
  4.  
  5. class Block(pygame.sprite.Sprite):
  6.     def __init__(self, blocks):
  7.         super().__init__()
  8.  
  9.         self.random_block = randint(1,7)
  10.  
  11.         self.image = blocks[self.random_block]
  12.         self.image = pygame.transform.scale(self.image,(self.image.get_width()*.4, self.image.get_height()*.4))
  13.         self.rect = self.image.get_rect()
  14.         self.rect.topleft = (300,0)
  15.         self.mask = pygame.mask.from_surface(self.image)
  16.  
  17.     def replace(self, block_colours):
  18.         image_pixel_array = pygame.PixelArray(self.image)
  19.         colour = block_colours[self.random_block]
  20.         image_pixel_array.replace(colour,(0,0,0))
  21.         del image_pixel_array
  22.  
  23.  
  24.  
  25.     def handle_keys(self, board_s, block, unactive_blocks):
  26.         keys = pygame.key.get_pressed()
  27.         if keys[pygame.K_LEFT] or keys[pygame.K_a]:
  28.             if self.rect.left - 60 >= 0:
  29.                 self.replace(block_colours)
  30.                 self.rect.x -= 60
  31.                 self.draw(block_group, board_s)
  32.         elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
  33.             if self.rect.right + 60 <= 600:
  34.                 self.replace(block_colours)
  35.                 self.rect.x += 60
  36.                 self.draw(block_group, board_s)
  37.         elif keys[pygame.K_UP] or keys[pygame.K_w]:
  38.             self.replace(block_colours)
  39.             topleft = self.rect.topleft
  40.             self.image = pygame.transform.rotate(self.image, 90)
  41.             self.rect = self.image.get_rect()
  42.             self.rect.topleft = topleft
  43.             self.mask = pygame.mask.from_surface(self.image)
  44.             self.draw(block_group, board_s)
  45.         elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
  46.             if self.rect.bottom + 60 <= 1200:
  47.                 self.replace(block_colours)
  48.                 self.rect.y += 60
  49.                 if pygame.sprite.spritecollide(block,unactive_blocks,False,pygame.sprite.collide_mask):
  50.                     self.rect.y -= 60
  51.                     block_sprite(blocks, block)
  52.                 self.draw(block_group, board_s)
  53.             else:
  54.                 block_sprite(blocks, block)
  55.    
  56.     def move_every_second(self, block, unactive_blocks):
  57.         if self.rect.bottom + 60 <= 1200:
  58.             self.replace(block_colours)
  59.             self.rect.y += 60
  60.             if pygame.sprite.spritecollide(block,unactive_blocks,False,pygame.sprite.collide_mask):
  61.                 self.rect.y -= 60
  62.                 block_sprite(blocks, block)
  63.             self.draw(block_group, board_s)
  64.         else:
  65.             block_sprite(blocks, block)
  66.    
  67.     def draw(self, block_group, board_s):
  68.         print(self.rect.x,self.rect.y)
  69.         block_group.draw(board_s)
  70.  
  71.  
  72. pygame.init()
  73.  
  74. pygame.display.set_caption('Tetris')
  75. screen = pygame.display.set_mode((720,1280))
  76. board_s = pygame.Surface((600,1200))
  77. screen.fill((0,0,0))
  78. board_s.fill((0,0,0))
  79.  
  80.  
  81. blocks = {
  82.     1: pygame.image.load('O.png').convert_alpha(),
  83.     2: pygame.image.load('I.png').convert_alpha(),
  84.     3: pygame.image.load('S.png').convert_alpha(),
  85.     4: pygame.image.load('Z.png').convert_alpha(),
  86.     5: pygame.image.load('L.png').convert_alpha(),
  87.     6: pygame.image.load('J.png').convert_alpha(),
  88.     7:pygame.image.load('T.png')}
  89.  
  90. block_colours = {
  91.     1: (0,136,255),
  92.     2: (0,255,64),
  93.     3: (208,0,255),
  94.     4: (0,4,255),
  95.     5: (255,106,0),
  96.     6: (102,0,255),
  97.     7: (255,0,42)
  98. }
  99.  
  100. unactive_blocks = pygame.sprite.Group()
  101.  
  102.  
  103. def block_sprite(blocks, unactive_block):
  104.     if unactive_block != 0:
  105.         unactive_blocks.add(unactive_block)
  106.     global block, block_group
  107.     block = Block(blocks)
  108.     block_group = pygame.sprite.Group()
  109.     block_group.add(block)
  110.     return unactive_blocks
  111. block_sprite(blocks, 0)
  112.  
  113. block.draw(block_group, board_s)
  114.  
  115.  
  116. MOVE_EVERY_SECOND = pygame.USEREVENT
  117. pygame.time.set_timer(MOVE_EVERY_SECOND, 1000)
  118. clock = pygame.time.Clock()
  119. while True:
  120.     for event in pygame.event.get():
  121.  
  122.         if event.type == pygame.QUIT:
  123.             pygame.quit()
  124.             exit()
  125.        
  126.         if event.type == MOVE_EVERY_SECOND:
  127.             block.move_every_second(block, unactive_blocks)
  128.    
  129.     block.handle_keys(board_s, block, unactive_blocks)
  130.    
  131.     screen.blit(board_s,(0,80))
  132.  
  133.     pygame.display.flip()
  134.  
  135.     ticks = clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement