Download presentation
Presentation is loading. Please wait.
1
Using the coords function (Coordinates)
Make it bounce Using the coords function (Coordinates)
2
Adding to our __init__ function
At the end of our __init__ function, add the following: self.x = 0 self.y = -1 self.canvas_height = self.canvas.winfo_height( ) The self.x and self.y do what you think it may do. They initialize the X and Y position of the ball. Don’t forget: The __ init__ function has two (2) underscores on either side of the init. This is a common function used in many different programming languages. Setting the object x and y as 0 and -1 will help with the position of the ball. The last line using the winfo_height function, returns the height of the canvas.
3
Changing our draw function
Change the draw function to the following: def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) if pos[1] <= 0: self.y = 1 if pos[3] > self.canvas_height: self.y = -1
4
Can you see why we set the main loop as “While 1”:
Can you see why we set the main loop as “While 1”: ? What do you think we asked our program to do with our updated draw function? Creating a variable pos (position), by calling the coords function will give us x and y coordinates of anything drawn to the canvas (as long as we know its object ID). We do know the object ID, as the coordinates are passed to the object variable id. Boolean statements use either True or False questions. T/F is represented as 1/0 – where 0 is false and 1 is true. So “While 1” is the same as “While True”.
5
print(self.canvas.coords(self.id))
If we were to print our coordinates… print(self.canvas.coords(self.id)) we would get something like this: [255, 29, 270, 44] What we have changed in our draw function: If the Y1 coordinate (the top of the ball) were to be less than or equal to 0, the Y object is set to 1 (telling the ball to stop moving). If the Y2 coordinate (the bottom of the ball) is greater than or equal to the canvas_height, the Y object is set back to -1
6
Bouncing Ball from tkinter import * import random import time
If we do nothing else right now, the ball should just ‘bounce’ up and down in the same horizontal position. (try it out and see!) from tkinter import * import random import time 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) self.x = 0 self.y = -1 self.canvas_height = self.canvas.winfo_height() def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) if pos[1] <= 0: self.y = 1 if pos[3] >= self.canvas_height: from tkinter import * import random import time 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) self.x = 0 self.y = -1 self.canvas_height = self.canvas.winfo_height() def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) if pos[1] <= 0: self.y = 1 if pos[3] >= self.canvas_height:
7
Bouncing Ball tk = Tk() tk.title("Game") tk.resizable(0, 0) tk.wm_attributes("-topmost", 1) canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) canvas.pack() tk.update() ball = Ball(canvas, 'red') while 1: ball.draw() tk.update_idletasks() time.sleep(0.01) tk = Tk() tk.title("Game") tk.resizable(0, 0) tk.wm_attributes("-topmost", 1) canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) canvas.pack() tk.update() ball = Ball(canvas, 'red') while 1: ball.draw() tk.update_idletasks() time.sleep(0.01)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.