# Programmierung 2 - Ping Pong Spiel # Willi Schlegel, Francine Theil, Kristin Weber # letzte Aenderung: 19.07.2017 #----------------------------------------------------------------- # Import der einzelnen Module import random as r import time from Tkinter import * # ---------------------------------------------------------------- # Spieloberflaeche tk = Tk() tk.title("Superfantastisches Ping-Pong-Spiel") canvas = Canvas(tk, width=500, height=400, bd=3, bg='pink') canvas.pack() label = canvas.create_text(10, 10, anchor=NW, text="Punktestand: 0") tk.update() paddle = Paddle(canvas, 'black') ball = Ball(canvas, 'white', 25, paddle) #---------------------------------------------------------------- # Eigenschaften des Paddles definieren class Ball(): def __init__(self, canvas, color, size, paddle): self.canvas = canvas self.paddle = paddle self.id = canvas.create_oval(15, 15, size, size, fill=color) self.canvas.move(self.id, 245, 100) self.xspeed = random.randrange(-3,3) self.yspeed = -1 self.hit_bottom = False self.score = 0 def draw(self): self.canvas.move(self.id, self.xspeed, self.yspeed) pos = self.canvas.coords(self.id) if pos[0] <= 0: self.xspeed = 3 if pos[1] <= 0: self.yspeed = 3 if pos[2] >= 500: self.xspeed = -3 if pos[3] >= 400: self.hit_bottom = True if self.hit_paddle(pos) == True: self.yspeed = -3 self.xspeed = random.randrange(-3,3) self.score += 1 def hit_paddle(self, pos): paddle_pos = self.canvas.coords(self.padle.id) if pos[2] >= padle_pos[0] and pos[0] <= paddle_pos[2]: if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]: return True return False