国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

香港云服务器
服務(wù)器之家 - 腳本之家 - Python - python實(shí)現(xiàn)俄羅斯方塊游戲

python實(shí)現(xiàn)俄羅斯方塊游戲

2021-03-09 00:44rongyongfeikai2 Python

這篇文章主要為大家介紹了python實(shí)現(xiàn)俄羅斯方塊游戲的詳細(xì)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在公司實(shí)習(xí)。公司推崇Python和Django框架,所以也得跟著學(xué)點(diǎn)。

簡(jiǎn)單瞅了下Tkinter,和Canvas配合在一起,還算是簡(jiǎn)潔的界面開發(fā)API。threading.Thread創(chuàng)建新的線程,其多線程機(jī)制也算是方便。

只是canvas.create_rectangle居然不是繪制矩形,而是新建了矩形控件這點(diǎn)讓人大跌眼鏡。先開始,在線程里每次都重繪多個(gè)矩形(隨數(shù)組變化),其實(shí)是每次都新建了N個(gè)矩形,結(jié)果內(nèi)存暴增。原來(lái),對(duì)矩形進(jìn)行變更時(shí),只需用canvas.itemconfig即可。

下面就是截圖(時(shí)間太晚,明日還得上班,做得非常粗糙...沒事時(shí)再慢慢修正)。

python實(shí)現(xiàn)俄羅斯方塊游戲

而代碼如下:

?
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#coding=utf-8
from Tkinter import *;
from random import *;
import thread;
from tkMessageBox import showinfo;
import threading;
from time import sleep;
class BrickGame(object):
 
 #是否開始
 start = True;
 #是否到達(dá)底部
 isDown = True;
 
 #窗體
 window = None;
 #frame
 frame1 = None;
 
 #繪圖類
 canvas = None;
 
 #標(biāo)題
 title = "BrickGame";
 #寬和高
 width = 350;
 height = 670;
 
 #行和列
 rows = 20;
 cols = 10;
 
 #幾種方塊
 brick = [
 
 [
 [
 [1,1,1],
 [0,0,1],
 [0,0,0]
 ],
 [
 [0,0,1],
 [0,0,1],
 [0,1,1]
 ],
 [
 [0,0,0],
 [1,0,0],
 [1,1,1]
 ],
 [
 [1,1,0],
 [1,0,0],
 [1,0,0]
 ]
 ],
 [
 [
 [0,0,0],
 [0,1,1],
 [0,1,1]
 ],
 [
 [0,0,0],
 [0,1,1],
 [0,1,1]
 ],
 [
 [0,0,0],
 [0,1,1],
 [0,1,1]
 ],
 [
 [0,0,0],
 [0,1,1],
 [0,1,1]
 ]
 ],
 [
 [
 [1,1,1],
 [0,1,0],
 [0,1,0]
 ],
 [
 [0,0,1],
 [1,1,1],
 [0,0,1]
 ],
 [
 [0,1,0],
 [0,1,0],
 [1,1,1]
 ],
 [
 [1,0,0],
 [1,1,1],
 [1,0,0]
 ]
 ],
 [
 [
 [0,1,0],
 [0,1,0],
 [0,1,0]
 ],
 [
 [0,0,0],
 [1,1,1],
 [0,0,0]
 ],
 [
 [0,1,0],
 [0,1,0],
 [0,1,0]
 ],
 [
 [0,0,0],
 [1,1,1],
 [0,0,0]
 ]
 ]
 ];
 
 #當(dāng)前的方塊
 curBrick = None;
 #當(dāng)前方塊數(shù)組
 arr = None;
 #當(dāng)前方塊形狀
 shape = -1;
 #當(dāng)前方塊的行和列(最左上角)
 curRow = -10;
 curCol = -10;
 
 #背景
 back = list();
 #格子
 gridBack = list();
 
 #初始化
 def init(self):
 
 for i in range(0,self.rows):
 
 self.back.insert(i,list());
 self.gridBack.insert(i,list());
 
 for i in range(0,self.rows):
 
 for j in range(0,self.cols):
 
 self.back[i].insert(j,0);
 self.gridBack[i].insert(j,self.canvas.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));
 
 #繪制游戲的格子
 def drawRect(self):
 
 for i in range(0,self.rows):
 
 for j in range(0,self.cols):
 
 
 if self.back[i][j]==1:
 
 self.canvas.itemconfig(self.gridBack[i][j],fill="blue",outline="white");
 
 elif self.back[i][j]==0:
 
 self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");
 
 
 #繪制當(dāng)前正在運(yùn)動(dòng)的方塊
 if self.curRow!=-10 and self.curCol!=-10:
 
 for i in range(0,len(self.arr)):
 
 for j in range(0,len(self.arr[i])):
 
 if self.arr[i][j]==1:
 
 self.canvas.itemconfig(self.gridBack[self.curRow+i][self.curCol+j],fill="blue",outline="white");
 
 #判斷方塊是否已經(jīng)運(yùn)動(dòng)到達(dá)底部
 if self.isDown:
 
 for i in range(0,3):
 
 for j in range(0,3):
 
 if self.arr[i][j]!=0:
 
 self.back[self.curRow+i][self.curCol+j] = self.arr[i][j];
 
 #判斷整行消除
 self.removeRow();
 
 #獲得下一個(gè)方塊
 self.getCurBrick();
 
 #判斷是否有整行需要消除
 def removeRow(self):
 
 for i in range(0,self.rows):
 
 tag1 = True;
 for j in range(0,self.cols):
 
 if self.back[i][j]==0:
 
 tag1 = False;
 break;
 
 if tag1==True:
 
 #從上向下挪動(dòng)
 for m in xrange(i-1,0,-1):
 
 for n in range(0,self.cols):
 
 self.back[m+1][n] = self.back[m][n];
 
 #獲得當(dāng)前的方塊
 def getCurBrick(self):
 
 self.curBrick = randint(0,len(self.brick)-1);
 self.shape = 0;
 #當(dāng)前方塊數(shù)組
 self.arr = self.brick[self.curBrick][self.shape];
 
 self.curRow = 0;
 self.curCol = 1;
 
 #是否到底部為False
 self.isDown = False;
 
 #監(jiān)聽鍵盤輸入
 def onKeyboardEvent(self,event):
 
 #未開始,不必監(jiān)聽鍵盤輸入
 if self.start == False:
 
 return;
 
 #記錄原來(lái)的值
 tempCurCol = self.curCol;
 tempCurRow = self.curRow;
 tempShape = self.shape;
 tempArr = self.arr;
 direction = -1;
 
 if event.keycode==37:
 
 #左移
 self.curCol-=1;
 direction = 1;
 elif event.keycode==38:
 #變化方塊的形狀
 self.shape+=1;
 direction = 2;
 
 if self.shape>=4:
 
 self.shape=0;
 self.arr = self.brick[self.curBrick][self.shape];
 elif event.keycode==39:
 
 direction = 3;
 #右移
 self.curCol+=1;
 elif event.keycode==40:
 
 direction = 4;
 #下移
 self.curRow+=1;
 
 if self.isEdge(direction)==False:
 
 self.curCol = tempCurCol;
 self.curRow = tempCurRow;
 self.shape = tempShape;
 self.arr = tempArr;
 
 self.drawRect();
 
 return True;
 
 #判斷當(dāng)前方塊是否到達(dá)邊界
 def isEdge(self,direction):
 
 tag = True;
 
 #向左,判斷邊界
 if direction==1:
 
 for i in range(0,3):
 
 for j in range(0,3):
 
 if self.arr[j][i]!=0 and (self.curCol+i<0 or self.back[self.curRow+j][self.curCol+i]!=0):
 
 tag = False;
 break;
 #向右,判斷邊界
 elif direction==3:
 
 for i in range(0,3):
 
 for j in range(0,3):
 
 if self.arr[j][i]!=0 and (self.curCol+i>=self.cols or self.back[self.curRow+j][self.curCol+i]!=0):
 
 tag = False;
 break;
 #向下,判斷底部
 elif direction==4:
 
 for i in range(0,3):
 
 for j in range(0,3):
 
 if self.arr[i][j]!=0 and (self.curRow+i>=self.rows or self.back[self.curRow+i][self.curCol+j]!=0):
 
 tag = False;
 self.isDown = True;
 break;
 #進(jìn)行變形,判斷邊界
 elif direction==2:
 
 if self.curCol<0:
 
 self.curCol=0;
 
 if self.curCol+2>=self.cols:
 
 self.curCol = self.cols-3;
 
 if self.curRow+2>=self.rows:
 
 self.curRow = self.curRow-3;
 
 
 return tag;
 
 #方塊向下移動(dòng)
 def brickDown(self):
 
 while True:
 
 if self.start==False:
 
 print("exit thread");
 break;
 
 tempRow = self.curRow;
 self.curRow+=1;
 
 if self.isEdge(4)==False:
 
 self.curRow = tempRow;
 
 self.drawRect();
 
 #每一秒下降一格
 sleep(1);
 
 #運(yùn)行
 def __init__(self):
 
 self.window = Tk();
 self.window.title(self.title);
 self.window.minsize(self.width,self.height);
 self.window.maxsize(self.width,self.height);
 
 self.frame1 = Frame(self.window,width=300,height=600,bg="black");
 self.frame1.place(x=20,y=30);
 
 self.canvas = Canvas(self.frame1,width=300,height=600,bg="black");
 
 self.init();
 
 #獲得當(dāng)前的方塊
 self.getCurBrick();
 
 #按照數(shù)組,繪制格子
 self.drawRect();
 
 self.canvas.pack();
 
 #監(jiān)聽鍵盤事件
 self.window.bind("<KeyPress>",self.onKeyboardEvent);
 
 #啟動(dòng)方塊下落線程
 downThread = threading.Thread(target=self.brickDown,args=());
 downThread.start();
 
 self.window.mainloop();
 
 self.start=False;
 
 pass;
 
if __name__=='__main__':
 
 brickGame = BrickGame();

估計(jì)用圖形界面會(huì)很少,因?yàn)楸救耸荳EB開發(fā)。不過(guò),怎樣也抑制不住這顆喜歡寫游戲的心啊!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/rongyongfeikai2/article/details/8892785

延伸 · 閱讀

精彩推薦
2139
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 免费不卡视频 | 噜噜噜视频在线观看 | 午夜电影网址 | 最新国产在线视频 | www久| 欧美精品一区二区在线观看 | 国产视频久久 | 欧美精品一区二区三区蜜桃视频 | 国产视频一区二 | 亚洲精品成人免费 | 亚洲国产精品久久 | 国产精品黄色 | 91在线激情 | 综合久久99| 国产欧美精品区一区二区三区 | 久久亚洲一区二区三区明星换脸 | 免费欧美 | 日日夜夜摸 | 日韩视频一区二区三区 | 久久久亚洲精 | 国产精品视屏 | 亚州av影院 | 久久一区二区三区四区 | 国产成人黄色av | 国产精品久久影院 | 互换娇妻呻吟hd中文字幕 | 成人日韩视频在线观看 | 亚洲视频一区在线观看 | 白浆在线 | 国产精品香蕉在线观看 | 最新毛片在线观看 | 亚洲视频在线播放 | 午夜精品一区二区三区在线视频 | 欧美视频在线观看 | 亚洲中字幕 | 我要看黄色一级大片 | 日韩视频在线免费观看 | 免费国产一区 | 日韩免费一区二区 | 日日骚视频 | 国产特级毛片aaaaaa毛片 |