通过本文主要向大家介绍了python 俄罗斯方块,python俄罗斯方块代码,用python做俄罗斯方块,用python写俄罗斯方块,超级俄罗斯方块简单版等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
俄罗斯方块游戏,使用Python实现,总共有350+行代码,实现了俄罗斯方块游戏的基本功能,同时会记录所花费时间,消去的总行数,所得的总分,还包括一个排行榜,可以查看最高记录。
排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等。
附源码:
from Tkinter import *
from tkMessageBox import *
import random
import time
#俄罗斯方块界面的高度
HEIGHT = 18
#俄罗斯方块界面的宽度
WIDTH = 10
ACTIVE = 1
PASSIVE = 0
TRUE = 1
FALSE = 0
root=Tk();root.title('Russia')
class App(Frame):
def __init__(self,master):
Frame.__init__(self)
master.bind('<Up>',self.Up)
master.bind('<Left>',self.Left)
master.bind('<Right>',self.Right)
master.bind('<Down>',self.Down)
#master.bind('<Down>',self.Space)
master.bind('<space>',self.Space)
master.bind('<Control-Shift-Key-F12>',self.Play)
master.bind('<Key-F6>',self.Pause)
self.backg="#%02x%02x%02x" % (120,150,30)
self.frontg="#%02x%02x%02x" % (40,120,150)
self.nextg="#%02x%02x%02x" % (150,100,100)
self.flashg="#%02x%02x%02x" % (210,130,100)
self.LineDisplay=Label(master,text='Lines: ',bg='black',fg='red')
self.Line=Label(master,text='0',bg='black',fg='red')
self.ScoreDisplay=Label(master,text='Score: ',bg='black',fg='red')
self.Score=Label(master,text='0',bg='black',fg='red')
#Display time
self.SpendTimeDisplay=Label(master,text='Time: ',bg='black',fg='red')
self.SpendTime=Label(master,text='0.0',bg='black',fg='red')
self.LineDisplay.grid(row=HEIGHT-2,column=WIDTH,columnspan=2)
self.Line.grid(row=HEIGHT-2,column=WIDTH+2,columnspan=3)
self.ScoreDisplay.grid(row=HEIGHT-1,column=WIDTH,columnspan=2)
self.Score.grid(row=HEIGHT-1,column=WIDTH+2,columnspan=3)
#Display time
self.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2)
self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3)
self.TotalTime=0.0
self.TotalLine=0;self.TotalScore=0
#Game over
self.isgameover=FALSE
#Pause
self.isPause=FALSE
#Start
self.isStart=FALSE
self.NextList=[];self.NextRowList=[]
r=0;c=0
for k in range(4*4):
LN=Label(master,text=' ',bg=str(self.nextg),fg='white',relief=FLAT,bd=4)
LN.grid(row=r,column=WIDTH+c,sticky=N+E+S+W)
self.NextRowList.append(LN)
c=c+1
if c>=4:
r=r+1;c=0
self.NextList.append(self.NextRowList)
self.NextRowList=[]
self.BlockList=[];self.LabelList=[]
self.BlockRowList=[];self.LabelRowList=[]
row=0;col=0
for i in range(HEIGHT*WIDTH):
L=Label(master,text=' ',bg=str(self.backg),fg='white',relief=FLAT,bd=4)
L.grid(row=row,column=col,sticky=N+E+S+W)
L.row=row;L.col=col;L.isactive=PASSIVE
self.BlockRowList.append(0);self.LabelRowList.append(L)
col=col+1
if col>=WIDTH:
row=row+1;col=0
self.BlockList.append(self.BlockRowList)
self.LabelList.append(self.LabelRowList)
self.BlockRowList=[];self.LabelRowList=[]
#file
fw=open('text.txt','a')
fw.close()
hasHead=FALSE
f=open('text.txt','r')
if f.read(5)=='score':
hasHead=TRUE
f.close()
self.file=open('text.txt','r+a')
if hasHead==FALSE:
self.file.write('score line time scorePtime linePtime scorePline date/n')
self.file.flush()
self.time=1000
self.OnTimer()
def __del__(self):
#self.file.close()
pass
def Pause(self,event):
self.isPause=1-self.isPause
def Up(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUE
xtotal=0;ytotal=0;count=0
for i in range(HEIGHT):
for j in range(WIDTH):
if LL[i][j].isactive==ACTIVE:
xtotal=xtotal+i;ytotal=ytotal+j;count=count+1
SourceList=[];DestList=[]
for i in range(HEIGHT):
for j in range(WIDTH):
if LL[i][j].isactive==ACTIVE:
x0=(xtotal+ytotal)/count;y0=(ytotal-xtotal )/count
xr=(xtotal+ytotal)%count;yr=(ytotal-xtotal)%count
x=x0-j;y=y0+i
if xr>=count/2:x=x+1
if yr>=count/2:y=y+1
SourceList.append([i,j]);DestList.append([x,y])
if x<0 or x>=HEIGHT or y<0 or y>=WIDTH:Moveable=FALSE
if x>=0 and x<HEIGHT and y>=0 and y<WIDTH and BL[x][y]==1 and LL[x][y].isactive==PASSIVE:Moveable=FALSE
if Moveable==TRUE:
for i in range(len(SourceList)):
self.Empty(SourceList[i][0],SourceList[i][1])
for i in range(len(DestList)):
self.Fill(DestList[i][0],DestList[i][1])
def Left(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUE
for i in range(HEIGHT):
for j in range(WIDTH):
if LL[i][j].isactive==ACTIVE and j-1<0:Moveable=FALSE
if LL[i][j].isactive==ACTIVE and j-1>=0 and BL[i][j-1]==1 and LL[i][j-1].isactive==PASSIVE:Moveable=FALSE
if Moveable==TRUE:
for i in range(HEIGHT):
for j in range(WIDTH):
if j-1>=0 and LL[i][j].isactive==ACTIVE and BL[i][j-1]==0:
self.Fill(i,j-1);self.Empty(i,j)
def Right(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUE
for i in range(HEIGHT):
for j in range(WIDTH):
if LL[i][j].isactive==ACTIVE and j+1>=WIDTH:Moveable=FALSE
if LL[i][j].isactive==ACTIVE and j+1<WIDTH and BL[i][j+1]==1 and LL[i][j+1].isactive==PASSIVE:Moveable=FALSE
if Moveable==TRUE:
for i in range(HEIGHT-1,-1,-1):
for j in range(WIDTH-1,-1,-1):
if j+1<WIDTH and LL[i][j].isactive==ACTIVE and BL[i][j+1]==0:
self.Fill(i,j+1);self.Empty(i,j)
def Space(self,event):
while 1:
if self.Down(0)==FALSE:break
def OnTimer(self):
if self.isStart==TRUE and self.isPause==FALSE:
self.TotalTime = self.TotalTime + float(self.time)/1000
self.SpendTime.config(text=str(self.TotalTime))
if self.isPause==FALSE:
self.Down(0)
if self.TotalScore>=1000:self.time=900
if self.TotalScore>=2000:self.time=750
if self.TotalScore>=3000:self.time=600
if self.TotalScore>=4000:self.time=400
self.after(self.time,self.OnTimer)
def Down(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUE
for i in range(HEIGHT):
for j in range(WIDTH):
if LL[i][j].isactive==ACTIVE and i+1>=HEIGHT:Moveable=FALSE
if LL[i][j].isactive==ACTIVE and i+1<HEIGHT and BL[i+1][j]==1 and LL[i+1][j].isactive==PASSIVE:Moveable=FALSE
if Moveable==TRUE:
for i in range(HEIGHT-1,-1,-1):
for j in range(WIDTH-1,-1,-1):
if i+1<HEIGHT and LL[i][j].isactive==ACTIVE and BL[i+1][j]==0:
self.Fill(i+1,j);self.Empty(i,j)
if Moveable==FALSE:
for i in range(HEIGHT):
for j in range(WIDTH):
LL[i][j].isactive=PASSIVE
self.JudgeLineFill()
self.Start()
if self.isgameover==TRUE:showinfo('T_T','The game is over!');self.Distroy();return FALSE
for i in range(4):
for j in range(4):
self.NextEmpty(i,j)
self.Rnd()
return Moveable
def JudgeLineFill(self):

