0% found this document useful (0 votes)
16 views43 pages

Group1 Turtle Graphics

Uploaded by

Jesuispeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views43 pages

Group1 Turtle Graphics

Uploaded by

Jesuispeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Turtle graphics

Lecture By: Nhep Bunthoeun


Our Team
1
Mao Somith

2
4 6

Korn Souty Nun Sambo Mao Chivon

3 5 7

Leang Panha Chon Bunham Chem Bunna


Overview
Introduction 01

24.1.3. Methods of RawTurtle/Turtle and 02


corresponding functions
24.1.4. Methods of TurtleScreen/Screen and
03
corresponding functions
24.1.5. Public classes 04
24.1.6. Help and configuration 05
24.1.7. DEMO 06
24.1.Introduction
Turtle graphic is a popular way for introducing programming to fundamental programmer.
It was part of the original Logo programming language developed by
( Wally Feurzig and Seymour Papert in 1966 ).

By combining together these and similar commands, intricate shapes and pictures.
The turtle has three attributes: a location, an orientation (or direction), and a pen. The pen, too, has attributes: color, width, and
on/off state (also called down and up).

turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15
pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25
degrees clockwise.
Example
import turtle

my_turtle = turtle.Turtle()

my_turtle.forward(105)

my_turtle.right(25)

turtle.mainloop()
24.1.Introduction
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it
uses tkinter for support the Turtle

The object-oriented interface uses essentially two+two classes:


• The TurtleScreen class defines graphics windows as a playground for the drawing turtles.
• RawTurtle (alias: RawPen) defines Turtle objects which draw on a TurtleScreen.

Turtle method:
To use multiple turtles on a screen one has to use the object-oriented interface.
Example

import turtle
screen = tur tle. Screen()
raw_tur tle = turtle. RawTurtle(screen)
for _ in r ange( 4):
raw_tur tle. forward(100)
raw_tur tle. right(90)
screen. mainloop()
Final Example

fr om turtle import *
color ('red', 'yellow')
begin_f ill()
while True:
forward(200)
left( 170)
if abs(pos( )) < 1:
br eak
end_fill()
done()
In the Python turtle module, the RawTurtle and Turtle classes are used for creating and manipulating turtle graphics. These classes provide various methods and
corresponding functions for drawing shapes, moving the turtle cursor, changing attributes like color and pen size, and more. Here's an overview of some common methods
24.1.3. Methods of RawTurtle/Turtle and corresponding
and their corresponding functions:

24.1.3.1. Turtle motion


24.1.3.2. Settings for measurement
functions
24.1.3.3. Pen control
24.1.3.4.1. Drawing state
24.1.3.4.2. Color control
24.1.3.4.3. Filling
24.1.3.4.4. More drawing control
24.1.3.5.1 Turtle state
24.1.3.5.2. Appearance
24.1.3.6. Using events
24.1.3.1. Turtle motion
Turtle motion refers to the movement of a turtle in the Turtle graphics environment. In the Python
Turtle module, you can control the motion of a turtle using various functions.

• Movement Commands:
⚬ forward(distance): Move the turtle forward by the specified distance.
⚬ backward(distance): Move the turtle backward by the specified distance.
• Turning Commands:
⚬ right(angle): Turn the turtle right by the specified angle.
⚬ left(angle): Turn the turtle left by the specified angle.
3.Setting Speed:
⚬ speed(speed=None): Set the turtle’s speed to an integer value in the range 0..10 or a
speed string.
4.Turtle.dot
is a function that draws a dot at the turtle's current position.
5.Turtle.undo
function in Python's Turtle module is used to undo the last action performed by the turtle.
24.1.3.2. Settings for measurement
In the Python turtle module, you can adjust settings related to measurement units and angles

Degree
The default unit for measuring angles in the Turtle module is degrees. This means that angles specified in
rotation functions like turtle.left() and turtle.right() are interpreted as degrees.
24.1.3.3. Pen control
Pen control in the Python turtle module refers to commands that control the drawing behavior of the turtle's
pen.
Drawing state
In the Turtle module of Python, the drawing state refers to whether the turtle's pen is currently down (drawing) or up
(not drawing).
turtle.penup(): This is useful for repositioning the turtle without drawing lines..
24.1.3.3. Pen control
turtle.pendown(): When the pen is down, the turtle draws lines as it moves.
Color control
In Python's Turtle module, color control refers to the ability to set and manipulate the color used
by the turtle for drawing on the screen.

Turtle pencolor
turtle.pencolor() function is used to set the color of the pen that the turtle
Color control
Turtle fillcolor
turtle.fillcolor(color) function is used to set the fill color that the turtle uses when filling shapes.
24.1.3.4.4. More drawing control
Turtle. reset The turtle.reset() function is a command used in the Python Turtle graphics library. When called, it
resets the turtle's position and other attributes to their default values.
24.1.3.5. Turtle state

24.1.3.5.1. Visibility

Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex
drawing, because hiding the turtle speeds up the drawing observably.

turtle.hideturtle() These methods can be used to hide and show turtle


turtle.showturtle() drawing icon.
The turtle.isvisible() is used to return True if the
turtle.isvisible()
Turtle is shown, False if it’s hidden.

Panha
24.1.3.5.2. Appearance
• The turtle.shape()
function is used to change the shape of this cursor. The shapes can be “arrow”, “turtle”, “circle”, “square
”, “triangle”, or "classic".
OUTPUT
24.1.3.5.2. Appearance
The turtle.resizemode() function in Python’s turtle module is used to change the size of the
turtle.

Different resizemodes have the following effects:


• “auto” adapts the appearance of the turtle
corresponding to the value of pensize.

• “user” adapts the appearance of the turtle according to


the values of stretchfactor and outlinewidth (outline),
which are set by shapesize()
• “noresize” no adaption of the turtle’s appearance takes
place.
24.1.3.5.2. Appearance
• turtle.shearfactor() :This function is used to set or return the current shearfactor. It
Shears the turtleshape according to the given shearfactor shear, which is the tangent of
the shear angle.

OUTPUT
24.1.3.5.2. Appearance
The turtle.turtlesize() this function is used to return or set the pen’s attributes x or y-
stretchfactors.

OUTPUT
24.1.3.6. Using event
Events in Python’s turtle module are used to make the program interactive and responsive t
o user actions such as mouse clicks or keyboard inputs
.
• turtle.onclick() :This function is used to bind fun to a mouse-click event on this turtle.
OUTPUT
24.1.3.6. Using event
• turtle.onrelease() :This function is used to bind fun to the mouse-button-release event
on this turtle.

OUTPUT
24.1.3.6. Using event
• turtle.ondrag() :This function is used to bind fun to mouse-move event on this turtle on
canvas.

OUTPUT
24.1.4. Methods of TurtleScreen/Screen and corresponding
functions

24.1.4.1. Window control

import turtle
This function is used to return the list of
screen = turtle.Screen()
turtles on the screen. This doesn’t require any screen.title("My Turtle Window")
argument. screen.bgcolor("lightblue")
screen.setup(width=800, height=600)
def on_click(x, y):
print(f"Clicked at ({x}, {y})")
screen.onclick(on_click)
turtle.done()

sambo
24.1.4.2. Animation control

• Animations are a great way to make Visualizations more attractive and user appealing. It helps us to demonstrate
Data Visualization in a Meaningful Way, you can use functions like forward(), backward(), left(), right(), etc.,
along with loops to create animated sequences.

• turtle.speed(speed): Set the turtle's movement speed. The


argument speed can range from 0 (slowest) to 10 (fastest).
• turtle.penup() and turtle.pendown(): Lift the pen (stop
drawing) and lower the pen (start drawing), respectively.
• turtle.goto(x, y): Move the turtle to the specified
coordinates (x, y).
• turtle.clear(): Clear the turtle's drawings.

sambo
24.1.4.3. Using screen events
import turtle
You can use functions like onscreenclick() or onkey() to bind event handlers to # Function to be called when the screen is clicked
specific actions. def on_click(x, y):
print(f"Mouse clicked at ({x}, {y})")

# Function to be called when a key is pressed


def on_key_press():
in Example print("Key pressed!")

• onscreenclick() is used to bind the on_click function to the left mouse # Create turtle screen
screen = turtle.Screen()
button click event. The on_click function moves the turtle to the clicked
# Set the screen title
position and draws a blue dot. screen.title("Turtle Screen Events")

• onkey() This function is used to bind fun to the key-release event of the # Set the screen click handler

key. In order to be able to register key-events, TurtleScreen must have screen.onscreenclick(on_click)

focus. # Set the key press handler


screen.onkeypress(on_key_press, "space")

• listen() is used to make the screen listen for events. # Listen to screen events
screen.listen()

# Keep the window open


turtle.done()

sambo
24.1.4.4. Input methods
• The two primary input methods in Turtle are textinput() and numinput()
• textinput(title, prompt): This method displays a prompt to the user and waits for the user to enter text. It returns the
entered text as a string.
import turtle
# Get user input as text
user_input = turtle.textinput("Text Input", "Enter your name:")
print("User entered:", user_input)
turtle.done()
• numinput(title, prompt, default=None, minval=None, maxval=None): This method is used to get numeric
input from the user.
import turtle
# Get user input as a number
user_input = turtle.numinput("Number Input", "Enter your age:", default=25, minval=1, maxval=100)
print("User entered:", user_input)
turtle.done()
sambo
24.1.4.5. Settings and special methods

• settings and special methods to control the turtle's behavior and appearance. Some key settings include speed(),
bgcolor(), pencolor(), pensize(), and shape(), allowing you to customize the drawing environment. Special
methods like forward(), backward(), left(), right(), penup(), pendown(), and circle() enable you to control the
turtle's movements and drawing actions.

• Example

sambo
24.1.4.6. Methods specific to Screen, not inherited from TurtleScreen

• Key methods include bgcolor() for setting the background color, title() for setting the window
title, screensize() for adjusting the window dimensions, setup() for configuring the window's
width, height, start position, and coordinates, tracer() for managing animation, and update() for
forcing a screen update.

• Example

sambo
24.1.5. Public classes
24.1.6. Help and configuration
24.1.6.1. How to use help

• help() function provides documentation and explanations about the turtle module’s functions,
methods, and classes. It’s a valuable resource for understanding and learning how to use turtle
graphics effectively.
24.1.6.2. Translation of docstrings into different
languages
Create and write docstring-dictionary to a Python script with the given filename. This function has to be
called explicitly (it is not used by the turtle graphics classes). The docstring dictionary will be written to
the Python script filename.py. It is intended to serve as a template for translation of the docstrings into
different languages.
If you (or your students) want to use turtle with online help in your native language, you have to translate
the docstrings and save the resulting file as e.g. turtle_docstringdict_german.py.

Example:

turtle.write_docstringdict(filename="turtle_docstringdict")

Parameters: filename – a string, used as filename


24.1.6.3. How to configure Screen and Turtles

+ Setting up the Screen Size and Position + Creating and Configuring a Turtle
24.1.7
24.1.7. Demo scripts

• A set of 15 demo articles showing various features of the new Turtle theme. The
Turtledemo.two_canvases example shows how to use two canvases at once with the turtle module.
This directory contains the Turtle.cfg file, as an example of how to create and use these files.

Bunham
24.1.8
24.1.8. Changes since Python 2.6

• A process with this name and this role is available as a Screen process. (Yes, in Python 2.6, this method
is just a duplicate of the related TurtleScreen/Screen method.) The Turtle.fill() method has been
deprecated. Added Turtle.filling() method. This behavior is similar to the parameterless fill() call in
Python 2.6.

Bunham
24.1.9
24.1.9. Changes since Python 3.0

• New Turtle.shearfactor(), Turtle.shapetransform(), and Turtle.get_shapepoly() methods have been


added. A new Screen.onkeypress() method is added to support Screen.onkey(), and its function is bound
to the keyrelease event. New Screen.mainloop() method has been added. So when using Screen and
Turtle you don't have to go through mainloop() again. Two new input methods Screen.textinput() and
Screen.numinput() have been added.

Bunham
THANK YOU!

You might also like