Introduction
基础的Python作业,主要就是用while和if结合三方库来画图。
Introduction
You will use Turtle graphics to draw a picture containing multiple shapes of multiple colors and arranged to be visually pleasing. Although you are free to decide the precise shape(s) and layout for your picture, some aspect of the picture must depend on a numeric value input by the user. For example, the input might determine the size of the shapes, the number of shapes, or their spacing.
Background
Originally written as a part of the logo programming language, Turtle graphics is one of the oldest graphics programs. It is a 2D graphics package that uses a Cartesian coordinate system and a “turtle,” which you can imagine has a pen attached to its body. The turtle can move around the plane, drawing as it goes. Python has a module that implements the behavior of the original turtle graphics program and this module is simply called “turtle” (see Appendix B of the text and the comments in the sample file turtleSample.py).
Project Description / Specification
Your program must:
- Output a brief descriptive message of what the program does.
- Repeatedly prompt for the input until the user supplies values of the correct form (discard incorrect inputs). Your prompt should say what form of input is needed.
- Draw a picture containing multiple shapes of multiple colors, where the input value(s) is (are) used to determine some aspect of the shapes and/or their layout.
- Adhere to 1-6 of the Coding Standard
In programming your solution, you must:
- Use at least two repetition (while or for) statements.
- Use at least one selection (if) statement.
We show example output produced by two different programs that meet these requirements at the end of this write-up. You may be creative and create your own program, or you may choose to mimic one of these two examples. The second example shows error checking being tested.
Creating Colors
There are many ways to create a color but a common one used in computer graphics is the process of additive color. Combining different amounts of red, green and blue can create most (but not all) colors. In turtle, you can specify a color by giving three floating-point values, each in the range from 0.0 to 1.0, indicating the amount (fraction or percent) of each color. For instance, (1.0, 0.0, 0.0) is red, (0.0, 1.0, 0.0) is green, and (0.5, 0.5, 0.0) is brown.
You can find the codes for many colors on a color chart.
A convenient way to generate different colors is to repeatedly call the random function in the random module to obtain values for the color amounts. First, import the random module: import random
Then, each call to random.random() returns a pseudo random (floating-point) number in the range 0.0 to 1.0. A sample program using this method to create a color and draw a figure is provided in the project directory: turtleSample.py
Using turtle graphics
In order to use turtle graphics in Python you must first import the turtle module. You can then use the help function in the shell to find out what methods this module includes and what they do. Just type import turtle in the Python Shell window, hit enter, and then type help(turtle) and scroll up through the list and information. For more details Google “Python 3 turtle.” A sample Python program, turtleSample.py, is provided in the project directory. The comments in this file describe methods that you might want to use for this project. When running your program in Spyder, you may need to look under other windows to find the turtle drawing window.
Keeping the window up
If the drawing window has a tendency to disappear too quickly, you can “hold” the window by using the sleep function in the time module, as follows:1
2import time
time.sleep(seconds)
The program will wait the number of seconds indicated before it ends.