Skip to content

Commit

Permalink
feat: added basic pygame visuals
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Prokhorikhin committed Apr 22, 2024
1 parent db75d86 commit c5ab56e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/game/TetrisGameManager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
import pygame
from pygame.locals import *
import time as t
Expand All @@ -7,6 +8,18 @@

baseScore = 100

# pygame visuals setup
BLOCK_SIZE = 30
WIDTH = 10
HEIGHT = 23
START_HEIGHT = 3
SCREEN_WIDTH = WIDTH * BLOCK_SIZE
SCREEN_HEIGHT = (HEIGHT - START_HEIGHT) * BLOCK_SIZE

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)

class TetrisGameManager:
currentPiece = None
nextPiece = None
Expand All @@ -27,12 +40,14 @@ def isGameOver(self):

def startGame(self):
pygame.init()
self.screen = pygame.display.set_mode((80, 140)) # Create a dummy window
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# self.screen = pygame.display.set_mode((400, 1080)) # Create a dummy window
pygame.display.set_caption('Tetris') # Set window title

clock = pygame.time.Clock()

while not self.board.gameOver:
self.draw_board(self.board.board)
self.inputHandling()
if self.board.blockHasLanded:
self.board.updateBoard() # Update the board after a block has landed and spawn a new block
Expand Down Expand Up @@ -65,6 +80,17 @@ def checkTimer(self):
if checkTime < newTime:
self.currentTime = newTime
self.movePiece(Action.SOFT_DROP)

def draw_board(self, board):
self.screen.fill(BLACK)
temp = deepcopy(board)
temp = temp[START_HEIGHT:]
for y in range(HEIGHT-START_HEIGHT):
for x in range(WIDTH):
if temp[y][x] == 1:
pygame.draw.rect(self.screen, BLUE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(self.screen, WHITE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)


def stopGame(self):
pygame.quit()
Expand Down

0 comments on commit c5ab56e

Please sign in to comment.