• No results found

Introduction to Programming in Python

N/A
N/A
Protected

Academic year: 2021

Share "Introduction to Programming in Python"

Copied!
34
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction to Programming in Python

Turtle Graphics

Dr. Bill Young

Department of Computer Science University of Texas at Austin

Last updated: June 4, 2021 at 11:44

(2)

Turtle Graphics

Turtle graphics was first developed as part of the children’s

programming language Logo in the late 1960’s. It exemplifies OOP

extremely well. You will be using classes already defined for you.

(3)

Turtle Graphics

Turtles are just Python objects, so you can use any Python constructs in turtle programs:

selection, loops, recursion, etc.

Turtles are objects that move about on a screen (window).

Various methods allow you to direct the turtle’s movement.

The turtle’s tail can be up or down. When it is down, the turtle draws on the screen as it moves.

You can draw some pretty awesome images!

(4)

A Turtle Drawing

(5)

A Turtle Drawing

(6)

A Turtle Drawing: I Drew This One

A version of this picture was published in: William D. Young.

“Modeling and Verification of a Simple Real-Time Gate

Controller,” in Michael Hinchey and Jonathan Bowen, editors,

Applications of Formal Methods, Prentice-Hall Series in Computer

(7)

The Turtle’s Data

Like all Python classes, the turtle class defines data and methods.

The data (state) of the turtle consists of:

Position: denoted by its current x and y coordinates; the units are pixels.

Heading: denoted by an angle in degrees. East is 0 degrees.

north is 90 degrees; west is 180 degrees; south is 270 degrees.

Color: the color can be set to 2

24

(∼ 16.8 million) colors.

Width: the width of the line drawn as the turtle moves (initially 2 pixels).

Down: a Boolean attribute indicating whether the turtle’s

tail is down.

(8)

Coordinate Grid

(9)

Turtle Methods

Many turtle methods are listing in your textbook (pages 81, 83) and online; Google “python turtle graphics.”

t = Turtle() create a new Turtle object and open its window t.home() move the turtle to (0, 0), pointing east

t.pendown() lower the tail (t.down() also works) t.penup() raise the tail (t.up() also works) t.pensize(k) set linewidth to k pixels

t.setheading(d) change heading to direction d t.left(d) turn left d degrees

t.right(d) turn right d degrees

t.speed(n) how fast the turtle moves (0 .. 10)

t.setx(n) set the turtle’s x coordinate, leave y unchanged

t.sety(n) set the turtle’s y coordinate, leave x unchanged

(10)

Turtle Methods

t.forward(n) move in the current direction n pixels t.backward(n) move in the reverse direction n pixels

t.goto(x, y) move to coordinates (x , y )

t.position() return the current position at a tuple (x, y) t.heading() return the current direction (angle)

t.isdown() return True if the pen is down

t.pencolor(r, g, b) change the color to the specified RGB value or named color

t.write(s, font) write a message to the screen (you can specify font

and size, e.g., “font=(’Arial’, 8, normal)”

(11)

Keeping it On Screen

Because the window goes away immediately after the program terminates, it may be hard to see the result unless you delay things. You can use t.done() for that.

t.done() make the screen persist until you close it

The turtle itself will appear on your screen as a small arrowhead.

You can decide whether to show or hide the turtle.

t.hideturtle() make the turtle invisible t.showturtle() make the turtle visible

t.isvisible() return True if the turtle is visible

(12)

A Turtle Function: Draw Square

i m p o r t t u r t l e

def d r a w S q u a r e ( ttl , x , y , l e n g t h ) :

""" D r a w s a s q u a r e u s i n g t u r t l e ttl , w i t h u p p e r l e f t c o r n e r at ( x , y ) , and s i d e of l e n g t h """

ttl . p e n u p () # r a i s e the pen

ttl . g o t o ( x , y ) # m o v e to s t a r t i n g p o s i t i o n ttl . s e t h e a d i n g (0) # p o i n t t u r t l e e a s t

ttl . p e n d o w n () # l o w e r the pen

for c o u n t in r a n g e(4) : # d r a w 4 s i d e s :

ttl . f o r w a r d ( l e n g t h ) # m o v e f o r w a r d l e n g t h ; ttl . r i g h t ( 9 0 ) # t u r n r i g h t 90 d e g r e e s

ttl . p e n u p () # r a i s e the pen

Bob = t u r t l e . T u r t l e () # our t u r t l e is n a m e d Bob Bob . s p e e d ( 1 0 ) # m a k e Bob c r a w l f a s t Bob . p e n s i z e (3) # l i n e w i d t h of 3 p i x e l s d r a w S q u a r e ( Bob , 0 , 0 , 100 ) # d r a w a s q u a r e at (0 ,0)

# w it h s i d e l e n g t h 100 t u r t l e . d o n e () # k e e p d r a w i n g s h o w i n g

(13)

What the Turtle Drew

(14)

Colors

I’m not sure this works on all versions of turtle graphics.

Colors are in the RGB system, using a triple: (R, G , B). Each element in the triple is an intensity from 0 to 255, indicating the contribution of R (red), G (green), and B (blue). For example:

black (0,0,0) red (255,0, 0) green (0, 255, 0)

blue (0, 0, 255) gray (127, 127, 127) white (255, 255, 255) burnt orange (255, 125, 25)

This is a nice website that allows you to find the RGB values for various colors: www.colorschemer.com/color-picker.

The named Python colors can be found here:

(15)

Color Wheel

(16)

Colors

Turtles have two “colormodes” and you’ll get an error if you try to do some things in the wrong mode. The modes are 1.0 and 255.

In mode 255, use triples of range 0 ≤ c ≤ 255. In mode 1, use triples (percentages) in range 0 . . . 1.

> > > t = T u r t l e ()

> > > t . p e n c o l o r (127 , 127 , 1 2 7 ) T r a c e b a c k ( m o s t r e c e n t c a l l l a s t ) :

F i l e " < stdin > " , l i n e 1 , in < module >

. . . .

r a i s e T u r t l e G r a p h i c s E r r o r ( " bad c o l o r s e q u e n c e : % s " % str ( c o l o r ) )

t u r t l e . T u r t l e G r a p h i c s E r r o r : bad c o l o r s e q u e n c e : (127 , 127 , 1 2 7 )

> > > t . p e n c o l o r (0.5 , 0.5 , 0 . 5 )

> > > t . s c r e e n . c o l o r m o d e ( 2 5 5 )

> > > p r i n t ( t . s c r e e n . c o l o r m o d e () ) 255

> > > t . p e n c o l o r (127 , 127 , 1 2 7 )

> > > t . s c r e e n . c o l o r m o d e (1)

(17)

Another Way

If you’re in colormode 255, you can also specify the color with a hex string value.

For example, the color Burnt Orange can be specified with the RGB decimal triple: (255, 125, 25).

You can also specify it with the hex string value: ’#FF7D19’

because 255 is hex (base 16) value FF, 125 is hex value 7D, and 25 is hex value 19.

> > > ttl . s c r e e n . c o l o r m o d e ( 2 5 5 )

> > > ttl . p e n c o l o r (255 , 125 , 25)

> > > p r i n t( ttl . p e n c o l o r () ) (255.0 , 125.0 , 2 5 . 0 )

> > > ttl . p e n c o l o r ( ’ # F F 7 D 1 9 ’ )

> > > p r i n t( ttl . p e n c o l o r () ) (255.0 , 125.0 , 2 5 . 0 )

(18)

Circles

You can draw circles, arcs, and dots using these functions:

t.circle(r, ext, step) draw a circle with radius r, ext (arc of circle drawn; 360 is entire circle), step (number of segments).

t.dot(d, color) draw a filled circle with diameter r and color Note: the circle is not centered at the starting point. If you want that you could write:

def c e n t e r e d C i r c l e ( ttl , r , x , y ) :

""" D r a w a c i r c l e w i t h r a d i u s r c e n t e r e d at ( x , y ) . """

ttl . up () # r a i s e the pen

a n g l e = ttl . h e a d i n g () # s a v e the c u r r e n t h e a d i n g ttl . s e t h e a d i n g (0) # set h e a d i n g e a s t

ttl . g o t o ( x , y - r ) # m o v e to b o t t o m of c i r c l e

ttl . d o w n () # pen d o w n

ttl . c i r c l e ( r ) # d r a w the c i r c l e

ttl . up () # pen up

(19)

Circles

def d r a w S o m e C i r c l e s ( ttl ) : ttl . s p e e d ( 1 0 )

ttl . p e n s i z e (3) # l i n e is 3 p i x e l s ttl . up ()

ttl . h o m e () # go to (0 , 0)

ttl . d o w n ()

ttl . p e n c o l o r ( ’ G r e e n ’ )

ttl . c i r c l e ( 2 5 ) # rad . 25 p i x e l s ttl . up ()

ttl . g o t o (0 , 0) ttl . p e n c o l o r ( ’ Red ’ ) ttl . d o w n ()

ttl . c i r c l e ( 5 0 , 1 8 0 ) # arc 180 deg . ttl . up ()

ttl . g o t o (0 , 0) ttl . p e n c o l o r ( ’ B l u e ’ ) ttl . d o w n ()

ttl . c i r c l e (75 ,360 ,8) # o c t o g o n ttl . up ()

Sam = t u r t l e . T u r t l e () d r a w S o m e C i r c l e s ( Sam )

(20)

More with Circles

def t a n g e n t C i r c l e s ( ttl ) :

""" P r i n t 10 t a n g e n t c i r c l e s . """

r = 10 # i n i t i a l r a d i u s n = 10 # c o u n t of c i r c l e s for i in r a n g e(1 , n + 1 , 1) :

ttl . c i r c l e ( r * i )

def c o n c e n t r i c C i r c l e s ( ttl ) :

""" P r i n t 10 c o n c e n t r i c c i r c l e s . """

r = 10 # i n i t i a l r a d i u s for i in r a n g e( 1 0 ) :

ttl . c i r c l e ( r * i ) ttl . up ()

ttl . s e t y (( r * i ) *( -1) ) ttl . d o w n ()

Ben = t u r t l e . T u r t l e () Ben . up () ; Ben . g o t o (0 , 1 5 0 ) Ben . d o w n () ; Ben . p e n c o l o r ( ’ B l u e ’ ) t a n g e n t C i r c l e s ( Ben )

Ben . up () ; Ben . g o t o (0 , -150)

(21)

Fill Areas

If you draw a closed region, you can fill it with a specified color:

t.fillcolor() sets the pen fill color

t.begin fill() call this before filling a shape t.end fill() call to no longer keep filling

t.filling() return True if filling, False otherwise

(22)

Drawing a Chessboard

def m a y b e F i l l S q u a r e ( ttl , x , y , lngth , fill , c o l o r ) :

""" B o o l e a n p a r a m e t e r f i l l s a y s w h e t h e r to f i l l . """

if f i l l :

ttl . f i l l c o l o r ( c o l o r ) ttl . b e g i n _ f i l l ()

d r a w S q u a r e ( ttl , x , y , l n g t h ) ttl . e n d _ f i l l ()

e l s e:

d r a w S q u a r e ( ttl , x , y , l n g t h )

def d r a w C h e s s b o a r d ( ttl , x , y , s q u a r e s i z e ) :

""" D r a w a t e a l and w h i t e c h e s s b o a r d . """

f i l l = T r u e

for j in r a n g e( 8 ) : for i in r a n g e( 8 ) :

x1 = x + i * s q u a r e s i z e y1 = y - j * s q u a r e s i z e

m a y b e F i l l S q u a r e ( ttl , x1 , y1 , s q u a r e s i z e , fill , ’ t e a l ’ )

f i l l = not f i l l f i l l = not f i l l

(23)

Our Chessboard

I don’t know why those weird lines are in there. They don’t show

up on the screen.

(24)

Sierpinski Curve

(25)

Some Complex Stuff: Sierpinski Curve

def o n e S i d e ( ttl , s , diag , l e v e l ) : if ( l e v e l == 0) :

r e t u r n e l s e:

o n e S i d e ( ttl , s , diag , l e v e l - 1 )

ttl . r i g h t ( 4 5 ) ; ttl . f o r w a r d ( d i a g ) ; ttl . r i g h t ( 4 5 ) o n e S i d e ( ttl , s , diag , l e v e l - 1 )

ttl . l e f t ( 9 0 ) ; ttl . f o r w a r d ( s ) ; ttl . l e f t ( 9 0 ) o n e S i d e ( ttl , s , diag , l e v e l - 1 )

ttl . r i g h t ( 4 5 ) ; ttl . f o r w a r d ( d i a g ) ; ttl . r i g h t ( 4 5 ) o n e S i d e ( ttl , s , diag , l e v e l - 1 )

def s i e r p i n s k i ( ttl , s , l e v e l ) : d i a g = s / m a t h . s q r t (2) for i in r a n g e(4) :

o n e S i d e ( ttl , s , diag , l e v e l ) ttl . r i g h t ( 4 5 )

ttl . f o r w a r d ( d i a g ) ttl . r i g h t ( 4 5 )

(26)

Some Complex Stuff: Fractal Triangles

(27)

Fractal Triangles

def d r a w O u t w a r d T r i a n g l e s ( ttl , s iz e ) : if s i z e < 10:

r e t u r n

for i in r a n g e( 3 ) :

ttl . f o r w a r d ( s i z e / 2 ) i n s e r t ( ttl , s i z e ) ttl . f o r w a r d ( s i z e / 2 ) ttl . r i g h t ( 120 )

def i n s e r t ( ttl , s i z e ) : ttl . l e f t ( 120 )

d r a w O u t w a r d T r i a n g l e s ( ttl , s i z e / 2 ) ttl . r i g h t ( 120 )

Ken = t u r t l e . T u r t l e () Ken . c o l o r ( " bl u e " )

d r a w O u t w a r d T r i a n g l e s ( Ken , 200 )

(28)

Saving Your Picture

Python saves your picture as a postscript file, but you can convert it. To save your picture as a jpeg, do the following:

f r o m PIL i m p o r t I m a g e

def s a v e _ a s _ j p g ( canvas , f i l e N a m e ) :

# s av e p o s t s c i p t i m a g e

c a n v a s . p o s t s c r i p t (f i l e = f i l e N a m e + ’ . eps ’ )

# use PIL to c o n v e r t to J P EG

img = I m a g e .o p e n( f i l e N a m e + ’ . eps ’ ) img . s a v e ( f i l e N a m e + ’ . j p e g ’ , ’ j p e g ’ )

< Y o u r d r a w i n g f u n c t i o n s >

ts = t u r t l e . g e t s c r e e n () tc = ts . g e t c a n v a s ()

# c r e a t e s a p o s t s c r i p t i m a g e f i l e

# s u b s t i t u t e y o u r own f i l e n a m e tc . p o s t s c r i p t (f i l e= " f i l e n a m e . eps " )

# c o n v e r t s to J P E G

s a v e _ a s _ j p g ( tc , " f i l e n a m e " )

(29)

Seychelles Flag

(30)

Drawing the Seychelles Flag

# T hi s d r a w s the f la g of the S e y c h e l l e s i s l a n d s :

i m p o r t t u r t l e

f r o m PIL i m p o r t I m a g e

def s a v e _ a s _ j p g ( canvas , f i l e N a m e ) :

# s am e as b e f o r e ...

def d r a w R e c t a n g l e ( ttl , x , y , width , h e i g h t ) :

""" D r a w a r e c t a n g l e of d i m e n s i o n s w i d t h and height , w i t h u p p e r l e f t c o r n e r at ( x , y ) . """

ttl . up () ttl . g o t o ( x , y ) ttl . s e t h e a d i n g (0) ttl . d o w n ()

for i in r a n g e(2) : ttl . f o r w a r d ( w i d t h ) ttl . r i g h t ( 9 0 ) ttl . f o r w a r d ( h e i g h t ) ttl . r i g h t ( 9 0 )

(31)

Drawing the Seychelles Flag

def d r a w T r i a n g l e ( ttl , x1 , y1 , x2 , y2 , x3 , y3 ) : ttl . p e n u p ()

ttl . g o t o ( x1 , y1 ) ttl . p e n d o w n () ttl . g o t o ( x2 , y2 ) ttl . g o t o ( x3 , y3 ) ttl . g o t o ( x1 , y1 ) ttl . p e n u p ()

def f i l l T r i a n g l e ( ttl , x1 , y1 , x2 , y2 , x3 , y3 , c o l o r ) :

# T hi s a s s u m e s c o l o r is g i v e n as a Hex s t r i n g v a l u e . ttl . f i l l c o l o r ( c o l o r )

ttl . b e g i n _ f i l l ()

d r a w T r i a n g l e ( ttl , x1 , y1 , x2 , y2 , x3 , y3 ) ttl . e n d _ f i l l ()

(32)

Drawing the Seychelles Flag

# set up the s c r e e n si z e ( in p i x e l s - 1 0 0 0 x 1 0 0 0 )

# set the s t a r t i n g p o i n t of the t u r t l e (0 , 0) t u r t l e . s e t u p (1500 , 1000 , 0 , 0)

m y B l u e = ’ # 0 0 3 8 8 2 ’ m y Y e l l o w = ’ # F C D 6 4 7 ’ m y R e d = ’ # D 1 2 4 2 1 ’ m y G r e e n = ’ # 0 0 7 3 3 6 ’ m y W h i t e = ’ # F F F F F F ’ Joe = t u r t l e . T u r t l e () Joe . s c r e e n . c o l o r m o d e ( 2 5 5 )

d r a w R e c t a n g l e ( Joe , 0 , 300 , 600 , 300 ) Joe . g o t o (0 , 0)

# d ra w b l u e t r i a n g l e

f i l l T r i a n g l e ( Joe , 0 , 0 , 0 , 300 , 200 , 300 , m y B l u e )

# d ra w y e l l o w t r i a n g l e

f i l l T r i a n g l e ( Joe , 0 , 0 , 200 , 300 , 400 , 300 , m y Y e l l o w )

# d ra w red t r i a n g l e

f i l l T r i a n g l e ( Joe , 0 , 0 , 400 , 300 , 600 , 300 , m y R e d )

# d ra w w h i t e t r i a n g l e

f i l l T r i a n g l e ( Joe , 0 , 0 , 600 , 300 , 600 , 150 , m y W h i t e )

# d ra w g r e e n t r i a n g l e

(33)

Drawing the Seychelles Flag

Joe . h i d e t u r t l e ()

ts = t u r t l e . g e t s c r e e n () tc = ts . g e t c a n v a s ()

# c r e a t e s a p o s t s c r i p t i m a g e f i l e

# s u b s t i t u t e y o u r own f i l e n a m e

tc . p o s t s c r i p t (f i l e= " S e y c h e l l e s F l a g . eps " )

# c o n v e r t s to J P E G

s a v e _ a s _ j p g ( tc , " S e y c h e l l e s F l a g " ) t u r t l e . d o n e ()

(34)

Some Sample Projects

Write Turtle graphics functions that will do the following:

1

draw a cube;

2

draw a regular polygon with k sides and radius r (distance from center to one of the vertices);

3

draw m concentric circles, varying the color as you go outward;

4

draw the flag of some country;

5

draw a building.

References

Related documents

of them, allow's purchase this book Python Programming For Beginners: An Introduction To The Python Computer Language And Computer Programming By Jason Cannon by downloading and

library for the Python programming language defines an interface to usual processing and data analysis methods to be applied on aircraft trajectories and airspaces3.

This video will help you setup Python Environment in your computer and write your first Python program.. Spotle.ai

  This doesn't mean that Python can only work with numbers..   But numbers are the most primitive kind of data we work with, so we

 Like an abstract data type, a class encapsulates both state (variables) and behavior (methods)..  Unlike abstract data types, classes can be defined in terms of other classes

We monitored reptile populations of softshell turtle (Amyda cartilaginea Boddaert, 1770) and reticulated python (Python reticulatus Schneider, 1801) by visiting and interviewing

➢ Python supports object-oriented and function-based programming while Java supports object-oriented programming only.. ➢ Python is simpler than Java, and typically applies to

Intro to Python basics, PythonWin development environment, data structures, ArcGIS API, decision making, looping. • 2 nd Quarter EXAM I