亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python實現俄羅斯方塊游戲(改進版)

瀏覽:7日期:2022-08-02 13:36:53

本文為大家分享了python實現俄羅斯方塊游戲,繼上一篇的改進版,供大家參考,具體內容如下

1.加了方塊預覽部分

2.加了開始按鈕

在公司實習抽空寫的,呵呵。覺得Python還不錯,以前覺得像個玩具語言。希望能夠用它做更多大事吧?。?!加油。

截圖如下:

python實現俄羅斯方塊游戲(改進版)

代碼如下:

#coding=utf-8from Tkinter import *;from random import *;import thread; from tkMessageBox import showinfo;import threading;from time import sleep;class BrickGame(object): #是否開始 start = True; #是否到達底部 isDown = True; #窗體 window = None; #frame frame1 = None; frame2 = None; #按鈕 btnStart = None; #繪圖類 canvas = None; canvas1 = None; #標題 title = 'BrickGame'; #寬和高 width = 450; height = 670; #行和列 rows = 20; cols = 10; #下降方塊的線程 downThread = None; #幾種方塊 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] ] ] ]; #當前的方塊 curBrick = None; #當前方塊數組 arr = None; arr1 = None; #當前方塊形狀 shape = -1; #當前方塊的行和列(最左上角) 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'); #繪制預覽方塊 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'); #繪制當前正在運動的方塊 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');#判斷方塊是否已經運動到達底部 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(); #獲得下一個方塊 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:#從上向下挪動 for m in xrange(i-1,0,-1): for n in range(0,self.cols): self.back[m+1][n] = self.back[m][n]; #獲得當前的方塊 def getCurBrick(self): self.curBrick = randint(0,len(self.brick)-1); self.shape = 0; #當前方塊數組 self.arr = self.brick[self.curBrick][self.shape]; self.arr1 = self.arr; self.curRow = 0; self.curCol = 1; #是否到底部為False self.isDown = False; #監聽鍵盤輸入 def onKeyboardEvent(self,event): #未開始,不必監聽鍵盤輸入 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; #判斷當前方塊是否到達邊界 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; #進行變形,判斷邊界 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; #方塊向下移動 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); #點擊開始 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 isDead(self): for j in range(0,len(self.back[0])): if self.back[0][j]!=0:showinfo('提示','你掛了,再來一盤吧!'); self.start = False; break; #運行 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.frame2 = Frame(self.window,width=90,height=90,bg='black'); self.frame2.place(x=340,y=60); 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='開始',command=self.clickStart); self.btnStart.place(x=340,y=400,width=80,height=25); self.init(); #獲得當前的方塊 self.getCurBrick(); #按照數組,繪制格子 self.drawRect(); self.canvas.pack(); self.canvas1.pack(); #監聽鍵盤事件 self.window.bind('<KeyPress>',self.onKeyboardEvent); #啟動方塊下落線程 self.downThread = threading.Thread(target=self.brickDown,args=()); self.downThread.start(); self.window.mainloop(); self.start=False; pass; if __name__==’__main__’: brickGame = BrickGame();

更多俄羅斯方塊精彩文章請點擊專題:俄羅斯方塊游戲集合 進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产性生活视频 | 色综合合久久天天综合绕视看 | 欧美情趣视频 | 性欧美巨大 | 亚洲qingse中文久久网 | 九九香蕉 | 国产v综合v亚洲欧美大另类 | 亚洲欧美专区精品久久 | 亚洲一区二区三区一品精 | 久久99精品国产一区二区三区 | 午夜成年视频 | 亚洲精品一区亚洲精品 | 欧美r级毛片在线播放 | 久久久久久网站 | 国产超薄肉色丝袜的免费网站 | 亚洲性色图 | 日本黄色大片免费看 | 亚洲精品国产福利 | 日本一级看片免费播放 | 大伊人青草狠狠久久 | 国产精品国产三级国产普通话对白 | 中国免费毛片 | 亚洲第一区在线观看 | 午夜黄色 | 国产在线乱码在线视频 | 国产亚洲精品国产第一 | 美女国产一区 | 又黄又爽又猛午夜性色播在线播放 | 在线观看黄免费 | 最新欧美精品一区二区三区 | 国产丰满主播丝袜勾搭秀 | 国产v欧美v日本v精品 | 成人午夜性a一级毛片美女 成人午夜性影院视频 | 色视频网站人成免费 | 国产精品福利在线观看秒播 | 日韩精品中文字幕视频一区 | 色的视频在线观看免费播放 | 免费一级毛片在线播放放视频 | 麻豆果冻国产91在线极品 | 久久久久青草大香线综合精品 | 青青青网 |