从网上整理的8款python小游戏,在python3.9下测试,全部成功通过
1.俄罗斯方块
1)实现效果

2)实现代码
#_*_ coding:utf-8 _*_ from tkinter import * import random import time import tkinter.messagebox #俄罗斯方块界面的高度 HEIGHT = 20 #俄罗斯方块界面的宽度 WIDTH = 10 ACTIVE = 1 PASSIVE = 0 TRUE = 1 FALSE = 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('俄罗斯方块') 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('<space>',self.Space) master.bind('<Control-Shift-Key-F12>',self.Play) master.bind('<Key-P>',self.Pause) master.bind('<Key-S>',self.StartByS) # rgb颜色值 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') 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) 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 #游戏结束 self.isgameover=FALSE #暂停 self.isPause=FALSE #开始 self.isStart=FALSE self.NextList=[] #整个小背景 self.NextRowList=[] #一行小背景 self.px=0 self.py=0 #记录方块参考点 #渲染小背景 r=0;c=0 for k in range(4*4): LN=Label(master,text=' ',bg=str(self.nextg),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=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); #大背景每个格子初始化为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','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 #格子Label Moveable=TRUE #是否可旋转 #代码编写开始 nowStyle = style[self.xnow][(self.ynow)] newStyle = style[self.xnow][(self.ynow+1)%4] #算出下一俄罗斯方块 self.ynow = (self.ynow+1)%4 #此行代码非常重要,否则响应UP时,只能变第一次 print("nowStyle:"+str(nowStyle)+"=====>>newStyle:"+str(newStyle)) #根据现有形状中每个label的坐标计算出旋转后目标坐标(x,y) SourceList=[];DestList=[] for i in range(4): 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=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: self.py-=1 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: self.py+=1 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 Down(self,event): BL=self.BlockList;LL=self.LabelList Moveable=TRUE for i

最低0.47元/天 解锁文章
2345

被折叠的 条评论
为什么被折叠?



