STEP 11: If our time is even (60, 58, 56...) we'll create a star. But first, we need a random y-coordinate!
Add another Random Integer indented in the if-statement. It should be indented twice ········¬
Change my_var to y and the range to (0, 230).
To make the stars appear randomly on the stage, we need a random xy-coordinate pair! Stars should appear anywhere horizontally, but only in the sky, so our random ranges for our x and y are a little different.
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background("moon")
sprite = codesters.Sprite("hedgehog", 0, -210)
score = 0
#my_display = codesters.Display(my_var, x, y)
score_display = codesters.Display(score, -210, -210)
time = 60
#my_display = codesters.Display(my_var, x, y)
time_display = codesters.Display(time, 210, -210)
def left_key():
rotation = sprite.get_rotation()
print(rotation)
sprite.set_rotation(rotation+3)
# add other actions...
stage.event_key("left", left_key)
def right_key():
rotation = sprite.get_rotation()
print(rotation)
sprite.set_rotation(rotation-3)
# add other actions...
stage.event_key("right", right_key)
def interval():
global time
time -=1
time_display.update(time)
if time %2 == 0:
pass
x = random.randint(-230,230)
stage.event_interval(interval, 1)
t = codesters.Teacher()
ifs = t.find_block('if')
rands = t.get_parameters_for_function("randint")
rand_text = t.find_text('randint')
func = t.find_text('interval()')
call = t.find_function('event_interval')
try:
tval1 = ifs[0][1].replace(' ','')
tval1b = ifs[0][0]
tval1_indent = t.get_indent_at_line(tval1b)
except:
tval1 = "DNE"
tval1b = -1
tval1_indent = "DNE"
try:
tval2a = int(rands[1][0])
tval2b = int(rands[1][1])
tval2c = rand_text[1][0]
tval2_indent = t.get_indent_at_line(tval2c)
tval2d = rand_text[1][1].split('=')[0].replace(" ", "")
except:
tval2a = -1
tval2b = -1
tval2c = -1
tval2_indent = "DNE"
tval2d = "DNE"
try:
def_line = int(func[0][0])
def_line_indent = t.get_indent_at_line(def_line)
except:
def_line = -1
def_line_indent = -1
try:
call_line = int(call[0][0])
call_line_indent = t.get_indent_at_line(call_line)
except:
call_line = -1
call_line_indent = -1
t1_indent = TestObjective()
t1_indent.add_success(tval1_indent == 4 and (def_line <tval1b < call_line), "Great job!")
t1_indent.add_failure(tval1_indent != 4, "Make sure you indent the if statement inside the interval event!")
t1_indent.add_failure(not (def_line <tval1b < call_line), "Make sure if-statement is inside your interval event.")
t2 = TestObjective()
t2.add_success(tval2a == 0 and tval2b == 230 and tval2d == 'y', "Great job!")
t2.add_failure(tval2a == "DNE", "Did you add Random Integer inside the if-statement?")
t2.add_failure((tval2a == 1 or tval2b == 10), "Did you change the range in random.randint()?")
t2.add_failure((tval2a != 0 or tval2b != 230), "Did you set the range in .randint() correctly?")
t2.add_failure(tval2d != 'y', "Did you name the random variable x instead of my_var?")
t2_indent = TestObjective()
t2_indent.add_success(tval2_indent == 8 and (tval1b <tval2c < call_line), "Great job!")
t2_indent.add_failure(tval2_indent != 8, "Make sure you indent Random Integer twice (8 spaces total)!")
t2_indent.add_failure(not (tval1b <tval2c < call_line), "Make sure Random Integer is inside your if-statement.")
tester = TestManager()
tester.add_test_list([t1_indent, t2, t2_indent])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.