9.3. Exercises#
Each exercise carries the difficulty level indication:
[*] Easy
[**] Moderate
[***] Advanced
🖌️ Uses the NaNoPy graphical environment
The highest difficulty level is meant for students who wish to challenge themselves and/or have previous experience in programming.
Important
Do all NaNoPy exercises (🖌️), whatever their difficulty level. They build on each other, so skipping one makes the next ones harder.
Exercise 9.2 ([*] Diagonal red line pixel by pixel 🖌️)
Write a program that draws a diagonal red line by using the writer’s draw_pixel function.
Exercise 9.3 ([*] Diagonal red line with draw_line 🖌️)
Drawing lines pixel by pixel is not the most efficient way of doing so. Write a program that draws a diagonal line using the writer’s draw_line function.
pen.draw_line(x1, y1, x2, y2, c)
The first two arguments (x1 and y1) indicate the origin of the line.
The next two arguments (x2 and y2) are the coordinates of the end of
the line. The last argument (c) is the colour of the line.
Exercise 9.4 ([*] Random lines 🖌️)
Write a program that draws several lines with random beginning and end points. Make sure each line starts and ends within the screen.
Hint: Make use of the random package in your code. Two examples are shown below:
import random as rnd
# Return random number between 0.0 and 1.0:
rnd.random()
# Return an integer between -2 and 2 (both included):
rnd.randint(-2,2)
Exercise 9.5 ([*] Blue circle 🖌️)
Write a program that draws, centred on the screen, a blue circle with a radius of 50 pixels using the writer’s draw_circle function:
pen.draw_circle(x, y, r, c, f)
The first two arguments (x and y) specify the centre of the circle. The
third argument is the radius of the circle. The fourth argument specifies the
colour, and the last argument is a Boolean to specify whether the circle should
be filled or not.
Five seconds after drawing the circle, the screen should be cleared. The code below shows you how to how delay (pause in milliseconds) and then clear the screen:
screen.pause(5000)
screen.clear()
screen.update() # The screen needs to be updated to see the effect of the clearing
screen.keep_window()
Exercise 9.6 ([*] Yellow flashing light 🖌️)
Write a program that simulates a yellow flashing light in the middle of the graphical window that flashes until you stop the program.
Hint: use an infinite while loop (while screen.running(): … ), this will stop the program from running when the window is closed. A while loop will perform its code as long as the condition you state is true.
Exercise 9.7 ([***] Rock paper scissors (lizard, Spock))
Exercise A: Write an interactive Python implementation of the “Rock paper scissors” game:
Ask user for their choice (rock, paper, or scissors). Make sure that the user makes a valid choice.
Compare user input with random computer choice.
Notify the participant whether they won or lost.
Hint: Python’s random package and random.choice() function may come in handy.
# Your code here
Exercise B: Include “lizard” and “Spock” into game (see the rules: https://www.instructables.com/How-to-Play-Rock-Paper-Scissors-Lizard-Spock/).
# Your code here