From 07fd97c19e014de6eec3042042d2ccd000cca52d Mon Sep 17 00:00:00 2001 From: lll Date: Thu, 7 Jun 2018 16:10:42 -0700 Subject: [PATCH 1/7] fixed name inputbox to read consistent length --- Block/BlockI.py | 161 +++++++++++++++----------- Block/BlockJ.py | 68 +++++++---- Block/BlockL.py | 80 ++++++++----- Block/BlockO.py | 3 + Block/BlockS.py | 4 + Block/BlockT.py | 3 + Block/BlockZ.py | 2 + Metris.py | 256 ++++++++++++++++++++++++++--------------- inputbox.py | 15 ++- sugargame/__init__.py | 1 + sugargame/canvas.py | 86 ++++++++++++++ sugargame/event.py | 258 ++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 715 insertions(+), 222 deletions(-) create mode 100644 sugargame/__init__.py create mode 100644 sugargame/canvas.py create mode 100644 sugargame/event.py diff --git a/Block/BlockI.py b/Block/BlockI.py index 8937e99..f2a2a12 100644 --- a/Block/BlockI.py +++ b/Block/BlockI.py @@ -80,170 +80,177 @@ def rotateL(self): self.orientation += 1 - def rotateL2(self): + # rotation about index 1 + def rotateL4(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(-self.size + 2*self.size) - self.blockList[1].setRelativeY(2*self.size) #### + self.blockList[0].setRelativeY(-self.size) # + 2*self.size) + # self.blockList[1].setRelativeY(2*self.size) #### self.blockList[2].setRelativeX(-2*self.size) - self.blockList[2].setRelativeY(-2*self.size + 2*self.size) + self.blockList[2].setRelativeY(-2*self.size) # + 2*self.size) self.blockList[3].setRelativeX(-3*self.size) - self.blockList[3].setRelativeY(-3*self.size + 2*self.size) + self.blockList[3].setRelativeY(-3*self.size) # + 2*self.size) # move everything down 2 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(self.size - self.size) - self.blockList[1].setRelativeY(-self.size) #### + self.blockList[0].setRelativeY(self.size) # - self.size) + # self.blockList[1].setRelativeY(-self.size) #### self.blockList[2].setRelativeX(-2 * self.size) - self.blockList[2].setRelativeY(2 * self.size - self.size) + self.blockList[2].setRelativeY(2 * self.size) # - self.size) self.blockList[3].setRelativeX(-3 * self.size) - self.blockList[3].setRelativeY(3 * self.size - self.size) + self.blockList[3].setRelativeY(3 * self.size) # - self.size) # move everything up 1 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(self.size - 2*self.size) - self.blockList[1].setRelativeY(-2*self.size) #### + self.blockList[0].setRelativeY(self.size) # - 2*self.size) + # self.blockList[1].setRelativeY(-2*self.size) #### self.blockList[2].setRelativeX(2 * self.size) - self.blockList[2].setRelativeY(2 * self.size - 2*self.size) + self.blockList[2].setRelativeY(2 * self.size) # - 2*self.size) self.blockList[3].setRelativeX(3 * self.size) - self.blockList[3].setRelativeY(3 * self.size - 2*self.size) + self.blockList[3].setRelativeY(3 * self.size) # - 2*self.size) # move everything up 2 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(-self.size + self.size) - self.blockList[1].setRelativeY(self.size) #### + self.blockList[0].setRelativeY(-self.size) # + self.size) + # self.blockList[1].setRelativeY(self.size) #### self.blockList[2].setRelativeX(2 * self.size) - self.blockList[2].setRelativeY(-2 * self.size + self.size) + self.blockList[2].setRelativeY(-2 * self.size) # + self.size) self.blockList[3].setRelativeX(3 * self.size) - self.blockList[3].setRelativeY(-3 * self.size + self.size) + self.blockList[3].setRelativeY(-3 * self.size) # + self.size) # move everything down 1 self.orientation += 1 - def rotateR2(self): + # rotation about index 1 + def rotateR4(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(self.size - self.size) - self.blockList[1].setRelativeY(-self.size) #### + self.blockList[0].setRelativeY(self.size) # - self.size) + # self.blockList[1].setRelativeY(-self.size) #### self.blockList[2].setRelativeX(-2 * self.size) - self.blockList[2].setRelativeY(2 * self.size - self.size) + self.blockList[2].setRelativeY(2 * self.size) # - self.size) self.blockList[3].setRelativeX(-3 * self.size) - self.blockList[3].setRelativeY(3 * self.size - self.size) + self.blockList[3].setRelativeY(3 * self.size) # - self.size) # move everything up 1 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(self.size - 2*self.size) - self.blockList[1].setRelativeY(-2*self.size) #### + self.blockList[0].setRelativeY(self.size) # - 2*self.size) + # self.blockList[1].setRelativeY(-2*self.size) #### self.blockList[2].setRelativeX(2 * self.size) - self.blockList[2].setRelativeY(2 * self.size - 2*self.size) + self.blockList[2].setRelativeY(2 * self.size) # - 2*self.size) self.blockList[3].setRelativeX(3 * self.size) - self.blockList[3].setRelativeY(3 * self.size - 2*self.size) + self.blockList[3].setRelativeY(3 * self.size) # - 2*self.size) # move everything up 2 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(-self.size + self.size) - self.blockList[1].setRelativeY(self.size) #### + self.blockList[0].setRelativeY(-self.size) # + self.size) + # self.blockList[1].setRelativeY(self.size) #### self.blockList[2].setRelativeX(2 * self.size) - self.blockList[2].setRelativeY(-2 * self.size + self.size) + self.blockList[2].setRelativeY(-2 * self.size) # + self.size) self.blockList[3].setRelativeX(3 * self.size) - self.blockList[3].setRelativeY(-3 * self.size + self.size) + self.blockList[3].setRelativeY(-3 * self.size) # + self.size) # move everything down 1 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(-self.size + 2*self.size) - self.blockList[1].setRelativeY(2*self.size) #### + self.blockList[0].setRelativeY(-self.size) # + 2*self.size) + # self.blockList[1].setRelativeY(2*self.size) #### self.blockList[2].setRelativeX(-2 * self.size) - self.blockList[2].setRelativeY(-2 * self.size + 2*self.size) + self.blockList[2].setRelativeY(-2 * self.size) # + 2*self.size) self.blockList[3].setRelativeX(-3 * self.size) - self.blockList[3].setRelativeY(-3 * self.size + 2*self.size) + self.blockList[3].setRelativeY(-3 * self.size) # + 2*self.size) # move everything down 2 self.orientation -= 1 + # rotation about index 3 def rotateL3(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(2*self.size) - self.blockList[0].setRelativeY(2*self.size - self.size) + self.blockList[0].setRelativeY(2*self.size) # - self.size) self.blockList[1].setRelativeX(3*self.size) - self.blockList[1].setRelativeY(3*self.size - self.size) + self.blockList[1].setRelativeY(3*self.size) # - self.size) self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(self.size - self.size) - self.blockList[3].setRelativeY(-self.size) #### + self.blockList[2].setRelativeY(self.size) # - self.size) + # self.blockList[3].setRelativeY(-self.size) #### # move everything up 1 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(2 * self.size) - self.blockList[0].setRelativeY(-2 * self.size + 2*self.size) + self.blockList[0].setRelativeY(-2 * self.size) # + 2*self.size) self.blockList[1].setRelativeX(3 * self.size) - self.blockList[1].setRelativeY(-3 * self.size + 2*self.size) + self.blockList[1].setRelativeY(-3 * self.size) # + 2*self.size) self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(-self.size + 2*self.size) - self.blockList[3].setRelativeY(2*self.size) #### + self.blockList[2].setRelativeY(-self.size) # + 2*self.size) + # self.blockList[3].setRelativeY(2*self.size) #### # move everything down 2 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(-2 * self.size) - self.blockList[0].setRelativeY(-2 * self.size + self.size) + self.blockList[0].setRelativeY(-2 * self.size) # + self.size) self.blockList[1].setRelativeX(-3 * self.size) - self.blockList[1].setRelativeY(-3 * self.size + self.size) + self.blockList[1].setRelativeY(-3 * self.size) # + self.size) self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(-self.size + self.size) - self.blockList[3].setRelativeY(self.size) #### + self.blockList[2].setRelativeY(-self.size) # + self.size) + # self.blockList[3].setRelativeY(self.size) #### # move everything down 1 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(-2 * self.size) - self.blockList[0].setRelativeY(2 * self.size - 2*self.size) + self.blockList[0].setRelativeY(2 * self.size) # - 2*self.size) self.blockList[1].setRelativeX(-3 * self.size) - self.blockList[1].setRelativeY(3 * self.size - 2*self.size) + self.blockList[1].setRelativeY(3 * self.size) # - 2*self.size) self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(self.size - 2*self.size) - self.blockList[3].setRelativeY(-2*self.size) #### + self.blockList[2].setRelativeY(self.size) # - 2*self.size) + # self.blockList[3].setRelativeY(-2*self.size) #### # move everything up 2 self.orientation += 1 + # rotation about index 3 def rotateR3(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(2 * self.size) - self.blockList[0].setRelativeY(-2 * self.size + 2*self.size) + self.blockList[0].setRelativeY(-2 * self.size) # + 2*self.size) self.blockList[1].setRelativeX(3 * self.size) - self.blockList[1].setRelativeY(-3 * self.size + 2*self.size) + self.blockList[1].setRelativeY(-3 * self.size) # + 2*self.size) self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(-self.size + 2*self.size) - self.blockList[3].setRelativeY(2*self.size) #### + self.blockList[2].setRelativeY(-self.size) # + 2*self.size) + # self.blockList[3].setRelativeY(2*self.size) #### # move everything down 2 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(-2 * self.size) - self.blockList[0].setRelativeY(-2 * self.size + self.size) + self.blockList[0].setRelativeY(-2 * self.size) # + self.size) self.blockList[1].setRelativeX(-3 * self.size) - self.blockList[1].setRelativeY(-3 * self.size + self.size) + self.blockList[1].setRelativeY(-3 * self.size) # + self.size) self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(-self.size + self.size) - self.blockList[3].setRelativeY(self.size) #### + self.blockList[2].setRelativeY(-self.size) # + self.size) + # self.blockList[3].setRelativeY(self.size) #### # move everything down 1 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(-2 * self.size) - self.blockList[0].setRelativeY(2 * self.size - 2*self.size) + self.blockList[0].setRelativeY(2 * self.size) # - 2*self.size) self.blockList[1].setRelativeX(-3 * self.size) - self.blockList[1].setRelativeY(3 * self.size - 2*self.size) + self.blockList[1].setRelativeY(3 * self.size) # - 2*self.size) self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(self.size - 2*self.size) - self.blockList[3].setRelativeY(-2*self.size) #### + self.blockList[2].setRelativeY(self.size) # - 2*self.size) + # self.blockList[3].setRelativeY(-2*self.size) #### # move everything up 2 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(2 * self.size) - self.blockList[0].setRelativeY(2 * self.size - self.size) + self.blockList[0].setRelativeY(2 * self.size) # - self.size) self.blockList[1].setRelativeX(3 * self.size) - self.blockList[1].setRelativeY(3 * self.size - self.size) + self.blockList[1].setRelativeY(3 * self.size) # - self.size) self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(self.size - self.size) - self.blockList[3].setRelativeY(-self.size) #### + self.blockList[2].setRelativeY(self.size) # - self.size) + # self.blockList[3].setRelativeY(-self.size) #### # move everything up 1 self.orientation -= 1 - def rotateL4(self): + # center rotation v2 + def rotateL2(self): # rotate about the block blockList[0] # [1][0][2][3] if self.orientation % 4 == 0: + self.blockList[0].setRelativeX(-self.size) + self.blockList[0].setY(self.blockList[0].getY() + self.size) self.blockList[1].setY(self.blockList[0].getY() + self.size) self.blockList[1].setX(self.blockList[0].getX()) @@ -259,6 +266,8 @@ def rotateL4(self): # [0] # [1] elif self.orientation % 4 == 1: + self.blockList[0].setRelativeX(self.size) + self.blockList[0].setX(self.blockList[0].getX() + self.size) self.blockList[1].setX(self.blockList[0].getX() + self.size) self.blockList[1].setY(self.blockList[0].getY()) @@ -271,6 +280,8 @@ def rotateL4(self): # [3][2][0][1] elif self.orientation % 4 == 2: + self.blockList[0].setRelativeX(self.size) + self.blockList[0].setY(self.blockList[0].getY() - self.size) self.blockList[1].setY(self.blockList[0].getY() - self.size) self.blockList[1].setX(self.blockList[0].getX()) @@ -286,6 +297,8 @@ def rotateL4(self): # [2] # [3] elif self.orientation % 4 == 3: + self.blockList[0].setRelativeX(-self.size) + self.blockList[0].setX(self.blockList[0].getX() - self.size) self.blockList[1].setX(self.blockList[0].getX() - self.size) self.blockList[1].setY(self.blockList[0].getY()) @@ -298,10 +311,13 @@ def rotateL4(self): self.orientation += 1 - def rotateR4(self): + # center rotation v2 + def rotateR2(self): # rotate about the block blockList[0] # [1][0][2][3] if self.orientation % 4 == 0: + self.blockList[0].setRelativeX(self.size) + self.blockList[0].setX(self.blockList[0].getX() + self.size) self.blockList[1].setY(self.blockList[0].getY() - self.size) self.blockList[1].setX(self.blockList[0].getX()) @@ -317,6 +333,8 @@ def rotateR4(self): # [0] # [1] elif self.orientation % 4 == 1: + self.blockList[0].setRelativeX(self.size) + self.blockList[0].setY(self.blockList[0].getY() - self.size) self.blockList[1].setX(self.blockList[0].getX() - self.size) self.blockList[1].setY(self.blockList[0].getY()) @@ -329,6 +347,8 @@ def rotateR4(self): # [3][2][0][1] elif self.orientation % 4 == 2: + self.blockList[0].setRelativeX(-self.size) + self.blockList[0].setX(self.blockList[0].getX() - self.size) self.blockList[1].setY(self.blockList[0].getY() + self.size) self.blockList[1].setX(self.blockList[0].getX()) @@ -344,6 +364,8 @@ def rotateR4(self): # [2] # [3] elif self.orientation % 4 == 3: + self.blockList[0].setRelativeX(-self.size) + self.blockList[0].setY(self.blockList[0].getY() + self.size) self.blockList[1].setX(self.blockList[0].getX() + self.size) self.blockList[1].setY(self.blockList[0].getY()) @@ -434,3 +456,6 @@ def setX(self, x): def getColor(self): return self.color + + def getColor(self): + return self.color \ No newline at end of file diff --git a/Block/BlockJ.py b/Block/BlockJ.py index 7e01733..0e9d5f8 100644 --- a/Block/BlockJ.py +++ b/Block/BlockJ.py @@ -8,7 +8,7 @@ def __init__(self, x, y, size): self.isCurrent = False self.size = size self.orientation = 0 - self.color = (0,0,139) + self.color = (0,77,255) ## |1| ## |2||0||3| @@ -64,32 +64,40 @@ def rotateL(self): def rotateL2(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(-self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) self.blockList[1].setRelativeX(self.size) - self.blockList[1].setRelativeY(self.size) + self.blockList[1].setRelativeY(self.size) # + self.size) self.blockList[2].setRelativeX(2*self.size) - self.blockList[3].setRelativeY(-2*self.size) + # self.blockList[2].setRelativeY(self.size) #### + self.blockList[3].setRelativeY(-2*self.size) # + self.size) + # move everything down 1 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(-self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) self.blockList[1].setRelativeX(self.size) - self.blockList[1].setRelativeY(-self.size) - self.blockList[2].setRelativeY(-2 * self.size) + self.blockList[1].setRelativeY(-self.size) # + self.size) + self.blockList[2].setRelativeY(-2 * self.size) # + self.size) self.blockList[3].setRelativeX(-2 * self.size) + # self.blockList[3].setRelativeY(self.size) #### + # move everything down 1 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) self.blockList[1].setRelativeX(-self.size) - self.blockList[1].setRelativeY(-self.size) + self.blockList[1].setRelativeY(-self.size) # - self.size) self.blockList[2].setRelativeX(-2 * self.size) - self.blockList[3].setRelativeY(2 * self.size) + # self.blockList[2].setRelativeY(-self.size) #### + self.blockList[3].setRelativeY(2 * self.size) # - self.size) + # move everything up 1 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) self.blockList[1].setRelativeX(-self.size) - self.blockList[1].setRelativeY(self.size) - self.blockList[2].setRelativeY(2 * self.size) + self.blockList[1].setRelativeY(self.size) # - self.size) + self.blockList[2].setRelativeY(2 * self.size) # - self.size) self.blockList[3].setRelativeX(2 * self.size) + # self.blockList[3].setRelativeY(-self.size) #### + # move everything up 1 # if self.orientation % 4 == 0: # self.blockList[0].setRelativeY(-2*self.size) @@ -121,32 +129,40 @@ def rotateL2(self): def rotateR2(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(-self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) self.blockList[1].setRelativeX(self.size) - self.blockList[1].setRelativeY(-self.size) - self.blockList[2].setRelativeY(-2 * self.size) + self.blockList[1].setRelativeY(-self.size) # + self.size) + self.blockList[2].setRelativeY(-2 * self.size) # + self.size) self.blockList[3].setRelativeX(-2 * self.size) + # self.blockList[3].setRelativeY(self.size) #### + # move everything down 1 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) self.blockList[1].setRelativeX(-self.size) - self.blockList[1].setRelativeY(-self.size) + self.blockList[1].setRelativeY(-self.size) # - self.size) self.blockList[2].setRelativeX(-2 * self.size) - self.blockList[3].setRelativeY(2 * self.size) + # self.blockList[2].setRelativeY(-self.size) #### + self.blockList[3].setRelativeY(2 * self.size) # - self.size) + # move everything up 1 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) self.blockList[1].setRelativeX(-self.size) - self.blockList[1].setRelativeY(self.size) - self.blockList[2].setRelativeY(2 * self.size) + self.blockList[1].setRelativeY(self.size) # - self.size) + self.blockList[2].setRelativeY(2 * self.size) # - self.size) self.blockList[3].setRelativeX(2 * self.size) + # self.blockList[3].setRelativeY(-self.size) #### + # move everything up 1 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(-self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) self.blockList[1].setRelativeX(self.size) - self.blockList[1].setRelativeY(self.size) + self.blockList[1].setRelativeY(self.size) # + self.size) self.blockList[2].setRelativeX(2 * self.size) - self.blockList[3].setRelativeY(-2 * self.size) + # self.blockList[2].setRelativeY(self.size) #### + self.blockList[3].setRelativeY(-2 * self.size) # + self.size) + # move everything down 1 # if self.orientation % 4 == 0: # self.blockList[0].setRelativeX(-2 * self.size) @@ -356,3 +372,5 @@ def setX(self, x): for i in range(0, len(self.blockList)): self.blockList[i].setRelativeX(dx) + def getColor(self): + return self.color \ No newline at end of file diff --git a/Block/BlockL.py b/Block/BlockL.py index 3f0a0fd..77cec68 100644 --- a/Block/BlockL.py +++ b/Block/BlockL.py @@ -75,7 +75,8 @@ def rotateL(self): self.orientation = self.orientation + 1 - def rotateL2(self): + # rotation about index 1 + def rotateL3(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(self.size) self.blockList[0].setRelativeY(self.size) @@ -107,7 +108,8 @@ def rotateL2(self): self.orientation = self.orientation + 1 - def rotateR2(self): + # rotation about index 1 + def rotateR3(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(self.size) self.blockList[0].setRelativeY(-self.size) @@ -139,36 +141,44 @@ def rotateR2(self): self.orientation = self.orientation - 1 - - def rotateL3(self): + # center rotation + def rotateL2(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(-self.size) - self.blockList[1].setRelativeY(-2*self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) + self.blockList[1].setRelativeY(-2*self.size) # + self.size) self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(-self.size) + self.blockList[2].setRelativeY(-self.size) # + self.size) self.blockList[3].setRelativeX(2*self.size) + # self.blockList[3].setRelativeY(self.size) #### + # move everything down 1 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(-self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) self.blockList[1].setRelativeX(-2 * self.size) + # self.blockList[1].setRelativeY(self.size) #### self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(self.size) - self.blockList[3].setRelativeY(-2 * self.size) + self.blockList[2].setRelativeY(self.size) # + self.size) + self.blockList[3].setRelativeY(-2 * self.size) # + self.size) + # move everything down 1 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(self.size) - self.blockList[1].setRelativeY(2 * self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) + self.blockList[1].setRelativeY(2 * self.size) # - self.size) self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(self.size) + self.blockList[2].setRelativeY(self.size) # - self.size) self.blockList[3].setRelativeX(-2 * self.size) + # self.blockList[3].setRelativeY(-self.size) #### + # move everything up 1 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) self.blockList[1].setRelativeX(2 * self.size) + # self.blockList[1].setRelativeY(-self.size) #### self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(-self.size) - self.blockList[3].setRelativeY(2 * self.size) + self.blockList[2].setRelativeY(-self.size) # - self.size) + self.blockList[3].setRelativeY(2 * self.size) # - self.size) + # move everything up 1 # if self.orientation % 4 == 0: # self.blockList[0].setRelativeX(2*self.size) @@ -197,35 +207,44 @@ def rotateL3(self): self.orientation = self.orientation + 1 - def rotateR3(self): + # center rotation + def rotateR2(self): if self.orientation % 4 == 0: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(-self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) self.blockList[1].setRelativeX(-2 * self.size) + # self.blockList[1].setRelativeY(self.size) #### self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(self.size) - self.blockList[3].setRelativeY(-2 * self.size) + self.blockList[2].setRelativeY(self.size) # + self.size) + self.blockList[3].setRelativeY(-2 * self.size) # + self.size) + # move everything down 1 elif self.orientation % 4 == 1: self.blockList[0].setRelativeX(-self.size) - self.blockList[0].setRelativeY(self.size) - self.blockList[1].setRelativeY(2 * self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) + self.blockList[1].setRelativeY(2 * self.size) # - self.size) self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(self.size) + self.blockList[2].setRelativeY(self.size) # - self.size) self.blockList[3].setRelativeX(-2 * self.size) + # self.blockList[3].setRelativeY(-self.size) #### + # move everything up 1 elif self.orientation % 4 == 2: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(self.size) + self.blockList[0].setRelativeY(self.size) # - self.size) self.blockList[1].setRelativeX(2 * self.size) + # self.blockList[1].setRelativeY(-self.size) #### self.blockList[2].setRelativeX(self.size) - self.blockList[2].setRelativeY(-self.size) - self.blockList[3].setRelativeY(2 * self.size) + self.blockList[2].setRelativeY(-self.size) # - self.size) + self.blockList[3].setRelativeY(2 * self.size) # - self.size) + # move everything up 1 elif self.orientation % 4 == 3: self.blockList[0].setRelativeX(self.size) - self.blockList[0].setRelativeY(-self.size) - self.blockList[1].setRelativeY(-2 * self.size) + self.blockList[0].setRelativeY(-self.size) # + self.size) + self.blockList[1].setRelativeY(-2 * self.size) # + self.size) self.blockList[2].setRelativeX(-self.size) - self.blockList[2].setRelativeY(-self.size) + self.blockList[2].setRelativeY(-self.size) # + self.size) self.blockList[3].setRelativeX(2 * self.size) + # self.blockList[3].setRelativeY(self.size) #### + # move everything down 1 # if self.orientation % 4 == 0: # self.blockList[0].setRelativeY(-2 * self.size) @@ -369,4 +388,5 @@ def setX(self, x): for i in range (0, len(self.blockList)): self.blockList[i].setRelativeX(dx) - + def getColor(self): + return self.color diff --git a/Block/BlockO.py b/Block/BlockO.py index 9c66976..b49e1b7 100644 --- a/Block/BlockO.py +++ b/Block/BlockO.py @@ -62,3 +62,6 @@ def setX(self, x): self.x = x for i in range (0, len(self.blockList)): self.blockList[i].setRelativeX(dx) + + def getColor(self): + return self.color \ No newline at end of file diff --git a/Block/BlockS.py b/Block/BlockS.py index 4d3f117..a68bbbc 100644 --- a/Block/BlockS.py +++ b/Block/BlockS.py @@ -286,3 +286,7 @@ def setX(self, x): self.x = x for i in range(0, len(self.blockList)): self.blockList[i].setRelativeX(dx) + + def getColor(self): + return self.color + diff --git a/Block/BlockT.py b/Block/BlockT.py index eb29ea1..109dbe8 100644 --- a/Block/BlockT.py +++ b/Block/BlockT.py @@ -293,3 +293,6 @@ def setX(self, x): self.x = x for i in range (0, len(self.blockList)): self.blockList[i].setRelativeX(dx) + + def getColor(self): + return self.color diff --git a/Block/BlockZ.py b/Block/BlockZ.py index a011866..c11705d 100644 --- a/Block/BlockZ.py +++ b/Block/BlockZ.py @@ -296,3 +296,5 @@ def setX(self, x): for i in range(0, len(self.blockList)): self.blockList[i].setRelativeX(dx) + def getColor(self): + return self.color \ No newline at end of file diff --git a/Metris.py b/Metris.py index db4284a..e727b9a 100644 --- a/Metris.py +++ b/Metris.py @@ -3,6 +3,7 @@ import random, time, sys, math import json import datetime +import threading from Block.BlockT import * from Block.BlockO import * @@ -22,12 +23,14 @@ def __init__(self): self.WHITE = (255, 255, 255) self.RED = (255, 0, 0) - self.BLACK = (20, 20, 20) + self.BLACK = (0, 0, 0) self.GRAY = (185, 185, 185) - self.BORDER_COLOR = (255, 255, 255) - self.INNER_BG = (0, 75, 75) - self.INNER_BG2 = (0, 50, 50) + self.BORDER_COLOR = (50, 50, 100) + self.INNER_BG = (25, 25, 40) + self.INNER_BG2 = (75, 75, 100) + self.GRID_COLOR = (100, 100, 125) self.TEXTCOLOR = self.WHITE + self.COMBOCOLOR = self.WHITE self.TEXTSHADOWCOLOR = self.GRAY self.COLORS = [(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)] self.COLORS2 = [(200,0,0), (0,200,0), (0,0,200), (200,200,0), (200,0,200)] @@ -47,12 +50,6 @@ def __init__(self): 'Press B to go back', 'Press M to mute/unmute', ] - - self.SCOREFONT = pygame.font.Font('freesansbold.ttf', 18) - self.BASICFONT = pygame.font.Font('freesansbold.ttf', 18) - self.BIGFONT = pygame.font.Font('freesansbold.ttf', 50) - self.TIMEFONT = pygame.font.Font('freesansbold.ttf', 48) - self.TIMEFONT2 = pygame.font.Font('freesansbold.ttf', 50) self.HEIGHT = 900 self.WIDTH = self.HEIGHT * 4 / 3 self.BLOCK_SIZE = self.HEIGHT / 28 @@ -63,6 +60,13 @@ def __init__(self): self.INIT_X = self.LEFT_BOUNDARY + 5 * self.BLOCK_SIZE self.INIT_Y = 3*self.BLOCK_SIZE + self.SCOREFONT = pygame.font.Font('freesansbold.ttf', self.BLOCK_SIZE * 3 / 4) + self.BASICFONT = pygame.font.Font('freesansbold.ttf', self.BLOCK_SIZE * 3 / 4) + self.BASICFONT_OUTLINE = pygame.font.Font('freesansbold.ttf', self.BLOCK_SIZE * 3 / 4 + 2) + self.BIGFONT = pygame.font.Font('freesansbold.ttf', 2 * self.BLOCK_SIZE) + self.TIMEFONT = pygame.font.Font('freesansbold.ttf', self.BLOCK_SIZE * 5 / 4) + self.TIMEFONT2 = pygame.font.Font('freesansbold.ttf', 53) + self.MID_FILES = ['mp3s/m0', 'mp3s/m1', 'mp3s/m2', 'mp3s/m3', 'mp3s/m4', 'mp3s/m5', 'mp3s/m6', 'mp3s/m7', 'mp3s/m8', 'mp3s/m9', 'mp3s/m10', 'mp3s/m11', 'mp3s/m12', 'mp3s/m13'] @@ -116,7 +120,7 @@ def __init__(self): self.triple = False self.double = False self.single = False - + self.main_menu = None self.MOVE = pygame.USEREVENT + 4 @@ -126,7 +130,7 @@ def __init__(self): self.tolerance = False self.tolerable = True - self.lock = False + self.lock = threading.Lock() ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -160,7 +164,7 @@ def runGame(self): self.mult = 0 self.scr_mult = 5 self.drawCompliment(comp_input) - fontsize = 18 + fontsize = self.BLOCK_SIZE * 3 / 4 self.total_lines = 0 TICK = pygame.USEREVENT + 1 @@ -176,12 +180,19 @@ def runGame(self): pygame.time.set_timer(self.SLACK, 0) pygame.time.set_timer(self.NOSLACK, 0) + # drawing bg + # self.GAMEDISPLAY.fill(self.BLACK, [0, 0, self.WIDTH, self.HEIGHT]) + # =========GRADIENT BG=================== + spacing = self.HEIGHT / 100 + for i in range(14, 100): + self.GAMEDISPLAY.fill((100 + i, 105 + i, 155 + i), [0, spacing * i, self.WIDTH, spacing]) + while not self.gameExit: - + if self.bankedpoints > 0: self.score += 1 self.bankedpoints -= 1 - + # checkForQuit() for event in pygame.event.get(): ## if event.type == pygame.QUIT: @@ -199,9 +210,9 @@ def runGame(self): ## self.INIT_X = self.LEFT_BOUNDARY + 5 * self.BLOCK_SIZE ## self.INIT_Y = self.BLOCK_SIZE #================================================== - if event.type == pygame.VIDEORESIZE: - self.GAMEDISPLAY = pygame.display.set_mode(event.size, pygame.RESIZABLE) - self.GAMEDISPLAY.fill(self.BLACK) + # if event.type == pygame.VIDEORESIZE: + # self.GAMEDISPLAY = pygame.display.set_mode(event.size, pygame.RESIZABLE) + # self.GAMEDISPLAY.fill(self.BLACK) #================================================== if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: pos = self.soundOn.get_rect() @@ -243,12 +254,12 @@ def runGame(self): elif event.key == pygame.K_UP and controlsOn == True: if self.currentBlock: self.block.rotateR() - # if self.checkCollisionRotation(): - # self.block.rotateL4() + # self.spin = True + self.checkCollisionRotation() + # self.block.rotateL() if self.checkCollision(): self.block.rotateL() self.block.rotateR2() - self.spin = True if self.checkCollision(): self.block.setY(self.block.getY() + self.BLOCK_SIZE) if self.checkCollision(): @@ -281,12 +292,12 @@ def runGame(self): elif event.key == pygame.K_z and controlsOn == True: if self.currentBlock: self.block.rotateL() - # if self.checkCollisionRotation(): + # self.spin = True + self.checkCollisionRotation() # self.block.rotateR4() if self.checkCollision(): self.block.rotateR() self.block.rotateL2() - self.spin = True # self.block.setY(self.block.getY() + self.BLOCK_SIZE) # if self.checkCollision(): # self.block.setY(self.block.getY() - self.BLOCK_SIZE) @@ -308,12 +319,12 @@ def runGame(self): if self.checkCollision(): self.block.setY(self.block.getY() - self.BLOCK_SIZE) self.block.rotateR4() - self.spin = False + # self.spin = False if self.tolerable: self.tolerance = True pygame.time.set_timer(self.SLACK, 2000) else: - self.spin = False + # self.spin = False if self.tolerable: self.tolerance = True pygame.time.set_timer(self.SLACK, 2000) @@ -404,16 +415,16 @@ def runGame(self): self.tolerance = False pygame.time.set_timer(self.SLACK, 0) if event.type == self.NOSLACK: + self.tolerance = False self.tolerable = True pygame.time.set_timer(self.NOSLACK, 0) # drawing bg - self.GAMEDISPLAY.fill(self.BLACK, [0, 0, self.WIDTH, self.HEIGHT]) - #=========GRADIENT BG=================== + # =========GRADIENT BG=================== spacing = self.HEIGHT / 100 - for i in range (14, 100): - self.GAMEDISPLAY.fill((100+i, 105+i, 155+i), [0, spacing*i, self.WIDTH, spacing]) + for i in range(85, 100): + self.GAMEDISPLAY.fill((100 + i, 105 + i, 155 + i), [0, spacing * i, self.WIDTH, spacing]) #=========RANDOM BLOCK BG=============== # selector = 0 @@ -469,7 +480,7 @@ def runGame(self): self.tolerance = False self.tolerable = True hasSwap = True - + # drawing shadow if not self.block is None: difference = self.getShadowDifference(self.block.getPerimeter()) @@ -549,20 +560,20 @@ def runGame(self): # drawing timeleft ======================TIMED METRIS================= if time / 1000 - (time / 1000 / 60) * 60 < 10: - timeleft_text_outline = self.TIMEFONT.render(str(time / 1000 / 60) + " : 0" + str(time / 1000 - (time / 1000 / 60) * 60), True, (50, 50, 100)) + timeleft_text_shadow = self.TIMEFONT.render(str(time / 1000 / 60) + " : 0" + str(time / 1000 - (time / 1000 / 60) * 60), True, (50, 50, 100)) timeleft_text = self.TIMEFONT.render(str(time/1000/60) + " : 0" + str(time/1000 - (time/1000/60)*60), True, (255,255,0)) time_rect = timeleft_text.get_rect() time_rect.center = (self.WIDTH/2, self.TOP_BOUNDARY - 1.5 * self.BLOCK_SIZE) - time_outline_rect = timeleft_text.get_rect() - time_outline_rect.center = (self.WIDTH / 2 + 5, self.TOP_BOUNDARY - 1.5 * self.BLOCK_SIZE + 5) + time_shadow_rect = timeleft_text.get_rect() + time_shadow_rect.center = (self.WIDTH / 2 + 5, self.TOP_BOUNDARY - 1.5 * self.BLOCK_SIZE + 5) else: - timeleft_text_outline = self.TIMEFONT.render(str(time / 1000 / 60) + " : " + str(time / 1000 - (time / 1000 / 60) * 60), True, (50, 50, 100)) + timeleft_text_shadow = self.TIMEFONT.render(str(time / 1000 / 60) + " : " + str(time / 1000 - (time / 1000 / 60) * 60), True, (50, 50, 100)) timeleft_text = self.TIMEFONT.render(str(time/1000/60) + " : " + str(time/1000 - (time/1000/60)*60), True, (255,255,0)) time_rect = timeleft_text.get_rect() time_rect.center = (self.WIDTH/2, self.TOP_BOUNDARY - 1.5 * self.BLOCK_SIZE) - time_outline_rect = timeleft_text.get_rect() - time_outline_rect.center = (self.WIDTH / 2 + 5, self.TOP_BOUNDARY - 1.5 * self.BLOCK_SIZE + 5) - self.GAMEDISPLAY.blit(timeleft_text_outline, time_outline_rect) + time_shadow_rect = timeleft_text.get_rect() + time_shadow_rect.center = (self.WIDTH / 2 + 5, self.TOP_BOUNDARY - 1.5 * self.BLOCK_SIZE + 5) + self.GAMEDISPLAY.blit(timeleft_text_shadow, time_shadow_rect) self.GAMEDISPLAY.blit(timeleft_text, time_rect) #===================================================================== @@ -573,9 +584,9 @@ def runGame(self): self.GAMEDISPLAY.blit(level_text, (self.RIGHT_BOUNDARY + self.BLOCK_SIZE + 10, self.TOP_BOUNDARY + 0.5 * self.BLOCK_SIZE)) #==============POP UP SCORE===================================== - if self.bankedpoints > 0 and fontsize < 24: + if self.bankedpoints > 0 and fontsize < self.BLOCK_SIZE * 3 / 4 + 6: fontsize += 1 - if self.bankedpoints is 0 and fontsize > 18: + if self.bankedpoints is 0 and fontsize > self.BLOCK_SIZE * 3 / 4: fontsize -= 1 self.SCOREFONT = pygame.font.Font('freesansbold.ttf', fontsize) screen_text = self.BASICFONT.render("Score: ", True, self.WHITE) @@ -654,16 +665,17 @@ def runGame(self): self.drawSoundIcon() self.drawMusicIcon() - if self.level_prev != self.level: - old_var = mus_var - while mus_var == old_var: - mus_var = randint(0,12) - pygame.mixer.music.load(self.MID_FILES[mus_var]) - self.level_prev = self.level - pygame.mixer.music.play(-1,0.0) - if self.isMusicOn == False: - pygame.mixer.music.pause() - + #======= NEW SONG EVERY NEW LEVEL ========== + # if self.level_prev != self.level: + # old_var = mus_var + # while mus_var == old_var: + # mus_var = randint(0,12) + # pygame.mixer.music.load(self.MID_FILES[mus_var]) + # self.level_prev = self.level + # pygame.mixer.music.play(-1,0.0) + # if self.isMusicOn == False: + # pygame.mixer.music.pause() + pygame.display.update() self.clock.tick(self.speed) @@ -686,15 +698,18 @@ def runGame(self): def tick(self): if self.gameExit: return - self.lock = True if self.tolerable and (self.dy is not 0 or self.dx is not 0): self.tolerance = True pygame.time.set_timer(self.SLACK, 1100 - self.fallFreq) + + #self.lock.acquire() if self.block != None: self.block.setY(self.block.getY() + self.BLOCK_SIZE) if self.checkCollision(): self.block.setY(self.block.getY() - self.BLOCK_SIZE) if not self.tolerance: # self.dy is 0: + self.COMBOCOLOR = self.block.getColor() + self.spin = self.checkForSpin() self.currentBlock = False if self.tolerance: # self.dy is not 0: self.tolerable = False @@ -707,7 +722,7 @@ def tick(self): self.checkLandedAndDelete() self.playSound('landSound.wav') self.checkGameOver() - self.lock = False + #self.lock.release() def checkLandedAndDelete(self): y = 0 @@ -725,6 +740,32 @@ def rowFilled(self, y): return False return True + def checkForSpin(self): + if self.block == None: + return False + self.block.setY(self.block.getY() - self.BLOCK_SIZE) + if self.checkCollision(): + self.block.setY(self.block.getY() + 2 * self.BLOCK_SIZE) + if self.checkCollision(): + self.block.setY(self.block.getY() - self.BLOCK_SIZE) + self.block.setX(self.block.getX() - self.BLOCK_SIZE) + if self.checkCollision(): + self.block.setX(self.block.getX() + 2 * self.BLOCK_SIZE) + if self.checkCollision(): + self.block.setX(self.block.getX() - self.BLOCK_SIZE) + return True + else: + self.block.setX(self.block.getX() - self.BLOCK_SIZE) + return False + else: + self.block.setX(self.block.getX() + self.BLOCK_SIZE) + return False + else: + self.block.setY(self.block.getY() - self.BLOCK_SIZE) + return False + else: + self.block.setY(self.block.getY() + self.BLOCK_SIZE) + return False def deleteRows(self, rows): self.total_lines += len(rows) @@ -742,13 +783,14 @@ def deleteRows(self, rows): self.landed[x][y] = self.landed[x][y - 1] self.playSound('clr.wav') - # update self.score self.d_tspin = False self.t_tspin = False self.tetris = False self.triple = False self.double = False self.single = False + + # update self.score if len(rows) == 1: self.bankedpoints += 10 self.single = True @@ -756,9 +798,9 @@ def deleteRows(self, rows): ret = 0 multiplier = 1 pt_score = 5 - if self.spin and len(rows) == 2: + if self.spin and len(rows) == 2 and type(self.block) == BlockT: self.d_tspin = True - elif self.spin and len(rows) == 3: + elif self.spin and len(rows) == 3 and type(self.block) == BlockT: self.t_tspin = True elif len(rows) == 2: self.double = True @@ -782,7 +824,7 @@ def checkGameOver(self): self.gameOver() done = True break - + def checkCollision(self): @@ -823,55 +865,55 @@ def checkBlockCollision(self): self.block.setY(self.block.getY() + self.BLOCK_SIZE) return isCollision return False - + def checkCollisionRotation(self): if not self.currentBlock: return False blockPerimeter = self.block.getPerimeter() - + for i in range(0, len(blockPerimeter)): if blockPerimeter[i].getX() < self.LEFT_BOUNDARY - self.BLOCK_SIZE: self.block.setX(self.block.getX() + 2 * self.BLOCK_SIZE) - self.checkBlockCollision() + # self.checkBlockCollision() if self.checkCollision(): self.block.setX(self.block.getX() - 2 * self.BLOCK_SIZE) return True break elif blockPerimeter[i].getX() > self.RIGHT_BOUNDARY: self.block.setX(self.block.getX() - 2 * self.BLOCK_SIZE) - self.checkBlockCollision() + # self.checkBlockCollision() if self.checkCollision(): self.block.setX(self.block.getX() + 2 * self.BLOCK_SIZE) return True break - elif blockPerimeter[i].getY() > self.BOTTOM_BOUNDARY: - self.block.setY(self.block.getY() - 2 * self.BLOCK_SIZE) - self.checkBlockCollision() - if self.checkCollision(): - self.block.setY(self.block.getX() + 2 * self.BLOCK_SIZE) - return True - break + # elif blockPerimeter[i].getY() > self.BOTTOM_BOUNDARY: + # self.block.setY(self.block.getY() - 2 * self.BLOCK_SIZE) + # self.checkBlockCollision() + # if self.checkCollision(): + # self.block.setY(self.block.getX() + 2 * self.BLOCK_SIZE) + # return True + # break for i in range(0, len(blockPerimeter)): if blockPerimeter[i].getX() < self.LEFT_BOUNDARY: self.block.setX(self.block.getX() + self.BLOCK_SIZE) - self.checkBlockCollision() + # self.checkBlockCollision() if self.checkCollision(): self.block.setX(self.block.getX() - self.BLOCK_SIZE) return True break elif blockPerimeter[i].getX() > self.RIGHT_BOUNDARY - self.BLOCK_SIZE: self.block.setX(self.block.getX() - self.BLOCK_SIZE) - self.checkBlockCollision() + # self.checkBlockCollision() if self.checkCollision(): self.block.setX(self.block.getX() + self.BLOCK_SIZE) return True break elif blockPerimeter[i].getY() > self.BOTTOM_BOUNDARY - self.BLOCK_SIZE: - self.block.setY(self.block.getY() - self.BLOCK_SIZE) - self.checkBlockCollision() + # self.block.setY(self.block.getY() - self.BLOCK_SIZE) + # self.checkBlockCollision() if self.checkCollision(): - self.block.setY(self.block.getX() + self.BLOCK_SIZE) + # self.block.setY(self.block.getX() + self.BLOCK_SIZE) return True break return False @@ -907,7 +949,7 @@ def drawShadow(self, blockList, dist_y): pygame.draw.rect(self.GAMEDISPLAY, blockList[i].getColor(), [blockList[i].getX() + self.BLOCK_SIZE/10, blockList[i].getY() + dist_y + self.BLOCK_SIZE/10, self.BLOCK_SIZE - self.BLOCK_SIZE/5, self.BLOCK_SIZE - self.BLOCK_SIZE/5]) - pygame.draw.rect(self.GAMEDISPLAY, (0, 25, 25), + pygame.draw.rect(self.GAMEDISPLAY, self.INNER_BG, [blockList[i].getX() + self.BLOCK_SIZE/8, blockList[i].getY() + dist_y + self.BLOCK_SIZE/8, self.BLOCK_SIZE - self.BLOCK_SIZE/4, self.BLOCK_SIZE - self.BLOCK_SIZE/4]) @@ -1303,21 +1345,20 @@ def drawGridLines(self): ## (self.LEFT_BOUNDARY + j * SPACING + SPACING, self.TOP_BOUNDARY + i * self.BLOCK_SIZE), ## 1) for i in range (1, 10): - pygame.draw.line(self.GAMEDISPLAY, (0, 150, 150), + pygame.draw.line(self.GAMEDISPLAY, self.GRID_COLOR, (self.LEFT_BOUNDARY + i * self.BLOCK_SIZE, self.TOP_BOUNDARY), (self.LEFT_BOUNDARY + i * self.BLOCK_SIZE, self.BOTTOM_BOUNDARY), 1) for i in range (1, 20): - pygame.draw.line(self.GAMEDISPLAY, (0, 150, 150), + pygame.draw.line(self.GAMEDISPLAY, self.GRID_COLOR, (self.LEFT_BOUNDARY, self.TOP_BOUNDARY + i * self.BLOCK_SIZE), (self.LEFT_BOUNDARY + 10 * self.BLOCK_SIZE, self.TOP_BOUNDARY + i * self.BLOCK_SIZE), 1) def move(self): - if self.block is None: - return - if self.lock: + if self.block is None or self.currentBlock is False: return + #self.lock.acquire() # self.accl = self.accl * 3 / 5 pygame.time.set_timer(self.MOVE, 100) self.block.setX(self.block.getX() + self.dx) @@ -1326,6 +1367,7 @@ def move(self): self.block.setY(self.block.getY() + self.dy) if self.checkCollision(): self.block.setY(self.block.getY() - self.dy) + #self.lock.release() def generateQues(self): if self.level <= 4: @@ -1475,34 +1517,58 @@ def drawCompliment(self, rand): def drawCombo(self): if self.single: - combo_surf = self.BASICFONT.render("Single", True, self.TEXTCOLOR) + combo_outline_surf = self.BASICFONT.render("Single", True, self.BLACK) + combo_surf = self.BASICFONT.render("Single", True, self.WHITE) + combo_outline_rect = combo_outline_surf.get_rect() + combo_outline_rect.center = (self.WIDTH / 2 + 1, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE + 1) combo_rect = combo_surf.get_rect() combo_rect.center = (self.WIDTH / 2, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE) + self.GAMEDISPLAY.blit(combo_outline_surf, combo_outline_rect) self.GAMEDISPLAY.blit(combo_surf, combo_rect) elif self.double: - combo_surf = self.BASICFONT.render("Double", True, self.TEXTCOLOR) + combo_outline_surf = self.BASICFONT.render("Double", True, self.BLACK) + combo_surf = self.BASICFONT.render("Double", True, self.WHITE) + combo_outline_rect = combo_outline_surf.get_rect() + combo_outline_rect.center = (self.WIDTH / 2 + 1, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE + 1) combo_rect = combo_surf.get_rect() combo_rect.center = (self.WIDTH / 2, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE) + self.GAMEDISPLAY.blit(combo_outline_surf, combo_outline_rect) self.GAMEDISPLAY.blit(combo_surf, combo_rect) elif self.triple: - combo_surf = self.BASICFONT.render("Triple", True, self.TEXTCOLOR) + combo_outline_surf = self.BASICFONT.render("Triple", True, self.BLACK) + combo_surf = self.BASICFONT.render("Triple", True, self.WHITE) + combo_outline_rect = combo_outline_surf.get_rect() + combo_outline_rect.center = (self.WIDTH / 2 + 1, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE + 1) combo_rect = combo_surf.get_rect() combo_rect.center = (self.WIDTH / 2, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE) + self.GAMEDISPLAY.blit(combo_outline_surf, combo_outline_rect) self.GAMEDISPLAY.blit(combo_surf, combo_rect) elif self.tetris: - combo_surf = self.BASICFONT.render("Tetris", True, self.TEXTCOLOR) + combo_outline_surf = self.BASICFONT.render("Tetris", True, self.BLACK) + combo_surf = self.BASICFONT.render("Tetris", True, self.COMBOCOLOR) + combo_outline_rect = combo_outline_surf.get_rect() + combo_outline_rect.center = (self.WIDTH / 2 + 1, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE + 1) combo_rect = combo_surf.get_rect() combo_rect.center = (self.WIDTH / 2, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE) + self.GAMEDISPLAY.blit(combo_outline_surf, combo_outline_rect) self.GAMEDISPLAY.blit(combo_surf, combo_rect) elif self.d_tspin: - combo_surf = self.BASICFONT.render("T-spin Double", True, self.TEXTCOLOR) + combo_outline_surf = self.BASICFONT.render("T-spin Double", True, self.BLACK) + combo_surf = self.BASICFONT.render("T-spin Double", True, self.COMBOCOLOR) + combo_outline_rect = combo_outline_surf.get_rect() + combo_outline_rect.center = (self.WIDTH / 2 + 1, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE + 1) combo_rect = combo_surf.get_rect() combo_rect.center = (self.WIDTH / 2, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE) + self.GAMEDISPLAY.blit(combo_outline_surf, combo_outline_rect) self.GAMEDISPLAY.blit(combo_surf, combo_rect) elif self.t_tspin: - combo_surf = self.BASICFONT.render("T-spin Triple", True, self.TEXTCOLOR) + combo_outline_surf = self.BASICFONT.render("T-spin Triple", True, self.BLACK) + combo_surf = self.BASICFONT.render("T-spin Triple", True, self.COMBOCOLOR) + combo_outline_rect = combo_outline_surf.get_rect() + combo_outline_rect.center = (self.WIDTH / 2 + 1, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE + 1) combo_rect = combo_surf.get_rect() combo_rect.center = (self.WIDTH / 2, self.BOTTOM_BOUNDARY + 1.5 * self.BLOCK_SIZE) + self.GAMEDISPLAY.blit(combo_outline_surf, combo_outline_rect) self.GAMEDISPLAY.blit(combo_surf, combo_rect) @@ -1639,16 +1705,20 @@ def drawHighScore(self, text, lvl, pts, tL): pressKeySurf, pressKeyRect = self.makeTextObjs('Total = ' + str(pts) + " x " + str(tL/10.0), self.BASICFONT, self.TEXTCOLOR) pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 170) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) - while i == 5 and new_score != int(pts * (tL/10.0)): + if i == 5: + pressKeySurf, pressKeyRect = self.makeTextObjs("press any key to skip", self.BASICFONT, self.TEXTCOLOR) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 270) + self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) + while i == 5 and new_score != int(pts * (tL/10.0)): event = pygame.event.poll() if event.type == pygame.KEYDOWN: - if event.key == pygame.K_RETURN: - new_score = int(pts * (tL/10.0)) + # if event.key == pygame.K_RETURN: + new_score = int(pts * (tL/10.0)) pressKeySurf, pressKeyRect = self.makeTextObjs(str(int(new_score)), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 195) - pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [int(self.WIDTH / 6), int(self.HEIGHT / 2) + 185, self.WIDTH / 2, 25]) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 220) + pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [0, int(self.HEIGHT / 2) + 180, self.WIDTH, 2 * self.BLOCK_SIZE]) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) - if new_score > int(pts * (tL/10.0)): + if new_score > int(pts * (tL/10.0)): new_score -= 1 else: new_score += 1 @@ -1656,8 +1726,8 @@ def drawHighScore(self, text, lvl, pts, tL): self.clock.tick(self.speed) if i == 5 and new_score == int(pts * (tL/10.0)): pressKeySurf, pressKeyRect = self.makeTextObjs(str(int(new_score)), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 195) - pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [int(self.WIDTH / 6), int(self.HEIGHT / 2) + 185, self.WIDTH / 2, 25]) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 220) + pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [0, int(self.HEIGHT / 2) + 180, self.WIDTH, 2 * self.BLOCK_SIZE]) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) i += 1 pygame.display.update() @@ -1871,7 +1941,7 @@ def buildMain(self): instruction_menu.add_line(PYGAMEMENU_TEXT_NEWLINE) instruction_menu.add_line(PYGAMEMENU_TEXT_NEWLINE) instruction_menu.add_line(PYGAMEMENU_TEXT_NEWLINE) - instruction_menu.add_option('Return to menu', PYGAME_MENU_BACK) + instruction_menu.add_option('Press \'Enter\' to return', PYGAME_MENU_BACK) # Leaderboard MENU leaderboard_menu = pygameMenu.TextMenu(self.GAMEDISPLAY, @@ -1919,7 +1989,7 @@ def buildMain(self): # leaderboard_menu.add_option('View top 10 self.scores!', leaderboard_function) leaderboard_menu.add_line(PYGAMEMENU_TEXT_NEWLINE) - leaderboard_menu.add_option('Return to menu', PYGAME_MENU_BACK) + leaderboard_menu.add_option('Press \'Enter\' to return', PYGAME_MENU_BACK) # MAIN MENU self.main_menu = pygameMenu.Menu(self.GAMEDISPLAY, diff --git a/inputbox.py b/inputbox.py index bdfc028..25f979b 100644 --- a/inputbox.py +++ b/inputbox.py @@ -50,21 +50,24 @@ def ask(screen, question): global current_string current_string = [] display_box(screen, question + ": " + string.join(current_string, "")) - counter = 20 + counter = 15 while True: inkey = get_key() - if inkey == K_BACKSPACE: - current_string = current_string[0:-1] - counter += 2 + if inkey == K_BACKSPACE and counter <= 15: + if not counter == 15: + current_string = current_string[0:-1] + counter += 1 elif inkey == K_RETURN: break elif inkey == K_MINUS: - current_string.append("-") + if counter > 0: + current_string.append("-") + counter -= 1 elif inkey <= 127: if counter > 0: current_string.append(chr(inkey)) + counter -= 1 display_box(screen, question + ": " + string.join(current_string, "")) - counter -= 1 return '{:_^15}'.format(string.join(current_string, ""))[:15] diff --git a/sugargame/__init__.py b/sugargame/__init__.py new file mode 100644 index 0000000..64477cf --- /dev/null +++ b/sugargame/__init__.py @@ -0,0 +1 @@ +__version__ = '1.2' diff --git a/sugargame/canvas.py b/sugargame/canvas.py new file mode 100644 index 0000000..2299f4d --- /dev/null +++ b/sugargame/canvas.py @@ -0,0 +1,86 @@ +import os +from gi.repository import Gtk +from gi.repository import GObject +from gi.repository import GLib +from sugar3.activity.activity import PREVIEW_SIZE +import pygame +import event + +CANVAS = None + + +class PygameCanvas(Gtk.EventBox): + def __init__(self, activity, pointer_hint=True, + main=None, modules=[pygame]): + GObject.GObject.__init__(self) + + global CANVAS + assert CANVAS == None, "Only one PygameCanvas can be created, ever." + CANVAS = self + + # Initialize Events translator before widget gets "realized". + self.translator = event.Translator(activity, self) + + self._activity = activity + self._main = main + self._modules = modules + + self.set_can_focus(True) + + self._socket = Gtk.Socket() + self._socket.connect('realize', self._realize_cb) + self.add(self._socket) + + self.show_all() + + def _realize_cb(self, widget): + + # Preinitialize Pygame with the X window ID. + os.environ['SDL_WINDOWID'] = str(widget.get_id()) + for module in self._modules: + module.init() + + # Restore the default cursor. + widget.props.window.set_cursor(None) + + # Confine the Pygame surface to the canvas size + r = self.get_allocation() + self._screen = pygame.display.set_mode((r.width, r.height), + pygame.RESIZABLE) + + # Hook certain Pygame functions with GTK equivalents. + self.translator.hook_pygame() + + # Call the caller's main loop as an idle source + if self._main: + GLib.idle_add(self._main) + + def get_pygame_widget(self): + return self._socket + + def get_preview(self): + """ + Return preview of main surface + How to use in activity: + def get_preview(self): + return self.game_canvas.get_preview() + """ + + if not hasattr(self, '_screen'): + return None + + _tmp_dir = os.path.join(self._activity.get_activity_root(), + 'tmp') + _file_path = os.path.join(_tmp_dir, 'preview.png') + + width = PREVIEW_SIZE[0] + height = PREVIEW_SIZE[1] + _surface = pygame.transform.scale(self._screen, (width, height)) + pygame.image.save(_surface, _file_path) + + f = open(_file_path, 'r') + preview = f.read() + f.close() + os.remove(_file_path) + + return preview diff --git a/sugargame/event.py b/sugargame/event.py new file mode 100644 index 0000000..dee8d78 --- /dev/null +++ b/sugargame/event.py @@ -0,0 +1,258 @@ +import logging +from gi.repository import Gdk +from gi.repository import GObject +import pygame +import pygame.event + + +class _MockEvent(object): + def __init__(self, keyval): + self.keyval = keyval + + +class Translator(object): + key_trans = { + 'Alt_L': pygame.K_LALT, + 'Alt_R': pygame.K_RALT, + 'Control_L': pygame.K_LCTRL, + 'Control_R': pygame.K_RCTRL, + 'Shift_L': pygame.K_LSHIFT, + 'Shift_R': pygame.K_RSHIFT, + 'Super_L': pygame.K_LSUPER, + 'Super_R': pygame.K_RSUPER, + 'KP_Page_Up': pygame.K_KP9, + 'KP_Page_Down': pygame.K_KP3, + 'KP_End': pygame.K_KP1, + 'KP_Home': pygame.K_KP7, + 'KP_Up': pygame.K_KP8, + 'KP_Down': pygame.K_KP2, + 'KP_Left': pygame.K_KP4, + 'KP_Right': pygame.K_KP6, + + } + + mod_map = { + pygame.K_LALT: pygame.KMOD_LALT, + pygame.K_RALT: pygame.KMOD_RALT, + pygame.K_LCTRL: pygame.KMOD_LCTRL, + pygame.K_RCTRL: pygame.KMOD_RCTRL, + pygame.K_LSHIFT: pygame.KMOD_LSHIFT, + pygame.K_RSHIFT: pygame.KMOD_RSHIFT, + } + + def __init__(self, activity, inner_evb): + """Initialise the Translator with the windows to which to listen""" + self._activity = activity + self._inner_evb = inner_evb + + # Enable events + # (add instead of set here because the main window is already realized) + self._activity.add_events( + Gdk.EventMask.KEY_PRESS_MASK | + Gdk.EventMask.KEY_RELEASE_MASK | + Gdk.EventMask.VISIBILITY_NOTIFY_MASK + ) + + self._inner_evb.set_events( + Gdk.EventMask.POINTER_MOTION_MASK | + Gdk.EventMask.POINTER_MOTION_HINT_MASK | + Gdk.EventMask.BUTTON_MOTION_MASK | + Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK + ) + + self._activity.set_can_focus(True) + self._inner_evb.set_can_focus(True) + + # Callback functions to link the event systems + self._activity.connect('unrealize', self._quit_cb) + self._activity.connect('visibility_notify_event', self._visibility_cb) + self._activity.connect('configure-event', self._resize_cb) + self._inner_evb.connect('key_press_event', self._keydown_cb) + self._inner_evb.connect('key_release_event', self._keyup_cb) + self._inner_evb.connect('button_press_event', self._mousedown_cb) + self._inner_evb.connect('button_release_event', self._mouseup_cb) + self._inner_evb.connect('motion-notify-event', self._mousemove_cb) + self._inner_evb.connect('screen-changed', self._screen_changed_cb) + + # Internal data + self.__keystate = [0] * 323 + self.__button_state = [0, 0, 0] + self.__mouse_pos = (0, 0) + self.__repeat = (None, None) + self.__held = set() + self.__held_time_left = {} + self.__held_last_time = {} + self.__tick_id = None + + def hook_pygame(self): + pygame.key.get_pressed = self._get_pressed + pygame.key.set_repeat = self._set_repeat + pygame.mouse.get_pressed = self._get_mouse_pressed + pygame.mouse.get_pos = self._get_mouse_pos + + def update_display(self): + if pygame.display.get_init(): + pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE)) + + def _resize_cb(self, widget, event): + if pygame.display.get_init(): + evt = pygame.event.Event(pygame.VIDEORESIZE, + size=(event.width,event.height), + width=event.width, height=event.height) + pygame.event.post(evt) + return False # continue processing + + def _screen_changed_cb(self, widget, previous_screen): + self.update_display() + + def _quit_cb(self, data=None): + pygame.event.post(pygame.event.Event(pygame.QUIT)) + + def _visibility_cb(self, widget, event): + self.update_display() + return False + + def _keydown_cb(self, widget, event): + key = event.keyval + if key in self.__held: + return True + else: + if self.__repeat[0] is not None: + self.__held_last_time[key] = pygame.time.get_ticks() + self.__held_time_left[key] = self.__repeat[0] + self.__held.add(key) + + return self._keyevent(widget, event, pygame.KEYDOWN) + + def _keyup_cb(self, widget, event): + key = event.keyval + if self.__repeat[0] is not None: + if key in self.__held: + # This is possibly false if set_repeat() + # is called with a key held + del self.__held_time_left[key] + del self.__held_last_time[key] + self.__held.discard(key) + + return self._keyevent(widget, event, pygame.KEYUP) + + def _keymods(self): + mod = 0 + for key_val, mod_val in self.mod_map.iteritems(): + mod |= self.__keystate[key_val] and mod_val + return mod + + def _keyevent(self, widget, event, type): + key = Gdk.keyval_name(event.keyval) + if key is None: + # No idea what this key is. + return False + + keycode = None + if key in self.key_trans: + keycode = self.key_trans[key] + elif hasattr(pygame, 'K_' + key.upper()): + keycode = getattr(pygame, 'K_' + key.upper()) + elif hasattr(pygame, 'K_' + key.lower()): + keycode = getattr(pygame, 'K_' + key.lower()) + elif key == 'XF86Start': + # view source request, specially handled... + self._activity.view_source() + else: + logging.error('Key %s unrecognized' % key) + + if keycode is not None: + if type == pygame.KEYDOWN: + mod = self._keymods() + self.__keystate[keycode] = type == pygame.KEYDOWN + if type == pygame.KEYUP: + mod = self._keymods() + ukey = unichr(Gdk.keyval_to_unicode(event.keyval)) + if ukey == '\000': + ukey = '' + evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod) + self._post(evt) + + return True + + def _get_pressed(self): + return self.__keystate + + def _get_mouse_pressed(self): + return self.__button_state + + def _mousedown_cb(self, widget, event): + self.__button_state[event.button - 1] = 1 + return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN) + + def _mouseup_cb(self, widget, event): + self.__button_state[event.button - 1] = 0 + return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP) + + def _mouseevent(self, widget, event, type): + evt = pygame.event.Event(type, button=event.button, pos=(event.x, + event.y)) + self._post(evt) + return True + + def _mousemove_cb(self, widget, event): + # From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/ + # if this is a hint, then let's get all the necessary + # information, if not it's all we need. + if event.is_hint: + win, x, y, state = event.window.get_device_position(event.device) + else: + x = event.x + y = event.y + state = event.get_state() + + rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1]) + self.__mouse_pos = (x, y) + + self.__button_state = [ + state & Gdk.ModifierType.BUTTON1_MASK and 1 or 0, + state & Gdk.ModifierType.BUTTON2_MASK and 1 or 0, + state & Gdk.ModifierType.BUTTON3_MASK and 1 or 0, + ] + + evt = pygame.event.Event(pygame.MOUSEMOTION, + pos=self.__mouse_pos, rel=rel, + buttons=self.__button_state) + self._post(evt) + return True + + def _tick_cb(self): + cur_time = pygame.time.get_ticks() + for key in self.__held: + delta = cur_time - self.__held_last_time[key] + self.__held_last_time[key] = cur_time + + self.__held_time_left[key] -= delta + if self.__held_time_left[key] <= 0: + self.__held_time_left[key] = self.__repeat[1] + self._keyevent(None, _MockEvent(key), pygame.KEYDOWN) + + return True + + def _set_repeat(self, delay=None, interval=None): + if delay is not None and self.__repeat[0] is None: + self.__tick_id = GObject.timeout_add(10, self._tick_cb) + elif delay is None and self.__repeat[0] is not None: + GObject.source_remove(self.__tick_id) + self.__repeat = (delay, interval) + + def _get_mouse_pos(self): + return self.__mouse_pos + + def _post(self, evt): + try: + pygame.event.post(evt) + except pygame.error, e: + if str(e) == 'video system not initialized': + pass + elif str(e) == 'Event queue full': + logging.error("Event queue full!") + pass + else: + raise e From 33bf9ecd105824f4d68e2bd20e696fba1618fea8 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 10 Jun 2018 02:07:34 -0700 Subject: [PATCH 2/7] renders black screen, menu underneath maybe --- TestActivity.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 TestActivity.py diff --git a/TestActivity.py b/TestActivity.py new file mode 100644 index 0000000..c69be18 --- /dev/null +++ b/TestActivity.py @@ -0,0 +1,103 @@ +from gettext import gettext as _ + +import sys +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +import pygame + +from sugar3.activity.activity import Activity +from sugar3.graphics.toolbarbox import ToolbarBox +from sugar3.activity.widgets import ActivityToolbarButton +from sugar3.graphics.toolbutton import ToolButton +from sugar3.activity.widgets import StopButton + + +# sys.path.append('..') # Import sugargame package from top directory. +import sugargame +import sugargame.canvas + +import Metris + + +class TestActivity(Activity): + def __init__(self, handle): + Activity.__init__(self, handle) + + self.paused = False + + # Create the game instance. + self.game = Metris.Metris() + + # Build the activity toolbar. + self.build_toolbar() + + # Build the Pygame canvas and start the game running + # (self.game.run is called when the activity constructor + # returns). + self._pygamecanvas = sugargame.canvas.PygameCanvas(self, + main=self.game.runNewGame, + modules=[pygame.display]) + + # Note that set_canvas implicitly calls read_file when + # resuming from the Journal. + self.set_canvas(self._pygamecanvas) + self._pygamecanvas.grab_focus() + + def build_toolbar(self): + toolbar_box = ToolbarBox() + self.set_toolbar_box(toolbar_box) + toolbar_box.show() + + activity_button = ActivityToolbarButton(self) + toolbar_box.toolbar.insert(activity_button, -1) + activity_button.show() + + # Pause/Play button: + + pause_play = ToolButton('media-playback-pause') + pause_play.set_tooltip(_("Pause")) + pause_play.set_accelerator(_('space')) + pause_play.connect('clicked', self._pause_play_cb) + pause_play.show() + + toolbar_box.toolbar.insert(pause_play, -1) + + # Blank space (separator) and Stop button at the end: + + separator = Gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + toolbar_box.toolbar.insert(separator, -1) + separator.show() + + stop_button = StopButton(self) + toolbar_box.toolbar.insert(stop_button, -1) + stop_button.show() + stop_button.connect('clicked', self._stop_cb) + + def _pause_play_cb(self, button): + # Pause or unpause the game. + self.paused = not self.paused + self.game.set_paused(self.paused) + + # Update the button to show the next action. + if self.paused: + button.set_icon_name('media-playback-start') + button.set_tooltip(_("Start")) + else: + button.set_icon_name('media-playback-pause') + button.set_tooltip(_("Pause")) + + def _stop_cb(self, button): + self.game.running = False + + def read_file(self, file_path): + self.game.read_file(file_path) + + def write_file(self, file_path): + self.game.write_file(file_path) + + def get_preview(self): + return self._pygamecanvas.get_preview() + From c368331ee97e7cf30fa653bd0fb790582c9a63ac Mon Sep 17 00:00:00 2001 From: lll Date: Sun, 10 Jun 2018 15:07:59 -0700 Subject: [PATCH 3/7] tried to make metris gamedisplay to get sugar gamedisplay --- Metris.py | 15 +++++++++++---- activity/activity.info | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Metris.py b/Metris.py index e727b9a..57e8d2c 100644 --- a/Metris.py +++ b/Metris.py @@ -72,9 +72,16 @@ def __init__(self): 'mp3s/m8', 'mp3s/m9', 'mp3s/m10', 'mp3s/m11', 'mp3s/m12', 'mp3s/m13'] self.score = 0 - self.GAMEDISPLAY = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.RESIZABLE) + if pygame.display.get_surface() != None: + self.GAMEDISPLAY = pygame.display.get_surface() + else: + self.GAMEDISPLAY = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.RESIZABLE) pygame.display.set_caption('Metris') + # dirty = [] + # dirty.append(pygame.draw.rect(self.GAMEDISPLAY, (255, 255, 255), + # pygame.Rect(0, 0, self.WIDTH, self.HEIGHT))) + # pygame.display.update(dirty) pygame.display.update() self.gameExit = False @@ -2016,17 +2023,17 @@ def buildMain(self): self.main_menu.add_option('Quit', PYGAME_MENU_EXIT) - def main(self): + def runMain(self): # global self.main_menu self.main_menu.mainloop(pygame.event.get()) def runNewGame(self): while True: self.buildMain() - self.main() + self.runMain() if __name__ == '__main__': while True: game = Metris() game.buildMain() - game.main() + game.runMain() diff --git a/activity/activity.info b/activity/activity.info index 40c7bb3..6f87972 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -4,7 +4,7 @@ activity_version = 2 bundle_id = ctcubc.Metris icon = metris show_launcher = yes -exec = sugar-activity MetrisActivity.metris-activity +exec = sugar-activity TestActivity.TestActivity repository = https://github.com/CodeTheChangeUBC/OLPC license = GPLv3+ category = maths game From 9361fd309b7654706387b490c3bd540b091be412 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 11 Jun 2018 13:25:24 -0700 Subject: [PATCH 4/7] it's working, windows are buggy --- Metris.py | 3 +++ activity/activity.info | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Metris.py b/Metris.py index 57e8d2c..32bfe97 100644 --- a/Metris.py +++ b/Metris.py @@ -1,4 +1,5 @@ import pygame +from gi.repository import Gtk import inputbox import random, time, sys, math import json @@ -2031,6 +2032,8 @@ def runNewGame(self): while True: self.buildMain() self.runMain() + while Gtk.events_pending(): + Gtk.main_iteration() if __name__ == '__main__': while True: diff --git a/activity/activity.info b/activity/activity.info index 6f87972..1aff6c8 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -2,7 +2,7 @@ name = Metris activity_version = 2 bundle_id = ctcubc.Metris -icon = metris +icon = activity show_launcher = yes exec = sugar-activity TestActivity.TestActivity repository = https://github.com/CodeTheChangeUBC/OLPC From b27e75f3776d9e5d54ecefd27cb9a20de3869f75 Mon Sep 17 00:00:00 2001 From: lll Date: Mon, 11 Jun 2018 14:13:33 -0700 Subject: [PATCH 5/7] fixed windows --- Metris.py | 65 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/Metris.py b/Metris.py index 32bfe97..6a72025 100644 --- a/Metris.py +++ b/Metris.py @@ -22,6 +22,22 @@ def __init__(self): pygame.init() + if pygame.display.get_surface() != None: + self.GAMEDISPLAY = pygame.display.get_surface() + self.HEIGHT = self.GAMEDISPLAY.get_height() + self.WIDTH = self.HEIGHT * 4 / 3 + else: + self.HEIGHT = 900 + self.WIDTH = self.HEIGHT * 4 / 3 + self.GAMEDISPLAY = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.RESIZABLE) + + pygame.display.set_caption('Metris') + # dirty = [] + # dirty.append(pygame.draw.rect(self.GAMEDISPLAY, (255, 255, 255), + # pygame.Rect(0, 0, self.WIDTH, self.HEIGHT))) + # pygame.display.update(dirty) + pygame.display.update() + self.WHITE = (255, 255, 255) self.RED = (255, 0, 0) self.BLACK = (0, 0, 0) @@ -51,8 +67,7 @@ def __init__(self): 'Press B to go back', 'Press M to mute/unmute', ] - self.HEIGHT = 900 - self.WIDTH = self.HEIGHT * 4 / 3 + self.BLOCK_SIZE = self.HEIGHT / 28 self.LEFT_BOUNDARY = self.WIDTH / 2 - 5 * self.BLOCK_SIZE self.RIGHT_BOUNDARY = self.WIDTH /2 + 5 * self.BLOCK_SIZE @@ -73,17 +88,6 @@ def __init__(self): 'mp3s/m8', 'mp3s/m9', 'mp3s/m10', 'mp3s/m11', 'mp3s/m12', 'mp3s/m13'] self.score = 0 - if pygame.display.get_surface() != None: - self.GAMEDISPLAY = pygame.display.get_surface() - else: - self.GAMEDISPLAY = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.RESIZABLE) - - pygame.display.set_caption('Metris') - # dirty = [] - # dirty.append(pygame.draw.rect(self.GAMEDISPLAY, (255, 255, 255), - # pygame.Rect(0, 0, self.WIDTH, self.HEIGHT))) - # pygame.display.update(dirty) - pygame.display.update() self.gameExit = False @@ -1908,13 +1912,13 @@ def buildMain(self): menu_alpha=100, menu_color=MENU_BACKGROUND_COLOR, menu_color_title=COLOR_RED, - menu_height=int(WINDOW_SIZE[1] * 1), - menu_width=int(WINDOW_SIZE[0] * 1), + menu_height=int(self.HEIGHT), #WINDOW_SIZE[1] * 1), + menu_width=int(self.WIDTH), #WINDOW_SIZE[0] * 1), onclose=PYGAME_MENU_DISABLE_CLOSE, option_shadow=False, title='Play menu', - window_height=WINDOW_SIZE[1], - window_width=WINDOW_SIZE[0] + window_height=self.HEIGHT, #WINDOW_SIZE[1], + window_width=self.WIDTH #WINDOW_SIZE[0] ) play_menu.add_option('Start', self.play_function) @@ -1930,15 +1934,15 @@ def buildMain(self): font_size_title=30, font_title=font_tit, menu_color_title=COLOR_GREEN, - menu_height=int(WINDOW_SIZE[1] * 1), - menu_width=int(WINDOW_SIZE[0] * 1), + menu_height=int(self.HEIGHT), #WINDOW_SIZE[1] * 1), + menu_width=int(self.WIDTH), #WINDOW_SIZE[0] * 1), onclose=PYGAME_MENU_DISABLE_CLOSE, option_shadow=True, text_color=COLOR_WHITE, text_fontsize=20, title='Instruction', - window_height=WINDOW_SIZE[1], - window_width=WINDOW_SIZE[0], + window_height=self.HEIGHT, #WINDOW_SIZE[1], + window_width=self.WIDTH, #WINDOW_SIZE[0], # menu_color=MENU_BACKGROUND_COLOR, font_size=30, menu_alpha=100, @@ -1961,15 +1965,15 @@ def buildMain(self): font_title=font_tit, # menu_color=MENU_BACKGROUND_COLOR, menu_color_title=COLOR_BLUE, - menu_height=int(WINDOW_SIZE[1] * 1), - menu_width=int(WINDOW_SIZE[0] * 1), + menu_height=int(self.HEIGHT), #WINDOW_SIZE[1] * 1), + menu_width=int(self.WIDTH), #WINDOW_SIZE[0] * 1), onclose=PYGAME_MENU_DISABLE_CLOSE, option_shadow=True, text_color=COLOR_WHITE, text_fontsize=20, title='Leaderboard', - window_height=WINDOW_SIZE[1], - window_width=WINDOW_SIZE[0], + window_height=self.HEIGHT, #WINDOW_SIZE[1], + window_width=self.WIDTH, #WINDOW_SIZE[0], font_size=30, menu_alpha=100, menu_color=COLOR_BLACK @@ -2008,15 +2012,15 @@ def buildMain(self): font_size=30, menu_alpha=100, menu_color=COLOR_BLACK, - menu_height=int(WINDOW_SIZE[1] * 1), - menu_width=int(WINDOW_SIZE[0] * 1), + menu_height=int(self.HEIGHT), #WINDOW_SIZE[1] * 1), + menu_width=int(self.WIDTH), #WINDOW_SIZE[0] * 1), menu_color_title=COLOR_RED, onclose=PYGAME_MENU_DISABLE_CLOSE, font_title=font_tit, option_shadow=True, title='Metris', - window_height=WINDOW_SIZE[1], - window_width=WINDOW_SIZE[0] + window_height=self.HEIGHT, #WINDOW_SIZE[1], + window_width=self.WIDTH #WINDOW_SIZE[0] ) self.main_menu.add_option('Play', play_menu) self.main_menu.add_option('Instruction', instruction_menu) @@ -2029,7 +2033,8 @@ def runMain(self): self.main_menu.mainloop(pygame.event.get()) def runNewGame(self): - while True: + self.running = True + while self.running: self.buildMain() self.runMain() while Gtk.events_pending(): From f188a354e781f9151fadfe49c2eff6e1bc9a4dd8 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 16 Jun 2018 18:49:57 -0700 Subject: [PATCH 6/7] IT WORKS ON SUGAR --- Metris.py | 32 ++++++++++--- TestActivity.py | 103 ----------------------------------------- activity/activity.info | 2 +- metris-activity.py | 79 ++++++++++++++++++++++++------- metris.png | Bin 1477 -> 0 bytes metris.svg | 49 -------------------- pygameMenu/menu.py | 6 ++- 7 files changed, 92 insertions(+), 179 deletions(-) delete mode 100644 TestActivity.py delete mode 100644 metris.png delete mode 100644 metris.svg diff --git a/Metris.py b/Metris.py index 6a72025..03865a8 100644 --- a/Metris.py +++ b/Metris.py @@ -19,7 +19,9 @@ class Metris: def __init__(self): + pass + def makeGameObject(self): pygame.init() if pygame.display.get_surface() != None: @@ -151,6 +153,7 @@ def __init__(self): def runGame(self): + self.gameExit = False self.blockSet = self.getRandomBlockSet(None) @@ -201,6 +204,9 @@ def runGame(self): while not self.gameExit: + while Gtk.events_pending(): + Gtk.main_iteration() + if self.bankedpoints > 0: self.score += 1 self.bankedpoints -= 1 @@ -1626,8 +1632,12 @@ def drawMusicIcon(self): def checkForQuit(self): for event in pygame.event.get(QUIT): # get all the QUIT events + while Gtk.events_pending(): + Gtk.main_iteration() self.terminate() # terminate if any QUIT events are present for event in pygame.event.get(KEYUP): # get all the KEYUP events + while Gtk.events_pending(): + Gtk.main_iteration() if event.key == K_ESCAPE: self.terminate() # terminate if the KEYUP event was for the Esc key pygame.event.post(event) # put the other KEYUP event objects back @@ -1649,6 +1659,8 @@ def checkForNewHiscore(self): return -1 for i in range (len(data)-1, 0, -1): + while Gtk.events_pending(): + Gtk.main_iteration() if self.score * (self.total_lines / 10.0) > data[i]["score"] and self.score * (self.total_lines / 10.0) <= data[i-1]["score"]: return i if self.score * (self.total_lines / 10.0) > int(data[0]["score"]): @@ -1811,6 +1823,9 @@ def leaderboard_function(self): # Application events playevents = pygame.event.get() for e in playevents: + while Gtk.events_pending(): + Gtk.main_iteration() + if e.type == QUIT: exit() elif e.type == KEYDOWN: @@ -1865,6 +1880,9 @@ def play_function(self): # Application events playevents = pygame.event.get() for e in playevents: + while Gtk.events_pending(): + Gtk.main_iteration() + if e.type == QUIT: exit() elif e.type == KEYDOWN: @@ -2033,15 +2051,15 @@ def runMain(self): self.main_menu.mainloop(pygame.event.get()) def runNewGame(self): + self.makeGameObject() self.running = True while self.running: self.buildMain() self.runMain() - while Gtk.events_pending(): - Gtk.main_iteration() + self.runGame() -if __name__ == '__main__': - while True: - game = Metris() - game.buildMain() - game.runMain() +# if __name__ == '__main__': +# while True: +# game = Metris() +# game.buildMain() +# game.runMain() diff --git a/TestActivity.py b/TestActivity.py deleted file mode 100644 index c69be18..0000000 --- a/TestActivity.py +++ /dev/null @@ -1,103 +0,0 @@ -from gettext import gettext as _ - -import sys -import gi -gi.require_version('Gtk', '3.0') -from gi.repository import Gtk -import pygame - -from sugar3.activity.activity import Activity -from sugar3.graphics.toolbarbox import ToolbarBox -from sugar3.activity.widgets import ActivityToolbarButton -from sugar3.graphics.toolbutton import ToolButton -from sugar3.activity.widgets import StopButton - - -# sys.path.append('..') # Import sugargame package from top directory. -import sugargame -import sugargame.canvas - -import Metris - - -class TestActivity(Activity): - def __init__(self, handle): - Activity.__init__(self, handle) - - self.paused = False - - # Create the game instance. - self.game = Metris.Metris() - - # Build the activity toolbar. - self.build_toolbar() - - # Build the Pygame canvas and start the game running - # (self.game.run is called when the activity constructor - # returns). - self._pygamecanvas = sugargame.canvas.PygameCanvas(self, - main=self.game.runNewGame, - modules=[pygame.display]) - - # Note that set_canvas implicitly calls read_file when - # resuming from the Journal. - self.set_canvas(self._pygamecanvas) - self._pygamecanvas.grab_focus() - - def build_toolbar(self): - toolbar_box = ToolbarBox() - self.set_toolbar_box(toolbar_box) - toolbar_box.show() - - activity_button = ActivityToolbarButton(self) - toolbar_box.toolbar.insert(activity_button, -1) - activity_button.show() - - # Pause/Play button: - - pause_play = ToolButton('media-playback-pause') - pause_play.set_tooltip(_("Pause")) - pause_play.set_accelerator(_('space')) - pause_play.connect('clicked', self._pause_play_cb) - pause_play.show() - - toolbar_box.toolbar.insert(pause_play, -1) - - # Blank space (separator) and Stop button at the end: - - separator = Gtk.SeparatorToolItem() - separator.props.draw = False - separator.set_expand(True) - toolbar_box.toolbar.insert(separator, -1) - separator.show() - - stop_button = StopButton(self) - toolbar_box.toolbar.insert(stop_button, -1) - stop_button.show() - stop_button.connect('clicked', self._stop_cb) - - def _pause_play_cb(self, button): - # Pause or unpause the game. - self.paused = not self.paused - self.game.set_paused(self.paused) - - # Update the button to show the next action. - if self.paused: - button.set_icon_name('media-playback-start') - button.set_tooltip(_("Start")) - else: - button.set_icon_name('media-playback-pause') - button.set_tooltip(_("Pause")) - - def _stop_cb(self, button): - self.game.running = False - - def read_file(self, file_path): - self.game.read_file(file_path) - - def write_file(self, file_path): - self.game.write_file(file_path) - - def get_preview(self): - return self._pygamecanvas.get_preview() - diff --git a/activity/activity.info b/activity/activity.info index 1aff6c8..b96e499 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -4,7 +4,7 @@ activity_version = 2 bundle_id = ctcubc.Metris icon = activity show_launcher = yes -exec = sugar-activity TestActivity.TestActivity +exec = sugar-activity metris-activity.MetrisActivity repository = https://github.com/CodeTheChangeUBC/OLPC license = GPLv3+ category = maths game diff --git a/metris-activity.py b/metris-activity.py index 81050c3..4f6f825 100644 --- a/metris-activity.py +++ b/metris-activity.py @@ -1,35 +1,52 @@ -import gi +from gettext import gettext as _ -import sugargame -import sugargame.canvas +import sys +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk import pygame -from sugar3.activity import activity + +from sugar3.activity.activity import Activity from sugar3.graphics.toolbarbox import ToolbarBox from sugar3.activity.widgets import ActivityToolbarButton from sugar3.graphics.toolbutton import ToolButton from sugar3.activity.widgets import StopButton -from gettext import gettext as _ -from Metris import * -class MetrisActivity(activity.Activity): + +# sys.path.append('..') # Import sugargame package from top directory. +import sugargame +import sugargame.canvas + +import Metris + +class MetrisActivity(Activity): def __init__(self, handle): - activity.Activity.__init__(self, handle) - self.game = Metris() # call menu instead - self.game.runNewGame() + Activity.__init__(self, handle) + self.paused = False + + self.game = Metris.Metris() # call menu instead + self._pygamecanvas = sugargame.canvas.PygameCanvas(self, + main=self.game.runNewGame, + modules=[pygame.display]) + + # Note that set_canvas implicitly calls read_file when + # resuming from the Journal. + self.set_canvas(self._pygamecanvas) + self._pygamecanvas.grab_focus() # self.game.buildMain() # self.game.main() - toolbarbox = ToolbarBox() - activity_button = ActivityToolbarButton(self) - toolbarbox.toolbar.insert(activity_button, 0) - self.set_toolbar_box(toolbarbox) + # toolbarbox = ToolbarBox() + # activity_button = ActivityToolbarButton(self) + # toolbarbox.toolbar.insert(activity_button, 0) + # self.set_toolbar_box(toolbarbox) #activity_button.show() #toolbar_box.show() - - stop_button = StopButton(self) - toolbarbox.toolbar.insert(stop_button, -1) + # + # stop_button = StopButton(self) + # toolbarbox.toolbar.insert(stop_button, -1) # save_bundle_button = ToolButton('save-as-bundle') # save_bundle_button.set_tooltip(_('Create bundle (.xo file)')) @@ -37,4 +54,30 @@ def __init__(self, handle): # save_bundle_button.connect('clicked', self.save_bundle) # save_bundle_button.show() - self.show_all() \ No newline at end of file + # self.show_all() + + + def _pause_play_cb(self, button): + # Pause or unpause the game. + self.paused = not self.paused + self.game.set_paused(self.paused) + + # Update the button to show the next action. + if self.paused: + button.set_icon_name('media-playback-start') + button.set_tooltip(_("Start")) + else: + button.set_icon_name('media-playback-pause') + button.set_tooltip(_("Pause")) + + def _stop_cb(self, button): + self.game.running = False + + def read_file(self, file_path): + self.game.read_file(file_path) + + def write_file(self, file_path): + self.game.write_file(file_path) + + def get_preview(self): + return self._pygamecanvas.get_preview() \ No newline at end of file diff --git a/metris.png b/metris.png deleted file mode 100644 index 5ed82fcffa51cb739a0e5110b4e6243433484d61..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1477 zcmV;$1v>hPP)Hg1+lHrgWSWcKdPn90sKGrRqvPeo9CG3uKX#J{(IASm?@+di}}l?o-=)F3E6 zwD^Ni=!>T7nL9I?X}YoAW$t|Qo$sD|?zw001?ah|SeB6#0T!CBEf+H4bBB+JJu8re zhoBb*p;u8ID_yBf0ya+zcePvJL&AGs+11_tpRKn>9TgyPA7ZoSs0)aX0r00)%XR^J z`jH<$>RKN5V(7OqK*TS4xZz{h!*f1C3ECFkK$#7nA@pGN!$;%jYv zwjAKwmYb0gKL(K8-kPtb5${A?tlI~wzMrJ6wTdBr=Y%%%EaEMQ&o}4FQ^DA)s*}Z> z!FI&AHCpoWI|RUqx?7s@$8!5^Q=anY%X@i5{QA6kNcMelpE>R6eCYFpmMsVT zrI(b06~u#xf1yS}_UGdMvD``!0~u->P=lA4?YN`hilQ z|3tHka)7T{2CGqwjZfMwx$5irQN_*|e4l)UHmiYuz74Yp1t^#>hrJ3-SOXDcC_o0^ z7T9R1gAN8V6s;5)ieI5-7aQlmJn}lUna#nz!j%5V$X|o`xX!dHWQRV27P1=rj;t2b zW$~+pTw@bIek?ZvKPDL<64`^#UNTAck#RBsB6*5DP4<%UA_FqU$I>2EH_cM;u)Q~SI+rg`Rn{L z_AC5qq~L$#SMj%U$6Cz0vP{G5Y*=%5RT^yu;}-DInZ=349rJPVM6C3K^oO)8y(fJr{l>k`ead~!ea?NsT>_Ci%bnxC;Vy6= zb6>{xYV#Ue-+LB$7`JEXmTRm^AtP)R9u{)KHsMiWGV&)32xCG~*nyU<>-!d;FP=Re z4r3qYr~6#KE>;1F`>_J_P5xC?ROxV(DIHdCO*p$HRQI@7^PwV@Pvuf+ z5K}u-6REM(K@W$srgorh0{i?O)v0c>QtHxU-hBdD(>iYJ4b2sIOVX2K8m~4gmYVA5 zh^QEb$V`rCQ-|7ZS{nuL-t>?3n=-o(6I(7vocj#GzCZEo`!3>+v;dYIfPu#&ZWzzX z2i^rZ^Mu;6+rb@?NPG+6)c5T6zxpzGe*M(x+{AON=PiJ>H#?ob-|uwRK0yDg0B4PV z0id6JRRdfL?*IS+XGugsRA>e5Sj!FqAq*7b!O!-yy{&eWCS${_TPPSz_X3nYX9^)( zw)b~g_`5Eg`<{9h@M{K>fn=Z)fo!2p0K3hTfo=#p9o#zYck@z8lbeWFOh^He{bRek zig=W#OP?v{8DPkWTu_cen#Eb3fv_`BL&$qBYCgWn!VCD;GSNk43t5Vz5w`yAdME>V z3t6};>+nO2F=n*W&=$gCM$L=;FVy-l8P;fx&W_ZNmxIet1M~^F97@6qK - - - - diff --git a/pygameMenu/menu.py b/pygameMenu/menu.py index 7a1ef2e..6e3296c 100644 --- a/pygameMenu/menu.py +++ b/pygameMenu/menu.py @@ -21,7 +21,9 @@ import config_controls as _ctrl import config_menu as _cfg import locals as _locals - +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk # Library imports from selector import Selector as _Selector import pygame as _pygame @@ -683,6 +685,8 @@ def mainloop(self, events): return if self._actual._dopause: while not self._done: + while Gtk.events_pending(): + Gtk.main_iteration() time.sleep(0.016666) if self._main(): return From 1d30eec4bcb7c3c4530817c7165a48e71521ab78 Mon Sep 17 00:00:00 2001 From: lll Date: Mon, 18 Jun 2018 19:49:49 -0700 Subject: [PATCH 7/7] game now takes up entire screen; keys are all responsive; font size scale with screen size except pop up game over; added toolbar, stop button terminates the game --- Metris.py | 59 +++++++++++++++++++++++++++------------------- inputbox.py | 8 +++++++ metris-activity.py | 28 +++++++++++++++++++++- 3 files changed, 70 insertions(+), 25 deletions(-) diff --git a/Metris.py b/Metris.py index 03865a8..7078215 100644 --- a/Metris.py +++ b/Metris.py @@ -1,4 +1,6 @@ import pygame +import gi +gi.require_version('Gtk', '3.0') from gi.repository import Gtk import inputbox import random, time, sys, math @@ -27,7 +29,7 @@ def makeGameObject(self): if pygame.display.get_surface() != None: self.GAMEDISPLAY = pygame.display.get_surface() self.HEIGHT = self.GAMEDISPLAY.get_height() - self.WIDTH = self.HEIGHT * 4 / 3 + self.WIDTH = self.GAMEDISPLAY.get_width() else: self.HEIGHT = 900 self.WIDTH = self.HEIGHT * 4 / 3 @@ -1195,6 +1197,10 @@ def paused(self): self.startTime = pygame.mixer.music.get_pos() while pause: + + while Gtk.events_pending(): + Gtk.main_iteration() + self.checkForQuit() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: @@ -1218,18 +1224,18 @@ def paused(self): self.flipSoundIcon() self.GAMEDISPLAY.fill(self.WHITE, [self.LEFT_BOUNDARY, self.TOP_BOUNDARY, 10 * self.BLOCK_SIZE, 20 * self.BLOCK_SIZE]) - myfont = pygame.font.SysFont('Comic Sans MS', 30) + myfont = pygame.font.SysFont('Comic Sans MS', self.BLOCK_SIZE * 3 / 4) pausedText = myfont.render("Paused", True, self.BLACK) textWidth = pausedText.get_rect().width textHeight = pausedText.get_rect().height self.GAMEDISPLAY.blit(pausedText, (self.LEFT_BOUNDARY + (self.RIGHT_BOUNDARY - self.LEFT_BOUNDARY) / 2 - textWidth / 2, self.TOP_BOUNDARY + (self.BOTTOM_BOUNDARY - self.TOP_BOUNDARY) / 2 - textHeight / 2)) - myfont = pygame.font.SysFont('Comic Sans MS', 15) + myfont = pygame.font.SysFont('Comic Sans MS', self.BLOCK_SIZE / 2) additionalText = myfont.render("Press \"p\" to resume!", True, self.BLACK) additionalTextWidth = additionalText.get_rect().width additionalTextHeight = additionalText.get_rect().height - myfont = pygame.font.SysFont('Comic Sans MS', 15) + myfont = pygame.font.SysFont('Comic Sans MS', self.BLOCK_SIZE / 2) additionalText2 = myfont.render("Press \"Esc\" to quit!", True, self.BLACK) self.GAMEDISPLAY.blit(additionalText, @@ -1280,12 +1286,15 @@ def gameOver(self): pygame.mixer.music.stop() self.playSound('end.wav') - initialSize = 16 + initialSize = self.BLOCK_SIZE self.updateHiscore(self.checkForNewHiscore()) while pause: + while Gtk.events_pending(): + Gtk.main_iteration() + self.checkForQuit() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: @@ -1317,7 +1326,7 @@ def gameOver(self): gameOverText = myfont.render("Game Over", True, self.BLACK) textWidth = gameOverText.get_rect().width textHeight = gameOverText.get_rect().height - myfont = pygame.font.SysFont('Comic Sans MS', initialSize - 15) + myfont = pygame.font.SysFont('Comic Sans MS', initialSize - self.BLOCK_SIZE / 2) additionalText = myfont.render("Press \"r\" to restart!", True, self.BLACK) additionalTextWidth = additionalText.get_rect().width additionalTextHeight = additionalText.get_rect().height @@ -1340,7 +1349,7 @@ def gameOver(self): self.GAMEDISPLAY.blit(gameOverText2, (self.LEFT_BOUNDARY + (self.RIGHT_BOUNDARY - self.LEFT_BOUNDARY) / 2 - textWidth / 2 + 2, self.TOP_BOUNDARY + (self.BOTTOM_BOUNDARY - self.TOP_BOUNDARY) / 2 - textHeight / 2)) pygame.display.update() - if initialSize < 30: + if initialSize < self.BLOCK_SIZE: initialSize += 1 self.clock.tick(15) @@ -1712,35 +1721,37 @@ def drawHighScore(self, text, lvl, pts, tL): if i == 1: # Level text. pressKeySurf, pressKeyRect = self.makeTextObjs('Level: ' + str(lvl), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 95) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 3) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) if i == 2: # Score text. pressKeySurf, pressKeyRect = self.makeTextObjs('Score: ' + str(pts), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 120) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 4) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) if i == 3: # Lines text. pressKeySurf, pressKeyRect = self.makeTextObjs('Lines Cleared: ' + str(tL), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 145) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 5) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) if i == 4: # Total text. pressKeySurf, pressKeyRect = self.makeTextObjs('Total = ' + str(pts) + " x " + str(tL/10.0), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 170) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 6) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) if i == 5: pressKeySurf, pressKeyRect = self.makeTextObjs("press any key to skip", self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 270) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 9) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) while i == 5 and new_score != int(pts * (tL/10.0)): + while Gtk.events_pending(): + Gtk.main_iteration() event = pygame.event.poll() if event.type == pygame.KEYDOWN: # if event.key == pygame.K_RETURN: new_score = int(pts * (tL/10.0)) pressKeySurf, pressKeyRect = self.makeTextObjs(str(int(new_score)), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 220) - pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [0, int(self.HEIGHT / 2) + 180, self.WIDTH, 2 * self.BLOCK_SIZE]) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 7) + pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [0, int(self.HEIGHT / 2) + self.BLOCK_SIZE * 6.75, self.WIDTH, 2 * self.BLOCK_SIZE]) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) if new_score > int(pts * (tL/10.0)): new_score -= 1 @@ -1750,8 +1761,8 @@ def drawHighScore(self, text, lvl, pts, tL): self.clock.tick(self.speed) if i == 5 and new_score == int(pts * (tL/10.0)): pressKeySurf, pressKeyRect = self.makeTextObjs(str(int(new_score)), self.BASICFONT, self.TEXTCOLOR) - pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + 220) - pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [0, int(self.HEIGHT / 2) + 180, self.WIDTH, 2 * self.BLOCK_SIZE]) + pressKeyRect.center = (int(self.WIDTH / 2), int(self.HEIGHT / 2) + self.BLOCK_SIZE * 7) + pygame.draw.rect(self.GAMEDISPLAY, self.BLACK, [0, int(self.HEIGHT / 2) + self.BLOCK_SIZE * 6.75, self.WIDTH, 2 * self.BLOCK_SIZE]) self.GAMEDISPLAY.blit(pressKeySurf, pressKeyRect) i += 1 pygame.display.update() @@ -1926,7 +1937,7 @@ def buildMain(self): font=fontdir, font_color=COLOR_WHITE, font_title=font_tit, - font_size=30, + font_size=self.BLOCK_SIZE, menu_alpha=100, menu_color=MENU_BACKGROUND_COLOR, menu_color_title=COLOR_RED, @@ -1949,7 +1960,7 @@ def buildMain(self): color_selected=COLOR_GREEN, font=fontdir, font_color=COLOR_WHITE, - font_size_title=30, + font_size_title=self.BLOCK_SIZE, font_title=font_tit, menu_color_title=COLOR_GREEN, menu_height=int(self.HEIGHT), #WINDOW_SIZE[1] * 1), @@ -1957,12 +1968,12 @@ def buildMain(self): onclose=PYGAME_MENU_DISABLE_CLOSE, option_shadow=True, text_color=COLOR_WHITE, - text_fontsize=20, + text_fontsize=self.BLOCK_SIZE * 2 / 3, title='Instruction', window_height=self.HEIGHT, #WINDOW_SIZE[1], window_width=self.WIDTH, #WINDOW_SIZE[0], # menu_color=MENU_BACKGROUND_COLOR, - font_size=30, + font_size=self.BLOCK_SIZE, menu_alpha=100, menu_color=COLOR_BLACK ) @@ -1979,7 +1990,7 @@ def buildMain(self): color_selected=COLOR_BLUE, font=fontdir, font_color=COLOR_WHITE, - font_size_title=30, + font_size_title=self.BLOCK_SIZE, font_title=font_tit, # menu_color=MENU_BACKGROUND_COLOR, menu_color_title=COLOR_BLUE, @@ -1988,11 +1999,11 @@ def buildMain(self): onclose=PYGAME_MENU_DISABLE_CLOSE, option_shadow=True, text_color=COLOR_WHITE, - text_fontsize=20, + text_fontsize=self.BLOCK_SIZE * 2 / 3, title='Leaderboard', window_height=self.HEIGHT, #WINDOW_SIZE[1], window_width=self.WIDTH, #WINDOW_SIZE[0], - font_size=30, + font_size=self.BLOCK_SIZE, menu_alpha=100, menu_color=COLOR_BLACK ) @@ -2027,7 +2038,7 @@ def buildMain(self): color_selected=self.RED, font=fontdir, font_color=COLOR_WHITE, - font_size=30, + font_size=self.BLOCK_SIZE, menu_alpha=100, menu_color=COLOR_BLACK, menu_height=int(self.HEIGHT), #WINDOW_SIZE[1] * 1), diff --git a/inputbox.py b/inputbox.py index 25f979b..069a01a 100644 --- a/inputbox.py +++ b/inputbox.py @@ -15,11 +15,17 @@ import pygame, pygame.font, pygame.event, pygame.draw, string from pygame.locals import * +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + import re def get_key(): while 1: + while Gtk.events_pending(): + Gtk.main_iteration() event = pygame.event.poll() if event.type == KEYDOWN: return event.key @@ -52,6 +58,8 @@ def ask(screen, question): display_box(screen, question + ": " + string.join(current_string, "")) counter = 15 while True: + while Gtk.events_pending(): + Gtk.main_iteration() inkey = get_key() if inkey == K_BACKSPACE and counter <= 15: if not counter == 15: diff --git a/metris-activity.py b/metris-activity.py index 4f6f825..d3dda2f 100644 --- a/metris-activity.py +++ b/metris-activity.py @@ -56,6 +56,32 @@ def __init__(self, handle): # self.show_all() + def build_toolbar(self): + toolbar_box = ToolbarBox() + self.set_toolbar_box(toolbar_box) + toolbar_box.show() + activity_button = ActivityToolbarButton(self) + toolbar_box.toolbar.insert(activity_button, -1) + activity_button.show() + + # Pause/Play button: + pause_play = ToolButton('media-playback-pause') + pause_play.set_tooltip(_("Pause")) + pause_play.set_accelerator(_('space')) + pause_play.connect('clicked', self._pause_play_cb) + pause_play.show() + toolbar_box.toolbar.insert(pause_play, -1) + + # Blank space (separator) and Stop button at the end: + separator = Gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + toolbar_box.toolbar.insert(separator, -1) + separator.show() + stop_button = StopButton(self) + toolbar_box.toolbar.insert(stop_button, -1) + stop_button.show() + stop_button.connect('clicked', self._stop_cb) def _pause_play_cb(self, button): # Pause or unpause the game. @@ -71,7 +97,7 @@ def _pause_play_cb(self, button): button.set_tooltip(_("Pause")) def _stop_cb(self, button): - self.game.running = False + self.game.terminate() def read_file(self, file_path): self.game.read_file(file_path)