We are going to make a paddle ball
We are going to make a “paddle ball” game • Before we do, we need to learn a few concepts. One of the biggest new concepts we will learn is called Object Orientation • I have typed in the notes section of these slides, so for more information, please read the notes. Either View tab Notes page or… select the notes button at the bottom of the slide (if not in a slideshow)
Drawing Shapes Making shapes using tkinter
Using tkinter • Tkinter is a graphics module (like time, turtle, random…) • It is designed to work faster and better than the turtle module, which is designed to teach and practice using graphics (it’s basic). • Just like the time, random, and turtle modules, we have to import it before using.
We need a canvas! Import tkinter: from tkinter import * tk = Tk( ) canvas = Canvas(tk, width=500, height = 500) canvas. pack( )
Let’s make a box • We are going to begin by making a box ( a rectangle) • Now that we have a canvas, we will use a create_polygon function which will draw a shape using parameters. • If a triangle uses 6 parameters: canvas. create_polygon(10, 10, 60, 50, 35) • How many parameters do you think a rectangle will use?
Get rect[angle] newb • That was for Cole & Dustin • Begin by creating a canvas to draw our rectangle on: from tkinter import * tk = Tk( ) canvas = Canvas(tk, width = 500, height = 500) Coordinates for top left canvas. pack( ) and bottom right corners canvas. create_rectangle(10, 50, 300)
Plain rectangles are boring • Let’s add some color by filling them in. Same code as before, but with one modification: canvas. create_rectangle(10, 50, 300. fill = ‘green’) Now what has happened? Yes! A green rectangle.
Pick a color, any color We can use the color selector (just like in Word) to select a color to fill our rectangle with. This is called the colorchooser Add to your code: (before the create rectangle) colorchooser. askcolor( )
Use the color chooser in fill c = colorchooser. askcolor( ) canvas. create_rectangle(10, 50, 300, fill=c[1]) * When the color is selected, a tuple is created with the red, green, and blue value amounts as well as the hexadecimal value for all three. The [1] index selects the hexadecimal value.
More color options! • We can change the color of our object by configuring it. canvas. itemconfig(object, fill = ‘color’) • Our object is 1, because it is the object ID • We could give our shape a variable name and use that as well.
What we can also change: fill = ‘color’ outline = ‘color’ * The outline and fill color do not have to be the same colors
Arcs Arc – a segment of the circumference of a circle • tkinter actually draws an arc inside of a rectangle. • When we create an arc, we use coordinates just like a rectangle. • We also use style and extent arguments
Joakim Noah – Chicago Bulls Noah’s Arc
1 st set of coordinates (10, 10) Extent – the degrees of the angle of the arc 2 nd set of coordinates (200, 100) Style – its an arc
Creating an arc Canvas. create_arc(10, 200, 100, extent = 180, style = ARC
Arc angle measurements tkinter considers 360 o as the same as 0 o, For a full circle, we’ll use 359 o
- Slides: 17