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 14f1959..48abe49 100644 --- a/Metris.py +++ b/Metris.py @@ -1,8 +1,12 @@ import pygame +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk import inputbox import random, time, sys, math import json import datetime +import threading from Block.BlockT import * from Block.BlockO import * @@ -17,17 +21,37 @@ class Metris: def __init__(self): + pass + def makeGameObject(self): pygame.init() + if pygame.display.get_surface() != None: + self.GAMEDISPLAY = pygame.display.get_surface() + self.HEIGHT = self.GAMEDISPLAY.get_height() + self.WIDTH = self.GAMEDISPLAY.get_width() + 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 = (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)] @@ -48,13 +72,6 @@ def __init__(self): '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 self.LEFT_BOUNDARY = self.WIDTH / 2 - 5 * self.BLOCK_SIZE self.RIGHT_BOUNDARY = self.WIDTH /2 + 5 * self.BLOCK_SIZE @@ -63,15 +80,18 @@ 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'] self.score = 0 - self.GAMEDISPLAY = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.RESIZABLE) - - pygame.display.set_caption('Metris') - pygame.display.update() self.gameExit = False @@ -116,7 +136,7 @@ def __init__(self): self.triple = False self.double = False self.single = False - + self.main_menu = None self.MOVE = pygame.USEREVENT + 4 @@ -126,7 +146,7 @@ def __init__(self): self.tolerance = False self.tolerable = True - self.lock = False + self.lock = threading.Lock() ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -135,6 +155,7 @@ def __init__(self): def runGame(self): + self.gameExit = False self.blockSet = self.getRandomBlockSet(None) @@ -160,7 +181,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 +197,22 @@ 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: - + + while Gtk.events_pending(): + Gtk.main_iteration() + if self.bankedpoints > 0: self.score += 1 self.bankedpoints -= 1 - + # checkForQuit() for event in pygame.event.get(): ## if event.type == pygame.QUIT: @@ -199,9 +230,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 +274,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 +312,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 +339,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 +435,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 +500,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 +580,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 +604,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 +685,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 +718,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 +742,7 @@ def tick(self): self.checkLandedAndDelete() self.playSound('wavs/landSound.wav') self.checkGameOver() - self.lock = False + #self.lock.release() def checkLandedAndDelete(self): y = 0 @@ -724,7 +759,34 @@ def rowFilled(self, y): if self.landed[x][y] == None: 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) # delete rows @@ -741,13 +803,14 @@ def deleteRows(self, rows): self.landed[x][y] = self.landed[x][y - 1] self.playSound('wavs/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 @@ -755,9 +818,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 @@ -781,7 +844,7 @@ def checkGameOver(self): self.gameOver() done = True break - + def checkCollision(self): @@ -822,55 +885,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 @@ -906,7 +969,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]) @@ -1134,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: @@ -1157,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, @@ -1218,12 +1285,15 @@ def gameOver(self): pygame.mixer.music.stop() self.playSound('wavs/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: @@ -1255,7 +1325,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 @@ -1278,7 +1348,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) @@ -1301,21 +1371,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) @@ -1324,6 +1393,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: @@ -1473,34 +1543,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) @@ -1546,8 +1640,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 @@ -1569,6 +1667,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"]): @@ -1620,33 +1720,39 @@ 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) - 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) + 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)) + # 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) + 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)): + if new_score > int(pts * (tL/10.0)): new_score -= 1 else: new_score += 1 @@ -1654,8 +1760,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) + 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() @@ -1727,6 +1833,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: @@ -1781,6 +1890,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: @@ -1825,17 +1937,17 @@ 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, - 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) @@ -1848,20 +1960,20 @@ 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(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, + text_fontsize=self.BLOCK_SIZE * 2 / 3, 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, + font_size=self.BLOCK_SIZE, menu_alpha=100, menu_color=COLOR_BLACK ) @@ -1870,7 +1982,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, @@ -1878,20 +1990,20 @@ 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, - 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, + text_fontsize=self.BLOCK_SIZE * 2 / 3, title='Leaderboard', - window_height=WINDOW_SIZE[1], - window_width=WINDOW_SIZE[0], - font_size=30, + window_height=self.HEIGHT, #WINDOW_SIZE[1], + window_width=self.WIDTH, #WINDOW_SIZE[0], + font_size=self.BLOCK_SIZE, menu_alpha=100, menu_color=COLOR_BLACK ) @@ -1918,7 +2030,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, @@ -1926,18 +2038,18 @@ 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(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) @@ -1945,17 +2057,20 @@ 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.makeGameObject() + self.running = True + while self.running: self.buildMain() - self.main() + self.runMain() + self.runGame() -if __name__ == '__main__': - while True: - game = Metris() - game.buildMain() - game.main() +# if __name__ == '__main__': +# while True: +# game = Metris() +# game.buildMain() +# game.runMain() diff --git a/activity/activity.info b/activity/activity.info index 40c7bb3..b96e499 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -2,9 +2,9 @@ name = Metris activity_version = 2 bundle_id = ctcubc.Metris -icon = metris +icon = activity show_launcher = yes -exec = sugar-activity MetrisActivity.metris-activity +exec = sugar-activity metris-activity.MetrisActivity repository = https://github.com/CodeTheChangeUBC/OLPC license = GPLv3+ category = maths game diff --git a/inputbox.py b/inputbox.py index bdfc028..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 @@ -50,21 +56,26 @@ def ask(screen, question): global current_string current_string = [] display_box(screen, question + ": " + string.join(current_string, "")) - counter = 20 + counter = 15 while True: + while Gtk.events_pending(): + Gtk.main_iteration() 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/metris-activity.py b/metris-activity.py index 81050c3..d3dda2f 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,56 @@ 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 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.terminate() + + 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 5ed82fc..0000000 Binary files a/metris.png and /dev/null differ diff --git a/metris.svg b/metris.svg deleted file mode 100644 index bfd49bc..0000000 --- a/metris.svg +++ /dev/null @@ -1,49 +0,0 @@ - - - - - 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 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