I need help on Code HS. The assignment is called Tell a Story 6.1.6. I am trying to change from scene one to scene two. The message under the canvas keeps repeating but not changing scenes. Please help! This is AP computer science principles.



# Define the function to draw the white screen and "Click to Begin!" prompt
def draw_white_screen():
# Adding a white rectangle to simulate the white screen
rect_white = Rectangle(400, 500)
rect_white.set_position(200, 250)
rect_white.set_color('white')
add(rect_white)

# Prompt the user to click to begin
click_prompt = Text("Click to Begin!")
click_prompt.set_position(200 - click_prompt.get_width() / 2, 250)
add(click_prompt)

# Add mouse click handler to start the story
add_mouse_click_handler(start_story)

# Define the function to draw the initial background
def draw_initial_background():
# Adding a green rectangle as a background for sky
rect_background = Rectangle(400, 500)
rect_background.set_position(0, 0)
rect_background.set_color(Color.green)
add(rect_background)

# To make a blue rectangle for the sky
rect_sky = Rectangle(400, 300)
rect_sky.set_position(0, 0)
rect_sky.set_color(Color.blue)
add(rect_sky)

# To make a yellow circle for the sun
circle_sun = Circle(80)
circle_sun.set_position(350, 50)
circle_sun.set_color(Color.yellow)
add(circle_sun)

# To make a brown rectangle for a tree trunk
rect_trunk = Rectangle(10, 80)
rect_trunk.set_position(30, 250)
rect_trunk.set_color(Color.brown)
add(rect_trunk)

# To make a purple circle for a flower (assuming circle_flower is a placeholder)
circle_flower = Circle(30)
circle_flower.set_position(37, 250) # Adjust position as needed
circle_flower.set_color(Color.green)
add(circle_flower)

# Define the function to start the story
def start_story(x, y):
clear()
draw_initial_background()
draw_scene1()

# Define the function for the next scene
def draw_next_screen(x, y):
global scene_counter
scene_counter += 1

if scene_counter == 1:
draw_scene1()
elif scene_counter == 2:
draw_scene2()
elif scene_counter == 3:
draw_scene3()
else:
draw_scene4()

# Call the function to draw the white screen and "Click to Begin!" prompt
draw_white_screen()

# Define the function to draw an eye
def draw_eye(center_x, center_y):
left_eye = Circle(10)
left_eye.set_color(Color.white)
left_eye.set_position(center_x - 15, center_y)
add(left_eye)

right_eye = Circle(10)
right_eye.set_color(Color.white)
right_eye.set_position(center_x + 15, center_y)
add(right_eye)

# Define the function to draw a pupil
def draw_pupil(center_x, center_y):
left_pupil = Circle(4)
left_pupil.set_position(center_x - 15, center_y)
left_pupil.set_color(Color.blue)
add(left_pupil)

right_pupil = Circle(4)
right_pupil.set_position(center_x + 15, center_y)
right_pupil.set_color(Color.blue)
add(right_pupil)
def draw_scene1():
# To make a yellow rectangle face
rect_face = Rectangle(60, 70)
rect_face.set_position(250, 250)
rect_face.set_color(Color.yellow) # Change to yellow
add(rect_face)

# Add eyes with left-pointing blue pupils to the yellow rectangle face
eye_center_x = 275
eye_center_y = 275
draw_eye(eye_center_x, eye_center_y)
draw_pupil(eye_center_x, eye_center_y)

# Add the "Such a lovely day!" text to the scene
lovely_day_text = Text("Such a lovely day!")
lovely_day_text.set_position(250 - lovely_day_text.get_width() / 2, 200)
add(lovely_day_text)
# Print the scene text
print("Once upon a time there was a square named Bob")



# Add mouse click handler for the next scene
add_mouse_click_handler(draw_next_screen)

Respuesta :

Explanation:

It seems like you're trying to transition from scene one to scene two when the mouse is clicked, but the code is not functioning as expected. One issue might be that you're not clearing the canvas before drawing the next scene, so the elements from the previous scene remain visible. Let's make some adjustments to your code to fix this issue:

```python

# Add import statement for Color

from codehs import Color

# Define a global variable to track the scene

scene_counter = 1

# Define the function to clear the canvas

def clear_canvas():

clear()

# Define the function to draw the white screen and "Click to Begin!" prompt

def draw_white_screen():

clear_canvas()

# Adding a white rectangle to simulate the white screen

rect_white = Rectangle(400, 500)

rect_white.set_position(200, 250)

rect_white.set_color('white')

add(rect_white)

# Prompt the user to click to begin

click_prompt = Text("Click to Begin!")

click_prompt.set_position(200 - click_prompt.get_width() / 2, 250)

add(click_prompt)

# Add mouse click handler to start the story

add_mouse_click_handler(start_story)

# Define the function to draw the initial background

def draw_initial_background():

clear_canvas()

# Adding a green rectangle as a background for sky

rect_background = Rectangle(400, 500)

rect_background.set_position(0, 0)

rect_background.set_color(Color.green)

add(rect_background)

# To make a blue rectangle for the sky

rect_sky = Rectangle(400, 300)

rect_sky.set_position(0, 0)

rect_sky.set_color(Color.blue)

add(rect_sky)

# To make a yellow circle for the sun

circle_sun = Circle(80)

circle_sun.set_position(350, 50)

circle_sun.set_color(Color.yellow)

add(circle_sun)

# To make a brown rectangle for a tree trunk

rect_trunk = Rectangle(10, 80)

rect_trunk.set_position(30, 250)

rect_trunk.set_color(Color.brown)

add(rect_trunk)

# To make a purple circle for a flower (assuming circle_flower is a placeholder)

circle_flower = Circle(30)

circle_flower.set_position(37, 250) # Adjust position as needed

circle_flower.set_color(Color.green)

add(circle_flower)

# Define the function to start the story

def start_story(x, y):

draw_initial_background()

draw_scene1()

# Define the function for the next scene

def draw_next_screen(x, y):

global scene_counter

scene_counter += 1

if scene_counter == 1:

draw_scene1()

elif scene_counter == 2:

draw_scene2()

elif scene_counter == 3:

draw_scene3()

else:

draw_scene4()

# Call the function to draw the white screen and "Click to Begin!" prompt

draw_white_screen()

```

Make sure to apply similar changes to clear the canvas and redraw elements for subsequent scenes to ensure a smooth transition between scenes. Also, make sure that the `draw_scene2()` function and subsequent scene-drawing functions are defined correctly with the appropriate elements for each scene.

ACCESS MORE