Skip to content

Commit

Permalink
feat: ✨ work done on 02.04
Browse files Browse the repository at this point in the history
Co-authored-by: Håvard Fossdal <[email protected]>
Co-authored-by: Eduard Prokhorikhin <[email protected]>
Co-authored-by: Sindre Fossdal
<[email protected]>
  • Loading branch information
JonBergland committed Apr 2, 2024
1 parent ecd4638 commit 4d6f741
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 99 deletions.
88 changes: 34 additions & 54 deletions src/game/TetrisGameManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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
Expand All @@ -65,77 +65,55 @@ 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):
self.currentPiece = self.newPiece()
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()



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:
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
78 changes: 68 additions & 10 deletions src/game/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Loading

0 comments on commit 4d6f741

Please sign in to comment.