From 4d6f741011d021992a9f338f50902e93d29ae2f0 Mon Sep 17 00:00:00 2001 From: Jon Bergland Date: Tue, 2 Apr 2024 20:05:26 +0200 Subject: [PATCH] feat: :sparkles: work done on 02.04 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Håvard Fossdal Co-authored-by: Eduard Prokhorikhin Co-authored-by: Sindre Fossdal --- src/game/TetrisGameManager.py | 88 +++++++++-------------- src/game/block.py | 78 +++++++++++++++++--- src/game/board.py | 130 +++++++++++++++++++++++++--------- 3 files changed, 197 insertions(+), 99 deletions(-) diff --git a/src/game/TetrisGameManager.py b/src/game/TetrisGameManager.py index 2134048..f444af6 100644 --- a/src/game/TetrisGameManager.py +++ b/src/game/TetrisGameManager.py @@ -32,6 +32,7 @@ def __init__(self, board): self.board = board self.score = 0 self.currentTime = int(round(t.time() * 1000)) + self.board.setGameOver(False) self.switcher = { Key.down: lambda: self.movePiece("DOWN"), @@ -55,8 +56,7 @@ def onPress(self, key): # Get the function to execute based on the key, or default action action = self.switcher.get(key, default_action) action() - print("_____________________________________________________") - self.board.printBoard() + #self.board.printBoard() #print(action) # Execute the function #return action @@ -65,28 +65,27 @@ def onRelease(self, key): pass def rotatePiece(self, direction): - pass + if direction == "UP": + self.board.rotateBlockRight() + self.board.printBoard() # self.currentPiece.rotate(direction) def movePiece(self, direction): - print("Moving piece") print(direction) - if self.legalMove(): - if direction == "DOWN": - self.board.moveBlockDown() - elif direction == "LEFT": - self.board.moveBlockLeft() - elif direction == "RIGHT": - self.board.moveBlockRight() + if direction == "DOWN": + self.board.moveBlockDown() + elif direction == "LEFT": + self.board.moveBlockLeft() + elif direction == "RIGHT": + self.board.moveBlockRight() - # if self.legalMove(): - # self.board.moveBlockDown(direction) + self.board.printBoard() def dropPiece(self, newPiece): self.movePiece("DOWN") def isGameOver(self): - return self.board.validMove() + return self.board.isGameOver() #return self.board.isGameOver() def startGame(self): @@ -94,38 +93,20 @@ def startGame(self): self.nextPiece = self.newPiece() self.board.printBoard() - with Listener( - on_press=self.onPress, - on_release=self.onRelease) as listener: - listener.join() - while not self.isGameOver(): - # action = 1#input("Enter action: ") ## valid commands: [moveLeft, moveRight, moveDown, softDrop, hardDrop, quitGame, rotateLeft, rotateRight, rotate180] - # if action == "moveLeft" and self.legalMove(): - # self.movePiece(LEFT) - # elif action == "moveRight" and self.legalMove(): - # self.movePiece(RIGHT) - # elif action == "moveDown" and self.legalMove(): - # self.dropPiece(DOWN) - # elif action == "softDrop": - # self.softDrop() - # elif action == "h": - # self.hardDrop() - # elif action == "rotateLeft": - # self.rotatePiece(-1) - # elif action == "rotateRight": - # self.rotatePiece(1) - # elif action == "rotate180": - # self.rotatePiece(2) - # elif action == "q": - # self.stopGame() - # break - # else: - # self.checkTimer() - - t.sleep(0.1) # Add a small delay to reduce CPU usage + listener = Listener( + on_press=self.onPress, + on_release=self.onRelease) + listener.start() + + while not self.board.gameOver: + + #self.checkTimer() - # Stop the listener when the game is over - listener.stop() + t.sleep(0.1) # Add a small delay to reduce CPU usage + + # Stop the listener when the game is over + print("Stopping listener") + listener.stop() @@ -133,9 +114,6 @@ def newPiece(self): pass #return self.pieces.getNewPiece() - def legalMove(self): - return self.board.validMove() - # def clearLines(self): # linesCleared = self.board.checkGameState() # if linesCleared == 4: @@ -168,7 +146,7 @@ def placePiece(self, direction): else: self.movePiece(DOWN) return False - if self.isGameOver(): + if self.board.gameOver: self.stopGame() return True clearLines = self.board.checkGameState() @@ -179,19 +157,21 @@ def placePiece(self, direction): def checkTimer(self): - checkTime = self.currentTime + 1000 + checkTime = self.currentTime + 1000/self.updateTimer newTime = int(round(t.time() * 1000)) - if (checkTime > newTime): - self.movePiece("DOWN") - print("Timer checked") + if (checkTime < newTime): + self.currentTime = newTime + #self.movePiece("DOWN") + #print("Timer checked") + #self.board.printBoard() return True def stopGame(self): #print("Game Over") + self.board.setGameOver(True) sys.exit() - self.board.stop_game() if __name__ == "__main__": board = Board() diff --git a/src/game/block.py b/src/game/block.py index 867655d..7a26aca 100644 --- a/src/game/block.py +++ b/src/game/block.py @@ -44,21 +44,79 @@ def set_coordinates(self, x, y): self.x = x self.y = y - def rotate_left(self): - self.rotation = (self.rotation + 1) % len(self.figures[self.type]) + def rotate_left(self, undo: bool = False): + if not undo: + self.rotation = (self.rotation - 1) % len(self.figures[self.type]) + else: + self.rotate_right() + - def rotate_right(self): - self.rotation = (self.rotation - 1) % len(self.figures[self.type]) + def rotate_right(self, undo: bool = False): + if not undo: + self.rotation = (self.rotation + 1) % len(self.figures[self.type]) + else: + self.rotate_left() def image(self): return self.figures[self.type][self.rotation] - def move_down(self): - self.y += 1 + def move_up(self, undo: bool = False): + if not undo: + self.y -= 1 + else: + self.move_down() + + def move_down(self, undo: bool = False): + if not undo: + self.y += 1 + else: + self.move_up() - def move_left(self): - self.x -= 1 + def move_left(self, undo: bool = False): + if not undo: + self.x -= 1 + else: + self.move_right() + + def move_right(self, undo: bool = False): + if not undo: + self.x += 1 + else: + self.move_left() + + def getBottom(): + pass + + + def getListCoordinates(self): + imageList = self.image() + listCoordinates = [] + for i in range(len(imageList)): + x = 0 + y = 0 + listNr = imageList[i] + restList = imageList[i] % 4 + divList = imageList[i] // 4 + + if restList == 0: + y = self.y + divList - 1 + x = self.x - 1 + + elif restList == 1: + y = self.y + divList - 1 + x = self.x + + elif restList == 2: + y = self.y + divList - 1 + x = self.x + 1 + + elif restList == 3: + y = self.y + divList - 1 + x = self.x + 2 + + listCoordinates.append((x, y)) + + return listCoordinates - def move_right(self): - self.x += 1 + diff --git a/src/game/board.py b/src/game/board.py index 0c4874b..9373617 100644 --- a/src/game/board.py +++ b/src/game/board.py @@ -15,7 +15,8 @@ class Board: gameOver = False rowsRemoved = 0 board=[] - block = Block(0,5, random.randint(0,6)) #random.randint(0,6) er for å velge en tilfeldig brikke + block= None + # block = Block(0,5, random.randint(0,6)) #random.randint(0,6) er for å velge en tilfeldig brikke def __init__(self): for i in range(0,self.rows-1): @@ -24,44 +25,85 @@ def __init__(self): newLine.append(0) self.board.append(newLine) - self.block = Block(0,5, random.randint(0,6)) + self.prevBoard = self.board + self.block = Block(0,5, random.randint(0,6)) + self.placeBlock() def printBoard(self): + print("___________________________________") for row in self.board: - print((' ').join(str(row))) + print('|' + ' '.join(self.checkCharacter(cell) for cell in row) + '|') + + print("‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾") + + def checkCharacter(self, character): + if character == 1: + return '■' + else: + return '▧' + def rotateBlockRight(self): - if self.block.rotateRight().validMove(): - self.placeBlock(self.block.rotate_right()) + if self.validMove(self.block.rotate_right): + self.placeBlock() + def moveBlockDown(self): - if self.block.moveDown().validMove(): - self.placeBlock(self.block.move_down()) + if self.validMove(self.block.move_down): + self.placeBlock() def moveBlockLeft(self): - if self.block.moveLeft().validMove(): - self.placeBlock(self.block.move_left()) + if self.validMove(self.block.move_left): + self.placeBlock() + def moveBlockRight(self): - if self.block.moveRight().validMove(): - self.placeBlock(self.block.move_right()) + if self.validMove(self.block.move_right): + self.placeBlock() + def rotateBlockLeft(self): - if self.block.rotateLeft().validMove(): - self.placeBlock(self.block.rotate_left()) + if self.validMove(self.block.rotate_left): + self.placeBlock() def gameOver(self): return self.gameOver - def validMove(self): + def setGameOver(self,state): + self.gameOver = state + + + + + + def blockLanded(self): + + pass + + + + + + # def validMove(self): + # for i in range(4): + # for j in range(4): + # if i * 4 + j in self.block.image(): + # if i + self.block.y > self.rows - 1 or \ + # j + self.block.x > self.columns - 1 or \ + # j + self.block.x < 0 or \ + # self.board[i + self.block.y][j + self.block.x] > 0: + # return False # Returnerer False ved kollisjon eller ugyldig trekk + + def validMove(self, f): + f() for i in range(4): for j in range(4): if i * 4 + j in self.block.image(): @@ -69,7 +111,10 @@ def validMove(self): j + self.block.x > self.columns - 1 or \ j + self.block.x < 0 or \ self.board[i + self.block.y][j + self.block.x] > 0: - return False # Returnerer False ved kollisjon eller ugyldig trekk + + return True + f(undo=True) + return False # Return True if the move is valid def clearRow(self, rownumber): @@ -92,35 +137,50 @@ def checkGameState(self): self.clearRow(rowIndex) # Fjerner raden basert på dens indeks amount += 1 # Øker telleren for antall fjernede rader return amount # Returnerer totalt antall fjernede rader - - def placeBlock(self): - if not self.block.validMove(): - for i in range(4): - for j in range(4): - if i * 4 + j in self.block.image(): - self.board[i + self.block.x][j + self.block.y] = 1 #self.block.color - return self.checkGameState() #hvis denne sjekkes hver gang og gior false, var det ikke mulig å plassere blokken og spillet er over ffs. - + + + def placeBlock(self): + self.board = self.prevBoard + # if self.validMove(): + for i in range(4): + for j in range(4): + if i * 4 + j in self.block.image(): + self.board[i + self.block.y][j + self.block.x] = 1 #self.block.color + return self.checkGameState() #hvis denne sjekkes hver gang og gir false, var det ikke mulig å plassere blokken og spillet er over ffs. + + def newBlock(self): + self.block = Block(0,5, random.randint(0,6)) + + for i in range(4): + for j in range(4): + if i * 4 + j in self.block.image(): + self.board[i + self.block.y][j + self.block.x] = 1 #self.block.color + return self.checkGameState() #hvis denne sjekkes hver gang og gir false, var det ikke mulig å plassere blokken og spillet er over ffs. + + def newBlock(self): + self.block = Block(0,5, random.randint(0,6)) + + for i in range(4): + for j in range(4): + if i * 4 + j in self.block.image(): + self.board[i + self.block.y][j + self.block.x] = 1 #self.block.color + return self.checkGameState() #hvis denne sjekkes hver gang og gir false, var det ikke mulig å plassere blokken og spillet er over ffs. + + def newBlock(self): + self.block = Block(0,5, random.randint(0,6)) + + + - - - - - - - - - - - +