Practice Exercises for Drawing

Contents

Practice Exercises for Drawing#

1#

Modify the following program template to print "It works!" on the canvas. “It works!” template“It works!” solution“It works!” (Checker)

# Print to canvas

###################################################
# Student should add code where relevant to the following.

try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui


# Draw handler
def draw(canvas):
    canvas.draw_text("It works!",[120, 112], 48, "Red")
    

# Create frame and assign callbacks to event handlers
frame = simplegui.create_frame("It works", 400, 200)
frame.set_draw_handler(draw)
frame.start()

2#

Given the following program template, add draw commands to the draw handler to draw "This is easy?" on the canvas. The precise size and location of the text on the canvas is not important. “This is easy?” template“This is easy?” solution“This is easy?” (Checker)

# Display "This is easy?"

###################################################
# Student should add code where relevant to the following.

try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui
 

# Draw handler
def draw(canvas):
    canvas.draw_text('This is easy?', [0, 200], 48, 'yellow')
    

# Create frame and assign callbacks to event handlers
frame = simplegui.create_frame("This is easy", 400, 200)
frame.set_draw_handler(draw)


# Start the frame animation
frame.start()

3#

Create a canvas of size 96×96, and draw the letter "X" with font size 48 in the upper left portion of the canvas. Review the syntax for the SimpleGUI method draw_text and the layout of canvas coordinates. Draw “X” templateDraw “X” solutionDraw “X” (Checker)

# Display an X

###################################################
# Student should add code where relevant to the following.

try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui
 

# Draw handler
def draw(canvas):
	canvas.draw_text('X', [0, 48], 48, 'yellow')
    

# Create frame and assign callbacks to event handlers
f = simplegui.create_frame('X', 96, 96)
f.set_draw_handler(draw)


# Start the frame animation
f.start()

4#

Write a function format_time that takes an integer number of seconds in range(0, 3600) and converts it into string that states the number of minutes and seconds. Remember to use the operations // and %. (Note that this example requires no interactive code.) Format time templateFormat time solutionFormat time (Checker)

# Define a function that returns formatted minutes and seconds

###################################################
# Time formatting function
# Student should enter function on the next lines.
def format_time(seconds):
	minutes = seconds // 60
	remaining_seconds = seconds % 60
	return f'{minutes} minutes and {remaining_seconds} seconds'

###################################################
# Tests

print(format_time(23))
print(format_time(1237))
print(format_time(0))
print(format_time(1860))

###################################################
# Output to console
#0 minutes and 23 seconds
#20 minutes and 37 seconds
#0 minutes and 0 seconds
#31 minutes and 0 seconds

5#

Challenge: Complete the program template below and add two buttons that control the radius of a white ball centered in the middle of the canvas. Clicking the “Increase radius” button should increase the radius of the ball. Clicking the “Decrease radius” button should decrease the radius of the ball, except that the ball radius should always be positive. Ball radius templateBall radius solutionBall radius (Checker)

# Move a ball

###################################################
# Student should add code where relevant to the following.
try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui
 

# Define globals - Constants are capitalized in Python
HEIGHT = 400
WIDTH = 400
RADIUS_INCREMENT = 5
ball_radius = 20

# Draw handler
def draw(canvas):
    canvas.draw_circle([WIDTH // 2, HEIGHT // 2], ball_radius, 5, 'yellow')
    
# Event handlers for buttons
def increase_radius():
    global ball_radius
    ball_radius += RADIUS_INCREMENT


def decrease_radius():
    global ball_radius
    ball_radius -= RADIUS_INCREMENT
    if ball_radius <= 0:
        ball_radius = 5


# Create frame and assign callbacks to event handlers
frame = simplegui.create_frame("Ball control", WIDTH, HEIGHT)
frame.set_draw_handler(draw)
frame.add_button("Increase radius", increase_radius)
frame.add_button("Decrease radius", decrease_radius)


# Start the frame animation
frame.start()