Step 10: We have just used loops to create the firework. Now, we will use another loop to start each particle moving!
From drag out Enumerate. It should be indented once!
Change index, value to count, p and change my_list to particle_list.
This code will pick each particle p stored in particle_list and let us do things to it! We will finally be able to set each particle in motion!
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background_color("black")
amount = 10
speed = 5
color_list = ["purple", "yellow", "red"]
stage.disable_all_walls()
def click():
x = stage.click_x()
y = stage.click_y()
particle_list = []
for color in color_list:
for counter in range(amount):
sprite = codesters.Circle(x, y, 10, color)
particle_list.append(sprite)
angle = 0
# add other actions...
stage.event_click(click)
t = codesters.Teacher()
loops = t.find_text('enumerate')
# speeds = t.get_parameters_for_function('set_x_speed')
try:
tval1 = int(loops[0][0])
except:
tval1 = 0
try:
tval4 = t.get_indent_at_line(tval1)
except:
tval4 = -1
try:
tval5 = loops[0][1].replace(" ", "")
except:
tval5 = "DNE"
try:
tval2 = int(speeds[0][0])
except:
tval2 = "DNE"
try:
tval3 = t.get_indent_at_line(t.find_text('set_x_speed')[0][0])
except:
tval3 = -1
try:
tval6 = t.get_indent_at_line(t.find_text('+=')[0][0])
tval7 = t.find_text('+=')[0][1].replace(" ", "")
except:
tval6 = -1
tval7 = "DNE"
t1 = TestObjective()
t1.add_success(tval1 != 0 and tval4 == 4 and tval5 == 'forcount,pinenumerate(particle_list):', "Great job!")
t1.add_failure(tval1 == 0, "Did you add an Enumerate command?")
t1.add_failure(tval4 == -1, "Did you drag out a Enumerate command?")
t1.add_failure(tval4 != 4, "Oops! Make the loop is indented once!")
t1.add_failure('index' in tval5, "Did you change index to count?")
t1.add_failure('value' in tval5, "Did you change value to p?")
t1.add_failure('my_list' in tval5, "Did you change my_list to particle_list?")
t1.add_failure(tval5 != 'forcount,pinenumerate(particle_list):', "Did you change the loop correctly?")
t2 = TestObjective()
t2.add_success(tval2 == 5 and tval3 == 8, "Great job!")
t2.add_failure(tval2 == "DNE", "Did you add Set x Speed in the new loop?")
t2.add_failure(tval3 != 8, "Did you indent .set_x_speed() twice?")
t3 = TestObjective()
t3.add_success(tval6 == 8 and tval7 == "count+=1", "Great job!")
t3.add_failure(tval7 == "DNE", "Did you add Add Value in the new loop?")
t3.add_failure(tval7 != "count+=1", "Did you change my_var to count?")
################################################
# Pass test code
# - set pass_required to True if pass is required to prevent bad input error
# - include tpass before t1 in the test list
pass_required = True
passes = t.find_text("pass")
num_pass_only = 0
for p in passes:
if p[1].lower().replace(' ','') == "pass":
num_pass_only += 1
tpass = TestObjective()
if not pass_required:
tpass.add_success(num_pass_only == 0, "Great job!")
tpass.add_creative(num_pass_only > 0, "Great job! Feel free to delete pass statements now.")
################################################
tester = TestManager()
tester.add_test_list([tpass, 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.