python编写俄罗斯方块


Posted in Python onMarch 13, 2020

本文实例为大家分享了python实现俄罗斯方块的具体代码,供大家参考,具体内容如下

#coding=utf-8 
from tkinter import *
from random import *
import threading
from tkinter.messagebox import showinfo
from tkinter.messagebox import askquestion
import threading
from time import sleep
 
 
class BrickGame(object): 
 
 #是否开始 
 start = True; 
 #是否到达底部 
 isDown = True; 
 isPause = False; 
 #窗体 
 window = None; 
 #frame 
 frame1 = None; 
 frame2 = None; 
 
 #按钮 
 btnStart = None; 
 
 #绘图类 
 canvas = None; 
 canvas1 = None; 
 
 #标题 
 title = "BrickGame"; 
 #宽和高 
 width = 450; 
 height = 670; 
 
 #行和列 
 rows = 20; 
 cols = 10; 
 
 #下降方块的线程 
 downThread = None; 
 
 #几种方块 
 brick = [ 
  [ 
   [ 
    [0,1,1], 
    [1,1,0], 
    [0,0,0] 
   ], 
   [ 
    [1,0,0], 
    [1,1,0], 
    [0,1,0] 
   ], 
   [ 
    [0,1,1], 
    [1,1,0], 
    [0,0,0] 
   ], 
   [ 
    [1,0,0], 
    [1,1,0], 
    [0,1,0] 
   ] 
 ],
 [ 
   [ 
    [1,1,1], 
    [1,0,0], 
    [0,0,0] 
   ], 
   [ 
    [0,1,1], 
    [0,0,1], 
    [0,0,1] 
   ], 
   [ 
    [0,0,0], 
    [0,0,1], 
    [1,1,1] 
   ], 
   [ 
    [1,0,0], 
    [1,0,0], 
    [1,1,0] 
   ] 
 ], 
 [ 
   [ 
    [1,1,1], 
    [0,0,1], 
    [0,0,0] 
   ], 
   [ 
    [0,0,1], 
    [0,0,1], 
    [0,1,1] 
   ], 
   [ 
    [0,0,0], 
    [1,0,0], 
    [1,1,1] 
   ], 
   [ 
    [1,1,0], 
    [1,0,0], 
    [1,0,0] 
   ] 
 ], 
 [ 
   [ 
    [0,0,0], 
    [0,1,1], 
    [0,1,1] 
   ], 
   [ 
    [0,0,0], 
    [0,1,1], 
    [0,1,1] 
   ], 
   [ 
    [0,0,0], 
    [0,1,1], 
    [0,1,1] 
   ], 
   [ 
    [0,0,0], 
    [0,1,1], 
    [0,1,1] 
   ]   
 ], 
 [ 
   [ 
    [1,1,1], 
    [0,1,0], 
    [0,0,0] 
   ], 
   [ 
    [0,0,1], 
    [0,1,1], 
    [0,0,1] 
   ], 
   [ 
    [0,0,0], 
    [0,1,0], 
    [1,1,1] 
   ], 
   [ 
    [1,0,0], 
    [1,1,0], 
    [1,0,0] 
   ] 
 ], 
 [ 
   [ 
    [0,1,0], 
    [0,1,0], 
    [0,1,0]
    
   ], 
   [ 
    [0,0,0], 
    [1,1,1], 
    [0,0,0]
    
   ], 
   [ 
    [0,1,0], 
    [0,1,0], 
    [0,1,0] 
   ], 
   [ 
    [0,0,0], 
    [1,1,1], 
    [0,0,0] 
   ] 
 ],
 [ 
   [ 
    [1,1,0], 
    [0,1,1], 
    [0,0,0] 
   ], 
   [ 
    [0,0,1], 
    [0,1,1], 
    [0,1,0] 
   ], 
   [ 
    [0,0,0], 
    [1,1,0], 
    [0,1,1] 
   ], 
   [ 
    [0,1,0], 
    [1,1,0], 
    [1,0,0] 
   ] 
 ]
 
 
 ]; 
 
 #当前的方块 
 curBrick = None; 
 #当前方块数组 
 arr = None; 
 arr1 = None; 
 #当前方块形状 
 shape = -1; 
 #当前方块的行和列(最左上角) 
 curRow = -10; 
 curCol = -10; 
 
 #背景 
 back = list(); 
 #格子 
 gridBack = list(); 
 preBack = list(); 
 
 #初始化 
 def init(self): 
  
 for i in range(0,self.rows): 
  
  self.back.insert(i,list()); 
  self.gridBack.insert(i,list()); 
  
 for i in range(0,self.rows): 
  
  for j in range(0,self.cols): 
   
  self.back[i].insert(j,0); 
  self.gridBack[i].insert(j,self.canvas.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black")); 
   
 for i in range(0,3): 
  
  self.preBack.insert(i,list()); 
  
 for i in range(0,3): 
  
  for j in range(0,3): 
   
  self.preBack[i].insert(j,self.canvas1.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black")); 
 
 #绘制游戏的格子 
 def drawRect(self): 
 for i in range(0,self.rows): 
   
   for j in range(0,self.cols): 
    
   
   if self.back[i][j]==1: 
    
    self.canvas.itemconfig(self.gridBack[i][j],fill="blue",outline="white"); 
    
   elif self.back[i][j]==0: 
    
    self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white"); 
    
 #绘制预览方块 
 for i in range(0,len(self.arr1)): 
  
  for j in range(0,len(self.arr1[i])): 
   
  if self.arr1[i][j]==0: 
   
   self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white"); 
   
  elif self.arr1[i][j]==1: 
   
   self.canvas1.itemconfig(self.preBack[i][j],fill="orange",outline="white"); 
 
    
 #绘制当前正在运动的方块 
 if self.curRow!=-10 and self.curCol!=-10: 
  
  for i in range(0,len(self.arr)): 
   
  for j in range(0,len(self.arr[i])): 
   
   if self.arr[i][j]==1:   
    
   self.canvas.itemconfig(self.gridBack[self.curRow+i][self.curCol+j],fill="blue",outline="white"); 
    
 #判断方块是否已经运动到达底部 
 if self.isDown: 
  
  for i in range(0,3): 
   
  for j in range(0,3): 
   
   if self.arr[i][j]!=0: 
    
   self.back[self.curRow+i][self.curCol+j] = self.arr[i][j]; 
    
  #判断整行消除 
  self.removeRow(); 
  
  #判断是否死了 
  self.isDead(); 
   
  #获得下一个方块 
  self.getCurBrick(); 
 
 #判断是否有整行需要消除 
 def removeRow(self): 
 count=0
 for i in range(0,self.rows): 
 
  tag1 = True;  
  for j in range(0,self.cols): 
   
  if self.back[i][j]==0: 
   
   tag1 = False; 
   break; 
  
  if tag1==True: 
   
  #从上向下挪动
  count=count+1
  for m in range(i-1,0,-1): 
   
   for n in range(0,self.cols): 
    
   self.back[m+1][n] = self.back[m][n]; 
 
   
 
 scoreValue = eval(self.scoreLabel2['text'])
 scoreValue += 5*count*(count+3)
 self.scoreLabel2.config(text=str(scoreValue))
  
 #获得当前的方块 
 def getCurBrick(self): 
  
 self.curBrick = randint(0,len(self.brick)-1); 
 self.shape = 0; 
 #当前方块数组 
 self.arr = self.brick[self.curBrick][self.shape]; 
 self.arr1 = self.arr; 
  
 self.curRow = 0; 
 self.curCol = 1; 
  
 #是否到底部为False 
 self.isDown = False; 
  
 #监听键盘输入 
 def onKeyboardEvent(self,event): 
  
 #未开始,不必监听键盘输入 
 if self.start == False: 
  
  return;
 
 if self.isPause == True:
  return;
  
 #记录原来的值 
 tempCurCol = self.curCol; 
 tempCurRow = self.curRow; 
 tempShape = self.shape; 
 tempArr = self.arr; 
 direction = -1; 
  
 if event.keycode==37: 
  
  #左移 
  self.curCol-=1; 
  direction = 1; 
 elif event.keycode==38: 
  #变化方块的形状 
  self.shape+=1; 
  direction = 2; 
  
  if self.shape>=4: 
   
  self.shape=0; 
  self.arr = self.brick[self.curBrick][self.shape]; 
 elif event.keycode==39: 
  
  direction = 3; 
  #右移 
  self.curCol+=1; 
 elif event.keycode==40: 
  
  direction = 4; 
  #下移 
  self.curRow+=1; 
  
 if self.isEdge(direction)==False: 
  
  self.curCol = tempCurCol; 
  self.curRow = tempCurRow; 
  self.shape = tempShape; 
  self.arr = tempArr; 
   
 self.drawRect(); 
  
 return True; 
 
 #判断当前方块是否到达边界 
 def isEdge(self,direction): 
  
 tag = True; 
 
 #向左,判断边界 
 if direction==1: 
  
  for i in range(0,3): 
   
  for j in range(0,3): 
   
   if self.arr[j][i]!=0 and (self.curCol+i<0 or self.back[self.curRow+j][self.curCol+i]!=0): 
    
   tag = False; 
   break; 
 #向右,判断边界 
 elif direction==3: 
  
  for i in range(0,3): 
   
  for j in range(0,3): 
   
   if self.arr[j][i]!=0 and (self.curCol+i>=self.cols or self.back[self.curRow+j][self.curCol+i]!=0): 
    
   tag = False; 
   break; 
 #向下,判断底部 
 elif direction==4: 
  
  for i in range(0,3): 
   
  for j in range(0,3): 
   
   if self.arr[i][j]!=0 and (self.curRow+i>=self.rows or self.back[self.curRow+i][self.curCol+j]!=0): 
    
   tag = False; 
   self.isDown = True; 
   break; 
 #进行变形,判断边界 
 elif direction==2: 
  
  if self.curCol<0: 
   
  self.curCol=0; 
  
  if self.curCol+2>=self.cols: 
   
  self.curCol = self.cols-3; 
   
  if self.curRow+2>=self.rows: 
   
  self.curRow = self.curRow-3; 
  
  
 return tag; 
 
 #方块向下移动 
 def brickDown(self): 
  
 while True: 
  
  if self.start==False: 
   
  print("exit thread"); 
  break; 
  if self.isPause==False: 
  tempRow = self.curRow; 
  self.curRow+=1; 
   
  if self.isEdge(4)==False: 
   
   self.curRow = tempRow; 
   
  self.drawRect(); 
    
  #每一秒下降一格 
  sleep(1); 
  
 #点击开始 
 def clickStart(self): 
  
 self.start = True; 
  
 for i in range(0,self.rows): 
  
  for j in range(0,self.cols): 
   
  self.back[i][j] = 0; 
  self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white"); 
   
 for i in range(0,len(self.arr)): 
  
  for j in range(0,len(self.arr[i])): 
   
  self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white"); 
   
 self.getCurBrick(); 
 self.drawRect(); 
  
 self.downThread = threading.Thread(target=self.brickDown,args=()); 
 self.downThread.start();
 
 def clickPause(self):
 self.isPause=not self.isPause
 print(self.isPause)
 if not self.isPause:
  self.btnPause["text"]="暂停"
 else:
  self.btnPause["text"]="恢复"
  
 
 def clickReStart(self):
 ackRestart =askquestion("重新开始","你确定要重新开始吗?")
 if ackRestart == 'yes':
  self.clickStart()
 else:
  return
 
 def clickQuit(self):
 ackQuit =askquestion("退出","你确定要退出吗?")
 if ackQuit == 'yes':
  self.window.destroy()
  exit()
 
 
  
 #判断是否死了 
 def isDead(self): 
  
 for j in range(0,len(self.back[0])): 
  
  if self.back[0][j]!=0: 
   
  showinfo("提示","你挂了,再来一盘吧!"); 
  self.start = False; 
  break; 
  
 #运行 
 def __init__(self): 
  
 self.window = Tk(); 
 self.window.title(self.title); 
 self.window.minsize(self.width,self.height); 
 self.window.maxsize(self.width,self.height);   
  
 self.frame1 = Frame(self.window,width=300,height=600,bg="black"); 
 self.frame1.place(x=20,y=30); 
 
 self.scoreLabel1 = Label(self.window,text="Score:",font=(30))
 self.scoreLabel1.place(x=340,y=60)
 self.scoreLabel2 = Label(self.window,text="0",fg='red',font=(30))
 self.scoreLabel2.place(x=410,y=60)
 
 self.frame2 = Frame(self.window,width=90,height=90,bg="black"); 
 self.frame2.place(x=340,y=120); 
  
 self.canvas = Canvas(self.frame1,width=300,height=600,bg="black"); 
 self.canvas1 = Canvas(self.frame2,width=90,height=90,bg="black"); 
  
 self.btnStart = Button(self.window,text="开始",command=self.clickStart); 
 self.btnStart.place(x=340,y=400,width=80,height=25); 
 
 self.btnPause = Button(self.window,text="暂停",command=self.clickPause); 
 self.btnPause.place(x=340,y=450,width=80,height=25);
 
 self.btnReStart = Button(self.window,text="重新开始",command=self.clickReStart); 
 self.btnReStart.place(x=340,y=500,width=80,height=25);
 
 self.btnQuit = Button(self.window,text="退出",command=self.clickQuit); 
 self.btnQuit.place(x=340,y=550,width=80,height=25);
 
 
 
 self.init(); 
  
 #获得当前的方块 
 self.getCurBrick(); 
  
 #按照数组,绘制格子
 
 
 self.drawRect();
  
 self.canvas.pack();
   
  
 self.canvas1.pack(); 
 
 #监听键盘事件 
 self.window.bind("<KeyPress>",self.onKeyboardEvent); 
  
 #启动方块下落线程
 self.downThread = threading.Thread(target=self.brickDown,args=()); 
 self.downThread.start();  
  
 self.window.mainloop(); 
  
 self.start=False; 
  
 pass; 
 
if __name__=='__main__': 
 
 brickGame = BrickGame();

更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用python编写android截屏脚本双击运行即可
Jul 21 Python
Python判断操作系统类型代码分享
Nov 22 Python
在阿里云服务器上配置CentOS+Nginx+Python+Flask环境
Jun 18 Python
Python实现学校管理系统
Jan 11 Python
python使用tkinter实现简单计算器
Jan 30 Python
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题
Sep 27 Python
python程序封装为win32服务的方法
Mar 07 Python
pycharm运行和调试不显示结果的解决方法
Nov 30 Python
python 实现Flask中返回图片流给前端展示
Jan 09 Python
基于python实现生成指定大小txt文档
Jul 20 Python
Python3读写ini配置文件的示例
Nov 06 Python
用Python可视化新冠疫情数据
Jan 18 Python
探秘TensorFlow 和 NumPy 的 Broadcasting 机制
Mar 13 #Python
自定义Django Form中choicefield下拉菜单选取数据库内容实例
Mar 13 #Python
django处理select下拉表单实例(从model到前端到post到form)
Mar 13 #Python
python实现俄罗斯方块游戏(改进版)
Mar 13 #Python
Python之Django自动实现html代码(下拉框,数据选择)
Mar 13 #Python
Tensorflow中的dropout的使用方法
Mar 13 #Python
python实现简单俄罗斯方块
Mar 13 #Python
You might like
中篇:安装及配置PHP
2006/12/13 PHP
一个典型的PHP分页实例代码分享
2011/07/28 PHP
php通过array_push()函数添加多个变量到数组末尾的方法
2015/03/18 PHP
phpmyadmin下载、安装、配置教程
2017/05/16 PHP
javascript中检测变量的类型的代码
2010/12/28 Javascript
js jquery分别实现动态的文件上传操作按钮的添加和删除
2014/01/13 Javascript
js判断ie版本号的简单实现代码
2014/03/05 Javascript
浅谈JSON.parse()和JSON.stringify()
2015/07/14 Javascript
javascript实现自动输出文本(打字特效)
2015/08/27 Javascript
Bootstrap入门书籍之(三)栅格系统
2016/02/17 Javascript
纯原生js实现table表格的增删
2017/01/05 Javascript
原生Aajax 和jQuery Ajax 写法个人总结
2017/03/24 jQuery
ES6新数据结构Map功能与用法示例
2017/03/31 Javascript
JS实现根据数组对象的某一属性排序操作示例
2019/01/14 Javascript
详解TypeScript+Vue 插件 vue-class-component的使用总结
2019/02/18 Javascript
微信小程序canvas绘制圆角base64图片的实现
2019/08/18 Javascript
[02:32]DOTA2完美大师赛场馆静安体育中心观赛全攻略
2017/11/08 DOTA
python实现应用程序在右键菜单中添加打开方式功能
2017/01/09 Python
用 Python 爬了爬自己的微信朋友(实例讲解)
2017/08/25 Python
python数据持久存储 pickle模块的基本使用方法解析
2019/08/30 Python
win7下 python3.6 安装opencv 和 opencv-contrib-python解决 cv2.xfeatures2d.SIFT_create() 的问题
2019/10/24 Python
使用Python爬取弹出窗口信息的实例
2020/03/14 Python
QML用PathView实现轮播图
2020/06/03 Python
移动端html5 meta标签的神奇功效
2016/01/06 HTML / CSS
爱淘宝:淘宝网购物分享平台
2017/04/28 全球购物
印度在线杂货店:bigbasket
2018/08/23 全球购物
初中三年毕业生的自我评价分享
2014/02/14 职场文书
互联网创业计划书写作技巧攻略
2014/03/23 职场文书
幼儿园大班开学寄语
2014/08/02 职场文书
2014年教师党员自我评议
2014/09/19 职场文书
2014卖家双十一活动策划书
2014/09/29 职场文书
年度考核登记表个人总结
2015/03/06 职场文书
单位考核鉴定意见
2015/06/05 职场文书
小学运动会通讯稿
2015/07/18 职场文书
Python 循环读取数据内存不足的解决方案
2021/05/25 Python
一文弄懂MySQL索引创建原则
2022/02/28 MySQL