代码搬运修改自python编写俄罗斯方块 更新时间:2020年03月13日 09:39:17 作者:勤勉之

from tkinter import *
from random import *
import threading
from tkinter.messagebox 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 range(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()```

pygame写俄罗斯方块的更多相关文章

  1. 从零开始---控制台用c写俄罗斯方块游戏(1)

    从零开始---控制台用c写俄罗斯方块游戏(1) 很少写博文,一来自身知识有限,二来自己知道,已经有很多这样的博文了,三就是因为懒,文笔也一般,四来刚出来工作,时间也不多 之所以写这篇博文,是因为应群里 ...

  2. pygame写的弹力球

    这是pygame写的弹力球 运行效果: ======================================================== 代码部分: ================= ...

  3. 作业:用pygame实现俄罗斯方块

    用Pygame实现俄罗斯方块 参考资料后我安装了Pygame,参考了网上的代码实现了俄罗斯方块小游戏.我试着理解网上的代码的原理和含义,对这些代码的原理有了一个粗略地理解,代码通过参数,RGB值等来实 ...

  4. Javascript写俄罗斯方块游戏

    俄罗斯方块这个游戏也做了移动端的兼容, 这个游戏难点是怎么翻转方块, 自己实现的方式是把方块放到一个二维数组, 然后逆时针旋转二维数组. 也有别的方法,比如直接用一个全局变量代表一个方向, 翻转的时候 ...

  5. pygame写贪吃蛇

    python小白尝试写游戏.. 学了点pygame不知道那什么练手好,先拿贪吃蛇开刀吧. 一个游戏可以粗略的分为两个部分: 数据(变量) 处理数据(函数,方法) 设计变量 首先预想下,画面的那些部分需 ...

  6. 基于Pygame写的翻译方法

    发布时间:2018-11-01   技术:pygame+easygui   概述 实现一个翻译功能,中英文的互相转换.并可以播放翻译后的内容. 翻译接口调用的是百度翻译的api接口. 详细 代码下载: ...

  7. electron写俄罗斯方块游戏(Tetris)

    背景 在折腾ES6,突然想起大学时用c语言写过俄罗斯方块,本项目中主要是利用ES6的Class特性进行面向对象编程.项目采用node.js v6.2.0 + electron v1.1.0 进行桌面开 ...

  8. 用Python和Pygame写游戏-从入门到精通(py2exe篇)

    这次不是直接讲解下去,而是谈一下如何把我们写的游戏做成一个exe文件,这样一来,用户不需要安装python就可以玩了.扫清了游戏发布一大障碍啊! perl,python,java等编程语言,非常好用, ...

  9. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...

随机推荐

  1. 【然天一】随机读写(4k)百盘天梯

    随机读写适用于大量小文件的读写,是最贴近办公和编程的使用场景.现在很多硬盘厂商只宣传它们的连续读写(Seq),但除了游戏和视频剪辑场景之外并没有什么卵用. 总结一下: 傲腾秒杀全部 NAND SLC ...

  2. SP419/422 TRANSP(2) - Transposing is Fun

    首先可以发现转置本质上就是一个置换,问题就转化为求一个排列排成有序的最少次数. 这是一个经典问题,答案为点数减循环置换的个数,考虑如何求循环置换. 发现有两个特殊性质:置换为转置,矩阵的边长为 \(2 ...

  3. js判断变量是否为空字符串、null、undefined

    let _isEmpty = (input) => { return input + '' === 'null' || input + '' === 'undefined' || input.t ...

  4. 【struts2】中method={1}详解

    我们在使用struts2的时候,有时候为了简化struts2的配置项而采用通配符的方式,如下代码: <action name="ajaxregister!*" class=& ...

  5. 《Effective Python》笔记——第3章 类与继承

    一.尽量用辅助类来维护程序的状态 如下,用字典存储简单数据 class SimpleGradebook(): def __init__(self): self.__grades = {} def ad ...

  6. Vue3.X安装

    1.查看node.js和npm版本 $ node -v //建议v10以上版本 $ npm -v 2.若已安装了2.x的旧版本,需要先卸载 npm uninstall vue-cli -g 3.安装淘 ...

  7. webfunny前端系统:如何解决警报设置触发常见问题

    大家好,经常会有小伙伴在使用webfunny监控系统中,遇到无法触发警报的问题,其实都是一些配置上的疏漏,在这里给大家总结一下: PS:只要消息中心里有警报,则说明触发机制没有问题.其他方式没有触发, ...

  8. Linux命令安装Mysql

    关键步骤: 4.创建用户组和用户 groupadd mysql useradd -r -g mysql mysql 5.修改权限 chown -R mysql:mysql ./ 6.安装数据库 ./s ...

  9. Python基础—迭代器、生成器(Day13)

    一.迭代器 1.可迭代对象:遵循可迭代协议,内部含有__iter__方法的对象就叫做可迭代对象.(str.list.tulpe.dict.set) 查询数据类型的方法 s = 'laonanhai' ...

  10. 阿里云人脸1:N搜索开源版-Java版(文末附开源地址)

    ​ 一.人脸检测相关概念 人脸检测(Face Detection)是检测出图像中人脸所在位置的一项技术,是人脸智能分析应用的核心组成部分,也是最基础的部分.人脸检测方法现在多种多样,常用的技术或工具大 ...