Advertisement
cookertron

Tetris Clone using Python & Pygame

Oct 28th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.45 KB | None | 0 0
  1. import math, random, sys, time
  2. import pygame
  3. import pygame.gfxdraw
  4. from pygame.locals import *
  5.  
  6. # define some colors
  7. FUCHSIA = (255,   0, 255)
  8. PURPLE  = (128,   0, 128)
  9. TEAL    = (  0, 128, 128)
  10. LIME    = (  0, 255,   0)
  11. GREEN   = (  0, 128,   0)
  12. OLIVE   = (128, 128,   0)
  13. YELLOW  = (255, 255,   0)
  14. ORANGE  = (255, 165,   0)
  15. RED     = (255,   0,   0)
  16. MAROON  = (128,   0,   0)
  17. SILVER  = (192, 192, 192)
  18. GREY    = (128, 128, 128)
  19. NIGHT   = (32,   32,  32)
  20. BLUE    = (  0,   0, 255)
  21. NAVY    = (  0,   0, 128)
  22. AQUA    = (  0, 255, 255)
  23. WHITE   = (255, 255, 255)
  24. BLACK   = (  0,   0,   0)
  25.  
  26. # define some data
  27. BLOCKS = {1: {'startingY' : [0, 2], 'block' : [[(-1, 0), (0, 0), (1, 0), (2, 0)], [(0, -2), (0, -1), (0, 0), (0, 1)]]}, # long one
  28.           2: {'startingY' : [1], 'block' : [[(-1, -1), (0, -1), (-1, 0), (0, 0)]]}, # square
  29.           3: {'startingY' : [1, 0], 'block' : [[(0, -1), (-1, 0), (0, 0), (-1, 1)], [(-1, 0), (0, 0), (0, 1), (1, 1)]]}, # lightening 1
  30.           4: {'startingY' : [1, 0], 'block' : [[(-1, -1), (-1, 0), (0, 0), (0, 1)], [(0, 0), (1, 0), (0, 1), (-1, 1)]]}, # lightening 2
  31.           5: {'startingY' : [0, 2, 0, 2], 'block' : [[(-1, 0), (0, 0), (1, 0), (-1, 1)], [(-1, -2), (0, -2), (0, -1), (0, 0)], [(1, 0), (-1, 1), (0, 1), (1, 1)], [(-1, -2), (-1, -1), (-1, 0), (0, 0)]]},
  32.           6: {'startingY' : [0, 2, 0, 2], 'block' : [[(-1, 0), (-1, 1), (0, 1), (1, 1)], [(-1, -2), (0, -2), (-1, -1), (-1, 0)], [(-1, 0), (0, 0), (1, 0), (1, 1)], [(0, -2), (0, -1), (0, 0), (-1, 0)]]},
  33.           7: {'startingY' : [0, 1, 0, 1], 'block' : [[(0, 0), (-1, 1), (0, 1), (1, 1)], [(-1, -1), (-1, 0), (-1, 1), (0, 0)], [(-1, 0), (0, 0), (1, 0), (0, 1)], [(-1, 0), (0, -1), (0, 0), (0, 1)]]},
  34.          }
  35.  
  36. BLOCK_COLORS = [BLACK, NAVY, YELLOW, RED, PURPLE, GREEN, ORANGE, TEAL]
  37.  
  38. # exit the program
  39. def quit():
  40.     for event in pygame.event.get():
  41.         if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  42.             return True
  43.     return False
  44.  
  45. # draw a patterned title
  46. class tiles:
  47.     def __init__(self):
  48.         global NIGHT, W, H
  49.        
  50.         self.tileSize = 32
  51.         self.tile = pygame.Surface((self.tileSize, self.tileSize))
  52.        
  53.         for linePos in range(0, self.tileSize * 2, 8):
  54.             pygame.draw.line(self.tile, NIGHT, (linePos, 0), (linePos - self.tileSize, self.tileSize), 1)
  55.        
  56.         #self.tile.convert()       
  57.         self.displayWidthInTiles = (W / self.tileSize) * self.tileSize
  58.         if W % self.tileSize > 0: self.displayWidthInTiles += self.tileSize
  59.  
  60.         self.displayHeightInTiles = (H / self.tileSize) * self.tileSize
  61.         if H % self.tileSize > 0: self.displayHeightInTiles += self.tileSize
  62.  
  63.         print self.displayWidthInTiles, self.displayHeightInTiles
  64.  
  65.     def fillDisplay(self, displaySurf):
  66.         global AREAS, W, H
  67.         for tilePosY in range(0, self.displayHeightInTiles, self.tileSize):
  68.             for tilePosX in range(0, self.displayWidthInTiles, self.tileSize):
  69.                 displaySurf.blit(self.tile, (tilePosX, tilePosY))
  70.         AREAS.append((0, 0, W, H))
  71.        
  72. class blockZone:
  73.     def __init__(self):
  74.         global BLOCKS, W, H
  75.        
  76.         #define the area in which the blocks fall and stack in
  77.         self.height = (int(H * 0.9722222222222222) / 20) * 20
  78.         self.topMargin = (H - self.height) / 2     
  79.  
  80.         self.blockSize = int(float(self.height) / 20)
  81.        
  82.         self.width = self.blockSize * 10
  83.         self.leftMargin = (W - self.width) / 2
  84.        
  85.         self.bzSurf = pygame.Surface((self.width, self.height))
  86.    
  87.         self.stack = [[0 for y in range(20)] for x in range(10)]
  88.        
  89.         self.currentBlock = 7
  90.         self.rotation = 3
  91.         self.blockPosX = 5
  92.         self.blockPosY = BLOCKS[self.currentBlock]['startingY'][self.rotation]
  93.        
  94.     def blitToDisplay(self, displaySurf):
  95.         global AREAS
  96.         displaySurf.blit(self.bzSurf, (self.leftMargin, self.topMargin))
  97.         AREAS.append((self.leftMargin, self.topMargin, self.width, self.height))
  98.    
  99.     def drawStack(self):
  100.         global BLOCK_COLORS
  101.         for x in range(10):
  102.             for y in range(20):
  103.                 pygame.draw.rect(self.bzSurf, BLOCK_COLORS[self.stack[x][y]], (x * self.blockSize, y * self.blockSize, self.blockSize, self.blockSize), 0)
  104.        
  105.     def drawBlock(self, delete = False):
  106.         global BLOCKS, BLOCK_COLORS
  107.        
  108.         block = BLOCKS[self.currentBlock]['block'][self.rotation]
  109.         if delete:
  110.             blockID = 0
  111.         else:
  112.             blockID = self.currentBlock
  113.            
  114.         for bit in block:
  115.             y = self.blockPosY + bit[1]
  116.             self.stack[self.blockPosX + bit[0]][y] = blockID
  117.    
  118.     def fall(self):
  119.         self.drawBlock(True)
  120.         self.blockPosY += 1
  121.         #self.testCollision()
  122.         self.drawBlock()
  123.    
  124.     def rotateBlock(self):
  125.         global BLOCKS
  126.         self.rotation += 1
  127.         self.rotation %= len(BLOCKS[self.currentBlock]['block']) - 1
  128.    
  129. # define display surface           
  130. W, H = 1280, 720
  131. HW, HH = W / 2, H / 2
  132.  
  133. # initialise display
  134. pygame.init()
  135. FONT = pygame.font.SysFont(None, 72)
  136. DS = pygame.display.set_mode((W, H))
  137. pygame.display.set_caption("Tetris Clone")
  138. FPS = 1
  139. SPF = 1.00 / FPS
  140.  
  141. # the areas of the display that need updating
  142. AREAS = []
  143.  
  144. # create the background tiles instance
  145. TILES = tiles()
  146. TILES.fillDisplay(DS)
  147.  
  148. BZ = blockZone()
  149. BZ.drawBlock()
  150.  
  151. # start FPS monitoring
  152. FPSTime = time.time()
  153.  
  154. # main loop
  155. while not quit():
  156.     k = pygame.key.get_pressed()
  157.     if k[K_DOWN]:
  158.         SPF = 1.00 / (FPS * 20)
  159.     else:
  160.         SPF = 1.00 / FPS
  161.  
  162.     if k[K_UP]:
  163.         BZ.drawBlock(True)
  164.         BZ.rotateBlock()
  165.         BZ.drawBlock()
  166.    
  167.     if k[K_RETURN]:
  168.         pygame.image.save(DS, "screenshot.png")
  169.         pygame.quit()
  170.         sys.exit()
  171.        
  172.     BZ.drawStack()
  173.     BZ.blitToDisplay(DS)
  174.  
  175.     if time.time() > FPSTime + SPF:
  176.         BZ.fall()
  177.         FPSTime = time.time()
  178.    
  179.     pygame.display.update(AREAS)
  180.     AREAS = []
  181.        
  182.     #DS.fill(BLACK)
  183. pygame.quit()
  184. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement