Skip to content

Commit

Permalink
Merge pull request #13 from CogitoNTNU/12-make-tetris-game-integration
Browse files Browse the repository at this point in the history
feat: ✨ merged all the files for the game
  • Loading branch information
Eduard-Prokhorikhin authored Apr 2, 2024
2 parents 9eb7a0d + 5a005dd commit c031af8
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 155 deletions.
125 changes: 83 additions & 42 deletions src/game/TetrisGameManager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from pynput.keyboard import Key, Listener
import time as t
from Board import Board
import sys


baseScore = 100
DOWN = (0, 1)
Expand All @@ -14,7 +17,6 @@
soft drop and hard drop implementation
"""


class TetrisGameManager:

currentPiece = None
Expand All @@ -23,6 +25,7 @@ class TetrisGameManager:
updateTimer = 1
streak = 1
currentTime = None
board = None

def __init__(self, board):
self.board = board
Expand All @@ -35,65 +38,99 @@ def __init__(self, board):


def onPress(self, key):

switcher = {

Key.down: self.movePiece("DOWN"),
Key.left: self.movePiece("LEFT"),
Key.right: self.movePiece("RIGHT"),
Key.space: "HardDrop",
Key.esc: self.stopGame(),
Key.up: self.rotatePiece("UP"),
}


# Default action if key not found
default_action = lambda: "Key not recognized"

# Get the function to execute based on the key, or default action
switcher.get(key, default_action)
print("_____________________________________________________")
self.board.printBoard()
#print(action)
# Execute the function
#return action

def onRelease(self, key):
pass


def rotatePiece(self, direction):
self.currentPiece.rotate(direction)

pass
# self.currentPiece.rotate(direction)

def movePiece(self, direction):
if self.legalMove():
if direction == "DOWN":
self.board.moveBlockDown()
elif direction == "LEFT":
self.board.moveBlockLeft()
elif direction == "RIGHT":
self.board.moveBlockRight()

if self.legalMove(direction):
self.currentPiece.move(direction)
# if self.legalMove():
# self.board.moveBlockDown(direction)

def dropPiece(self, newPiece):
self.movePiece(DOWN)
self.movePiece("DOWN")

def isGameOver(self):
return self.board.isGameOver()
return self.board.validMove()
#return self.board.isGameOver()

def startGame(self):
self.currentPiece = self.newPiece()
self.nextPiece = self.newPiece()

while not self.isGameOver():
action = input("Enter action: ") ## valid commands: [moveLeft, moveRight, moveDown, softDrop, hardDrop, quitGame, rotateLeft, rotateRight, rotate180]
if action == "moveLeft" and self.legalMove(LEFT):
self.movePiece(LEFT)
elif action == "moveRight" and self.legalMove(RIGHT):
self.movePiece(RIGHT)
elif action == "moveDown" and self.legalMove(DOWN):
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.moveDown()

self.board.printBoard()

with Listener(
on_press=self.onPress,
on_release=self.onRelease) as listener:
listener.join()
while not self.isGameOver():
#action = 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

# Stop the listener when the game is over
listener.stop()



def newPiece(self):
return self.pieces.getNewPiece()
pass
#return self.pieces.getNewPiece()

def legalMove(self, x, y):
return self.board.legalMove(x, y)
def legalMove(self):
return self.board.validMove()

# def clearLines(self):
# linesCleared = self.board.checkGameState()
Expand Down Expand Up @@ -141,15 +178,19 @@ def checkTimer(self):
checkTime = self.currentTime + 1000
newTime = int(round(t.time() * 1000))
if (checkTime > newTime):
self.movePiece(DOWN)
self.movePiece("DOWN")
print("Timer checked")


return True

def stopGame(self):
print("Game Over")
sys.exit()
self.board.stop_game()

if __name__ == "__main__":
board = Board()
game = TetrisGameManager(board)
game.startGame()

if __name__ == "__main__":
millisec = int(round(t.time() * 1000))
print(millisec)
52 changes: 24 additions & 28 deletions src/game/block.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import random

import random

class Block:

def __init__(self, x, y, blockType):
self.x = x
self.y = y
self.type = random.randint(0, 6)

self.color = self.colors[random.randint(0,6)]
# self.type = random.randint(0,6)
self.type = blockType
self.color = self.colors[blockType]
# self.color = random.choice(self.colors)
# self.rotation = random.randint(0, len(self.figures[self.type]) - 1)
self.rotation = 0


Expand All @@ -28,41 +39,26 @@ def __init__(self, x, y, blockType):
(255, 255, 0) # O
]


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_right(self):
self.rotation = (self.rotation - 1) % len(self.figures[self.type])

def get_block(self):
return self.figures[self.type][self.rotation]

def get_color(self):
return self.colors[self.type]

def get_x(self):
return self.x

def get_y(self):
return self.y
def image(self): return self.figures[self.type][self.rotation]

def move_down(self):
self.y += 1

def set_x(self, x):
self.x = x
def move_left(self):
self.x -= 1

def set_y(self, y):
self.y = y
def move_right(self):
self.x += 1

def get_position(self):
return self.x, self.y

def image(self):
return self.figures[self.type][self.rotation]

def get_block_coordinates(self):
# Calculate the coordinates for each block in the figure
positions = self.figures[self.type][self.rotation]
for i in range(4):
for j in range(4):
return
# If the block is in the figure, calculate the coordinates
## TODO: Fix the coordinates
Loading

0 comments on commit c031af8

Please sign in to comment.