STEP 7: Each time the interval event runs, we get a point. That means we need score to increase!
To change an existing variable inside a function or event, we need to make the variable global.
No more spinning please! Find this code in the interval event and delete it: sprite.turn_left(15)
Go to the LOGIC tab and toolkit. Drag Global Variable out so that it is indented inside the event.
Drag Add Value onto the next line. Make sure it's also indented! Change all my_var variables to score.
To navigate the page using the TAB key, first press ESC to exit the code editor.
# Program variables (DON'T DELETE!!!)
block_speed = -6 # controls how fast the pipes move across the stage
block_gap = 200 # controls the space between the top and bottom of each pipe
block_interval = 1 # controls how often a new pipe appears
gravity = 8 # controls how fast the sprite falls
flappiness = 5 # controls how much the sprite moves up
# Store and display score
score = 0
score_text = codesters.Text("Score: " + str(score), 100, 200, "yellow")
########################################################################
# ADD CODE BELOW THIS LINE #
########################################################################
stage.set_background("space")
stage.disable_all_walls()
sprite = codesters.Sprite("dinosaur")
sprite.set_size(0.4)
sprite.set_say_color("yellow")
sprite.say("TAP THE SPACE BAR TO GUIDE ME THROUGH THE BLOCKS!", 3)
sprite.go_to(-200, 0)
stage.set_gravity(gravity)
def space_bar():
sprite.jump(flappiness)
stage.event_key("space", space_bar)
def interval():
sprite.turn_left(15)
# add any other actions...
stage.event_interval(interval, block_interval)
t = codesters.Teacher()
turn_lefts = t.find_function("turn_left")
globals_ = t.find_text("global")
increments = t.find_text("+=")
try:
tval1 = turn_lefts[0][1].lower().replace(' ','')
except:
tval1 = "DNE"
try:
tval2 = globals_[0][1].lower().replace(' ','')
tval2_indent = t.get_indent_at_line(globals_[0][0])
except:
tval2 = "DNE"
tval2_indent = "DNE"
try:
tval3 = increments[0][1].lower().replace(' ','')
tval3_indent = t.get_indent_at_line(increments[0][0])
except:
tval3 = "DNE"
tval3_indent = "DNE"
t1 = TestObjective()
t1.add_success(tval1 == "DNE", "Great Job!")
t1.add_failure(tval1 != "DNE", "Did you remove sprite.turn_left(15) from inside the interval event?")
t2 = TestObjective()
t2.add_success('globalscore' in tval2 and tval2_indent == 4, "Great job!")
t2.add_failure(tval2 == "DNE", "Did you add Global Variable inside the interval event?")
t2.add_failure(tval2 != "DNE" and 'score' not in tval2, "Did you change my_var to score so that score is global?")
t2.add_failure(tval2 != "DNE" and tval2_indent != 4, "Make sure the Global Variable is indented inside the interval event!")
t3 = TestObjective()
t3.add_success('score+=1' in tval3 and tval3_indent == 4, "Great job!")
t3.add_failure(tval3 == "DNE", "Did you place Add Value into the interval event?")
t3.add_failure(tval3 != "DNE" and 'score' not in tval3, "Did you change my_var to score so that score increases by 1?")
t3.add_failure(tval3 != "DNE" and tval3_indent != 4, "Make sure the Add Value is indented inside the interval event!")
tester = TestManager()
tester.add_test_list([t1, t2, t3])
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.