######################################################################## HELPER…
- User
########################################################################
HELPER FUNCTIONS
########################################################################
def set_stage(): """ Sets up the stage and introduces the game """
codestage.set_background("space") sprite = codesters.Sprite("astronaut1") sprite.set_say_color("white") sprite.say("Only click on the aliens and ufos!") stage.wait(2) sprite.say("Don't click on the astronauts!") stage.wait(2) sprite.hide()def add_click_event(sprite, my_display, my_dictionary): """ Attaches a click event to a sprite """ def click(sprite): stage.remove_sprite(sprite) image = sprite.get_image_name() my_value = my_dictionary.get(image) print(my_value) global score score += my_value my_display.update(score) # add other actions...
codesprite.event_click(click)def play_game(my_display, my_dictionary): """ Creates an interval event to start the game """ image_list = list(my_dictionary.keys()) print(image_list) def interval(): x = random.randint(-200, 200) y = random.randint(-200, 200) image = random.choice(image_list) sprite = codesters.Sprite(image, x, y) add_click_event(sprite, my_display, my_dictionary) stage.wait(2) stage.remove_sprite(sprite) # add any other actions...
codestage.event_interval(interval, 2)########################################################################
MAIN FUNCTION
########################################################################
def main(): """ Sets up the program and calls other functions """ set_stage() global score score = 0 my_display = codesters.Display(score, 0, 175) my_dictionary = {"alien1": 10, "alien2": 20, "ufo": 5} my_dictionary["astronaut1"] = -100 print(my_dictionary) play_game(my_display, my_dictionary) my_dictionary["astronaut2"] = -200 print(my_dictionary)
main()
Let's add one more astronaut to make it more difficult!
In , drag in Add Entry indented inside main() and above where you call play_game(). Change "fish" to "astronaut2" and 0 to -200. Click Run and then click on the Console. See how "astronaut2" is now a part of our dictionary?
- Assistant
def main(): """ Sets up the program and calls other functions """ set_stage() global score score = 0 my_display = codesters.Display(score, 0, 175) my_dictionary = {"alien1": 10, "alien2": 20, "ufo": 5} my_dictionary["astronaut1"] = -100 my_dictionary["astronaut2"] = -200 print(my_dictionary) play_game(my_display, my_dictionary)
code# Add Entry to dictionary my_dictionary["astronaut2"] = -200 print(my_dictionary)main()
- User
########################################################################
HELPER FUNCTIONS
########################################################################
def set_stage(): """ Sets up the stage and introduces the game """
code - Assistant
You need to make the modifications in your "Add Entry to dictionary" code block code. Specifically, you need to replace "fish" with "astronaut2" and 0 with -200. Here's the updated code snippet for that part:
python# Add Entry to dictionary my_dictionary["astronaut2"] = -200After making this change, your code will include the addition of "astronaut2" with a value of -200 in your dictionary. When you run the program and check the console output, you should see "astronaut2" included in the dictionary with the specified value.
- User
but with all code
- Assistant
Sure, here is the full code with the modification of adding "astronaut2" with a value of -200 to the dictionary:
- User
did you drag entry above where you call play_game()
- Assistant