Python实例练手项目源码 - 俄罗斯方块

举报
CodeAllen 发表于 2021/10/29 23:58:06 2021/10/29
【摘要】 游戏效果:   源码: #_*_ coding:utf-8 _*_from tkinter import *import randomimport timeimport tkinter.messagebox #俄罗斯方块界面的高度HEIGHT = 20 #俄罗斯方块界面的宽度WIDTH = 10 ACTIVE...

游戏效果:

 

源码:


  
  1. #_*_ coding:utf-8 _*_
  2. from tkinter import *
  3. import random
  4. import time
  5. import tkinter.messagebox
  6. #俄罗斯方块界面的高度
  7. HEIGHT = 20
  8. #俄罗斯方块界面的宽度
  9. WIDTH = 10
  10. ACTIVE = 1
  11. PASSIVE = 0
  12. TRUE = 1
  13. FALSE = 0
  14. style = [
  15. [[(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
  16. [[(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
  17. [[(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
  18. [[(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
  19. [[(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
  20. [[(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)]],#田
  21. [[(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)]]#长条
  22. ]
  23. root=Tk();
  24. root.title('俄罗斯方块')
  25. class App(Frame):
  26. def __init__(self,master):
  27. Frame.__init__(self)
  28. master.bind('<Up>',self.Up)
  29. master.bind('<Left>',self.Left)
  30. master.bind('<Right>',self.Right)
  31. master.bind('<Down>',self.Down)
  32. master.bind('<space>',self.Space)
  33. master.bind('<Control-Shift-Key-F12>',self.Play)
  34. master.bind('<Key-P>',self.Pause)
  35. master.bind('<Key-S>',self.StartByS)
  36. # rgb颜色值
  37. self.backg="#%02x%02x%02x" % (120,150,30) #大背景
  38. self.frontg="#%02x%02x%02x" % (40,120,150) #下一个形状颜色
  39. self.nextg="#%02x%02x%02x" % (150,100,100) #小背景
  40. self.flashg="#%02x%02x%02x" % (210,130,100) #炸的颜色
  41. self.LineDisplay=Label(master,text='Lines: ',bg='black',fg='red')
  42. self.Line=Label(master,text='0',bg='black',fg='red')
  43. self.ScoreDisplay=Label(master,text='Score: ',bg='black',fg='red')
  44. self.Score=Label(master,text='0',bg='black',fg='red')
  45. self.SpendTimeDisplay=Label(master,text='Time: ',bg='black',fg='red')
  46. self.SpendTime=Label(master,text='0.0',bg='black',fg='red')
  47. self.LineDisplay.grid(row=HEIGHT-2,column=WIDTH,columnspan=2)
  48. self.Line.grid(row=HEIGHT-2,column=WIDTH+2,columnspan=3)
  49. self.ScoreDisplay.grid(row=HEIGHT-1,column=WIDTH,columnspan=2)
  50. self.Score.grid(row=HEIGHT-1,column=WIDTH+2,columnspan=3)
  51. self.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2)
  52. self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3)
  53. self.TotalTime=0.0
  54. self.TotalLine=0
  55. self.TotalScore=0
  56. #游戏结束
  57. self.isgameover=FALSE
  58. #暂停
  59. self.isPause=FALSE
  60. #开始
  61. self.isStart=FALSE
  62. self.NextList=[] #整个小背景
  63. self.NextRowList=[] #一行小背景
  64. self.px=0
  65. self.py=0 #记录方块参考点
  66. #渲染小背景
  67. r=0;c=0
  68. for k in range(4*4):
  69. LN=Label(master,text=' ',bg=str(self.nextg),fg='white',relief=FLAT,bd=3)
  70. LN.grid(row=r,column=WIDTH+c,sticky=N+E+S+W)
  71. self.NextRowList.append(LN)
  72. c=c+1
  73. if c>=4:
  74. r=r+1;c=0
  75. self.NextList.append(self.NextRowList)
  76. self.NextRowList=[]
  77. #渲染大背景
  78. self.BlockList=[]
  79. self.BlockRowList=[]
  80. self.LabelList=[]
  81. self.LabelRowList=[]
  82. row=0;col=0
  83. for i in range(HEIGHT*WIDTH):
  84. L=Label(master,text=' ',bg=str(self.backg),fg='white',relief=FLAT,bd=4)
  85. L.grid(row=row,column=col,sticky=N+E+S+W)
  86. L.row=row;L.col=col;L.isactive=PASSIVE
  87. self.BlockRowList.append(0); #大背景每个格子初始化为0值
  88. self.LabelRowList.append(L)
  89. col=col+1
  90. if col>=WIDTH:
  91. row=row+1;col=0
  92. self.BlockList.append(self.BlockRowList)
  93. self.LabelList.append(self.LabelRowList)
  94. self.BlockRowList=[]
  95. self.LabelRowList=[]
  96. #file
  97. fw=open('text.txt','a')
  98. fw.close()
  99. hasHead=FALSE
  100. f=open('text.txt','r')
  101. if f.read(5)=='score':
  102. hasHead=TRUE
  103. f.close()
  104. self.file=open('text.txt','a')
  105. if hasHead==FALSE:
  106. self.file.write('score line time scorePtime linePtime scorePline date/n')
  107. self.file.flush()
  108. self.time=1000
  109. self.OnTimer()
  110. def __del__(self):
  111. #self.file.close()
  112. pass
  113. def Pause(self,event):
  114. self.isPause=1-self.isPause
  115. def Up(self,event):
  116. BL=self.BlockList #格子的值
  117. LL=self.LabelList #格子Label
  118. Moveable=TRUE #是否可旋转
  119. #代码编写开始
  120. nowStyle = style[self.xnow][(self.ynow)]
  121. newStyle = style[self.xnow][(self.ynow+1)%4] #算出下一俄罗斯方块
  122. self.ynow = (self.ynow+1)%4 #此行代码非常重要,否则响应UP时,只能变第一次
  123. print("nowStyle:"+str(nowStyle)+"=====>>newStyle:"+str(newStyle))
  124. #根据现有形状中每个label的坐标计算出旋转后目标坐标(x,y)
  125. SourceList=[];DestList=[]
  126. for i in range(4):
  127. SourceList.append([ nowStyle[i][0]+self.px, nowStyle[i][1]+self.py])
  128. x = newStyle[i][0]+self.px
  129. y = newStyle[i][1]+self.py
  130. DestList.append([x, y])
  131. if x<0 or x>=HEIGHT or y<0 or y>=WIDTH : #or BL[x][y]==1 or LL[x][y].isactive==PASSIVE
  132. Moveable=FALSE
  133. if Moveable==TRUE:
  134. for i in range(len(SourceList)):
  135. self.Empty(SourceList[i][0],SourceList[i][1])
  136. for i in range(len(DestList)):
  137. self.Fill(DestList[i][0],DestList[i][1])
  138. def Left(self,event):
  139. BL=self.BlockList;LL=self.LabelList
  140. Moveable=TRUE
  141. for i in range(HEIGHT):
  142. for j in range(WIDTH):
  143. if LL[i][j].isactive==ACTIVE and j-1<0:Moveable=FALSE
  144. 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
  145. if Moveable==TRUE:
  146. self.py-=1
  147. for i in range(HEIGHT):
  148. for j in range(WIDTH):
  149. if j-1>=0 and LL[i][j].isactive==ACTIVE and BL[i][j-1]==0:
  150. self.Fill(i,j-1);self.Empty(i,j)
  151. def Right(self,event):
  152. BL=self.BlockList;LL=self.LabelList
  153. Moveable=TRUE
  154. for i in range(HEIGHT):
  155. for j in range(WIDTH):
  156. if LL[i][j].isactive==ACTIVE and j+1>=WIDTH:Moveable=FALSE
  157. 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
  158. if Moveable==TRUE:
  159. self.py+=1
  160. for i in range(HEIGHT-1,-1,-1):
  161. for j in range(WIDTH-1,-1,-1):
  162. if j+1<WIDTH and LL[i][j].isactive==ACTIVE and BL[i][j+1]==0:
  163. self.Fill(i,j+1);self.Empty(i,j)
  164. def Down(self,event):
  165. BL=self.BlockList;LL=self.LabelList
  166. Moveable=TRUE
  167. for i in range(HEIGHT):
  168. for j in range(WIDTH):
  169. if LL[i][j].isactive==ACTIVE and i+1>=HEIGHT:Moveable=FALSE
  170. 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
  171. if Moveable==TRUE and self.isStart :
  172. self.px+=1
  173. for i in range(HEIGHT-1,-1,-1):
  174. for j in range(WIDTH-1,-1,-1):
  175. if i+1<HEIGHT and LL[i][j].isactive==ACTIVE and BL[i+1][j]==0:
  176. self.Fill(i+1,j);self.Empty(i,j);
  177. if Moveable==FALSE:
  178. for i in range(HEIGHT):
  179. for j in range(WIDTH):
  180. LL[i][j].isactive=PASSIVE
  181. self.JudgeLineFill()
  182. self.Start()
  183. if self.isgameover==TRUE:showinfo('T_T','The game is over!');self.Distroy();return FALSE
  184. for i in range(4):
  185. for j in range(4):
  186. self.NextEmpty(i,j)
  187. self.Rnd()
  188. return Moveable
  189. def Space(self,event):
  190. while 1:
  191. if self.Down(0)==FALSE:break
  192. def OnTimer(self):
  193. if self.isStart==TRUE and self.isPause==FALSE:
  194. self.TotalTime = self.TotalTime + float(self.time)/1000
  195. self.SpendTime.config(text=str(self.TotalTime))
  196. if self.isPause==FALSE:
  197. self.Down(0)
  198. if self.TotalScore>=1000:self.time=900
  199. if self.TotalScore>=2000:self.time=750
  200. if self.TotalScore>=3000:self.time=600
  201. if self.TotalScore>=4000:self.time=400
  202. self.after(self.time,self.OnTimer) #随着分数增大,俄罗斯方块下降速度加快
  203. def JudgeLineFill(self):
  204. BL=self.BlockList;LL=self.LabelList
  205. count=0;LineList=[]
  206. for i in range(WIDTH):LineList.append(1)
  207. #display flash
  208. for i in range(HEIGHT):
  209. if BL[i]==LineList:
  210. count=count+1
  211. for k in range(WIDTH):
  212. LL[i][k].config(bg=str(self.flashg))
  213. LL[i][k].update()
  214. if count!=0:self.after(100)
  215. #delete block
  216. for i in range(HEIGHT):
  217. if BL[i]==LineList:
  218. #count=count+1
  219. for j in range(i,0,-1):
  220. for k in range(WIDTH):
  221. BL[j][k]=BL[j-1][k]
  222. LL[j][k]['relief']=LL[j-1][k].cget('relief')
  223. LL[j][k]['bg']=LL[j-1][k].cget('bg')
  224. for l in range(WIDTH):
  225. BL[0][l]=0
  226. LL[0][l].config(relief=FLAT,bg=str(self.backg))
  227. self.TotalLine=self.TotalLine+count
  228. if count==1:self.TotalScore=self.TotalScore+1*WIDTH
  229. if count==2:self.TotalScore=self.TotalScore+3*WIDTH
  230. if count==3:self.TotalScore=self.TotalScore+6*WIDTH
  231. if count==4:self.TotalScore=self.TotalScore+10*WIDTH
  232. self.Line.config(text=str(self.TotalLine))
  233. self.Score.config(text=str(self.TotalScore))
  234. def Fill(self,i,j):
  235. if j<0:return
  236. if self.BlockList[i][j]==1:self.isgameover=TRUE
  237. self.BlockList[i][j]=1
  238. self.LabelList[i][j].isactive=ACTIVE
  239. self.LabelList[i][j].config(relief=RAISED,bg=str(self.frontg))
  240. def Empty(self,i,j):
  241. self.BlockList[i][j]=0
  242. self.LabelList[i][j].isactive=PASSIVE
  243. self.LabelList[i][j].config(relief=FLAT,bg=str(self.backg))
  244. def Play(self,event):
  245. showinfo('Made in China','^_^')
  246. def NextFill(self,i,j):
  247. self.NextList[i][j].config(relief=RAISED,bg=str(self.frontg))
  248. def NextEmpty(self,i,j):
  249. self.NextList[i][j].config(relief=FLAT,bg=str(self.nextg))
  250. def Distroy(self):
  251. #save
  252. if self.TotalScore!=0:
  253. #cehkongfu
  254. savestr='%-9u%-8u%-8.2f%-14.2f%-13.2f%-14.2f%s/n' % (
  255. self.TotalScore,self.TotalLine,self.TotalTime
  256. ,self.TotalScore/self.TotalTime
  257. ,self.TotalLine/self.TotalTime
  258. ,float(self.TotalScore)/self.TotalLine
  259. ,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
  260. self.file.seek(0,2)
  261. self.file.write(savestr)
  262. self.file.flush()
  263. for i in range(HEIGHT):
  264. for j in range(WIDTH):
  265. self.Empty(i,j)
  266. self.TotalLine=0;self.TotalScore=0;self.TotalTime=0.0
  267. self.Line.config(text=str(self.TotalLine))
  268. self.Score.config(text=str(self.TotalScore))
  269. self.SpendTime.config(text=str(self.TotalTime))
  270. self.isgameover=FALSE
  271. self.isStart=FALSE
  272. self.time=1000
  273. for i in range(4):
  274. for j in range(4):
  275. self.NextEmpty(i,j)
  276. #游戏开始方块
  277. def Start(self):
  278. nextStyle = style[self.x][self.y] #下一形状
  279. self.xnow = self.x
  280. self.ynow = self.y #记录大背景中的方块
  281. self.py = random.randint(0,6)
  282. print("给py赋任意值:"+str(self.py))
  283. self.px = 0
  284. for ii in range(4):
  285. self.Fill(int(nextStyle[ii][0]),int(nextStyle[ii][1])+self.py)
  286. self.isStart=TRUE #游戏开始
  287. #预处理方块
  288. def Rnd(self):
  289. self.x=random.randint(0,6)
  290. self.y=random.randint(0,3)
  291. nextStyle = style[self.x][self.y] #下一形状
  292. for ii in range(4):
  293. self.NextFill(int(nextStyle[ii][0]),int(nextStyle[ii][1]))
  294. #游戏开始给出一次任意形状的方块
  295. def RndFirst(self):
  296. self.x=random.randint(0,6) #选择第一个方块style
  297. self.y=random.randint(0,3)
  298. def Show(self):
  299. self.file.seek(0)
  300. strHeadLine=self.file.readline()
  301. dictLine={}
  302. strTotalLine=''
  303. for OneLine in self.file.readlines():
  304. temp=int(OneLine[:5])
  305. dictLine[temp]=OneLine
  306. list=sorted(dictLine.items(),key=lambda d:d[0])
  307. ii=0
  308. for onerecord in reversed(list):
  309. ii=ii+1
  310. if ii<11:
  311. strTotalLine+=onerecord[1]
  312. showinfo('Ranking', strHeadLine+strTotalLine)
  313. def StartByS(self,event):
  314. self.RndFirst()
  315. self.Start()
  316. self.Rnd()
  317. def Start():
  318. app.RndFirst()
  319. app.Start()
  320. app.Rnd()
  321. def End():
  322. app.Distroy()
  323. def Set():
  324. print("设置功能待完善...")
  325. def Show():
  326. app.Show()
  327. #主菜单
  328. mainmenu=Menu(root)
  329. root['menu']=mainmenu
  330. #二级菜单:game
  331. gamemenu=Menu(mainmenu)
  332. mainmenu.add_cascade(label='游戏',menu=gamemenu)
  333. gamemenu.add_command(label='开始',command=Start)
  334. gamemenu.add_command(label='结束',command=End)
  335. gamemenu.add_separator()
  336. gamemenu.add_command(label='退出',command=root.quit)
  337. #二级菜单:set
  338. setmenu=Menu(mainmenu)
  339. mainmenu.add_cascade(label='设置',menu=setmenu)
  340. setmenu.add_command(label='设置',command=Set)
  341. #二级菜单:show
  342. showmenu=Menu(mainmenu)
  343. mainmenu.add_cascade(label='展示',menu=showmenu)
  344. showmenu.add_command(label='展示',command=Show)
  345. #绑定功能
  346. app=App(root)
  347. #程序入口
  348. root.mainloop()

 

文章来源: allen5g.blog.csdn.net,作者:CodeAllen的博客,版权归原作者所有,如需转载,请联系作者。

原文链接:allen5g.blog.csdn.net/article/details/117637540

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。