1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #Einfaches Ping-Pong Spiel basier auf GUI(Tkinter)
- #Authors : Milad Shirvani Filabadi, Kai Zhang,Yuan Wei
- #08.11.2017
- #Stufe 1 - Eigenschaften und Funktionen der Ball definieren
- #Stufe 2 - Eigenschaften und Funktionen der Paddle definieren
- #Stufe 3 - Fenster und canvas erstellen für zeichnen
- #Stufe 4 - Animation Schleife
- #Stufe 5 - Game Over
- from tkinter import *
- import random
- import time
- # Eigenschaften und Funktionen der Ball definieren
- class Ball:
- def __init__(self, canvas, color, size, paddle):
- self.canvas = canvas
- self.paddle = paddle
- self.id = canvas.create_oval(10, 10, 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[1] <= 0:
- self.yspeed = 3
- if pos[3] >= 400:
- self.hit_bottom = True
- if pos[0] <= 0:
- self.xspeed = 3
- if pos[2] >= 500:
- self.xspeed = -3
- 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.paddle.id)
- if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
- if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
- return True
- return False
|