| 
									
										
										
										
											2009-01-04 18:53:28 +00:00
										 |  |  | from tkinter import * | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Pong(Frame): | 
					
						
							|  |  |  |     def createWidgets(self): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         self.QUIT = Button(self, text='QUIT', foreground='red', | 
					
						
							|  |  |  |                            command=self.quit) | 
					
						
							|  |  |  |         self.QUIT.pack(side=LEFT, fill=BOTH) | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         ## The playing field | 
					
						
							|  |  |  |         self.draw = Canvas(self, width="5i", height="5i") | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         ## The speed control for the ball | 
					
						
							|  |  |  |         self.speed = Scale(self, orient=HORIZONTAL, label="ball speed", | 
					
						
							|  |  |  |                            from_=-100, to=100) | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         self.speed.pack(side=BOTTOM, fill=X) | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         # The ball | 
					
						
							|  |  |  |         self.ball = self.draw.create_oval("0i", "0i", "0.10i", "0.10i", | 
					
						
							|  |  |  |                                           fill="red") | 
					
						
							|  |  |  |         self.x = 0.05 | 
					
						
							|  |  |  |         self.y = 0.05 | 
					
						
							|  |  |  |         self.velocity_x = 0.3 | 
					
						
							|  |  |  |         self.velocity_y = 0.5 | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         self.draw.pack(side=LEFT) | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def moveBall(self, *args): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         if (self.x > 5.0) or (self.x < 0.0): | 
					
						
							|  |  |  |             self.velocity_x = -1.0 * self.velocity_x | 
					
						
							|  |  |  |         if (self.y > 5.0) or (self.y < 0.0): | 
					
						
							|  |  |  |             self.velocity_y = -1.0 * self.velocity_y | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         deltax = (self.velocity_x * self.speed.get() / 100.0) | 
					
						
							|  |  |  |         deltay = (self.velocity_y * self.speed.get() / 100.0) | 
					
						
							|  |  |  |         self.x = self.x + deltax | 
					
						
							|  |  |  |         self.y = self.y + deltay | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay) | 
					
						
							|  |  |  |         self.after(10, self.moveBall) | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, master=None): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         Frame.__init__(self, master) | 
					
						
							|  |  |  |         Pack.config(self) | 
					
						
							|  |  |  |         self.createWidgets() | 
					
						
							|  |  |  |         self.after(10, self.moveBall) | 
					
						
							| 
									
										
										
										
											1994-10-07 09:55:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | game = Pong() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | game.mainloop() |