-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
141 lines (119 loc) · 4.64 KB
/
block.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from turtle import Turtle
# class Stamp:
# def __init__(self, cellWidth, turtlesize, shape, edgeColor, edgeThickness, fillcolor):
# self.turtlesize = turtlesize
# self.cellWidth = cellWidth
# self.shape = shape
# self.edgeColor = edgeColor
# self.edgeThickness = edgeThickness
# self.fillcolor = fillcolor
# stampTurtle = Turtle()
# stampTurtle.resizemode("user")
# stampTurtle.turtlesize(cellWidth / 20, cellWidth / 20, edgeThickness)
# stampTurtle.shape("square")
# stampTurtle.pencolor(edgeColor)
# stampTurtle.fillcolor(fillcolor)
# self.stampTurtle = stampTurtle
# def stamp(self):
# pass
class Block:
def __init__(self, x, y, cellWidth, color, edgeColor, edgeThickness, relpivotX, relpivotY, blocks=[], active=True):
self.cellWidth = cellWidth
self.x = x
self.y = y
self.active = active
if blocks != []:
self.blocks = blocks
else:
self.blocks = []
self.edgeThickness = edgeThickness
self.edgeColor = edgeColor
self.color = color
self.pivotX = self.x + relpivotX * cellWidth
self.pivotY = self.y + relpivotY * cellWidth
def move(self, block, x, y):
block.up()
block.goto(x, y)
block.down()
def addBlock(self, relativeX, relativeY):
newBlock = Turtle()
newBlock.hideturtle()
newBlock.resizemode("user")
newBlock.turtlesize(self.cellWidth / 20, self.cellWidth / 20, self.edgeThickness)
newBlock.shape("square")
newBlock.pencolor(self.edgeColor)
newBlock.fillcolor(self.color)
self.move(newBlock, self.x + relativeX * self.cellWidth, self.y + relativeY * self.cellWidth)
self.blocks.append(newBlock)
def isActive(self):
return self.active
def makeInactive(self):
self.active = False
def moveDown(self):
if self.isActive():
self.pivotY -= self.cellWidth
for block in self.blocks:
self.move(block, block.xcor(), block.ycor() - self.cellWidth)
def isValidMove(self, grid):
for block in self.blocks:
pass
def isInBoundary(self):
for block in self.blocks:
if block.ycor() + self.cellWidth * 10 - self.cellWidth // 2 == 0:
self.active = False
return False
return True
def moveRight(self):
if self.isActive():
self.pivotX += self.cellWidth
for block in self.blocks:
self.move(block, block.xcor() + self.cellWidth, block.ycor())
def moveLeft(self):
if self.isActive():
self.pivotX -= self.cellWidth
for block in self.blocks:
self.move(block, block.xcor() - self.cellWidth, block.ycor())
def moveUp(self):
if self.isActive():
self.pivotY += self.cellWidth
for block in self.blocks:
self.move(block, block.xcor(), block.ycor() + self.cellWidth)
def rotateLeft(self):
outRight, outLeft, outDown = 0, 0, 0 # if the block is outside of boundary after rotation
for block in self.blocks:
blockX, blockY = block.xcor(), block.ycor()
newX = blockY - self.pivotY + self.pivotX
newY = self.pivotX + self.pivotY - blockX - self.cellWidth
if newX < -self.cellWidth * 4.5:
outLeft += 1
elif newX > self.cellWidth * 4.5:
outRight += 1
if newY < -self.cellWidth * 9.5:
outDown += 1
self.move(block, newX, newY)
for _ in range(outLeft):
self.moveRight()
for _ in range(outRight):
self.moveLeft()
for _ in range(outDown):
for block in self.blocks:
self.move(block, block.xcor(), block.ycor() + self.cellWidth)
def rotateRight(self):
for block in self.blocks:
blockX, blockY = block.xcor(), block.ycor()
newX = self.pivotX + self.pivotY - blockY - self.cellWidth
newY = blockX + self.pivotY - self.pivotX
self.move(block, newX, newY)
def unhideBlock(self):
for block in self.blocks:
block.showturtle()
def hideBlock(self):
for block in self.blocks:
block.hideturtle()
def redrawBlocks(self):
newBlocks = []
for block in self.blocks:
newBlocks.append(block.clone())
block.hideturtle()
del block
self.blocks = newBlocks