-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniTetris.py
More file actions
251 lines (210 loc) · 10.5 KB
/
MiniTetris.py
File metadata and controls
251 lines (210 loc) · 10.5 KB
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
import tkinter as tk
import random
import threading
import time
class TetrisApp:
def __init__(self, master):
self.master = master
self.master.geometry("600x700")
self.master.title("미니 테트리스")
# 제목 및 점수 레이블
self.nameLabel = tk.Label(master, text="미니 테트리스")
self.nameLabel.pack(anchor='w', padx=10, pady=10)
scoreInfoLabel = tk.Label(master, text="점수 : ")
scoreInfoLabel.place(x=200, y=10)
self.scoreLabel = tk.Label(master, text="0")
self.scoreLabel.place(x=250, y=10)
# 점수 초기화
self.score = 0
# 게임 상태 레이블
self.gameStatus = tk.Label(master, text="")
self.gameStatus.place(x=100, y=50)
# 다음 블록 위젯 생성 및 레이블
previewLabel = tk.Label(master, text="다음 블록:")
previewLabel.place(x=420, y=10)
self.previewBlockWidth = 50
self.previewBlockHeight = 50
self.previewBlockCanvas = tk.Canvas(master, width=self.previewBlockWidth, height=self.previewBlockHeight, bg='white')
self.previewBlockCanvas.place(x=500, y=10)
# 게임 시작 버튼
self.start_button = tk.Button(master, text="게임 시작", command=self.start_game)
self.start_button.place(x=350, y=50)
# 테트리스 Canvas 위젯 생성
self.tetrisWidth = 495
self.tetrisHeight = 495
self.cellSize = 50
self.boardWidth = 10
self.boardHeight = 10
self.tetrisCanvas = tk.Canvas(master, width=self.tetrisWidth, height=self.tetrisHeight, bg='white')
self.tetrisCanvas.place(x=50, y=100)
# 테트리스 판 그리기
self.board = [[None for _ in range(self.boardWidth)] for _ in range(self.boardHeight)]
for row in range(self.boardHeight):
for col in range(self.boardWidth):
x1 = col * self.cellSize
y1 = row * self.cellSize
x2 = x1 + self.cellSize
y2 = y1 + self.cellSize
self.tetrisCanvas.create_rectangle(x1, y1, x2 + 5, y2 + 5, fill='white')
# 블록 위치 초기화
self.current_row = 0
self.current_col = random.randint(0, self.boardWidth - 1)
# 스레드 시작
self.is_running = False # 초기에는 스레드가 실행되지 않음
self.thread = None
self.fall_speed = 0.3 # 초기 속도 설정
self.min_speed = 0.1 # 최소 속도 설정
self.speed_decrease = 0.00001 # 속도 감소량
# 방향키 입력 바인딩
self.master.bind("<Left>", self.move_left)
self.master.bind("<Right>", self.move_right)
def draw_block(self):
x1 = self.current_col * self.cellSize
y1 = self.current_row * self.cellSize
x2 = x1 + self.cellSize
y2 = y1 + self.cellSize
return self.tetrisCanvas.create_rectangle(x1, y1, x2, y2, fill=self.current_color, tags="block")
def update_preview_block(self):
# 5가지 색상 목록
colors = ['red', 'skyblue', 'blue', 'yellow', 'black']
self.preview_color = random.choice(colors) # 랜덤 색상 선택
self.previewBlockCanvas.delete("all") # 기존 블록 삭제
self.previewBlockCanvas.create_rectangle(0, 0, self.previewBlockWidth + 5, self.previewBlockHeight + 5, fill=self.preview_color)
return self.preview_color
def move_block(self):
self.current_block = self.draw_block()
self.update_preview_block() # 미리 보기 블록 색상 업데이트
while self.is_running:
if self.can_move_down():
self.current_row += 1
self.update_canvas() # 캔버스 업데이트
else:
self.place_block() # 블록을 고정시키고 새로운 블록 생성
time.sleep(self.fall_speed)
# 속도 조정
if self.fall_speed > self.min_speed:
self.fall_speed -= self.speed_decrease
def update_canvas(self):
self.tetrisCanvas.delete("block") # 이전 블록 삭제
self.current_block = self.draw_block() # 새로운 위치에 블록 그리기
def can_move_down(self):
# 아래로 이동할 수 있는지 확인
if self.current_row + 1 >= self.boardHeight: # 바닥에 닿았는지 확인
return False
if self.board[self.current_row + 1][self.current_col] is not None: # 아래에 다른 블록이 있는지 확인
return False
return True
def move_left(self, event):
if self.current_col > 0 and self.can_move_left():
self.current_col -= 1 # 왼쪽으로 이동
self.update_canvas() # 캔버스 업데이트
def move_right(self, event):
if self.current_col < self.boardWidth - 1 and self.can_move_right():
self.current_col += 1 # 오른쪽으로 이동
self.update_canvas() # 캔버스 업데이트
def can_move_left(self):
# 왼쪽으로 이동할 수 있는지 확인
if self.current_col > 0 and self.board[self.current_row][self.current_col - 1] is not None:
return False
return True
def can_move_right(self):
# 오른쪽으로 이동할 수 있는지 확인
if self.current_col < self.boardWidth - 1 and self.board[self.current_row][self.current_col + 1] is not None:
return False
return True
def place_block(self):
# 블록을 고정시키고 새로운 블록을 준비
self.board[self.current_row][self.current_col] = self.current_color
if self.current_row == 0: # 게임 오버 조건
self.is_running = False # 스레드 종료
self.start_button.config(text="게임 재시작", state=tk.NORMAL) # 버튼 활성화
self.gameStatus.config(text="게임 오버!!") # 게임 상태 레이블 업데이트
self.disable_controls() # 키 입력 비활성화
return
self.check_for_matches() # 블록 고정 후 매치 검사
self.current_row = 0
self.current_col = random.randint(0, self.boardWidth - 1)
self.current_color = self.preview_color # 미리 보기 색상으로 현재 색상 설정
self.update_canvas() # 새로운 블록 그리기
self.update_preview_block() # 미리 보기 블록 색상 업데이트
def check_for_matches(self):
# 가로로 3개 이상 같은 색상 블록 제거
for row in range(self.boardHeight):
count = 1
for col in range(1, self.boardWidth):
if self.board[row][col] == self.board[row][col - 1] and self.board[row][col] is not None:
count += 1
else:
if count >= 3:
for c in range(col - count, col):
self.board[row][c] = None # 블록 제거
self.score += 1 # 점수 증가
count = 1
if count >= 3: # 마지막 블록 확인
for c in range(self.boardWidth - count, self.boardWidth):
self.board[row][c] = None # 블록 제거
self.score += 1 # 점수 증가
# 세로로 3개 이상 같은 색상 블록 제거
for col in range(self.boardWidth):
count = 1
for row in range(1, self.boardHeight):
if self.board[row][col] == self.board[row - 1][col] and self.board[row][col] is not None:
count += 1
else:
if count >= 3:
for r in range(row - count, row):
self.board[r][col] = None # 블록 제거
self.score += 1
count = 1
if count >= 3: # 마지막 블록 확인
for r in range(self.boardHeight - count, self.boardHeight):
self.board[r][col] = None # 블록 제거
self.score += 1 # 점수 증가
self.update_board_canvas() # 보드 업데이트
self.update_score_label() # 점수 레이블 업데이트
self.drop_blocks() # 블록 아래로 내리기
def drop_blocks(self):
for col in range(self.boardWidth):
# 각 열에서 블록을 아래로 내림
empty_space = 0 # 비어있는 공간 수
for row in range(self.boardHeight - 1, -1, -1):
if self.board[row][col] is None:
empty_space += 1 # 빈 공간 증가
elif empty_space > 0:
# 현재 블록을 아래로 이동
self.board[row + empty_space][col] = self.board[row][col]
self.board[row][col] = None # 원래 위치는 비우기
self.update_board_canvas() # 보드 업데이트
def update_board_canvas(self):
self.tetrisCanvas.delete("all") # 모든 블록 삭제
for row in range(self.boardHeight):
for col in range(self.boardWidth):
if self.board[row][col] is not None:
x1 = col * self.cellSize
y1 = row * self.cellSize
x2 = x1 + self.cellSize
y2 = y1 + self.cellSize
self.tetrisCanvas.create_rectangle(x1, y1, x2, y2, fill=self.board[row][col])
def update_score_label(self):
self.scoreLabel.config(text=str(self.score)) # 점수 레이블 업데이트
def start_game(self):
if not self.is_running:
self.is_running = True
self.score = 0
self.update_score_label() # 점수 초기화
self.gameStatus.config(text="") # 게임 상태 레이블 업데이트
self.board = [[None for _ in range(self.boardWidth)] for _ in range(self.boardHeight)] # 보드 초기화
self.current_row = 0
self.current_col = random.randint(0, self.boardWidth - 1) # 블록 위치 초기화
self.current_color = self.update_preview_block() # 다음 블록 색상 업데이트
self.start_button.config(text="게임 진행중...", state=tk.DISABLED)
self.check_for_matches() # 블록 고정 후 매치 검사
self.thread = threading.Thread(target=self.move_block)
self.thread.start() # 게임 시작 스레드 실행
def disable_controls(self):
self.master.unbind("<Left>")
self.master.unbind("<Right>")
# Tkinter 애플리케이션 실행
app = tk.Tk()
tetris_app = TetrisApp(app)
app.mainloop()