Skip to content

Instantly share code, notes, and snippets.

@tombl
Created July 3, 2020 06:26
Show Gist options
  • Save tombl/c49de54cfd79e5ca5f7b3509129372df to your computer and use it in GitHub Desktop.
Save tombl/c49de54cfd79e5ca5f7b3509129372df to your computer and use it in GitHub Desktop.
import microbit as mb
import radio
PHASE_SELECT_PLAYER = 1
PHASE_PLAYING_GAME = 2
current_phase = PHASE_SELECT_PLAYER
this_player = 0
radio.config(channel=68, data_rate=radio.RATE_2MBIT)
radio.on()
remote_values = {}
position = 0
ball_x = 2
ball_y = 2
ball_vel_x = 1
ball_vel_y = 1
def update_remotes():
remote = radio.receive()
if remote == None:
return
key, value = eval(remote)
remote_values[key] = value
def send_remote(key, value):
radio.send(repr((key, value)))
loop_counter = 0
while True:
loop_counter += 1
loop_counter %= 25
update_remotes()
if current_phase == PHASE_SELECT_PLAYER:
if mb.button_a.was_pressed():
this_player = 0
mb.display.scroll("A")
print("I'm player A")
current_phase = PHASE_PLAYING_GAME
if mb.button_b.was_pressed():
this_player = 1
mb.display.scroll("B")
print("I'm player B")
current_phase = PHASE_PLAYING_GAME
elif current_phase == PHASE_PLAYING_GAME:
mb.display.clear()
if this_player == 0:
send_remote("ball_pos", (ball_x, ball_y))
other_position = (
remote_values["position"] if "position" in remote_values else 0
)
if ball_x <= 4:
mb.display.set_pixel(ball_y, ball_x, 9)
if loop_counter == 0:
print("x:", ball_x)
print("y:", ball_y)
print("xv:", ball_vel_x)
print("yv:", ball_vel_y)
ball_x += ball_vel_x
if ball_y < 1 and (ball_x == position or ball_x == (position + 1)):
if ball_x <= 1:
ball_x = 2 - ball_x
ball_vel_x = -ball_vel_x
else:
if ball_x <= 0:
ball_x = 0 - ball_x
ball_vel_x = -ball_vel_x
if ball_x > 8 and (
ball_x == other_position or ball_x == (other_position + 1)
):
if ball_x >= 8:
ball_x = 16 - ball_x
ball_vel_x = -ball_vel_x
else:
if ball_x >= 9:
ball_x = 18 - ball_x
ball_vel_x = -ball_vel_x
ball_y += ball_vel_y
if ball_y <= 0:
ball_y = 0 - ball_y
ball_vel_y = -ball_vel_y
if ball_y >= 4:
ball_y = 8 - ball_y
ball_vel_y = -ball_vel_y
# if ball_x < 1:
# raise SyntaxError("a ded")
# if ball_x > 8:
# raise SyntaxError("b ded")
else:
ball_x, ball_y = (
remote_values["ball_pos"] if "ball_pos" in remote_values else (0, 0)
)
if ball_x > 4:
print("x:", 4 - ball_y)
print("y:", ball_x, 9 - ball_x)
mb.display.set_pixel(4 - ball_y, 9 - ball_x, 9)
# if ball_x < 1:
# raise SyntaxError("a ded")
# if ball_x > 8:
# raise SyntaxError("b ded")
send_remote("position", position)
if mb.button_a.was_pressed():
position -= 1
position = max(0, position)
if mb.button_b.was_pressed():
position += 1
position = min(3, position)
mb.display.set_pixel(position, 0, 9)
mb.display.set_pixel(position + 1, 0, 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment