Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 make tetris game manager #6

Merged
merged 8 commits into from
Mar 19, 2024
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pybricks==3.2.0
pycparser==2.21
pygame==2.5.2
Pygments==2.16.1
pynput==1.7.6
pyparsing==3.1.1
python-dateutil==2.8.2
python-json-logger==2.0.7
Expand Down
155 changes: 155 additions & 0 deletions src/game/TetrisGameManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
from pynput.keyboard import Key, Listener
import time as t

baseScore = 100
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

""" TODO: Timer for piece drop
keyboard input for piece movement
keyboard input for piece rotation
keyboard input for piece drop
keyboard input for game start
soft drop and hard drop implementation
"""


class TetrisGameManager:

currentPiece = None
nextPiece = None
score = 0
updateTimer = 1
streak = 1
currentTime = None

def __init__(self, board):
self.board = board
self.score = 0
self.currentTime = int(round(t.time() * 1000))

# while True:
# with Listener(on_press=self.on_press, on_release=self.on_release) as listener:
# listener.join()


def onPress(self, key):
switcher = {

}

def onRelease(self, key):
pass


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


def movePiece(self, direction):

if self.legalMove(direction):
self.currentPiece.move(direction)

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

def isGameOver(self):
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()




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

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

# def clearLines(self):
# linesCleared = self.board.checkGameState()
# if linesCleared == 4:
# self.streak += 1
# else:
# self.streak = 1


def updateScore(self, linesCleared):
self.score += self.streak*(baseScore**linesCleared)

def softDrop(self):
if self.legalMove(DOWN):
self.dropPiece()
else:
self.placePiece(DOWN)

def hardDrop(self):
while self.legalMove(DOWN):
self.dropPiece()
self.placePiece(DOWN)

def placePiece(self, direction):
x = direction[0]
y = direction[1]
if not self.legalMove(x, y):
self.board.placePiece(x, y, self.currentPiece)
self.currentPiece = self.nextPiece
self.next_piece = self.nextPiece()
else:
self.movePiece(DOWN)
return False
if self.isGameOver():
self.stopGame()
return True
clearLines = self.board.checkGameState()
if clearLines:
self.board.clearLines(clearLines)
self.updateScore(clearLines)
return True


def checkTimer(self):
checkTime = self.currentTime + 1000
newTime = int(round(t.time() * 1000))
if (checkTime > newTime):
self.movePiece(DOWN)


return True

def stopGame(self):
self.board.stop_game()


if __name__ == "__main__":
millisec = int(round(t.time() * 1000))
print(millisec)
Loading