Explorar el Código

probe hinzugefügt

Kai Zhang %!s(int64=7) %!d(string=hace) años
padre
commit
5ece397197
Se han modificado 1 ficheros con 102 adiciones y 0 borrados
  1. 102 0
      probe.py

+ 102 - 0
probe.py

@@ -0,0 +1,102 @@
1
+#Einfaches Ping-Pong Spiel basier auf GUI(Tkinter)
2
+
3
+#Authors : Milad Shirvani Filabadi, Kai Zhang,Yuan Wei
4
+
5
+#08.11.2017
6
+
7
+#Stufe 1 - Eigenschaften und Funktionen der Ball definieren
8
+
9
+#Stufe 2 - Eigenschaften und Funktionen der Paddle definieren
10
+
11
+#Stufe 3 - Fenster und canvas erstellen für zeichnen
12
+
13
+#Stufe 4 - Animation Schleife
14
+
15
+#Stufe 5 - Game Over
16
+
17
+
18
+from tkinter import *
19
+import random
20
+import time
21
+
22
+# Eigenschaften und Funktionen der Ball definieren
23
+
24
+class Ball:
25
+    def __init__(self, canvas, color, size, paddle):
26
+        self.canvas = canvas
27
+        self.paddle = paddle
28
+        self.id = canvas.create_oval(10, 10, size, size, fill=color)
29
+        self.canvas.move(self.id, 245, 100)
30
+        self.xspeed = random.randrange(-3,3)
31
+        self.yspeed = -1
32
+        self.hit_bottom = False
33
+        self.score = 0
34
+
35
+    def draw(self):
36
+        self.canvas.move(self.id, self.xspeed, self.yspeed)
37
+        pos = self.canvas.coords(self.id)
38
+        if pos[1] <= 0:
39
+            self.yspeed = 3
40
+        if pos[3] >= 400:
41
+            self.hit_bottom = True
42
+        if pos[0] <= 0:
43
+            self.xspeed = 3
44
+        if pos[2] >= 500:
45
+            self.xspeed = -3
46
+        if self.hit_paddle(pos) == True:
47
+            self.yspeed = -3
48
+            self.xspeed = random.randrange(-3,3)
49
+            self.score += 1
50
+
51
+    def hit_paddle(self, pos):
52
+        paddle_pos = self.canvas.coords(self.paddle.id)
53
+        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
54
+            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
55
+                return True
56
+        return False
57
+
58
+# Eigenschaften und Funktionen der Paddle definieren
59
+class Paddle:
60
+    def __init__(self, canvas, color):
61
+        self.canvas = canvas
62
+        self.id = canvas.create_rectangle(0,0, 100, 10, fill=color)
63
+        self.canvas.move(self.id, 200, 300)
64
+        self.xspeed = 0
65
+        self.canvas.bind_all('<KeyPress-Left>', self.move_left)
66
+        self.canvas.bind_all('<KeyPress-Right>', self.move_right)
67
+
68
+    def draw(self):
69
+        self.canvas.move(self.id, self.xspeed, 0)
70
+        pos = self.canvas.coords(self.id)
71
+        if pos[0] <= 0:
72
+            self.xspeed = 0
73
+        if pos[2] >= 500:
74
+            self.xspeed = 0
75
+
76
+    def move_left(self, evt):
77
+        self.xspeed = -2
78
+    def move_right(self, evt):
79
+        self.xspeed = 2
80
+
81
+# Fenster und canvas erstellen für zeichnen
82
+tk = Tk()
83
+tk.title("Ping-Pong Spiel")
84
+canvas = Canvas(tk, width=500, height=400, bd=0, bg='papaya whip')
85
+canvas.pack()
86
+label = canvas.create_text(5, 5, anchor=NW, text="Punkt: 0")
87
+tk.update()
88
+paddle = Paddle(canvas, 'blue')
89
+ball = Ball(canvas, 'red', 25, paddle)
90
+
91
+# Animation Schleife
92
+while ball.hit_bottom == False:
93
+    ball.draw()
94
+    paddle.draw()
95
+    canvas.itemconfig(label, text="Punkt: "+str(ball.score))
96
+    tk.update_idletasks()
97
+    tk.update()
98
+    time.sleep(0.01)
99
+
100
+# Game Over
101
+go_label = canvas.create_text(250,200,text="GAME OVER",font=("Helvetica",30))
102
+tk.update()