Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, random, sys, time
- import pygame
- import pygame.gfxdraw
- from pygame.locals import *
- # define some colors
- FUCHSIA = (255, 0, 255)
- PURPLE = (128, 0, 128)
- TEAL = ( 0, 128, 128)
- LIME = ( 0, 255, 0)
- GREEN = ( 0, 128, 0)
- OLIVE = (128, 128, 0)
- YELLOW = (255, 255, 0)
- ORANGE = (255, 165, 0)
- RED = (255, 0, 0)
- MAROON = (128, 0, 0)
- SILVER = (192, 192, 192)
- GREY = (128, 128, 128)
- NIGHT = (32, 32, 32)
- BLUE = ( 0, 0, 255)
- NAVY = ( 0, 0, 128)
- AQUA = ( 0, 255, 255)
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- # define some data
- BLOCKS = {1: {'startingY' : [0, 2], 'block' : [[(-1, 0), (0, 0), (1, 0), (2, 0)], [(0, -2), (0, -1), (0, 0), (0, 1)]]}, # long one
- 2: {'startingY' : [1], 'block' : [[(-1, -1), (0, -1), (-1, 0), (0, 0)]]}, # square
- 3: {'startingY' : [1, 0], 'block' : [[(0, -1), (-1, 0), (0, 0), (-1, 1)], [(-1, 0), (0, 0), (0, 1), (1, 1)]]}, # lightening 1
- 4: {'startingY' : [1, 0], 'block' : [[(-1, -1), (-1, 0), (0, 0), (0, 1)], [(0, 0), (1, 0), (0, 1), (-1, 1)]]}, # lightening 2
- 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)]]},
- 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)]]},
- 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)]]},
- }
- BLOCK_COLORS = [BLACK, NAVY, YELLOW, RED, PURPLE, GREEN, ORANGE, TEAL]
- # exit the program
- def quit():
- for event in pygame.event.get():
- if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
- return True
- return False
- # draw a patterned title
- class tiles:
- def __init__(self):
- global NIGHT, W, H
- self.tileSize = 32
- self.tile = pygame.Surface((self.tileSize, self.tileSize))
- for linePos in range(0, self.tileSize * 2, 8):
- pygame.draw.line(self.tile, NIGHT, (linePos, 0), (linePos - self.tileSize, self.tileSize), 1)
- #self.tile.convert()
- self.displayWidthInTiles = (W / self.tileSize) * self.tileSize
- if W % self.tileSize > 0: self.displayWidthInTiles += self.tileSize
- self.displayHeightInTiles = (H / self.tileSize) * self.tileSize
- if H % self.tileSize > 0: self.displayHeightInTiles += self.tileSize
- print self.displayWidthInTiles, self.displayHeightInTiles
- def fillDisplay(self, displaySurf):
- global AREAS, W, H
- for tilePosY in range(0, self.displayHeightInTiles, self.tileSize):
- for tilePosX in range(0, self.displayWidthInTiles, self.tileSize):
- displaySurf.blit(self.tile, (tilePosX, tilePosY))
- AREAS.append((0, 0, W, H))
- class blockZone:
- def __init__(self):
- global BLOCKS, W, H
- #define the area in which the blocks fall and stack in
- self.height = (int(H * 0.9722222222222222) / 20) * 20
- self.topMargin = (H - self.height) / 2
- self.blockSize = int(float(self.height) / 20)
- self.width = self.blockSize * 10
- self.leftMargin = (W - self.width) / 2
- self.bzSurf = pygame.Surface((self.width, self.height))
- self.stack = [[0 for y in range(20)] for x in range(10)]
- self.currentBlock = 7
- self.rotation = 3
- self.blockPosX = 5
- self.blockPosY = BLOCKS[self.currentBlock]['startingY'][self.rotation]
- def blitToDisplay(self, displaySurf):
- global AREAS
- displaySurf.blit(self.bzSurf, (self.leftMargin, self.topMargin))
- AREAS.append((self.leftMargin, self.topMargin, self.width, self.height))
- def drawStack(self):
- global BLOCK_COLORS
- for x in range(10):
- for y in range(20):
- pygame.draw.rect(self.bzSurf, BLOCK_COLORS[self.stack[x][y]], (x * self.blockSize, y * self.blockSize, self.blockSize, self.blockSize), 0)
- def drawBlock(self, delete = False):
- global BLOCKS, BLOCK_COLORS
- block = BLOCKS[self.currentBlock]['block'][self.rotation]
- if delete:
- blockID = 0
- else:
- blockID = self.currentBlock
- for bit in block:
- y = self.blockPosY + bit[1]
- self.stack[self.blockPosX + bit[0]][y] = blockID
- def fall(self):
- self.drawBlock(True)
- self.blockPosY += 1
- #self.testCollision()
- self.drawBlock()
- def rotateBlock(self):
- global BLOCKS
- self.rotation += 1
- self.rotation %= len(BLOCKS[self.currentBlock]['block']) - 1
- # define display surface
- W, H = 1280, 720
- HW, HH = W / 2, H / 2
- # initialise display
- pygame.init()
- FONT = pygame.font.SysFont(None, 72)
- DS = pygame.display.set_mode((W, H))
- pygame.display.set_caption("Tetris Clone")
- FPS = 1
- SPF = 1.00 / FPS
- # the areas of the display that need updating
- AREAS = []
- # create the background tiles instance
- TILES = tiles()
- TILES.fillDisplay(DS)
- BZ = blockZone()
- BZ.drawBlock()
- # start FPS monitoring
- FPSTime = time.time()
- # main loop
- while not quit():
- k = pygame.key.get_pressed()
- if k[K_DOWN]:
- SPF = 1.00 / (FPS * 20)
- else:
- SPF = 1.00 / FPS
- if k[K_UP]:
- BZ.drawBlock(True)
- BZ.rotateBlock()
- BZ.drawBlock()
- if k[K_RETURN]:
- pygame.image.save(DS, "screenshot.png")
- pygame.quit()
- sys.exit()
- BZ.drawStack()
- BZ.blitToDisplay(DS)
- if time.time() > FPSTime + SPF:
- BZ.fall()
- FPSTime = time.time()
- pygame.display.update(AREAS)
- AREAS = []
- #DS.fill(BLACK)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement