This is my version of the popular game of tetris.
import math import pygame import numpy as np# for arrays import time import random WIDTH, HEIGHT = 800, 800 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Tetris") boardSizeX = 10 boardSizeY = 20 cellSize = 40 gridStart = WIDTH/2 - boardSizeX*cellSize/2 red = 255, 0, 0 grey = 128, 128, 128 shapeL = np.zeros((4,4)) shapeL[1, 0] = 1 shapeL[1, 1] = 1 shapeL[1, 2] = 1 shapeL[2, 2] = 1 shapeLR = np.zeros((4,4)) shapeLR[2, 0] = 1 shapeLR[2, 1] = 1 shapeLR[2, 2] = 1 shapeLR[1, 2] = 1 shapeS = np.zeros((4,4)) shapeS[2, 1] = 1 shapeS[1, 1] = 1 shapeS[1, 2] = 1 shapeS[0, 2] = 1 shapeSR = np.zeros((4,4)) shapeSR[0, 1] = 1 shapeSR[1, 1] = 1 shapeSR[1, 2] = 1 shapeSR[2, 2] = 1 shapeCube = np.zeros((4,4)) shapeCube[1, 1] = 1 shapeCube[2, 1] = 1 shapeCube[1, 2] = 1 shapeCube[2, 2] = 1 shapeI = np.zeros((4,4)) shapeI[1, 0] = 1 shapeI[1, 1] = 1 shapeI[1, 2] = 1 shapeI[1, 3] = 1 shapes = [shapeL, shapeLR, shapeS, shapeSR, shapeCube, shapeI] currentshape = shapes[random.randint(0, 5)] shapePos = [4, 0] envList = np.zeros((boardSizeX, boardSizeY)) # +2 is for the edges canmovedown = 1 canmoveleft = 1 canmoveright = 1 newshape = [] canrotate = 1 def renderSquare(cellx, celly, color): pygame.draw.rect(WIN, color, pygame.Rect(gridStart+cellSize*cellx+1, cellSize*celly+1, cellSize-1, cellSize-1)) def DrawList(List, x, y, color): # insert list1 to list at x, y for xi in range(0, len(List)): for yi in range(0, len(List[xi])): if List[xi, yi] == 1: renderSquare(x+xi, y+yi, color) def drawGrid(): for rowsx in range(0, boardSizeX+1): pygame.draw.line(WIN, (255, 255, 255), (gridStart + cellSize*rowsx, 0 ), (gridStart + cellSize*rowsx, HEIGHT )) for rowsy in range(0, boardSizeY+1): pygame.draw.line(WIN, (255, 255, 255), (gridStart ,rowsy * cellSize), (gridStart + boardSizeX*cellSize, rowsy * cellSize)) def rotateShapeCounterClockwise(): global currentshape canrotate = 1 newshape = np.zeros((4, 4)) newshape[3, 0] = currentshape[0, 0] newshape[3, 1] = currentshape[1, 0] newshape[3, 2] = currentshape[2, 0] newshape[3, 3] = currentshape[3, 0] newshape[2, 0] = currentshape[0, 1] newshape[2, 1] = currentshape[1, 1] newshape[2, 2] = currentshape[2, 1] newshape[2, 3] = currentshape[3, 1] newshape[1, 0] = currentshape[0, 2] newshape[1, 1] = currentshape[1, 2] newshape[1, 2] = currentshape[2, 2] newshape[1, 3] = currentshape[3, 2] newshape[0, 1] = currentshape[0, 3] newshape[0, 1] = currentshape[1, 3] newshape[0, 2] = currentshape[2, 3] newshape[0, 3] = currentshape[3, 3] for xi in range(0, len(newshape)): for yi in range(0, len(newshape[xi])): if newshape[xi, yi] == 1: if shapePos[0]+xi > boardSizeX-1 or shapePos[0]+xi < 1 or envList[shapePos[0] + xi, shapePos[1] + yi] == 1: canrotate = 0 if canrotate == 1: currentshape = newshape run = True while run: waitedTime = 0 startTime = time.time() while 0.150 - waitedTime > 0: event = pygame.event.wait(int(150 - waitedTime)) canmovedown = 1 canmoveleft = 1 canmoveright = 1 for xi in range(0, len(currentshape)): for yi in range(0, len(currentshape[xi])): if currentshape[xi, yi] == 1: if shapePos[1] + yi + 2 > boardSizeY or envList[shapePos[0] + xi, shapePos[1] + yi + 1] == 1 or canmovedown == 0: canmovedown = 0 if shapePos[0] + xi < 1 or envList[shapePos[0] + xi - 1, shapePos[1] + yi] == 1: canmoveleft = 0 if shapePos[0] + xi + 2 > boardSizeX or envList[shapePos[0] + xi + 1, shapePos[1] + yi] == 1: canmoveright = 0 #print(canmovedown, canmoveleft, canmoveright) if event.type == pygame.QUIT: run = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT and canmoveleft == 1: shapePos[0] -= 1 if event.key == pygame.K_RIGHT and canmoveright == 1: shapePos[0] += 1 if event.key == pygame.K_UP and canrotate == 1: rotateShapeCounterClockwise() WIN.fill((0, 0, 0)) drawGrid() DrawList(envList, 0, 0, grey) DrawList(currentshape, shapePos[0], shapePos[1], red) pygame.display.update() waitedTime = time.time() - startTime if canmovedown == 1: shapePos[1] += 1 else: for xi in range(0, len(currentshape)): for yi in range(0, len(currentshape[xi])): if currentshape[xi, yi] == 1: envList[shapePos[0]+xi, shapePos[1]+yi] = 1 currentshape = shapes[random.randint(0, 5)] shapePos = [4, 0] canmovedown = 1 canmoveleft = 1 canmoveright = 1 for ys in range(0, len(envList[0])): inarow = 0 for xs in range(0, len(envList)): if envList[xs, ys] == 1: inarow += 1 if inarow == 10: for y in reversed(range(1, ys+1)): print(y) for x in range(0, len(envList)): print(x, y, envList[x, y]) envList[x, y] = envList[x, y-1] WIN.fill((0, 0, 0)) drawGrid() DrawList(envList, 0, 0, grey) DrawList(currentshape, shapePos[0], shapePos[1], red) pygame.display.update() pygame.quit()