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

python編寫(xiě)俄羅斯方塊

 更新時(shí)間:2020年03月13日 09:39:17   作者:勤勉之  
這篇文章主要為大家詳細(xì)介紹了python編寫(xiě)俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)俄羅斯方塊的具體代碼,供大家參考,具體內(nèi)容如下

#coding=utf-8 
from tkinter import *
from random import *
import threading
from tkinter.messagebox import showinfo
from tkinter.messagebox import askquestion
import threading
from time import sleep
 
 
class BrickGame(object): 
 
 #是否開(kāi)始 
 start = True; 
 #是否到達(dá)底部 
 isDown = True; 
 isPause = False; 
 #窗體 
 window = None; 
 #frame 
 frame1 = None; 
 frame2 = None; 
 
 #按鈕 
 btnStart = None; 
 
 #繪圖類 
 canvas = None; 
 canvas1 = None; 
 
 #標(biāo)題 
 title = "BrickGame"; 
 #寬和高 
 width = 450; 
 height = 670; 
 
 #行和列 
 rows = 20; 
 cols = 10; 
 
 #下降方塊的線程 
 downThread = None; 
 
 #幾種方塊 
 brick = [ 
  [ 
   [ 
    [0,1,1], 
    [1,1,0], 
    [0,0,0] 
   ], 
   [ 
    [1,0,0], 
    [1,1,0], 
    [0,1,0] 
   ], 
   [ 
    [0,1,1], 
    [1,1,0], 
    [0,0,0] 
   ], 
   [ 
    [1,0,0], 
    [1,1,0], 
    [0,1,0] 
   ] 
 ],
 [ 
   [ 
    [1,1,1], 
    [1,0,0], 
    [0,0,0] 
   ], 
   [ 
    [0,1,1], 
    [0,0,1], 
    [0,0,1] 
   ], 
   [ 
    [0,0,0], 
    [0,0,1], 
    [1,1,1] 
   ], 
   [ 
    [1,0,0], 
    [1,0,0], 
    [1,1,0] 
   ] 
 ], 
 [ 
   [ 
    [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,0,0] 
   ], 
   [ 
    [0,0,1], 
    [0,1,1], 
    [0,0,1] 
   ], 
   [ 
    [0,0,0], 
    [0,1,0], 
    [1,1,1] 
   ], 
   [ 
    [1,0,0], 
    [1,1,0], 
    [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] 
   ] 
 ],
 [ 
   [ 
    [1,1,0], 
    [0,1,1], 
    [0,0,0] 
   ], 
   [ 
    [0,0,1], 
    [0,1,1], 
    [0,1,0] 
   ], 
   [ 
    [0,0,0], 
    [1,1,0], 
    [0,1,1] 
   ], 
   [ 
    [0,1,0], 
    [1,1,0], 
    [1,0,0] 
   ] 
 ]
 
 
 ]; 
 
 #當(dāng)前的方塊 
 curBrick = None; 
 #當(dāng)前方塊數(shù)組 
 arr = None; 
 arr1 = None; 
 #當(dāng)前方塊形狀 
 shape = -1; 
 #當(dāng)前方塊的行和列(最左上角) 
 curRow = -10; 
 curCol = -10; 
 
 #背景 
 back = list(); 
 #格子 
 gridBack = list(); 
 preBack = 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")); 
   
 for i in range(0,3): 
  
  self.preBack.insert(i,list()); 
  
 for i in range(0,3): 
  
  for j in range(0,3): 
   
  self.preBack[i].insert(j,self.canvas1.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"); 
    
 #繪制預(yù)覽方塊 
 for i in range(0,len(self.arr1)): 
  
  for j in range(0,len(self.arr1[i])): 
   
  if self.arr1[i][j]==0: 
   
   self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white"); 
   
  elif self.arr1[i][j]==1: 
   
   self.canvas1.itemconfig(self.preBack[i][j],fill="orange",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(); 
  
  #判斷是否死了 
  self.isDead(); 
   
  #獲得下一個(gè)方塊 
  self.getCurBrick(); 
 
 #判斷是否有整行需要消除 
 def removeRow(self): 
 count=0
 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)
  count=count+1
  for m in range(i-1,0,-1): 
   
   for n in range(0,self.cols): 
    
   self.back[m+1][n] = self.back[m][n]; 
 
   
 
 scoreValue = eval(self.scoreLabel2['text'])
 scoreValue += 5*count*(count+3)
 self.scoreLabel2.config(text=str(scoreValue))
  
 #獲得當(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.arr1 = self.arr; 
  
 self.curRow = 0; 
 self.curCol = 1; 
  
 #是否到底部為False 
 self.isDown = False; 
  
 #監(jiān)聽(tīng)鍵盤(pán)輸入 
 def onKeyboardEvent(self,event): 
  
 #未開(kāi)始,不必監(jiān)聽(tīng)鍵盤(pán)輸入 
 if self.start == False: 
  
  return;
 
 if self.isPause == True:
  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; 
  if self.isPause==False: 
  tempRow = self.curRow; 
  self.curRow+=1; 
   
  if self.isEdge(4)==False: 
   
   self.curRow = tempRow; 
   
  self.drawRect(); 
    
  #每一秒下降一格 
  sleep(1); 
  
 #點(diǎn)擊開(kāi)始 
 def clickStart(self): 
  
 self.start = True; 
  
 for i in range(0,self.rows): 
  
  for j in range(0,self.cols): 
   
  self.back[i][j] = 0; 
  self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white"); 
   
 for i in range(0,len(self.arr)): 
  
  for j in range(0,len(self.arr[i])): 
   
  self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white"); 
   
 self.getCurBrick(); 
 self.drawRect(); 
  
 self.downThread = threading.Thread(target=self.brickDown,args=()); 
 self.downThread.start();
 
 def clickPause(self):
 self.isPause=not self.isPause
 print(self.isPause)
 if not self.isPause:
  self.btnPause["text"]="暫停"
 else:
  self.btnPause["text"]="恢復(fù)"
  
 
 def clickReStart(self):
 ackRestart =askquestion("重新開(kāi)始","你確定要重新開(kāi)始嗎?")
 if ackRestart == 'yes':
  self.clickStart()
 else:
  return
 
 def clickQuit(self):
 ackQuit =askquestion("退出","你確定要退出嗎?")
 if ackQuit == 'yes':
  self.window.destroy()
  exit()
 
 
  
 #判斷是否死了 
 def isDead(self): 
  
 for j in range(0,len(self.back[0])): 
  
  if self.back[0][j]!=0: 
   
  showinfo("提示","你掛了,再來(lái)一盤(pán)吧!"); 
  self.start = False; 
  break; 
  
 #運(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.scoreLabel1 = Label(self.window,text="Score:",font=(30))
 self.scoreLabel1.place(x=340,y=60)
 self.scoreLabel2 = Label(self.window,text="0",fg='red',font=(30))
 self.scoreLabel2.place(x=410,y=60)
 
 self.frame2 = Frame(self.window,width=90,height=90,bg="black"); 
 self.frame2.place(x=340,y=120); 
  
 self.canvas = Canvas(self.frame1,width=300,height=600,bg="black"); 
 self.canvas1 = Canvas(self.frame2,width=90,height=90,bg="black"); 
  
 self.btnStart = Button(self.window,text="開(kāi)始",command=self.clickStart); 
 self.btnStart.place(x=340,y=400,width=80,height=25); 
 
 self.btnPause = Button(self.window,text="暫停",command=self.clickPause); 
 self.btnPause.place(x=340,y=450,width=80,height=25);
 
 self.btnReStart = Button(self.window,text="重新開(kāi)始",command=self.clickReStart); 
 self.btnReStart.place(x=340,y=500,width=80,height=25);
 
 self.btnQuit = Button(self.window,text="退出",command=self.clickQuit); 
 self.btnQuit.place(x=340,y=550,width=80,height=25);
 
 
 
 self.init(); 
  
 #獲得當(dāng)前的方塊 
 self.getCurBrick(); 
  
 #按照數(shù)組,繪制格子
 
 
 self.drawRect();
  
 self.canvas.pack();
   
  
 self.canvas1.pack(); 
 
 #監(jiān)聽(tīng)鍵盤(pán)事件 
 self.window.bind("<KeyPress>",self.onKeyboardEvent); 
  
 #啟動(dòng)方塊下落線程
 self.downThread = threading.Thread(target=self.brickDown,args=()); 
 self.downThread.start();  
  
 self.window.mainloop(); 
  
 self.start=False; 
  
 pass; 
 
if __name__=='__main__': 
 
 brickGame = BrickGame(); 

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

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

相關(guān)文章

  • Python常用GUI框架原理解析匯總

    Python常用GUI框架原理解析匯總

    這篇文章主要介紹了Python常用GUI框架原理解析匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Python獲取B站粉絲數(shù)的示例代碼

    Python獲取B站粉絲數(shù)的示例代碼

    這篇文章主要介紹了Python獲取B站粉絲數(shù)的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • python+Selenium自動(dòng)化測(cè)試——輸入,點(diǎn)擊操作

    python+Selenium自動(dòng)化測(cè)試——輸入,點(diǎn)擊操作

    這篇文章主要介紹了python+Selenium自動(dòng)化測(cè)試——輸入,點(diǎn)擊操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • pandas表連接 索引上的合并方法

    pandas表連接 索引上的合并方法

    今天小編就為大家分享一篇pandas表連接 索引上的合并方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • python實(shí)現(xiàn)canny邊緣檢測(cè)

    python實(shí)現(xiàn)canny邊緣檢測(cè)

    本文主要講解了canny邊緣檢測(cè)原理:計(jì)算梯度幅值和方向、根據(jù)角度對(duì)幅值進(jìn)行非極大值抑制、用雙閾值算法檢測(cè)和連接邊緣以及python 實(shí)現(xiàn)
    2020-09-09
  • pytest解讀fixtures之Teardown處理yield和addfinalizer方案

    pytest解讀fixtures之Teardown處理yield和addfinalizer方案

    這篇文章主要為大家介紹了pytest解讀fixtures之Teardown處理yield和addfinalizer的方案實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Django如何實(shí)現(xiàn)RBAC權(quán)限管理

    Django如何實(shí)現(xiàn)RBAC權(quán)限管理

    這篇文章主要介紹了Django如何實(shí)現(xiàn)RBAC權(quán)限管理問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python繪制浪漫星空的示例代碼

    Python繪制浪漫星空的示例代碼

    Python的turtle是一個(gè)基于tkinter的Python圖形庫(kù),可以幫助初學(xué)者輕松地理解和繪制圖形,本文就來(lái)通過(guò)turtle繪制一個(gè)浪漫的星空動(dòng)畫(huà)吧
    2023-09-09
  • 在 Windows 下搭建高效的 django 開(kāi)發(fā)環(huán)境的詳細(xì)教程

    在 Windows 下搭建高效的 django 開(kāi)發(fā)環(huán)境的詳細(xì)教程

    這篇文章主要介紹了如何在 Windows 下搭建高效的 django 開(kāi)發(fā)環(huán)境,本文通過(guò)一篇詳細(xì)教程實(shí)例代碼相結(jié)合給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Python 如何讀取.txt,.md等文本文件

    Python 如何讀取.txt,.md等文本文件

    這篇文章主要介紹了Python 讀取.txt,.md等文本文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論

四子王旗| 玛多县| 肥乡县| 汤原县| 盐津县| 巴彦淖尔市| 卢龙县| 安乡县| 醴陵市| 沅陵县| 呈贡县| 乌兰县| 长海县| 武清区| 内丘县| 富蕴县| 淄博市| 舞钢市| 凌源市| 南昌县| 东乌珠穆沁旗| 周宁县| 余姚市| 水城县| 万宁市| 衢州市| 弥渡县| 大庆市| 息烽县| 凉城县| 韶关市| 湾仔区| 宿迁市| 高唐县| 隆回县| 福泉市| 环江| 和龙市| 大姚县| 巴中市| 合山市|