1、乒乓球对战
https://makecode.microbit.org/_LYLgFbRLt2f3
https://makecode.microbit.org/_CW3L6f38cLv0
代码A
import random
import radio
from microbit import *
from music import play,POWER_UP,JUMP_DOWN,NYAN,FUNERAL
a_bat=2
b_bat=2
bat_map={0:4,1:3,2:2,3:1,4:0}
ball_x=2
ball_y=2
directions=[1,-1]
x_direction=random.choice(directions)
y_direction=random.choice(directions)
delay=500
counter=0
a_points=0
b_points=0
winning_score=5
game_over=False
def move_ball():
global ball_x,ball_y,x_direction,y_direction,counter,a_bat,b_bat,a_points,b_points,delay
display.set_pixel(ball_x,ball_y,0)
ball_x=ball_x+x_direction
ball_y=ball_y+y_direction
if ball_x<0:
ball_x=0
x_direction=1
if ball_x>4:
ball_x=4
x_direction=-1
if ball_y==0:
if ball_x==b_bat:
ball_y=0
y_direction=1
delay-=50
else:
play(POWER_UP,wait=False)
a_points+=1
ball_y=0
y_direction=1
radio.send('a'+str(a_points))
if ball_y==4:
if ball_x==a_bat:
ball_y=4
y_direction=-1
delay-=50
else:
play(JUMP_DOWN,wait=False)
b_points+=1
ball_y=4
y_direction=-1
radio.send('b'+str(b_points))
counter=0
radio.send('x'+str(ball_x))
radio.send('y'+str(ball_y))
radio.on()
while not game_over:
counter+=1
display.set_pixel(a_bat,4,6)
display.set_pixel(b_bat,0,6)
display.set_pixel(ball_x,ball_y,9)
if button_a.was_pressed():
display.set_pixel(a_bat,4,0)
a_bat=a_bat-1
if a_bat<0:
a_bat=0
radio.send('p'+str(a_bat))
if button_b.was_pressed():
display.set_pixel(a_bat,4,0)
a_bat=a_bat+1
if a_bat>4:
a_bat=4
radio.send('p'+str(a_bat))
incoming=radio.receive()
if incoming:
display.set_pixel(b_bat,0,0)
b_bat=bat_map[int(incoming)]
if counter==delay:
move_ball()
if a_points==winning_score or b_points==winning_score:
game_over=True
if a_points>b_points:
play(NYAN,wait=False)
display.scroll('A wins!')
else:
play(FUNERAL, wait=False)
display.scroll('B wins!')
display.scroll('press reset to play again ')
pingpongB
import radio
from microbit import *
from music import play,POWER_UP,JUMP_DOWN,NYAN,FUNERAL
a_bat=2
b_bat=2
bat_map={0:4,1:3,2:2,3:1,4:0}
ball_x=2
ball_y=2
a_points=0
b_points=0
winning_score=5
game_over=False
radio.on()
def parse_message():
global a_bat,incoming,bat_map,ball_x,ball_y,a_points,b_points
msg_type=incoming[:1]
msg=incoming[1:]
if msg_type=='p':
display.set_pixel(a_bat,0,0)
their_bat=int(msg)
a_bat=bat_map[their_bat]
if msg_type=='x':
display.set_pixel(ball_x,ball_y,0)
ball_x=bat_map[int(msg)]
if msg_type=='y':
display.set_pixel(ball_x,ball_y,0)
ball_y=bat_map[int(msg)]
if msg_type=='a':
a_points=int(msg)
play(JUMP_DOWN,wait=False)
if msg_type=='b':
b_points=int(msg)
play(POWER_UP,wait=False)
while not game_over:
display.set_pixel(b_bat,4,6)
display.set_pixel(a_bat,0,6)
display.set_pixel(ball_x,ball_y,9)
if button_a.was_pressed():
display.set_pixel(b_bat,4,0)
b_bat=b_bat-1
if b_bat<0:
b_bat=0
radio.send(str(b_bat))
if button_b.was_pressed():
display.set_pixel(b_bat,4,0)
b_bat=b_bat+1
if b_bat>4:
b_bat=4
radio.send(str(b_bat))
incoming=radio.receive()
if incoming:
parse_message()
if a_points==winning_score or b_points==winning_score:
game_over=True
if a_points<b_points:
play(NYAN,wait=False)
display.scroll('B wins!')
else:
play(FUNERAL,wait=False)
display.scroll('A wins!')
2、番茄钟
https://makecode.microbit.org/20385-83184-46996-14395
代码
from microbit import *
index4=0
blinkToggle=True
index=0
statusRunning=1
statusRest=2
status=statusIdle
pomodoroDecondes=0
plotMinutes=0
dailyCounter=1
#亮度设为150
while True:
#清屏命令
if status==statusIdle:
display.show(Image('00000:00100:01010:11111:00000'))
else:
if pomodoroDecondes==0:
if blinkToggle:
if status==statusRunning
display.show(Image.YES)
else:
display.show(Image.SQUARE)
blinkToggle=~blinkToggle
#延时1s
else:
plotMinutes=pomodoroDecondes//60
if plotMinutes==0:
if blinkToggle
if status==statusRest:
display.set_pixel(4,4,9)
else:
display.set_pixel(0,0,9)
blinkToggle=~blinkToggle
pomodoroDecondes-=2
pomodoroDecondes=max(pomodoroDeconds,0)
#延时2s
blinkToggle
3、迷宫
代码
from microbit import *
from music import NYAN,play
migong=[-1,0,0,0,1,
0,0,0,1,0,
0,1,1,0,0,
0,0,1,0,0,
1,0,0,0,0]
def draw_migong(migong):
for y in range(0,5):
for x in range(0,5):
if migong[x+y*5]>=0:
display.set_pixel(x,y,9*migong[x+y*5])
def direction_set(migong,position):
dir_set=[]
x,y=position
if x<=3 and migong[x+1+y*5]==0:
dir_set=[(x+1,y)]
if y<=3 and migong[x+(y+1)*5]==0:
dir_set+=[(x,y+1)]
if x>=1 and migong[x-1+y*5]==0:
dir_set+=[(x-1,y)]
return dir_set
def draw_path(path):
for i in path:
display.set_pixel(i[0],i[1],6)
path=[]
possible_set=[]
position_now=(0,0)
draw_migong(migong)
times=0
while True:
if position_now==(4,4):
display.scroll('Binggo!')
play(NYAN,wait=False)
draw_migong(migong)
for j in range(0,3):
for i in path:
display.set_pixel(i[0],i[1],0)
sleep(100)
display.set_pixel(i[0],i[1],5)
break
else:
possible_set=direction_set(migong,position_now)
if possible_set!=[]:
path.append(position_now)
position_now=possible_set[0]
migong[position_now[0]+position_now[1]*5]=-1
display.set_pixel(position_now[0],position_now[1],6)
else:
display.set_pixel(position_now[0],position_now[1],0)
migong[position_now[0]+position_now[1]*5]=-2
position_now = path.pop()
display.set_pixel(position_now[0],position_now[1],6)
sleep(1000)
draw_path(path)
times+=1
4、cutebot调试
1.超声波传感器误报;
2.直行左右轮调试
5、巡线小车(直角等过弯的情况)
(直角过不去)