質問編集履歴
2
意図の明確化
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
①ゲーム中に押されたキーと、押された時のゲーム開始からの時間の記録、保存
|
18
18
|
|
19
|
-
②保存された(key, timing)データに基づき、自動プレイ
|
19
|
+
②保存された(key, timing)データに基づき、自動再現プレイ
|
20
20
|
|
21
21
|
を実現したい。
|
22
22
|
|
1
未完成で投稿してしまったため、修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,211 +32,13 @@
|
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
###
|
35
|
+
### (質問ではなくなりますが参考までに)現時点までの改造
|
36
36
|
|
37
|
-
*すべてのコードを掲載したいのですが、文字数制限があるため、改造後のファイルから主に変更を加えたBoardクラスのnewPiece関数にとどめます。不明な点はぜひ質問してください!
|
37
|
+
*すべてのコードを掲載したいのですが、文字数制限があるため、改造後のファイルから主に変更を加えたBoardクラスのnewPiece関数にとどめます。ソースをご覧いただくか、不明な点はぜひ質問してください!
|
38
38
|
|
39
39
|
*コメントについてはmarkdownのルールに当たらないようにしていた名残で、 ###### を用いています
|
40
40
|
|
41
41
|
*途中理解していない行に変なコメントをつけておりますが、見逃していただけるとありがたいです( ´∀` )
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
```python3
|
46
|
-
|
47
|
-
import sys, random, pickle, datetime
|
48
|
-
|
49
|
-
from collections import deque
|
50
|
-
|
51
|
-
from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication
|
52
|
-
|
53
|
-
from PyQt5.QtCore import Qt, QBasicTimer, pyqtSignal
|
54
|
-
|
55
|
-
from PyQt5.QtGui import QPainter, QColor
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
class Board(QFrame):
|
62
|
-
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
def newPiece(self):
|
68
|
-
|
69
|
-
"""create a new piece"""
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
self.curPiece = Shape()
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
###### if the game is being replayed, provide the same shapes in the same order.
|
78
|
-
|
79
|
-
###### when all the stored shapes are replayed then continue and turn to provide random shapes as usual
|
80
|
-
|
81
|
-
if self.replay is not None:
|
82
|
-
|
83
|
-
if len(self.replay) > 0:
|
84
|
-
|
85
|
-
self.curPiece.setShape(self.replay.popleft())
|
86
|
-
|
87
|
-
else:
|
88
|
-
|
89
|
-
self.curPiece.setRandomShape()
|
90
|
-
|
91
|
-
else:
|
92
|
-
|
93
|
-
self.curPiece.setRandomShape()
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
self.curX = Board.BoardWidth // 2 + 1 # ######## don't understand following 2 lines
|
98
|
-
|
99
|
-
self.curY = Board.BoardHeight - 1 + self.curPiece.minY()
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
###### record shapes in deque
|
104
|
-
|
105
|
-
if self.isRecorded:
|
106
|
-
|
107
|
-
self.record.append(self.curPiece.shape())
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
if not self.tryMove(self.curPiece, self.curX, self.curY):
|
112
|
-
|
113
|
-
self.curPiece.setShape(Tetrominoe.NoShape)
|
114
|
-
|
115
|
-
self.timer.stop()
|
116
|
-
|
117
|
-
self.isStarted = False
|
118
|
-
|
119
|
-
self.msg2Statusbar.emit('--Game Over--')
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
###### if the game is recorded, pickle the list of shapes we've seen befor the game is over
|
124
|
-
|
125
|
-
if self.isRecorded:
|
126
|
-
|
127
|
-
now = str(datetime.datetime.now())
|
128
|
-
|
129
|
-
now = now.replace(':', '-')
|
130
|
-
|
131
|
-
now = now.replace(' ', '_')
|
132
|
-
|
133
|
-
now = now.replace('.', '_')
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
with open(f'tetris{self.numLinesRemoved}_{now}.pickle', 'wb') as f:
|
138
|
-
|
139
|
-
pickle.dump(self.record, f)
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
def tryMove(self, newPiece, newX, newY):
|
144
|
-
|
145
|
-
"""try to move a shape"""
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
for i in range(4):
|
150
|
-
|
151
|
-
x = newX + newPiece.x(i)
|
152
|
-
|
153
|
-
y = newY - newPiece.y(i)
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:
|
158
|
-
|
159
|
-
return False
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
if self.shapeAt(x, y) != Tetrominoe.NoShape:
|
164
|
-
|
165
|
-
return False
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
self.curPiece = newPiece
|
170
|
-
|
171
|
-
self.curX = newX
|
172
|
-
|
173
|
-
self.curY = newY
|
174
|
-
|
175
|
-
self.update()
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
return True
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
def drawSquare(self, painter, x, y, shape):
|
184
|
-
|
185
|
-
"""draws a square of a shape"""
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
colorTable = [0x000000, 0xCC6666, 0x66CC66, 0x6666CC,
|
190
|
-
|
191
|
-
0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00]
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
color = QColor(colorTable[shape])
|
196
|
-
|
197
|
-
painter.fillRect(x + 1, y + 1,
|
198
|
-
|
199
|
-
self.squareWidth() - 2, self.squareHeight() - 2,
|
200
|
-
|
201
|
-
color)
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
painter.setPen(color.lighter())
|
206
|
-
|
207
|
-
painter.drawLine(x, y + self.squareHeight() - 1, x, y)
|
208
|
-
|
209
|
-
painter.drawLine(x, y, x + self.squareWidth() - 1, y)
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
painter.setPen(color.darker())
|
214
|
-
|
215
|
-
painter.drawLine(x + 1, y + self.squareHeight() - 1,
|
216
|
-
|
217
|
-
x + self.squareWidth() - 1, y + self.squareHeight() - 1)
|
218
|
-
|
219
|
-
painter.drawLine(x + self.squareWidth() - 1, y + self.squareHeight() - 1,
|
220
|
-
|
221
|
-
x + self.squareWidth() - 1, y + 1)
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
if __name__ == '__main__':
|
230
|
-
|
231
|
-
app = QApplication([])
|
232
|
-
|
233
|
-
tetris = Tetris(isRecorded=False, replay=None)
|
234
|
-
|
235
|
-
sys.exit(app.exec_())
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
```
|
240
42
|
|
241
43
|
|
242
44
|
|