Download presentation
Presentation is loading. Please wait.
Published byStewart Gray Modified over 6 years ago
1
Paddle Ball! We will begin creating, in steps, a video game similar to ‘brick breaker’ or ‘pong’, were we can move paddles to hit a bouncing ball. I hope creating this game will be a fun activity for you, and a way for you to take what you have learned in programming to the next level. Once our game is completed, you will modify it to create a custom version of your own.
2
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) The previous PowerPoint went into detail about classes and objects – “Object Oriented Programming”. If you are still unclear, that is fine. Feel free to go back and review the video and the notes. Understanding this new concept will help us understand why we are doing what we are doing in this activity.
3
Making shapes using tkinter
Drawing Shapes Making shapes using tkinter
4
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. Brand new, just go with it. We will import it just like we did with the time module and turtle modules.
5
We need a canvas! Import tkinter: from tkinter import * tk = Tk( ) canvas = Canvas(tk, width=500, height = 500) canvas.pack( ) In order to draw a shape, we need to first have something to draw on. We create a ‘canvas’ to draw on. Just like an artist would. Yes, Brie and Skye, I know that not all artists have to paint on canvases, one could use any variety of mediums. Our parameters for our canvas are setting the width and height. The canvas.pack function just tells the canvas to “show up”. Without this, the canvas won’t appear and thus neither will our shapes we are drawing! If you were to read this code from top to bottom (how else would one read it???) It would go something like this: Hey, I’m going to use the tkinter graphics tool! I’m going to use all that it has to offer (the * means “import everything”) again, thinking of a library like a suitcase, you would here unpack everything instead of only taking out a pair of jeans or one item at a time. I’m going to create a shortcut variable for the function Tk(), so I don’t have to type that out each time. Imma make a 500x 500 canvas I want that canvas to show up on my screen [the canvas.pack just tells it to display on our screen]
6
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, 10, 60, 50, 35) How many parameters do you think a rectangle will use? Creating a triangle, we used the create_polygon function. The parameters of the function are pairs of coordinates. Beginning with bottom left we have 10, 10 (x and y values), then bottom right (10,60), and then the top (50, 35). You could plot this yourself on a Cartesian plane, but here the program is doing it for us. For a rectangle, one might think our rectangle should use 4 sets of coordinates, which would be 8 numbers all separated by commas. However, since a rectangle is a square, we only need the top right and bottom left coordinates, so two sets of coordinates.
7
Get rect[angle] newb from tkinter import *
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) canvas.pack( ) canvas.create_rectangle(10, 10, 50, 300) Sorry for the bad pun. Hey, teachers have to have fun too you know. I would be a very dry teacher if I didn’t enjoy my job! Remember: Our parameters for our canvas are setting the width and height. The canvas.pack function just tells the canvas to “show up”. Without this, the canvas won’t appear and thus neither will our shapes we are drawing! If you were to read this code from top to bottom : Hey, I’m going to use the tkinter graphics tool! I’m going to use all that it has to offer (the * means “import everything”) again, thinking of a library like a suitcase, you would here unpack everything instead of only taking out a pair of jeans or one item at a time. I’m going to create a shortcut variable for the function Tk(), so I don’t have to type that out each time. I am going to make a 500x 500 canvas. I want that canvas to show up on my screen I'm going to create a rectangle the top left corner is at coordinates 10, 10 and the bottom right corner is at corner 50, 300 Coordinates for top left and bottom right corners
8
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, 10, 50, 300. fill = ‘green’) Now what has happened? Yes! A green rectangle. We have included a new parameter or argument for our function. When we create our functions, we can add a parameter to alter or change what we have created. Now, we should have a green rectangle. Cool, huh?
9
Pick a color, any color colorchooser.askcolor( )
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( ) Adding this function allows us to first, set up our function that will allow us to choose a color, then we will include this as a parameter in our create_polygon function.
10
Use the color chooser in fill
c = colorchooser.askcolor( ) canvas.create_rectangle(10, 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. A tuple (pronounced: ‘too-pull’) is a list that cannot be changed (unmutable). Remember when we created our lists before: Example: listofnames = [‘Robert’, ‘Jessica’, ‘Brinay’, ‘Dre’, ‘Ed’, ‘Shelly’] in this list, we could rearrange, add, delete or change the elements of our list. A tuple would look like this: listofnames = (‘Robert’, ‘Jessica’, ‘Brinay’, ‘Dre’, ‘Ed’, ‘Shelly’) The tuple is set the same way, but has parentheses rather than brackets. Just know that a tuple is a list that can’t be changed…
11
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. Notice that in the canvas.itemconfig(object, fill = ‘color’) line, Object which is what we are creating, is listed as one of the arguments, or parameters of the function itemconfig. This is done because we are configuring our object [it is naming itself]. Think of it this way, if you were to create a function called “Comb hair” (yes, imagining that we were living in a computer program… just stay with me for a bit here…) you would want to know who’s hair to comb. In this example, you would have something like combHair(self, time). Meaning you will comb (your) hair for (x time). Many object oriented programs for Python will name themselves as a parameter. Its just a personality quirk of Python. Don’t worry about it too much, just know that is what it does. This is just a way for a function to call another function.
12
What we can also change:
fill = ‘color’ outline = ‘color’ * The outline and fill color do not have to be the same colors
13
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 Now that we know how to draw a shape and fill in the color, we can also draw an arc (Curved line)
14
Noah’s Arc Joakim Noah – Chicago Bulls Another pun. Sorry again.
15
Extent – the degrees of the angle of the arc
1st set of coordinates (10, 10) 2nd set of coordinates (200, 100) Extent – the degrees of the angle of the arc Style – its an arc
16
Creating an arc Canvas.create_arc(10, 10, 200, 100, extent = 180, style = ARC
17
Arc angle measurements
tkinter considers 360o as the same as 0o, For a full circle, we’ll use 359o
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.