用python编写一个简单的俄罗斯方块游戏的教程

俄罗斯方块游戏,使用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(”,self.up)
master.bind(”,self.left)
master.bind(”,self.right)
master.bind(”,self.down)
#master.bind(”,self.space)
master.bind(”,self.space)
master.bind(”,self.play)
master.bind(”,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=height or y=width:moveable=false
if x>=0 and x=0 and y=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=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

Posted in 未分类

发表评论