• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from Tkinter import *
2
3import string
4
5
6class 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)
11
12        ## The playing field
13        self.draw = Canvas(self, width="5i", height="5i")
14
15        ## The speed control for the ball
16        self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
17                           from_=-100, to=100)
18
19        self.speed.pack(side=BOTTOM, fill=X)
20
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
28
29        self.draw.pack(side=LEFT)
30
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
36
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
41
42        self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay)
43        self.after(10, self.moveBall)
44
45    def __init__(self, master=None):
46        Frame.__init__(self, master)
47        Pack.config(self)
48        self.createWidgets()
49        self.after(10, self.moveBall)
50
51
52game = Pong()
53
54game.mainloop()
55