149k views
2 votes
6.3 code practice edhisive

You should see the following code in your programming environment:

Import simplegui

Def draw_handler(canvas):
# your code goes here

Frame = simplegui.creat_frame(‘Testing’ , 600, 600)
Frame.set_canvas_background(“Black”)
Frame.set_draw_handler(draw_handler)
Frame.start()

Use the code above to write a program that, when run, draws 1000 points at random locations on a frame as it runs. For an added challenge, have the 1000 points be in different, random colors.

2 Answers

7 votes

Final answer:

To draw 1000 random colored points, modify the draw_handler in the provided code to generate random coordinates and colors for each point using the 'random' module. The draw_point method of the canvas object is used to draw each point.

Step-by-step explanation:

To create a program that draws 1000 random points in different colors on a canvas using the provided code template, we need to import additional modules for randomness and modify the draw_handler function. Here's how you might modify the code:

Import

simplegui

Import

random

Def

draw_handler(canvas):

for

i

in

range(1000):
# Generate random coordinates
x = random.randrange(0, 600)
y = random.randrange(0, 600)
# Generate a random color each time
color = simplegui_lib_draw_handler(randrange(0,256), randrange(0,256), randrange(0,256))
canvas.draw_point((x, y), color)

Frame = simplegui.create_frame('Testing', 600, 600)
Frame.set_canvas_background("Black")
Frame.set_draw_handler(draw_handler)
Frame.start()

This program uses the random module to generate random x and y coordinates for each point and a random color for each point before drawing it onto the canvas. When the frame is started, it will invoke the draw_handler function to draw these points.

User SANBI Samples
by
5.8k points
13 votes

Final answer:

To draw 1000 points at random locations on a frame, modify the draw_handler function and use a loop to generate random x and y coordinates. Use the canvas's draw_point method to draw a point at each coordinate, and generate a random color for each point.

Step-by-step explanation:

To draw 1000 points at random locations on a frame, you can use the given code and modify the draw_handler function. Inside the draw_handler function, you can use a loop to generate 1000 random x and y coordinates, and then use the canvas's draw_point method to draw a point at each coordinate. Additionally, you can generate a random color for each point using the canvas's random_color method. Here's an example of how the modified draw_handler function could look:

import simplegui

def draw_handler(canvas):
for _ in range(1000):
x = canvas.width * random.random()
y = canvas.height * random.random()
color = canvas.random_color()
canvas.draw_point((x, y), color)

frame = simplegui.create_frame('Testing', 600, 600)
frame.set_canvas_background('Black')
frame.set_draw_handler(draw_handler)
frame.start()

User Maiasaura
by
7.5k points