-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.py
executable file
·184 lines (128 loc) · 3.92 KB
/
tetris.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
import random
import pgzero
import colours
LONG_BOI = [ "xxxx"]
MR_T = [ " ", "xxx", " x "]
THE_ROCK = ["xx", "xx"]
SNAKE = ["xx ", " xx"]
SNAKE_TOO = [" xx", "xx "]
JAY = [" x", "xxx"]
SILENT_BOB = ["x ", "xxx"]
def rotate_90(data):
return list(zip(*data))
def rotate_180(data):
return [s[::-1] for s in data[::-1]]
def rotations(candidate):
yield candidate
yield rotate_90(candidate)
yield rotate_180(candidate)
yield rotate_90(rotate_180(candidate))
def and90(candidate):
return [candidate, rotate_90(candidate)]
PIECES = [
list(rotations(MR_T)),
[THE_ROCK],
and90(LONG_BOI),
and90(SNAKE),
and90(SNAKE_TOO),
and90(JAY),
and90(SILENT_BOB)]
BOARD = [['*'] + ([' '] * 10) + ['*'] for _ in range(15)]
BOARD.append(['*'] * 12)
class Piece:
def __init__(self, data):
self._data = data
self.x = 5
self.y = 0
self.orientation = 0
self.colour = colours.choose_random_colour()
@property
def data(self):
return self._data[self.orientation]
def rotate(self):
old_orientation = self.orientation
self.orientation += 1
if self.orientation == len(self._data):
self.orientation = 0
if self.collides():
self.orientation = old_orientation
def right(self):
return self.x + len(self.data)
def bottom(self):
return self.y + len(self.data)
def tiles(self):
for y, line in enumerate(self.data):
for x, char in enumerate(line):
if char != ' ':
yield x + self.x, y + self.y
def collides(self):
try:
for x, y in self.tiles():
if BOARD[y][x] != ' ':
return True
return False
except:
return False
def get_piece():
return Piece(random.choice(PIECES))
CURRENT_PIECE = get_piece()
BOARD_WIDTH = 10
GAME_STOP = False
def draw():
if GAME_STOP:
screen.fill((255, 0, 0))
else:
screen.fill((0, 0, 0))
piece = CURRENT_PIECE
for x, y in piece.tiles():
screen.draw.filled_rect(Rect(((x)*32, (y)*32), (31, 31)), piece.colour)
screen.draw.rect(Rect(((x)*32, (y)*32), (31, 31)), (0,0,0))
for y, row in enumerate(BOARD):
for x, char in enumerate(row):
colour = (255, 255, 255)
if char != ' ':
if isinstance(char, tuple):
colour = char
screen.draw.filled_rect(Rect((x*32, y*32), (32, 32)), colour)
if GAME_STOP:
screen.blit('gameover.png', (64, 64))
def on_key_down(key):
if key == keys.LEFT:
CURRENT_PIECE.x -= 1
if CURRENT_PIECE.collides():
CURRENT_PIECE.x += 1
elif key == keys.RIGHT:
CURRENT_PIECE.x += 1
if CURRENT_PIECE.collides():
CURRENT_PIECE.x -= 1
elif key == keys.DOWN:
ticky()
elif key == keys.SPACE:
CURRENT_PIECE.rotate()
elif key == keys.UP:
piece = CURRENT_PIECE
while CURRENT_PIECE == piece:
ticky()
def ticky():
global CURRENT_PIECE
global BOARD
global GAME_STOP
if GAME_STOP:
return
CURRENT_PIECE.y += 1
if CURRENT_PIECE.collides():
CURRENT_PIECE.y -= 1
for x, y in CURRENT_PIECE.tiles():
BOARD[y][x] = CURRENT_PIECE.colour
completed_rows = []
for y, row in enumerate(BOARD[0:-1]):
row_complete = not any(char==' ' for char in row)
if row_complete:
completed_rows.append(y)
BOARD = [row for y, row in enumerate(BOARD) if y not in completed_rows]
for _ in completed_rows:
BOARD.insert(0, ['.'] + ([' '] * BOARD_WIDTH) + ['.'])
CURRENT_PIECE = get_piece()
if CURRENT_PIECE.collides():
GAME_STOP = True
clock.schedule_interval(ticky, 0.25)