最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年03月25日 09:05:07   作者:rongyongfeikai2  
這篇文章主要為大家介紹了python實(shí)現(xiàn)俄羅斯方塊游戲的詳細(xì)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

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

而代碼如下:

#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; 
 
 #記錄原來的值 
 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ā)。不過,怎樣也抑制不住這顆喜歡寫游戲的心啊!

更多俄羅斯方塊精彩文章請點(diǎn)擊專題:俄羅斯方塊游戲集合 進(jìn)行學(xué)習(xí)。

更多關(guān)于python游戲的精彩文章請點(diǎn)擊查看以下專題:

python經(jīng)典小游戲匯總

python微信跳一跳游戲集合

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用pycharm調(diào)試ssh遠(yuǎn)程程序并實(shí)時(shí)同步文件的操作方法

    利用pycharm調(diào)試ssh遠(yuǎn)程程序并實(shí)時(shí)同步文件的操作方法

    這篇文章主要介紹了利用pycharm調(diào)試ssh遠(yuǎn)程程序并實(shí)時(shí)同步文件的操作方法,本篇文章提供了利用pycharm遠(yuǎn)程調(diào)試程序的方法,且使用的編譯器可以是服務(wù)器中的虛擬環(huán)境的編譯器,可以實(shí)時(shí)同步本地與服務(wù)器的文件內(nèi)容,需要的朋友可以參考下
    2022-11-11
  • python基于socket實(shí)現(xiàn)網(wǎng)絡(luò)廣播的方法

    python基于socket實(shí)現(xiàn)網(wǎng)絡(luò)廣播的方法

    這篇文章主要介紹了python基于socket實(shí)現(xiàn)網(wǎng)絡(luò)廣播的方法,涉及Python操作socket的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • pytest用例執(zhí)行順序和跳過執(zhí)行詳解

    pytest用例執(zhí)行順序和跳過執(zhí)行詳解

    本文主要介紹了pytest用例執(zhí)行順序和跳過執(zhí)行詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • TensorFlow實(shí)現(xiàn)創(chuàng)建分類器

    TensorFlow實(shí)現(xiàn)創(chuàng)建分類器

    這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)創(chuàng)建分類器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • python函數(shù)缺省值與引用學(xué)習(xí)筆記分享

    python函數(shù)缺省值與引用學(xué)習(xí)筆記分享

    有關(guān)一個(gè)在函數(shù)參數(shù)設(shè)置缺省值與引用的問題,這個(gè)問題是大多數(shù)Pythoner可能會(huì)忽視的問題,作個(gè)筆記,以備后閱,同時(shí)供需要的朋友參考
    2013-02-02
  • python自動(dòng)生成model文件過程詳解

    python自動(dòng)生成model文件過程詳解

    這篇文章主要介紹了python自動(dòng)生成model文件過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值
    2019-11-11
  • python開發(fā)前景如何

    python開發(fā)前景如何

    在本篇文章中小編給大家整理了關(guān)于python開發(fā)前景的知識(shí)點(diǎn)及相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2020-06-06
  • Python腳本實(shí)現(xiàn)datax全量同步mysql到hive

    Python腳本實(shí)現(xiàn)datax全量同步mysql到hive

    這篇文章主要和大家分享一下mysql全量同步到hive自動(dòng)生成json文件的python腳本,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參加一下
    2024-10-10
  • Python 比較兩個(gè)數(shù)組的元素的異同方法

    Python 比較兩個(gè)數(shù)組的元素的異同方法

    下面小編就為大家?guī)硪黄狿ython 比較兩個(gè)數(shù)組的元素的異同方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • 我用Python抓取了7000 多本電子書案例詳解

    我用Python抓取了7000 多本電子書案例詳解

    這篇文章主要介紹了我用Python抓取了7000 多本電子書案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論

南充市| 英山县| 富裕县| 大田县| 安达市| 鞍山市| 开阳县| 格尔木市| 克拉玛依市| 大港区| 株洲市| 体育| 金阳县| 靖宇县| 武平县| 安多县| 唐山市| 会东县| 奇台县| 彰武县| 江门市| 洪湖市| 白朗县| 常宁市| 怀远县| 丹凤县| 美姑县| 漳州市| 砚山县| 天等县| 江西省| 佛冈县| 壶关县| 兰州市| 玉田县| 邹平县| 永和县| 新密市| 响水县| 榕江县| 巴里|