|
| |||
|
|
| |||
|
COMMAND | EXEMPLE |
pendown() , pd() | pd() |
Sets the pen down and the spaceship resumes leaving a trail as it moves on the canvas. | |
penup() , pu() | pu() |
Sets the pen up and the spaceship stops leaving a trail as it moves on the canvas. | |
pensize(value), width(value) | pensize(25) |
Sets the pen size to a number of value pixels.
| |
pencolor(color) | pensize("#ff00ff") |
Sets the stroke color (default: black).
The color parameter can be set using standard color codes or html HEX values.
Examples:
pencolor('red')
"yellow" (), "white" (), "brown" (), "tan" (), "green" (), "aquamarine" (), "salmon" (), "purple" (), "orange" (), "gray" (). |
|
fillcolor(color) | fillcolor("yellow") |
Sets the fill color (default: black).
The color parameter can be set using standard color codes or html HEX values.
Examples:
fillcolor("red")
|
|
color(c_stroke[,c_fill]) | color("red","green") |
Changes the stroke and fill colors at the same time.
If c_fill is missing,
the color set by c_stroke is used for both attributes.
|
COMMAND | EXAMPLE | ||
stamp() | stamp() | ||
Draws a copy of the spaceship graphic on the canvas at the current position, like a stamp. | |||
begin_fill() | begin_fill() | ||
A new path is set and it describes a geometric shape (a polygon). | |||
end_fill() | end_fill() | ||
Closes the current path and fills the shape using the fill color (see fillcolor()).
Example. The program below draws a green square. The fill color is chosen, then the path is started by begin_fill() command:
The desired geometric shape is drawn, s end_fill() closes the path and fills it with green. The desired geometric shape is drawn, so end_fill() closes the path and fills it with green. |
|||
fill() | print(fill()) ; if (fill()) ... | ||
The function returns a boolean value based on the state of the fill mode:
True or False , being useful
in certain slightly more complex programs.
|
|||
dot() | dot() | ||
A point is drawn at the current position. The function is very useful if we want to
use it in combination with penup(), drawing only certain important points along the way
of the spaceship.
Example. Below we draw only the corners of a square:
|
|||
circle(radius, angle, step) | circle(50,360,100) | ||
A circle is characterized by radius , i.e. the distance from the center to any of its points.
Next, you need to understand that a circle is made of step points.
You can draw a full circle using 360 degrees for the value of angle
or only a part of it, more precisely an arc of a circle imposing a correspondingly smaller angle.
The larger the step , the finer the drawing of the geometric shape will be.
The magic of geometry. When step = 4 ,
a square will be drawn (like a rhombus), and when
step = 5 , a pentagon.
If we set step = 6 , we will get a hexagon, etc.
The more we increase the value of the parameter step ,
the more the polygon approaches the shape of the circle that inscribes it!
Example. Test the code below to understand it better:
|
COMMAND | EXAMPLE |
variable_name = clone() | clone1 = clone() |
A new variable, addressable by variable_name ,
will retain a new graphical object (an identical spaceship) which can be
commanded using the Python programming language. The position on the screen and the rest of the parameters will also be identical
with all the attributes of the "mother" ship. You can create one clone, two ... or as many as you want!
Caution! The "mother" ship can still be controlled through direct commands such as
fd(80), but
clone1 in the example will receive
statements like clone1.fd(50) because they are different objects on the screen,
and the addressing must be done properly!
Important note! Python is an interpreter and
executes the instructions sequentially, i.e. one after the other.
Thus, the graphic objects on the screen will alternately perform the animations depending on the program you created!
Example. Test the code below:
from turtle import *
|
|
variable_name = Turtle() | spaceship2 = Turtle() |
A new variable, addressable by variable_name ,
will hold a new graphic object (a new spaceship).
Unlike clone(), this function creates a new object with default parameters
and positioned at the (0,0) coordinates.
Example. Test the code below:
from turtle import *
A little trick. To simulate the simultaneous motion of two spaceships, we can move them alternatively by small steps (like 1 pixel), as in the example below:
from turtle import *
The speed of the two objects in this situation decreases considerably... |
COMMAND | EXAMPLE | ||
print(stuff) | print("Aloha, Mars!") | ||
Prints useful information in the Log/Output data panel. Simple text: print("This is a text!") Contents of variable x : print(x) Contents of variables x, y, z : print(x,y,z) Expressions: print(x**x) Separator (optional). Specify how to separate the objects, if there is more than one. Default is ' ' (a blankspace). Example: print(1,2,3,4,5,sep='_') Ending line (optional). Specify what to print at the end. Default is '\n' (line feed) Examples: print(1,2,3,4,5,end='---') , print(1,2,3,4,5,end='\n\n') , print(1,2,3,4,5,end='\n------\n') , etc. |
|||
write(message,move,align,style) | write("Hello!") | ||
The text of message is written graphically starting from the current position.
move parameter is a boolean
(True / False ) and
allows the ship to move or not while the message is being plotted on the canvas.
align
can get values like "left" , "center" or
"right" .
The text style is entered as a tuple:
(font, size, font_style) .
Example:
Remarks • The font_style parameter can be "normal" ,
"italic" or "bold" .
• To move the ship without drawing the line, we can of course use penup() before the write() command:
from turtle import *
• Only message is required as a parameter,
the rest being optional. Play with different values for the text style!
For example, the names of some standard fonts are: " Tahoma ", "Verdana ",
"Arial ", "Calibri ", etc.
|
|||
input(optional_message) | input() , input("x = ") | ||
The input() function allows user input from the keyboard. optional_message is
a String, representing a default message before the input.
Example: username = input("Your name is: ") ; print("Hello,", username) Important note.This function returns a string. In order to get the right type for various purposes, use type casting as bellow:
#as a string
Run the code and enter 12 each time, for example. Python is cool! Mind the type casting technique though.. you can't convert 'nine' to 9, or ... 3.14 to 3. You need to know what you are asking and what might be entered by the user! |