70个python项⽬代码_python项⽬实例源码#_*_ coding:utf-8 _*_
from tkinter import *
HEIGHT = 20
#俄罗斯⽅块界⾯的宽度
WIDTH = 10ACTIVE= 1PASSIVE=0
TRUE= 1FALSE=0
style=[
[[(0,0),(0,1),(1,1),(2,1)],[(1,0),(1,1),(1,2),(0,2)],[(0,1),(1,1),(2,1),(2,2)],[(1,0),(2,0),(1,1),(1,2)]],#j
[[(1,0),(1,1),(1,2),(2,1)],[(1,0),(0,1),(1,1),(2,1)],[(1,0),(1,1),(1,2),(0,1)],[(0,1),(1,1),(2,1),(1,2)]],#T
[[(0,1),(1,1),(2,1),(2,0)],[(0,0),(1,0),(1,1),(1,2)],[(0,1),(1,1),(2,1),(0,2)],[(1,0),(1,1),(1,2),(2,2)]],#反L
[[(0,0),(0,1),(1,1),(1,2)],[(2,1),(1,1),(1,2),(0,2)],[(0,0),(0,1),(1,1),(1,2)],[(2,1),(1,1),(1,2),(0,2)]],#Z
[[(1,0),(1,1),(0,1),(0,2)],[(0,1),(1,1),(1,2),(2,2)],[(1,0),(1,1),(0,1),(0,2)],[(0,1),(1,1),(1,2),(2,2)]],#反Z
[[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)]],#⽥
[[(1,0),(1,1),(1,2),(1,3)],[(0,1),(1,1),(2,1),(3,1)],[(1,0),(1,1),(1,2),(1,3)],[(0,1),(1,1),(2,1),(3,1)]]#长条
]
root=Tk();
root.title('俄罗斯⽅块')classApp(Frame):def __init__(self,master):
java最好的刷题工具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.Play)
master.bind('',self.Pause)
master.bind('',self.StartByS)#rgb颜⾊值
self.backg="#%02x%02x%02x" % (120,150,30) #⼤背景
self.frontg="#%02x%02x%02x" % (40,120,150) #下⼀个形状颜⾊
<="#%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')
self.SpendTimeDisplay=Label(master,text='Time:',bg='black',fg='red')
self.SpendTime=Label(master,text='0.0',bg='black',fg='red')
id(row=HEIGHT-2,column=WIDTH,columnspan=2)
id(row=HEIGHT-2,column=WIDTH+2,columnspan=3)
id(row=HEIGHT-1,column=WIDTH,columnspan=2) id(row=HEIGHT-1,column=WIDTH+2,columnspan=3)
id(row=HEIGHT-4,column=WIDTH,columnspan=2) id(row=HEIGHT-4,column=WIDTH+2,columnspan=3) self.TotalTime=0.0self.TotalLine=0
self.TotalScore=0#游戏结束
self.isgameover=FALSE#暂停
self.isPause=FALSE#开始
self.isStart=FALSE
self.NextList=[] #整个⼩背景
self.NextRowList=[] #⼀⾏⼩背景
self.px=0
self.py=0 #记录⽅块参考点
#渲染⼩背景
r=0;c=0for k in range(4*4):
LN=Label(master,text=' ',bg=),fg='white',relief=FLAT,bd=3) 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.BlockRowList=[]
self.LabelList=[]
self.LabelRowList=[]
row=0;col=0for i in range(HEIGHT*WIDTH):
L=Label(master,text=' ',bg=str(self.backg),fg='white',relief=FLAT,bd=4)
self.BlockRowList.append(0);#⼤背景每个格⼦初始化为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('','a')
fw.close()
hasHead=FALSE
f=open('','r')ad(5)=='score':
hasHead=TRUE
f.close()
self.file=open('','a')if hasHead==FALSE:
self.file.write('score line time scorePtime linePtime scorePline date/n')
self.file.flush()
self.time=1000self.OnTimer()def __del__(self):#self.file.close()
pass
defPause(self,event):
self.isPause=1-self.isPausedefUp(self,event):
BL=self.BlockList #格⼦的值
LL=self.LabelList #格⼦Label
Moveable=TRUE #是否可旋转
#代码编写开始
nowStyle =w][(w)]
newStyle= w][(w+1)%4] #算出下⼀俄罗斯⽅块
print("nowStyle:"+str(nowStyle)+"=====>>newStyle:"+str(newStyle))#根据现有形状中每个label的坐标计算出旋转后⽬标坐标(x,y) SourceList=[];DestList=[]for i in range(4):
python基础代码100例SourceList.append([ nowStyle[i][0]+self.px, nowStyle[i][1]+self.py])
x= newStyle[i][0]+self.px
y= newStyle[i][1]+self.py
DestList.append([x, y])if x<0 or x>=HEIGHT or y<0 or y>=WIDTH : #or BL[x][y]==1 or LL[x][y].isactive==PASSIVE
Moveable=FALSEif Moveable==TRUE:for i inrange(len(SourceList)):自定义格式化json文件
self.Empty(SourceList[i][0],SourceList[i][1])for i inrange(len(DestList)):
self.Fill(DestList[i][0],DestList[i][1])defLeft(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUEfor i inrange(HEIGHT):for j inrange(WIDTH):if LL[i][j].isactive==ACTIVE and j-1<0:Moveable=FALSEif LL[i] [j].isactive==ACTIVE and j-1>=0 and BL[i][j-1]==1 and LL[i][j-1].isactive==PASSIVE:Moveable=FALSEif Moveable==TRUE:
self.py-=1
for i inrange(HEIGHT):for j inrange(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)defRight(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUEfor i inrange(HEIGHT):for j inrange(WIDTH):if LL[i][j].isactive==ACTIVE and j+1>=WIDTH:Moveable=FALSEif LL[i][j].isactive==ACTIVE and j+1
self.py+=1
for i in range(HEIGHT-1,-1,-1):for j in range(WIDTH-1,-1,-1):if j+1
self.Fill(i,j+1);self.Empty(i,j)defDown(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUEfor i inrange(HEIGHT):for j inrange(WIDTH):if LL[i][j].isactive==ACTIVE and
i+1>=HEIGHT:Moveable=FALSEif LL[i][j].isactive==ACTIVE and i+1
self.px+=1
for i in range(HEIGHT-1,-1,-1):for j in range(WIDTH-1,-1,-1):if i+1htm是什么文件
self.Fill(i+1,j);self.Empty(i,j);if Moveable==FALSE:for i inrange(HEIGHT):for j inrange(WIDTH):
LL[i][j].isactive=PASSIVE
self.JudgeLineFill()
self.Start()if self.isgameover==TRUE:showinfo('T_T','The game is over!');self.Distroy();returnFALSEfor i in range(4):for j in range(4):
self.NextEmpty(i,j)
self.Rnd()returnMoveabledefSpace(self,event):while 1:if self.Down(0)==FALSE:break
defOnTimer(self):if self.isStart==TRUE and self.isPause==FALSE:
self.TotalTime= self.TotalTime + float(self.time)/fig(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=400self.after(self.time,self.OnTimer)#随着分数增⼤,俄罗斯⽅块下降速度加快
defJudgeLineFill(self):
BL=self.BlockList;LL=self.LabelList
count=0;LineList=[]for i in range(WIDTH):LineList.append(1)#display flash
for i inrange(HEIGHT):if BL[i]==LineList:
count=count+1
html中蓝字体代码for k inrange(WIDTH):
LL[i][k].config(bg=str(self.flashg))
LL[i][k].update()if count!=0:self.after(100)#delete block
for i inrange(HEIGHT):if BL[i]==LineList:#count=count+1
for j in range(i,0,-1):for k inrange(WIDTH):
BL[j][k]=BL[j-1][k]
LL[j][k]['relief']=LL[j-1][k].cget('relief')
LL[j][k]['bg']=LL[j-1][k].cget('bg')for l inrange(WIDTH):
BL[0][l]=0
LL[0][l].config(relief=FLAT,bg=str(self.backg))
self.TotalLine=self.TotalLine+countif count==1:self.TotalScore=self.TotalScore+1*WIDTHif
count==2:self.TotalScore=self.TotalScore+3*WIDTHif count==3:self.TotalScore=self.TotalScore+6*WIDTHif count==4:self.TotalScore=self.TotalScore+10*WIDTH
fig(text=str(self.TotalLine))
fig(text=str(self.TotalScore))defFill(self,i,j):if j<0:return
if self.BlockList[i][j]==1:self.isgameover=TRUE
self.BlockList[i][j]=1self.LabelList[i][j].isactive=ACTIVE
html表单做年月日怎么设置self.LabelList[i][j].config(relief=RAISED,bg=str(self.frontg))defEmpty(self,i,j):
self.BlockList[i][j]=0
self.LabelList[i][j].isactive=PASSIVE
self.LabelList[i][j].config(relief=FLAT,bg=str(self.backg))defPlay(self,event):
showinfo('Made in China','^_^')defNextFill(self,i,j):
self.NextList[i][j].config(relief=RAISED,bg=str(self.frontg))defNextEmpty(self,i,j):
self.NextList[i][j].config(relief=FLAT,bg=))defDistroy(self):#save
if self.TotalScore!=0:#cehkongfu
savestr='%-9u%-8u%-8.2f%-14.2f%-13.2f%-14.2f%s/n' %(
self.TotalScore,self.TotalLine,self.TotalTime
,self.TotalScore/self.TotalTime
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论