python编的俄罗斯方块游戏_python编写俄罗斯方块
本文实例为大家分享了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("",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编写俄罗斯方块相关推荐
- python编的俄罗斯方块游戏_Python使用pygame模块编写俄罗斯方块游戏的代码实例
文章先介绍了关于俄罗斯方块游戏的几个术语. 边框--由10*20个空格组成,方块就落在这里面. 盒子--组成方块的其中小方块,是组成方块的基本单元. 方块--从边框顶掉下的东西,游戏者可以翻转和改变位 ...
- python编辑俄罗斯方块_python编写俄罗斯方块
本文实例为大家分享了python实现俄罗斯方块的具体代码,供大家参考,具体内容如下 #coding=utf-8 from tkinter import * from random import * i ...
- 弹球游戏python代码含记分模式_python编写弹球游戏的实现代码
用Blender制作一个兵乓球小游戏,要编写Python代码需要安装pygame 包没有面对困难的勇气,也就没有享受快乐的权利. python 怎么写双人弹球游戏最难过的事不是别人又得罪你了,而是你自 ...
- python做飞机大战游戏_python实现飞机大战游戏
飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...
- python连续猜数游戏_python实现猜数游戏
本文实例为大家分享了python实现猜数游戏的具体代码,供大家参考,具体内容如下 一.问题描述: 使用python开发一个猜数小游戏,程序随机产生0~1024之间的数字,用户输入猜测数字,程序告诉用户 ...
- python 玩公众号游戏_Python入门太难?不如从玩塔防小游戏开始,玩通关就能学会编程...
我一直认为,在python入门阶段学习基础理论,太枯燥.所以我们整理了很多有关python的项目案例,有详细教程还有源码,希望能帮助更多对python感兴趣的人. 这是其中一个适合入门的Python项 ...
- python写飞机大战游戏_python实现飞机大战游戏
飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...
- python实现猜数字游戏_python如何实现猜数字游戏
python实现猜数字游戏的方法:使用条件语句实现判断,代码为[print('猜一个1-20之间的整数.');print('开始猜:');for i in range(1, 7):try:guess ...
- python实现2048小游戏_python—手把手教你实现2048小游戏
相信2048这个游戏对大家来说一定不陌生,下面这篇文章就主要给大家介绍了怎么用Python实现2048小游戏,文中通过注释与示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的 ...
最新文章
- [VBScript] 自动删除2小时以前生成的文件
- WinAPI【消息及相关结构体】
- nginx添加第三方模块,以及启用nginx本身支持的模块
- 一加手机安装鸿蒙系统,【新机】华为MatePad Pro 2官宣,刘作虎点赞鸿蒙手机
- 致敬创新者 | 看中国小企业掌握哪些核心技术?
- CCF201609-5 祭坛【线段树】(100分解题链接)
- 30分钟快速上手mybatis框架,内容简单易懂,绝无废话,追求速度的来
- Word中的Visio图直接转换为图片
- 【干货分享】花坊类字体设计思路
- [Proteus8]使用proteus8对单片机进行模拟仿真,记录方波图出现的过程
- python PNG图片显示
- grid 与axis
- U盘写保护的解决办法,亲自实践,原创!
- 中国睫毛生长液行业市场供需与战略研究报告
- 报错 Error from server (InternalError): an error on the server (““) has prevented the request from suc
- (转!)利用Keras实现图像分类与颜色分类
- html中spry的长度怎么更改,Dreamweaver中Spry区域功能说明
- SQL 增加或删除一列
- hdu 1535 Invitation Cards
- ICMP类型报文分类。
热门文章
- linux学习笔记-chkconfig
- hbuilderx的快捷键整理pdf_mac键盘快捷键详解,苹果电脑键盘快捷键图文教程
- mapreduce yarn内存参数
- Spring IOC容器-注解的方式
- 存储过程的参数可以使用sql的函数
- es统计有多少个分组_ES 24 - 如何通过Elasticsearch进行聚合检索 (分组统计)
- 安卓adapter适配器作用_自带安卓系统的便携屏,能玩出什么花样?
- md5不是对称密码算法_密码学中的消息摘要算法5(MD5)
- mysql语句数据库_数据库的Mysql语句
- dbms数据库管理系统_数据库管理系统(DBMS)中的视图