-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.py
187 lines (171 loc) · 7.34 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os, sys
import pygame
import random
from pygame.locals import *
from game_utility import *
class Mino():
"""A tetrimino. """
def __init__(self, board, blocknum = None):
self.board = board
if blocknum != None: #use this for testing specific pieces, take it out eventually
self.blocknum = blocknum
else:
self.blocknum = random.randrange(1,8)
if self.blocknum == I_MINO:
self.rotationList = [[(2,0),(1,0),(0,0),(-1,0)],
[(0,1),(0,0),(0,-1),(0,-2)],
[(2,0),(1,0),(0,0),(-1,0)],
[(1,1),(1,0),(1,-1),(1,-2)]]
elif self.blocknum == O_MINO:
self.rotationList = [[(0,0),(0,1),(1,0),(1,1)],
[(0,0),(0,1),(1,0),(1,1)],
[(0,0),(0,1),(1,0),(1,1)],
[(0,0),(0,1),(1,0),(1,1)]]
elif self.blocknum == T_MINO:
self.rotationList = [[(1,0),(0,0),(-1,0),(0,1)],
[(0,-1),(0,0),(0,1),(1,0)],
[(-1,0),(0,0),(1,0),(0,-1)],
[(0,1),(0,0),(0,-1),(-1,0)]]
elif self.blocknum == J_MINO:
self.rotationList = [[(1,1),(-1,0),(0,0),(1,0)],
[(1,-1),(0,1),(0,0),(0,-1)],
[(-1,-1),(1,0),(0,0),(-1,0)],
[(-1,1),(0,-1),(0,0),(0,1)]]
elif self.blocknum == L_MINO:
self.rotationList = [[(-1,1),(-1,0),(0,0),(1,0)],
[(1,1),(0,1),(0,0),(0,-1)],
[(1,-1),(1,0),(0,0),(-1,0)],
[(-1,-1),(0,-1),(0,0),(0,1)]]
elif self.blocknum == S_MINO:
self.rotationList = [[(-1,-1),(0,-1),(0,0),(1,0)],
[(-1,1),(-1,0),(0,0),(0,-1)],
[(1,1),(0,1),(0,0),(-1,0)],
[(1,-1),(1,0),(0,0),(0,1)]]
elif self.blocknum == Z_MINO:
self.rotationList = [[(1,-1),(0,-1),(0,0),(-1,0)],
[(-1,-1),(-1,0),(0,0),(0,1)],
[(-1,1),(0,1),(0,0),(1,0)],
[(1,1),(1,0),(0,0),(0,-1)]]
else:
print "Somehthing has gone terribly wrong."
self.rotationList = None
self.rotation = 0
self.x = 4 #swap out literal
self.y = 1
self.blockGroup = pygame.sprite.Group()
self.blockList = []
for i in range(0,4):
block = Block(self.blocknum)
dx, dy = self.rotationList[self.rotation][i]
self.blockList.append(block)
self.blockGroup.add(block)
self.updateBlocks(self.x, self.y)
self.locked = False
self.ghostBlockList = []
for i in range(0, 4):
ghostBlock = Block(GHOST)
self.ghostBlockList.append(ghostBlock)
def activate(self):
self.x = 4
self.y = 1
self.updateBlocks(self.x, self.y)
for ghostBlock in self.ghostBlockList:
self.blockGroup.add(ghostBlock)
self.updateGhostBlocks()
def deactivate(self):
self.x = 13
self.y = 0
for ghostBlock in self.ghostBlockList:
self.blockGroup.remove(ghostBlock)
self.updateBlocks(self.x, self.y)
def updateBlocks(self, x = None, y = None):
for i in range(0, 4):
if x != None:
self.x = x
if y!= None:
self.y = y
(dx, dy) = self.rotationList[self.rotation][i]
self.blockList[i].x = self.x+dx
self.blockList[i].y = self.y+dy
self.blockList[i].rect = (25*(self.x+dx)+BOARD_X,25*(self.y+dy)+BOARD_Y)
#starting numbers here should not be literal, use a var
def updateGhostBlocks(self):
for i in range(0, 4):
(hx, hy) = self.hardDropLocation()
(dx, dy) = self.rotationList[self.rotation][i]
self.ghostBlockList[i].x = hx+dx
self.ghostBlockList[i].y = hy+dy
self.ghostBlockList[i].rect = (25*(hx+dx)+BOARD_X,25*(hy+dy)+BOARD_Y)
def canMove(self, direction):
canMove = True
for block in self.blockList:
if direction == LEFT and (block.x <= 0 or self.board.blockMatrix[block.y][block.x-1]):
canMove = False
elif direction == RIGHT and (block.x >= 9 or self.board.blockMatrix[block.y][block.x+1]):
canMove = False
elif direction == DOWN and (block.y >= 19 or self.board.blockMatrix[block.y+1][block.x]):
canMove = False
self.locked = True
return canMove
#TODO: this needs a little work, if it can't rotate it should push out and then rotate instead of doing nothing
def canRotate(self, direction):
canRotate = True
if direction == CLOCK:
for (dx, dy) in self.rotationList[(self.rotation+1) % 4]:
if self.x+dx < 0 or self.x+dx > 9 or self.y+dy > 19:
canRotate = False
elif direction == COUNTERCLOCK:
for (dx, dy) in self.rotationList[(self.rotation-1) % 4]:
if self.x+dx < 0 or self.x+dx > 9 or self.y+dy > 19:
canRotate = False
return canRotate
def moveMino(self, direction):
if direction == DOWN and self.canMove(DOWN):
self.updateBlocks(x = None, y = self.y+1)
elif direction == RIGHT and self.canMove(RIGHT):
self.updateBlocks(x = self.x+1, y = None)
elif direction == LEFT and self.canMove(LEFT):
self.updateBlocks(x = self.x-1, y = None)
def rotateMino(self, direction):
if direction == CLOCK and self.canRotate(COUNTERCLOCK):
self.rotation = (self.rotation+1) % 4
elif direction == COUNTERCLOCK and self.canRotate(CLOCK):
self.rotation = (self.rotation-1) % 4
self.updateBlocks()
def updateMino(self, hardDrop, dropOne, moveDir, rotateDir):
if self.locked:
self.placeMino()
return True
if hardDrop:
x, y = self.hardDropLocation()
self.updateBlocks(x = x, y = y)
self.locked = True
elif dropOne:
self.moveMino(DOWN)
if moveDir != None:
self.moveMino(moveDir)
if rotateDir != None:
self.rotateMino(rotateDir)
self.updateGhostBlocks()
return False
def hardDropLocation(self):
minDiff = 20
for block in self.blockList:
diff = self.board.highestList[block.x]-1 - block.y
if diff < minDiff:
minDiff = diff
return (self.x, self.y+minDiff)
def placeMino(self):
for block in self.blockList:
self.board.blockMatrix[block.y][block.x] = block
self.board.blockGroup.add(block)
if block.y < self.board.highestList[block.x]:
self.board.highestList[block.x] = block.y
class Block(pygame.sprite.Sprite):
"""A tetrimino block. """
def __init__(self, blocknum):
pygame.sprite.Sprite.__init__(self)
blockstr = "block" + str(blocknum) + ".png"
self.image, self.rect = load_image(blockstr)
self.x = None
self.y = None