Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame^] | 1 | from Tkinter import * |
| 2 | |
| 3 | import string |
| 4 | |
| 5 | |
| 6 | class Pong(Frame): |
| 7 | def createWidgets(self): |
| 8 | self.QUIT = Button(self, {'text': 'QUIT', |
| 9 | 'fg': 'red', |
| 10 | 'command': self.quit}) |
| 11 | self.QUIT.pack({'side': 'left', 'fill': 'both'}) |
| 12 | |
| 13 | ## The playing field |
| 14 | self.draw = Canvas(self, {"width" : "5i", "height" : "5i"}) |
| 15 | |
| 16 | ## The speed control for the ball |
| 17 | self.speed = Scale(self, {"orient": "horiz", |
| 18 | "label" : "ball speed", |
| 19 | "from" : -100, |
| 20 | "to" : 100}) |
| 21 | |
| 22 | self.speed.pack({'side': 'bottom', "fill" : "x"}) |
| 23 | |
| 24 | # The ball |
| 25 | self.ball = self.draw.create_oval("0i", "0i", "0.10i", "0.10i", {"fill" : "red"}) |
| 26 | self.x = 0.05 |
| 27 | self.y = 0.05 |
| 28 | self.velocity_x = 0.3 |
| 29 | self.velocity_y = 0.5 |
| 30 | |
| 31 | self.draw.pack({'side': 'left'}) |
| 32 | |
| 33 | |
| 34 | def moveBall(self, *args): |
| 35 | if (self.x > 5.0) or (self.x < 0.0): |
| 36 | self.velocity_x = -1.0 * self.velocity_x |
| 37 | if (self.y > 5.0) or (self.y < 0.0): |
| 38 | self.velocity_y = -1.0 * self.velocity_y |
| 39 | |
| 40 | deltax = (self.velocity_x * self.speed.get() / 100.0) |
| 41 | deltay = (self.velocity_y * self.speed.get() / 100.0) |
| 42 | self.x = self.x + deltax |
| 43 | self.y = self.y + deltay |
| 44 | |
| 45 | self.draw.move(self.ball, `deltax` + "i", `deltay` + "i") |
| 46 | self.after(10, self.moveBall) |
| 47 | |
| 48 | |
| 49 | |
| 50 | def __init__(self, master=None): |
| 51 | Frame.__init__(self, master) |
| 52 | Pack.config(self) |
| 53 | self.createWidgets() |
| 54 | self.after(10, self.moveBall) |
| 55 | |
| 56 | |
| 57 | game = Pong() |
| 58 | |
| 59 | game.mainloop() |