python microbit typeerror,在MicroPython中使用microbit模块时出现索引错误

这篇博客介绍了作者作为一个业余爱好者,如何使用Python和microbit库开发一个基于整数控制的游戏。作者放弃了之前的代码,转而采用更简单的整数处理方式,以避免图像操作的复杂性。游戏包括玩家移动和随机敌人/墙的生成。通过按钮控制玩家的左右移动,并计划添加一个简单的计算器功能。
摘要由CSDN通过智能技术生成

我是个业余爱好者,所以我还在学习。我放弃了我的旧代码,因为我很早就被卡住了。我想出了一个更“基于整数”的脚本,而不是使用预先设置的图像。我认为使用简单的整数可以更容易地使用控制流,操纵整数(因此也可以是图像),而不必更改和测试图像。我也在考虑用这两个按钮创建一个计算器,这样建议就会派上用场。谢谢你帮了我的忙!

下面是更新后的代码,以防您怀疑。我正在尝试添加随机敌人/墙生成:from microbit import *

import random

game_over == False

player_x = 2

player_y = 4

#starting coords for 'player' pixel

light = 5

wall_light = 7

#brightness. self explanatory

wall_pos_x = [0, 1, 2, 3, 4]

wall_y = 0

#all possible coords of wall. y coords

#are changed in function

#generates enemy wall with a randomly generated hole

def enemy_wall():

open_x = random.randint(0,4)

open_y = 0

for wall_xs in range(open_x-1, open_x, -1)

wall_pos_x[wall_xs]

pass

#for loops will iterate over all possible x coords except

#the open hole's coords. for loop will use iteration and

#display all possible x coords except open_x.

def player(x, y, bright):

if x <= -1:

x = 0

elif x >= 5:

x = 4

display.set_pixel(x,y,bright)

return x

#if x coord is +- 1 more than available coords,

#doesnt move/change position at edge

#updated newer player control. push of button changes x coord by -+ 1

#cannot change y coord

while game_over != True:

player(player_x,player_y,light)

sleep(750)

#player coords re/displayed, and button cooldown 750ms

if button_a.is_pressed():

player_x = player(player_x-1,player_y,light)

display.clear()

#runs through player(), then clears display of

#previous pixel *player*.

elif button_b.is_pressed():

player_x = player(player_x+1,player_y,light)

display.clear()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值