Download presentation
Presentation is loading. Please wait.
Published byDennis Elliott Modified over 8 years ago
1
Drawing!
2
Why Drawing? Drawing is crucial to any graphical application Understanding
3
Window Basics Window size can be specified manually Any part of the window can be drawn on Windows can take user input (mouse events, and keyboard events)
4
The Co-ordinate System This is still a contested topic, though largely because many people are too lazy to change: 1.Old drawing systems matches CRT refresh, (0,0) is top-left, (width,height) is in bottom-right 2.Most new drawing systems use the positive portion of a cartesian plot (0,0) is bottom-left 3.Turtle in Python uses the whole cartesian plot
5
How Does Drawing Occur? Turtle uses something called Vector drawing, it works like human drawing. How do you draw? Get a pen Place the pen somewhere Move the pen and deposit ink
6
Configuring Turtle import turtle turtle.setup(400,500) wn = turtle.Screen() wn.title("Handling keypresses!") smiles = turtle.Turtle()
7
Drawing With Turtle #after the configuration smiles.pencolor("red") smiles.pendown() smiles.forward(100) smiles.right(90) smiles.forward(100)
8
Creating Drawing Functions For more complicated drawings it can be nice to define functions like drawing a complete triangle: def drawTriange(x,y,sideLength): smiles.begin_fill() # fill in the triangle after we finish drawing smiles.penup() smiles.setheading(0) smiles.goto(x,y) smiles.pendown() for i in range(3): smiles.forward(sideLength) smiles.left(120) smiles.end_fill()
9
Coding Break! Let’s do some drawing now, and then we can come back to take a look at handling events
10
What is an Event Handler? It’s something that handles events! More specifically it is a way for your program to respond to input from the user. The input() method allows for “blocking” event handling In many cases you don’t want to force user input at one point, but rather wait for it, and respond to it when it arrives
11
Adding Event Handlers Even handlers require 3 things: A method to be called when an event occurs A registration for that callback method, and event title Activating the window def forwardResponse(): smiles.forward(10) #we can also attach to Left,Right,q,etc… wn.onkey(forwardResponse, “Up”) wn.listen() wn.exitonclick()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.