python实现俄罗斯方块游戏(改进版)


Posted in Python onMarch 13, 2020

本文为大家分享了python实现俄罗斯方块游戏,继上一篇的改进版,供大家参考,具体内容如下

1.加了方块预览部分

2.加了开始按钮

在公司实习抽空写的,呵呵。觉得Python还不错,以前觉得像个玩具语言。希望能够用它做更多大事吧!!!加油。

截图如下:

python实现俄罗斯方块游戏(改进版)

代码如下:

#coding=utf-8
from Tkinter import *;
from random import *;
import thread;  
from tkMessageBox import showinfo;
import threading;
from time import sleep;
class BrickGame(object):
 
 #是否开始
 start = True;
 #是否到达底部
 isDown = True;
 
 #窗体
 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 = [
  
  [
     [
       [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,1,0]
     ],
     [
        [0,0,1],
        [1,1,1],
        [0,0,1]
     ],
     [
        [0,1,0],
        [0,1,0],
        [1,1,1]
     ],
     [
        [1,0,0],
        [1,1,1],
        [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]
     ]
  ]
 ];
 
 #当前的方块
 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):
  
  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:
    
    #从上向下挪动
    for m in xrange(i-1,0,-1):
     
     for n in range(0,self.cols):
      
      self.back[m+1][n] = self.back[m][n];
   
 #获得当前的方块
 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;
  
  #记录原来的值
  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;
   
   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 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.frame2 = Frame(self.window,width=90,height=90,bg="black");
  self.frame2.place(x=340,y=60);
  
  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.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写的ARP攻击代码实例
Jun 04 Python
以Flask为例讲解Python的框架的使用方法
Apr 29 Python
python实现识别相似图片小结
Feb 22 Python
Python中 Lambda表达式全面解析
Nov 28 Python
Python探索之pLSA实现代码
Oct 25 Python
Python paramiko模块的使用示例
Apr 11 Python
python scipy求解非线性方程的方法(fsolve/root)
Nov 12 Python
很酷的python表白工具 你喜欢我吗
Apr 11 Python
一篇文章弄懂Python中所有数组数据类型
Jun 23 Python
python保存字典和读取字典的实例代码
Jul 07 Python
python 爬取学信网登录页面的例子
Aug 13 Python
Python面向对象之内置函数相关知识总结
Jun 24 Python
Python之Django自动实现html代码(下拉框,数据选择)
Mar 13 #Python
Tensorflow中的dropout的使用方法
Mar 13 #Python
python实现简单俄罗斯方块
Mar 13 #Python
Python实现检测文件的MD5值来查找重复文件案例
Mar 12 #Python
python 判断txt每行内容中是否包含子串并重新写入保存的实例
Mar 12 #Python
python 两个一样的字符串用==结果为false问题的解决
Mar 12 #Python
python不相等的两个字符串的 if 条件判断为True详解
Mar 12 #Python
You might like
谈谈PHP语法(4)
2006/10/09 PHP
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
2012/01/16 PHP
PHP调用Linux的命令行执行文件压缩命令
2013/01/27 PHP
Linux Apache PHP Oracle 安装配置(具体操作步骤)
2013/06/17 PHP
php实现paypal 授权登录
2015/05/28 PHP
基于PHP实现微信小程序客服消息功能
2019/08/12 PHP
图片按比例缩放函数
2006/06/26 Javascript
Javascript 原型和继承(Prototypes and Inheritance)
2009/04/01 Javascript
JS无限树状列表实现代码
2011/01/11 Javascript
js实现有过渡渐变效果的图片轮播相册(兼容IE,ff)
2016/01/19 Javascript
jQuery实现加入收藏夹功能(主流浏览器兼职)
2016/12/24 Javascript
基于jQuery实现数字滚动效果
2017/01/16 Javascript
JavaScript原型继承_动力节点Java学院整理
2017/06/30 Javascript
Vue动态获取width的方法
2018/08/22 Javascript
Vue this.$router.push(参数)实现页面跳转操作
2020/09/09 Javascript
Vue使用CDN引用项目组件,减少项目体积的步骤
2020/10/30 Javascript
Webpack3+React16代码分割的实现
2021/03/03 Javascript
在python中的socket模块使用代理实例
2014/05/29 Python
python批量提取word内信息
2015/08/09 Python
基于Python实现对PDF文件的OCR识别
2016/08/05 Python
Python探索之URL Dispatcher实例详解
2017/10/28 Python
速记Python布尔值
2017/11/09 Python
Numpy中的mask的使用
2018/07/21 Python
Python坐标线性插值应用实现
2019/11/13 Python
python之生成多层json结构的实现
2020/02/27 Python
CSS3弹性盒模型开发笔记(三)
2016/04/26 HTML / CSS
css3实例教程 一款纯css3实现的发光屏幕旋转特效
2014/12/07 HTML / CSS
css3 实现元素弧线运动的示例代码
2020/04/24 HTML / CSS
中药专业大学生医药工作求职信
2013/10/25 职场文书
捐资助学倡议书
2014/04/15 职场文书
我的中国梦演讲稿小学篇
2014/08/19 职场文书
JavaScript实现简单图片切换
2021/04/29 Javascript
Python打包exe时各种异常处理方案总结
2021/05/18 Python
虚拟机linux端mysql数据库无法远程访问的解决办法
2021/05/26 MySQL
php将xml转化对象的实例详解
2021/11/17 PHP
Java 多线程协作作业之信号同步
2022/05/11 Java/Android