summaryrefslogtreecommitdiffstats
path: root/Demo/tkinter/matt/pong-demo-1.py
blob: 7d405c1654534e0902f7dd2746a72aa9a5a30a45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from Tkinter import *

import string


class Pong(Frame):
    def createWidgets(self):
	self.QUIT = Button(self, {'text': 'QUIT', 
				  'fg': 'red', 
				  'command': self.quit})
	self.QUIT.pack({'side': 'left', 'fill': 'both'})	

	## The playing field
	self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})

	## The speed control for the ball
	self.speed = Scale(self, {"orient":  "horiz", 
				  "label" : "ball speed", 
				  "from" : -100, 
				  "to" : 100})

	self.speed.pack({'side': 'bottom', "fill" : "x"})

	# 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

	self.draw.pack({'side': 'left'})


    def moveBall(self, *args):
	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

	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

	self.draw.move(self.ball,  `deltax` + "i", `deltay` + "i")
	self.after(10, self.moveBall)
	


    def __init__(self, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.createWidgets()
	self.after(10, self.moveBall)


game = Pong()

game.mainloop()