Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Demo / tkinter / matt / pong-demo-1.py
blobdacaa38272656b484d27ce0b44fc806c336b3fd6
1 from Tkinter import *
3 import string
6 class Pong(Frame):
7 def createWidgets(self):
8 self.QUIT = Button(self, text='QUIT', foreground='red',
9 command=self.quit)
10 self.QUIT.pack(side=LEFT, fill=BOTH)
12 ## The playing field
13 self.draw = Canvas(self, width="5i", height="5i")
15 ## The speed control for the ball
16 self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
17 from_=-100, to=100)
19 self.speed.pack(side=BOTTOM, fill=X)
21 # The ball
22 self.ball = self.draw.create_oval("0i", "0i", "0.10i", "0.10i",
23 fill="red")
24 self.x = 0.05
25 self.y = 0.05
26 self.velocity_x = 0.3
27 self.velocity_y = 0.5
29 self.draw.pack(side=LEFT)
31 def moveBall(self, *args):
32 if (self.x > 5.0) or (self.x < 0.0):
33 self.velocity_x = -1.0 * self.velocity_x
34 if (self.y > 5.0) or (self.y < 0.0):
35 self.velocity_y = -1.0 * self.velocity_y
37 deltax = (self.velocity_x * self.speed.get() / 100.0)
38 deltay = (self.velocity_y * self.speed.get() / 100.0)
39 self.x = self.x + deltax
40 self.y = self.y + deltay
42 self.draw.move(self.ball, `deltax` + "i", `deltay` + "i")
43 self.after(10, self.moveBall)
45 def __init__(self, master=None):
46 Frame.__init__(self, master)
47 Pack.config(self)
48 self.createWidgets()
49 self.after(10, self.moveBall)
52 game = Pong()
54 game.mainloop()