Download presentation
Presentation is loading. Please wait.
Published byGarry Bell Modified over 8 years ago
1
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program is running, such as pressing a key, moving the mouse. Events:,, canvas.bind_all(‘ ’, function)
2
Graph Identifier The number returned from create_ function: mytriangle=canvas.create_polygon(10,10,10,60,50,35) canvas.move(mytrangle, 5, 0) canvas.itemconfig(mytriangle, fill=‘blue’, outline=‘red’)
3
Bounce! Game
4
Create The Game Canvas from tkinter import * import random import time 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()
5
Create the Ball Class canvas.create_oval(x0,y0, x1,y1,fill=color)
6
Create the Ball Class from tkinter import * import random import time u class Ball: v def __init__(self, canvas, color): w self.canvas = canvas x self.id = canvas.create_oval(10, 10, 25, 25, fill=color) y self.canvas.move(self.id, 245, 100) def draw(self): pass
7
Change draw Function Make the ball move up and vanish: def draw(self): self.canvas.move(self.id, 0,-1) ball =Ball(canvas, 'red') while 1: ball.draw() tk.update_idletasks() tk.update() time.sleep(0.01)
8
Change draw function Make the ball bounce: 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: self.y=-1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.