AMPLIAR—PASO 3 : Nuestro marcador no se actualiza cuando golpeamos la canasta. ¡Arreglemos eso!
Las variables creadas FUERA de un evento no se pueden cambiar DENTRO de un evento . Usamos la palabra clave global para hacer una excepción a esta regla. global nos permite usar una variable dentro y fuera de un evento.
Dentro de su evento de colisión, arrastre Global Variable dentro de su instrucción if. Debe sangrarse dos veces .
Cambie el nombre de la variable de my_var a score .
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background("moon")
sprite = codesters.Sprite("alien1")
sprite.go_to(-175, -150)
net = codesters.Sprite("basketballnet")
net.go_to(215, 175)
instructions = codesters.Text("Click on the sprite to shoot the basketball. Hit the basket to score a point!")
instructions.go_to(0, 50)
instructions.set_color("white")
stage.set_gravity(10)
sprite.set_gravity_off()
def click(sprite):
ball = codesters.Sprite("basketball")
ball.go_to(-175, -125)
ball.set_x_speed(10)
ball.set_y_speed(10)
# add other actions...
sprite.event_click(click)
stage.disable_right_wall()
def collision(net, hit_sprite):
my_var = hit_sprite.get_image_name()
if my_var == "basketball":
sprite.say("Score!")
hit_sprite.hide()
# add any other actions...
net.event_collision(collision)
score = 0
#my_display = codesters.Display(my_var, x, y)
my_display = codesters.Display(score, -200, 150)
t = codesters.Teacher()
try:
param = t.find_text('global')
tval1 = param[0][1]
tval2 = t.get_indent_at_line(param[0][0])
except:
tval1 = "DNE"
tval1a = "DNE"
tval2 = "DNE"
t1 = TestObjective()
t1.add_success('score' in tval1 and tval2 == 8, "Great job!")
t1.add_failure(tval1 == "DNE", "Oops! Did you drag in Global Variable?")
t1.add_failure('my_var' in tval1, "Make sure the global variable name is score.")
t1.add_failure(tval2 < 8, "Oops! Did you indent your global variable 8 spaces inside your if statement?")
t1.add_failure('score' not in tval1, "Make sure your global variable name is score.")
tester = TestManager()
tester.add_test_list([t1])
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.