Browse Source

Definition der Eigenschaften und Funktionen des Paddles

Kristin 7 years ago
parent
commit
6473fc58cf
1 changed files with 35 additions and 0 deletions
  1. 35 0
      pingpong.py

+ 35 - 0
pingpong.py

@@ -70,3 +70,38 @@ class Ball():
70 70
 		return True
71 71
 
72 72
 	return False
73
+
74
+#---------------------------------------------------------------------------
75
+# Eigenschaften des Paddles definieren
76
+
77
+class Paddle:
78
+
79
+    def __init__(self, canvas, color):
80
+
81
+	self.canvas = canvas
82
+	self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
83
+	self.canvas.move(self.id, 200, 300)
84
+	self.xspeed = 0
85
+	self.canvas.bind_all('<KeyPress-Right>', self.move_right)
86
+        self.canvas.bind_all('<KeyPress-left>', self.move_left)
87
+
88
+
89
+    def draw(self):
90
+
91
+	self.canvas.move(self.id, self.xspeed, 0)
92
+	pos = self.canvas.coords(self.id)
93
+
94
+	if pos[0] <= 0:
95
+	    self.xspeed = 0
96
+
97
+	if pos[2] >= 500:
98
+	    self.xspeed = 0
99
+
100
+
101
+    def move_left(self, evt):
102
+	self.xspeed = -2
103
+
104
+    def move_right(self, evt):
105
+	self.xspeed = 2
106
+
107
+#----------------------------------------------------------------------------