Download presentation
Presentation is loading. Please wait.
1
Let’s begin our game!
2
Process Creating the game canvas Add a bouncing ball Add the paddle
3
Game Canvas from tkinter import * import random import time tk = Tk( )
# The tk.title - gives our window a title # The resizable makes window a fixed size (0,0) means cannot change either horizontally or vertically. # *Topmost - makes the window appear on top of all other windows from tkinter import * import random import time tk = Tk( ) tk.title(“Game”) tk.resizable(0, 0) tk.wm_attributes(“-topmost”, 1)
4
Game Canvas The first line is actually one line… just too much to display on one slide. The width and height we know already, but the bd and highlightthickness are both set to zero so The canvas.pack( ) tells the canvas to resize itself according to the width and height parameters given earlier. The tk.update is very important as it tells tkinter to initialize itself, preparing it for our animation. canvas=Canvas(tk, width = 500, height = 400, bd = 0, highlightthickness = 0) canvas.pack( ) tk.update ( ) canvas=Canvas(tk, width = 500, height = 400, bd = 0, highlightthickness = 0) canvas.pack() tk.update ()
5
Ball Class class Ball: def __init__(self, canvas, color):
The first function in the Ball class is the initialize function. When we want to set some properties to use later, we first get it ready by initializing it. *There are two (2) underscore lines on each side of the init function The self.id is setting up the “ball” we will use for the game. It is being created as an oval. Notice we pass the color into the circle. *this also stores the object ID as the oval. Remember the (1, 5, 0) of our triangle? The one (1) was the object ID. class Ball: def __init__(self, canvas, color): self.canvas = canvas self.id = canvas.create_oval(10,10,25,25, fill = color) self.canvas.move(self.id, 245, 100) def draw(self): pass Under def draw(self): we have the word pass. Just like if the teacher were to call on you in class for an answer and you were to reply “pass”. It means ‘I’ve got nothing now’. But just like in school, where the teacher may (and most likely will) come back to you, we will replace “pass” for more code.
6
Ball Class continued… We also have just set up the draw function, but have used the pass statement to not have it do anything just yet. Now, let’s create our red ball We create an object, ball of the Ball class. Here is where the object – class concept comes in to play. If you want to review, look at the “0 – Class objects” PowerPoint. But to quickly review… An object is simply a function; a way to create code that ‘does something’. A class is a way to organize those functions (objects) into groups. If you are wondering why create classes, then good! That means you are thinking . Classes can pass functions (also called ‘methods’) to one another. It is a sort of family relationship. In fact, classes can have a parent class and a child class… Just think of this as levels. But just as at home, one may be able to borrow clothes from their parents or a sibling; objects within a class family can share funtions. ball = Ball(canvas, ‘red’) The class describes what ‘it’ can do, but the object is what actually does it.
7
Main loop We will now create an animation loop. The main loop is the central part of the program and controls most of what happens. Our loop will tell tkinter to run forever and constantly redraw the screen, then sleep for one hundredth of a second. while 1: tk.update_idletasks( ) tk.update( ) time.sleep(0.01)
8
Making the ball move Let’s go back to our draw function, and replace the pass statement with some code. * Our __init__ function saved the canvas parameter as the object: canvas. We will use this with self.canvas and then call up our move function on the canvas object. Here is where we replace the pass statement. We are going to have the circle (oval) call itself [self.id] and tell itself to not move horizontally (X axis) and to move up one pixel on the screen. *** Remember the coordinate plane system we use is sort of flipped from the Cartesian plane we grew up with in Geometry and algebra. (0,0) is the top left of the screen. So -1 (Y axis) means move up one pixel. def draw(self): self.canvas.move(self.id, 0, -1) The parameters are: the ID for our oval, and 0 and -1. The zero means the ball does not move horizontally The -1 means move one pixel up on the screen.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.