123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- # Programmierung 2 - Ping Pong Spiel
- # Willi Schlegel, Francine Theil, Kristin Weber
- # letzte Aenderung: 21.07.2017
- #-----------------------------------------------------------------
- # Import der einzelnen Module
- import random
- import time
- from Tkinter import *
- import os
- import sys
- #----------------------------------------------------------------
- # Funktion fuer den Neustart des Programms
- def restart_program():
- python = sys.executable
- os.execl(python, python, * sys.argv)
- #----------------------------------------------------------------
- # Eigenschaften des Balles 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.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
- #---------------------------------------------------------------------------
- # Eigenschaften des Paddles definieren
- class Paddle:
- def __init__(self, canvas, color):
- self.canvas = canvas
- self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
- self.canvas.move(self.id, 200, 300)
- self.xspeed = 0
- self.canvas.bind_all('<KeyPress-Left>', self.move_left)
- self.canvas.bind_all('<KeyPress-Right>', self.move_right)
- def draw(self):
- self.canvas.move(self.id, self.xspeed, 0)
- pos = self.canvas.coords(self.id)
- if pos[0] <= 0:
- self.xspeed = 0
- if pos[2] >= 500:
- self.xspeed = 0
- def move_left(self, evt):
- self.xspeed = -5
- def move_right(self, evt):
- self.xspeed = 5
- #----------------------------------------------------------------------------
- # 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)
- neustart_button = Button(tk, text="Neues Spiel", command=restart_program)
- neustart_button.pack()
- start_button = Button(tk, text="Spiel starten", command=tk.quit)
- start_button.pack()
- exit_button = Button(tk, text='Quit', command=tk.destroy)
- exit_button.pack()
- tk.mainloop()
- def button_action():
- anweisungs_label.config()
- #-------------------------------------------------------------------------
- # Namenseingabe
- def anzeige():
- def show_entry_fields():
- print("Name: %s" % (e1.get()))
- master = Tk()
- Label(master, text="Name").grid(row=0)
- e1 = Entry(master)
- e1.grid(row=0, column=1)
- Button(master, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
- mainloop()
- #--------------------------------------------------------------------------
- # Schleife fuer das fortlaufende Bewegen des Balles
- while ball.hit_bottom == False:
- ball.draw()
- paddle.draw()
- canvas.itemconfig(label, text="Punktestand: "+str(ball.score))
- tk.update_idletasks()
- tk.update()
- time.sleep(0.01)
- print "Punktestand: ", ball.score
- if ball.hit_bottom == True:
- anzeige()
- # tk=Tk()
- # tk.title("Highscore-Liste")
- # canvas = Canvas(tk, width=500, height=400, bd=2, bg="white")
- # canvas.pack()
- #------------------------------------------------------------------------------------------------------------------
|