#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俄罗斯方块相关推荐

  1. python协程详解_python协程详解

    原博文 2019-10-25 10:07 − # python协程详解 ![python协程详解](https://pic2.zhimg.com/50/v2-9f3e2152b616e89fbad86 ...

  2. python俄罗斯方块算法详解_用 Python 写一个俄罗斯方块游戏 (

    @@ -2,34 +2,34 @@ > * 原文作者:[Dr Pommes](https://medium.com/@pommes) > * 译文出自:[掘金翻译计划](https://g ...

  3. 数据结构与算法--二进制详解 Python二进制算法详解 史上最详细的二进制讲解 彻底搞懂原码、反码、补码 Python的负数二进制表示形式

    阅读目录 原码.反码.补码 机器数 和 真值 原码.反码.补码的基础 Python中负数的处理 负数的补码如何转成十进制 位运算符 和 移位运算符 基本概述 妙用 二进制涉及的算法 原码.反码.补码 ...

  4. python int函数详解_Python内置函数OCT详解

    英文文档:oct ( x ) Convert an integer number to an octal string. The result is a valid Python expression ...

  5. python average函数详解_python 函数详解

    函数函数是代码的一种组织形式 函数应该能完成一项特定的工作,而且一般一个函数只完成一项工作 有些语言,分函数和过程两个概念,通俗解释是,有返回结果的是函数,无返回结果的叫过程,python不加以区分 ...

  6. python时间函数详解_Python:Numpy库基础分析——详解datetime类型的处理

    原标题:Python:Numpy库基础分析--详解datetime类型的处理 Python:Numpy库基础分析--详解datetime类型的处理 关于时间的处理,Python中自带的处理时间的模块就 ...

  7. python中configparser详解_Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  8. python 元类 详解_Python 元类详解 __new__、__init__、__call__、__metacalss__

    了解元类之前,先了解几个魔术方法: __new__.__init__.__call__ __new__: 对象的创建,是一个静态方法,第一个参数是cls.(想想也是,不可能是self,对象还没创建,哪 ...

  9. python requests库详解_python爬虫之路(一)-----requests库详解

    requests库 requests库是python实现的最简单易用的http库. requests库的功能详解. 我们可以自然而然地想到这些方法其实就是http协议对资源的操作. 调用request ...

  10. python average函数详解_python基础之函数详解

    Python基础之函数详解 一.函数的定义 到现在为止,我们已经掌握了Python的基本语法和数据类型等相关基础知识了,以进行一个项目的编写了,这个时候,就会发现,很多代码需要我们进行复制粘贴,这简直 ...

最新文章

  1. AI如何赋能金融行业?百度、图灵深视等同台分享技术实践
  2. Xamarin XAML语言教程构建ControlTemplate控件模板 (三)
  3. Node.js和NoSQL开发比特币加密货币应用程序(下)
  4. 撒花!吴恩达《Machine Learning Yearning》完结!
  5. redis 源码 zmalloc.c 实现
  6. iOS CGRectGetMaxX/Y 使用
  7. autocomplete自动补全,远程json数据
  8. 泰勒展开与找第一项系数不为1的解题策略
  9. 7. 锁定框架(The Locking Framework)
  10. MacOS新版本,不能装老版本Xcode
  11. Java课程设计-旅游管理系统
  12. .Net C# 微信APP支付的开发步骤
  13. !!obj与JavaScript中!!的作用
  14. C语言输出菱形不是梦
  15. 浪漫的七夕表白简单代码(简单的小代码)
  16. A - Round decimals
  17. 计算机专业英语容易挂科吗,大学英语专业容易挂科吗
  18. win10单机修复计算机在哪,win10如何进入高级修复选项
  19. Unity --- 角色动画的使用以及按键控制角色运动
  20. c++在文件中提取数字或字母

热门文章

  1. 突发!ITELLYOU要改版了!
  2. 校园歌手大赛10分c语言,校园歌手大赛评分细则
  3. Bzoj1972: [Sdoi2010]猪国杀 题解(大模拟+耐心+细心)
  4. padding属性的用法和作用
  5. 个人邮箱怎么注册?手机怎么注册个人邮箱呢?
  6. 三维重建——D2HC-RMVSNet网络详解
  7. EditPlus实现json格式化
  8. IDEA进行对json数据格式化
  9. MSM7225 600MHZ CPU和高通MSM 7201A 528MHz CPU的比较
  10. 调用微信接口实现微信授权登陆主体内容【code换取openid以及session_key】