-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtetris_pygame.py
283 lines (262 loc) · 8.35 KB
/
tetris_pygame.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
import pygame
import random
import datetime
import copy
from Tetris_module import draw_gridlines,draw_window,clear_rows,get_mino_positions
from Tetris_module import valid_space,lock_mino,create_grid,keyOparation,check_lost
from Tetris_module import draw_next_shape
s_width = 800 #display_size
s_height = 700 #display_size
play_width = 300 #play_size 360//30 = 10 blocks
play_height = 600 #play_size 660//30 = 20 blocks
block_size = 30
top_left_x = (s_width - play_width) / 2 #play_sizeの左上x座標
top_left_y = (s_height - play_height) / 2 #play_sizeの左上y座標
# SHAPE FORMATS
J = [
# MINO_ANGLE_0
[[0,0,0,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,1,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,1,0,0,0],
[0,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,0,0,0],
[0,0,1,1,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,1,0],
[0,0,0,1,0],
[0,0,0,0,0]]
]
L = [
# MINO_ANGLE_0
[[0,0,0,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,1,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,0,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,0,1,0],
[0,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]]
]
S = [
# MINO_ANGLE_0
[[0,0,0,0,0],
[0,0,1,1,0],
[0,1,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,0,1,0,0],
[0,0,1,1,0],
[0,0,0,1,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,0,0,0],
[0,0,1,1,0],
[0,1,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,1,0,0],
[0,0,1,1,0],
[0,0,0,1,0],
[0,0,0,0,0]]
]
Z = [
# MINO_ANGLE_0
[[0,0,0,0,0],
[0,1,1,0,0],
[0,0,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,0,1,0,0],
[0,1,1,0,0],
[0,1,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,0,0,0],
[0,1,1,0,0],
[0,0,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,1,0,0],
[0,1,1,0,0],
[0,1,0,0,0],
[0,0,0,0,0]]
]
I = [
# MINO_ANGLE_0
[[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,0,0,0,0],
[1,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,0,0,0],
[1,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]]
]
T = [
# MINO_ANGLE_0
[[0,0,0,0,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,0,1,0,0],
[0,0,1,1,0],
[0,0,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,1,0],
[0,0,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,1,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[0,0,0,0,0]]
]
O = [
# MINO_ANGLE_0
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,0,0],
[0,1,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_90
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,0,0],
[0,1,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_180
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,0,0],
[0,1,1,0,0],
[0,0,0,0,0]],
# MINO_ANGLE_270
[[0,0,0,0,0],
[0,0,0,0,0],
[0,1,1,0,0],
[0,1,1,0,0],
[0,0,0,0,0]]
]
shapes = [S,Z,I,O,J,L,T]
shape_colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (128, 0, 128)]
class Mino(object):
def __init__(self,x,y,shape):
self.x = x
self.y = y
self.shape = shape
self.color = shape_colors[shapes.index(shape)]
self.rotation = 0 #number from 0-3#回転角度の初期値は常に0度
#ランダムにミノを取得
def get_shape():
global shapes
return Mino(3,0,random.choice(shapes))#starting position and shape
def main():
#ゲーム画面生成
surface = pygame.display.set_mode((s_width,s_height))#displayを定義
pygame.display.set_caption('Tetris')#Top bar_title
#初期値となる固定ミノの情報を保持する配列を生成(0埋めなので、全部黒になる)
locked_positions = [[(0,0,0) for _ in range(int(play_width/block_size))] for _ in range(int(play_height/block_size))]
#初期値となるmino取得
current_mino = get_shape()
next_mino = get_shape()
next_mino2 = get_shape()
next_mino3 = get_shape()
score = 0
run = True #While関数を走らせるためのフラグ
time_t = (datetime.datetime.now()).second #秒数取得
#ループ
while run:
#現在gridに固定ミノ座標を登録
grid = create_grid(locked_positions)
for event in pygame.event.get():
if event.type == pygame.QUIT: #ESC key
run = False
pygame.display.quit()
if event.type == pygame.KEYDOWN: #ESC以外のキー入力(矢印キーとシフトキー)
run,current_mino = keyOparation(run,event.key,grid,current_mino)
second = (datetime.datetime.now()).second #秒数取得
if time_t != second: #1秒経過した場合
time_t = second
current_mino.y += 1 #ミノを1マス落下
if not valid_space(grid,current_mino):#当たり判定で重なってしまう場合
current_mino.y -= 1#ミノを重ならない状態に戻す
locked_positions = lock_mino(locked_positions,current_mino)#現在ミノを固定ミノにする
current_mino = copy.deepcopy(next_mino)#次ミノを現在ミノに繰りこし
next_mino = copy.deepcopy(next_mino2)
next_mino2 = copy.deepcopy(next_mino3)
next_mino3 = get_shape()
score = clear_rows(locked_positions,score)#ライン消し関数
#draw current_mino in grid
for i in range(len(current_mino.shape[current_mino.rotation])):#4
for j in range(len(current_mino.shape[current_mino.rotation][i])):#4
if current_mino.shape[current_mino.rotation][j][i] == 1:
grid[j+current_mino.y][i+current_mino.x] = current_mino.color
draw_window(surface,grid,score)#play画面に表示
draw_next_shape(next_mino,next_mino2,next_mino3,surface)#次のミノを表示
pygame.display.update()#画面更新
if check_lost(locked_positions):
print('you lost')
# draw_text_middle(surface,'YOU LOST!',80,(255,255,255))
pygame.display.update()
run = False
if __name__ == "__main__":
main()