python实现俄罗斯方块游戏


Posted in Python onMarch 25, 2020

在公司实习。公司推崇Python和Django框架,所以也得跟着学点。

简单瞅了下Tkinter,和Canvas配合在一起,还算是简洁的界面开发API。threading.Thread创建新的线程,其多线程机制也算是方便。

只是canvas.create_rectangle居然不是绘制矩形,而是新建了矩形控件这点让人大跌眼镜。先开始,在线程里每次都重绘多个矩形(随数组变化),其实是每次都新建了N个矩形,结果内存暴增。原来,对矩形进行变更时,只需用canvas.itemconfig即可。

下面就是截图(时间太晚,明日还得上班,做得非常粗糙...没事时再慢慢修正)。

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; 
 
 #绘图类 
 canvas = None; 
 
 #标题 
 title = "BrickGame"; 
 #宽和高 
 width = 350; 
 height = 670; 
 
 #行和列 
 rows = 20; 
 cols = 10; 
 
 #几种方块 
 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; 
 #当前方块形状 
 shape = -1; 
 #当前方块的行和列(最左上角) 
 curRow = -10; 
 curCol = -10; 
 
 #背景 
 back = list(); 
 #格子 
 gridBack = 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")); 
 
 #绘制游戏的格子 
 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"); 
 
 
 #绘制当前正在运动的方块 
 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.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.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 __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.canvas = Canvas(self.frame1,width=300,height=600,bg="black"); 
 
 self.init(); 
 
 #获得当前的方块 
 self.getCurBrick(); 
 
 #按照数组,绘制格子 
 self.drawRect(); 
 
 self.canvas.pack(); 
 
 #监听键盘事件 
 self.window.bind("<KeyPress>",self.onKeyboardEvent); 
 
 #启动方块下落线程 
 downThread = threading.Thread(target=self.brickDown,args=()); 
 downThread.start(); 
 
 self.window.mainloop(); 
 
 self.start=False; 
 
 pass; 
 
if __name__=='__main__': 
 
 brickGame = BrickGame();

估计用图形界面会很少,因为本人是WEB开发。不过,怎样也抑制不住这颗喜欢写游戏的心啊!

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

更多关于python游戏的精彩文章请点击查看以下专题:

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

Python 相关文章推荐
Python的函数嵌套的使用方法
Jan 24 Python
python里将list中元素依次向前移动一位
Sep 12 Python
Python实现去除代码前行号的方法
Mar 10 Python
Django objects的查询结果转化为json的三种方式的方法
Nov 07 Python
django admin组件使用方法详解
Jul 19 Python
对Python获取屏幕截图的4种方法详解
Aug 27 Python
基于python解线性矩阵方程(numpy中的matrix类)
Oct 21 Python
python装饰器相当于函数的调用方式
Dec 27 Python
Python利用Scrapy框架爬取豆瓣电影示例
Jan 17 Python
pytorch 修改预训练model实例
Jan 18 Python
通过代码简单了解django model序列化作用
Nov 12 Python
Autopep8的使用(python自动编排工具)
Mar 02 Python
python中的变量如何开辟内存
Jun 26 #Python
pyQt4实现俄罗斯方块游戏
Jun 26 #Python
朴素贝叶斯分类算法原理与Python实现与使用方法案例
Jun 26 #Python
python实现俄罗斯方块
Jun 26 #Python
解决python报错MemoryError的问题
Jun 26 #Python
pygame实现俄罗斯方块游戏
Jun 26 #Python
python和pygame实现简单俄罗斯方块游戏
Feb 19 #Python
You might like
PHP 编程安全性小结
2010/01/08 PHP
PHP学习笔记之二
2011/01/17 PHP
PHP中的session永不过期的解决思路及实现方法分享
2011/04/20 PHP
php getcwd与dirname(__FILE__)区别详解
2016/09/24 PHP
详解PHP归并排序的实现
2016/10/18 PHP
javascript实现的距离现在多长时间后的一个格式化的日期
2009/10/29 Javascript
Array, Array Constructor, for in loop, typeof, instanceOf
2011/09/13 Javascript
jQuery中jqGrid分页实现代码
2011/11/04 Javascript
图片在浏览器中底部对齐 解决方法之一
2011/11/30 Javascript
关闭页面window.location事件未执行的原因及解决方法
2014/09/01 Javascript
JavaScript将XML转成JSON的方法
2015/03/12 Javascript
JS实现三级折叠菜单特效,其它级可自动收缩
2015/08/06 Javascript
全国省市二级联动下拉菜单 js版
2016/05/10 Javascript
JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)
2017/01/06 Javascript
weui框架实现上传、预览和删除图片功能代码
2017/08/24 Javascript
angular4自定义组件详解
2017/09/28 Javascript
Javasript设计模式之链式调用详解
2018/04/26 Javascript
NodeJS加密解密及node-rsa加密解密用法详解
2018/10/12 NodeJs
NodeJs 实现简单WebSocket即时通讯的示例代码
2019/08/05 NodeJs
JS实现电商商品展示放大镜特效
2020/01/07 Javascript
磁盘垃圾文件清理器python代码实现
2020/08/24 Python
使用python编写udp协议的ping程序方法
2018/04/22 Python
使用Python实现将多表分批次从数据库导出到Excel
2020/05/15 Python
python实现数学模型(插值、拟合和微分方程)
2020/11/13 Python
python爬虫快速响应服务器的做法
2020/11/24 Python
Python使用Turtle模块绘制国旗的方法示例
2021/02/28 Python
国际知名设计师时装商店:Coggles
2016/09/05 全球购物
Bluebella法国官网:英国性感内衣品牌
2019/05/03 全球购物
党校个人自我鉴定范文
2014/03/28 职场文书
工程安全生产协议书
2014/11/21 职场文书
复兴之路纪录片观后感
2015/06/02 职场文书
读《方与圆》有感:交友方圆有度
2020/01/14 职场文书
详解nginx.conf 中 root 目录设置问题
2021/04/01 Servers
Matlab求解数组中的最大值及它所在的具体位置
2021/04/16 Python
Python基础之Socket通信原理
2021/04/22 Python
pycharm安装深度学习pytorch的d2l包失败问题解决
2022/03/25 Python