我们正在创建一个俄罗斯方块游戏,并在BBC micro:bit上运行。

游戏中落下的俄罗斯方块将基于以下四种形状:

游戏规则如下:按钮A:将当前方块移动到左侧

按钮B:将当前方块移动到右侧

按钮A和B同时:顺时针旋转当前方块

视频演示

游戏将使用mico:bit的LED屏幕,其由5*5的25个LED组成。

每个LED可以打开(值:9表示最大亮度)或关闭(值:0)

并显示边界部分(未落下的方块)

Python代码将使用二维数组(Python中的列表)来存储主屏幕显示(7×5)和当前方块(2×2)

下载此代码,您可以使用micro:bit网站上的Python编辑器。

Python代码

from microbit import *

from random import choice

#Set up the tetris grid

grid=[[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,1,1,1,1,1,1]]

#Store a list of 4 bricks, each brick is a 2x2 grid

bricks = [[9,9],[9,0]],[[9,9],[0,9]],[[9,9],[9,9]],[[9,9],[0,0]]

#select a brick randomly and position it at the center/top of the grid (y=0,x=3)

brick = choice(bricks)

x=3

y=0

frameCount=0

#A function to return the maximum of two values

def max(a,b):

if a>=b:

return a

else:

return b

#A function to hide the 2x2 brick on the LED screen

def hideBrick():

if x>0:

display.set_pixel(x-1,y,grid[y][x])

if x0 and y<4:

display.set_pixel(x-1,y+1,grid[y+1][x])

if x<5 and y0:

display.set_pixel(x-1,y,max(brick[0][0],grid[y][x]))

if x0 and y<4:

display.set_pixel(x-1,y+1,max(brick[1][0],grid[y+1][x]))

if x0) or (grid[y+1][x]>0 and pixel10>0) or (grid[y][x+1]>0 and pixel01>0) or (grid[y+1][x+1]>0 and pixel11>0)):

hideBrick()

brick[0][0] = pixel10

brick[1][0] = pixel11

brick[1][1] = pixel01

brick[0][1] = pixel00

showBrick()

#A function to move/translate the brick

def moveBrick(delta_x,delta_y):

global x,y

move=False

#Check if the move if possible: no collision with other blocks or borders of the grid

if delta_x==-1 and x>0:

if not ((grid[y][x-1]>0 and brick[0][0]>0) or (grid[y][x+1-1]>0 and brick[0][1]>0) or (grid[y+1][x-1]>0 and brick[1][0]>0) or (grid[y+1][x+1-1]>0 and brick[1][1]>0)):

move=True

elif delta_x==1 and x0 and brick[0][0]>0) or (grid[y][x+1+1]>0 and brick[0][1]>0) or (grid[y+1][x+1]>0 and brick[1][0]>0) or (grid[y+1][x+1+1]>0 and brick[1][1]>0)):

move=True

elif delta_y==1 and y0 and brick[0][0]>0) or (grid[y+1][x+1]>0 and brick[0][1]>0) or (grid[y+1+1][x]>0 and brick[1][0]>0) or (grid[y+1+1][x+1]>0 and brick[1][1]>0)):

move=True

#If the move is possible, update x,y coordinates of the brick

if move:

hideBrick()

x+=delta_x

y+=delta_y

showBrick()

#Return True or False to confirm if the move took place

return move

#A function to check for and remove completed lines

def checkLines():

global score

removeLine=False

#check each line one at a time

for i in range(0, 5):

#If 5 blocks are filled in (9) then a line is complete (9*5=45)

if (grid[i][1]+grid[i][2]+grid[i][3]+grid[i][4]+grid[i][5])==45:

removeLine = True

#Increment the score (10 pts per line)

score+=10

#Remove the line and make all lines above fall by 1:

for j in range(i,0,-1):

grid[j] = grid[j-1]

grid[0]=[1,0,0,0,0,0,1]

if removeLine:

#Refresh the LED screen

for i in range(0, 5):

for j in range(0, 5):

display.set_pixel(i,j,grid[j][i+1])

return removeLine

gameOn=True

score=0

showBrick()

#Main Program Loop - iterates every 50ms

while gameOn:

sleep(50)

frameCount+=1

#Capture user inputs

if button_a.is_pressed() and button_b.is_pressed():

rotateBrick()

elif button_a.is_pressed():

moveBrick(-1,0)

elif button_b.is_pressed():

moveBrick(1,0)

#Every 15 frames try to move the brick down

if frameCount==15 and moveBrick(0,1) == False:

frameCount=0

#The move was not possible, the brick stays in position

grid[y][x]=max(brick[0][0],grid[y][x])

grid[y][x+1]=max(brick[0][1],grid[y][x+1])

grid[y+1][x]=max(brick[1][0],grid[y+1][x])

grid[y+1][x+1]=max(brick[1][1],grid[y+1][x+1])

if checkLines()==False and y==0:

#The brick has reached the top of the grid - Game Over

gameOn=False

else:

#select a new brick randomly

x=3

y=0

brick = choice(bricks)

showBrick()

if frameCount==15:

frameCount=0

#End of Game

sleep(2000)

display.scroll("Game Over: Score: " + str(score))

注意:测试此代码时,您可能希望删除一些#注释,尤其是当您的micro:bit返回“内存已满”错误时。

micro bit python_python教程之micro:bit俄罗斯方块游戏相关推荐

  1. BeagleBone Black教程之BeagleBone Black设备的连接

    BeagleBone Black教程之BeagleBone Black设备的连接 BeagleBone Black开发前需要准备的材料 经过上面的介绍,相信你已经对BeagleBone有了大致的了解, ...

  2. ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge

    ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge Arduino环境与Linux环境的桥梁--Bridge 在第一章中介绍Arduino Yun硬件的时候提到过,它上面有 ...

  3. nmap教程之nmap命令使用示例(nmap使用方法)

    nmap教程之nmap命令使用示例(nmap使用方法) Nmap是一款网络扫描和主机检测的非常有用的工具.Nmap是不局限于仅仅收集信息和枚举,同时可以用来作为一个漏洞探测器或安全扫描器.它可以适用于 ...

  4. Python培训教程之Python基础知识点梳理

    Python语言是入门IT行业比较快速且简单的一门编程语言,学习Python语言不仅有着非常大的发展空间,还可以有一个非常好的工作,下面小编就来给大家分享一篇Python培训教程之Python基础知识 ...

  5. Wireshark数据抓包教程之Wireshark的基础知识

    Wireshark数据抓包教程之Wireshark的基础知识 Wireshark的基础知识 在这个网络信息时代里,计算机安全始终是一个让人揪心的问题,网络安全则有过之而无不及.Wireshark作为国 ...

  6. 转:Tkinter教程之Text(2)篇

    '''Tkinter教程之Text(2)篇''' '''6.使用tag来指定文本的属性''' #创建一个指定背景颜色的TAG # -*- coding: cp936 -*- from Tkinter  ...

  7. thymeleaf加载不了js引用_web前端教程之js中的模块化一

    web前端教程之js中的模块化一:我们知道最常见的模块化方案有CommonJS.AMD.CMD.ES6,AMD规范一般用于浏览器,异步的,因为模块加载是异步的,js解释是同步的,所以有时候导致依赖还没 ...

  8. iBATIS教程之like语句的写法浅析

    iBATIS教程之like语句的使用我们可以先看看网上搜了一下iBATIS的关于like的使用 select * from USERS where USER_NAME like '%wang%'; 这 ...

  9. pgsql数据库默认配置事务类型_PostgreSQL基础教程之:初始化配置

    PostgreSQL基础教程之:初始化配置 时间:2020-04-27 来源: PostgreSQL基础教程之:初始化配置 一.配置pg_hba.conf 先说明客户端认证配置文件pg_hba.con ...

最新文章

  1. RH系列linux上编译android2.3(gingerbread)
  2. 占据栅格地图(Occupancy Grid Map)
  3. php和python对比-从PHP与Python的语言比较去了解什么是图灵完备
  4. Monte Carlo仿真方法的基本思想及其特点
  5. php oracle 无查询结果,php - Oracle Insert查询不起作用,也不会抛出任何错误 - 堆栈内存溢出...
  6. 计科1高雨妍作业(2)
  7. flume1.7 TailDirSource断点续传与文件更名后数据重复采集的bug修复
  8. Github上如何找到自己想要的开源项目(小技巧:精确搜索)
  9. C#:DataTable查询结果判断某一列为空
  10. python3 爬虫
  11. oracle创建默认序列号,PLS-00103:为序列号oracle创建触发器(PLS-00103: Create trigger for sequence number oracle)...
  12. TeeChart柱状图
  13. IBATIS开发指南(夏昕)
  14. android 修改系统默认时间24小时制
  15. Android展开的TextView和点击底部滚动到顶部
  16. python关于 unittest的常见用法:前置条件与后置条件
  17. 身份证识别sdk在生活中的广泛应用
  18. Servlet设置欢迎页面!
  19. 【docker】gitlab + qqmail配置SMTP
  20. 解决白盒、mock、性能、自动化测试脚本中的数据自动生成问题——Spock-Genesis

热门文章

  1. Petalinux-conifg 错误失败
  2. gst-inspect-1.0汇总自用
  3. 仿菁优网首页动画效果
  4. 记一次vue^2.6.5-router^3.0.6的keep-alive事故
  5. 社区版emqx安装后修改登入到dashboard密码 http://ip:18083/
  6. 每个工程师都应该知道的事——射频发射机功率怎么测量
  7. layui在搜索的时候没有数据,在表格中显示暂无数据
  8. 经典大学课程:石油大学教学视频
  9. git解决The authenticity of host ‘github.com (192.30.255.112)‘ can‘t be established问题
  10. 近半数受访企业年度调薪比例在5%以下,约40%企业年度调薪率与上年度相比保持不变 | 美通社头条...