python编辑俄罗斯方块_python俄罗斯方块
#coding=utf-8
from tkinter import *
from random import *
importthreadingfrom tkinter.messagebox importshowinfofrom tkinter.messagebox importaskquestionimportthreadingfrom time importsleepclassBrickGame(object):#是否开始
start =True#是否到达底部
isDown =True
isPause=False#窗体
window =None#frame
frame1 =None
frame2=None#按钮
btnStart =None#绘图类
canvas =None
canvas1=None#标题
title = "IT Xiao Ang Zai"
#宽和高
width = 450height= 670
#行和列
rows = 20cols= 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 = -10curCol= -10
#背景
back =list()#格子
gridBack =list()
preBack=list()#初始化
definit(self):for i inrange(0, self.rows):
self.back.insert(i, list())
self.gridBack.insert(i, list())for i inrange(0, self.rows):for j inrange(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"))#绘制游戏的格子
defdrawRect(self):for i inrange(0, self.rows):for j inrange(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 inrange(0, len(self.arr1)):for j inrange(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 inrange(0, len(self.arr)):for j inrange(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")#判断方块是否已经运动到达底部
ifself.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()#判断是否有整行需要消除
defremoveRow(self):
count=0for i inrange(0, self.rows):
tag1=Truefor j inrange(0, self.cols):if self.back[i][j] ==0:
tag1=Falsebreak
if tag1 ==True:#从上向下挪动
count = count+1
for m in range(i-1, 0, -1):for n inrange(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))#获得当前的方块
defgetCurBrick(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#监听键盘输入
defonKeyboardEvent(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 -= 1direction= 1
elif event.keycode == 38:#变化方块的形状
self.shape += 1direction= 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()returnTrue#判断当前方块是否到达边界
defisEdge(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=Falsebreak
#向右,判断边界
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=Falsebreak
#向下,判断底部
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=Truebreak
#进行变形,判断边界
elif direction == 2:if self.curCol <0:
self.curCol=0if self.curCol+2 >=self.cols:
self.curCol= self.cols-3
if self.curRow+2 >=self.rows:
self.curRow= self.curRow-3
returntag#方块向下移动
defbrickDown(self):whileTrue: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)#点击开始
defclickStart(self):
self.start=Truefor i inrange(0, self.rows):for j inrange(0, self.cols):
self.back[i][j]=0
self.canvas.itemconfig(
self.gridBack[i][j], fill="black", outline="white")for i inrange(0, len(self.arr)):for j inrange(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()defclickPause(self):
self.isPause= notself.isPauseprint(self.isPause)if notself.isPause:
self.btnPause["text"] = "暂停"
else:
self.btnPause["text"] = "恢复"
defclickReStart(self):
ackRestart= askquestion("重新开始", "你确定要重新开始吗?")if ackRestart == 'yes':
self.clickStart()else:return
defclickQuit(self):
ackQuit= askquestion("退出", "你确定要退出吗?")if ackQuit == 'yes':
self.window.destroy()
exit()#判断是否死了
defisDead(self):for j inrange(0, len(self.back[0])):if self.back[0][j] !=0:
showinfo("提示", "你挂了,再来一盘吧!")
self.start=Falsebreak
#运行
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()
python编辑俄罗斯方块_python俄罗斯方块相关推荐
- python编辑配置_Python环境安装及编辑器配置(一)
在2018年决定写一些关于Python的文章,其实写博客这件事在2016年就有了,但是因为个人的一些原因一直被耽搁.所以2018年的目标之一就是写多一点的博客,不管是是生活还是工作.好吧,废话不多说, ...
- python 编辑视频_python也能玩视频剪辑!moviepy操作记录总结
moviepy库安装 今天咱们需要使用的第三方是moviepy,moviepy是用于视频编辑的Python模块,可用于基本操作(例如剪切,串联,标题插入),视频合成(也称为非线性编辑),视频处理或创建 ...
- python编的俄罗斯方块游戏_手把手制作Python小游戏:俄罗斯方块(一)
手把手制作Python小游戏:俄罗斯方块1 大家好,新手第一次写文章,请多多指教 A.准备工作: 这里我们运用的是Pygame库,因为Python没有内置,所以需要下载 如果没有pygame,可以到官 ...
- python图形小游戏代码_手把手制作Python小游戏:俄罗斯方块(一)
手把手制作Python小游戏:俄罗斯方块1 大家好,新手第一次写文章,请多多指教 A.准备工作: 这里我们运用的是Pygame库,因为Python没有内置,所以需要下载 如果没有pygame,可以到官 ...
- python 基于pygame实现俄罗斯方块
一.简单说明 80.90后的小伙伴都玩过"俄罗斯方块",那种"叱咤风云"场景 偶尔闪现在脑海 真的是太爽了:如果没有来得及玩过的同学,这次可以真正的自己做一个了 ...
- Python课程设计之俄罗斯方块
Python课程设计之俄罗斯方块 演示效果 下载地址 运行效果 基础页面 界面动起来 生成.移动.固定.变形 消除与得分 演示效果 点击查看 下载地址 点击下载 Python课程设计之俄罗斯方块 软件 ...
- python编辑用户登录界面_python编辑用户登入界面的实现代码
1.需求分析 登入界面需要达到以下要求: 系统要有登入和注册两个选项可供选择 系统要能够实现登入出错提示,比如账户密码错误等,用户信息保存在user_info.txt文件夹中 系统要能够进行登入错误次 ...
- Python学习教程(Python学习视频_Python学些路线):Day05 总结和练习
Python学习教程(Python学习视频_Python学些路线):总结和练习 练习清单 寻找"水仙花数". 寻找"完美数". "百钱百鸡" ...
- python关闭csv文件_使用Python编辑csv文件时跳过标题
使用Python编辑csv文件时跳过标题 我使用下面引用的代码使用Python编辑csv.代码中调用的函数形式代码的上半部分. 问题:我希望下面引用的代码从第2行开始编辑csv,我希望它排除包含标题的 ...
最新文章
- 页面的前进/后退/刷新方法
- -shape 填充 圆角矩形 圆形 环形
- 坑 之 使用numpy的tofile和fromfile读取数据
- (二)SpringBoot 整合 JPA
- 解析xml_Mybatis中mapper的xml解析详解
- Python中的闭包总结
- 【题解】Inspection UVa 1440 LA 4597 NEERC 2009
- sev2008服务器系统,在windows 2008 64位系统安装sql server 2000
- Matlab图像分割
- 计算机视觉及色彩空间RGB,HSV,HLS,Lab,LMS,XYZ,CMYK
- 【fpga里Verilog语言的小知识点】
- 在csdn中,如何获取积分
- 设备模型一(基本概念)
- Grad-CAM简介
- MQTT客户端远程控制WIFI设备(阿里云物联网平台)
- 鼠标经过下划线 css3,详解css3和伪元素实现鼠标移入时下划线向两边展开
- 由先序遍历和中序遍历得到后序遍历和层次遍历(二叉树)
- Stopwatch常用方法,不积硅步无以至千里
- ECharts 示例——圆环图:数据标签详细设置
- platform平台驱动模型简述(linux驱动开发篇)