Erstellen eines Ping Pong Spiels mittels Python unter Verwendung einer grafischen Benutzeroberfläche

pingpong.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Programmierung 2 - Ping Pong Spiel
  2. # Willi Schlegel, Francine Theil, Kristin Weber
  3. # letzte Aenderung: 21.07.2017
  4. #-----------------------------------------------------------------
  5. # Import der einzelnen Module
  6. import random
  7. import time
  8. from Tkinter import *
  9. import os
  10. import sys
  11. #----------------------------------------------------------------
  12. # Funktion fuer den Neustart des Programms
  13. def restart_program():
  14. python = sys.executable
  15. os.execl(python, python, * sys.argv)
  16. #----------------------------------------------------------------
  17. # Eigenschaften des Balles definieren
  18. class Ball():
  19. def __init__(self, canvas, color, size, paddle):
  20. self.canvas = canvas
  21. self.paddle = paddle
  22. self.id = canvas.create_oval(15, 15, size, size, fill=color)
  23. self.canvas.move(self.id, 245, 100)
  24. self.xspeed = random.randrange(-3,3)
  25. self.yspeed = -1
  26. self.hit_bottom = False
  27. self.score = 0
  28. def draw(self):
  29. self.canvas.move(self.id, self.xspeed, self.yspeed)
  30. pos = self.canvas.coords(self.id)
  31. if pos[0] <= 0:
  32. self.xspeed = 3
  33. if pos[1] <= 0:
  34. self.yspeed = 3
  35. if pos[2] >= 500:
  36. self.xspeed = -3
  37. if pos[3] >= 400:
  38. self.hit_bottom = True
  39. if self.hit_paddle(pos) == True:
  40. self.yspeed = -3
  41. self.xspeed = random.randrange(-3,3)
  42. self.score += 1
  43. def hit_paddle(self, pos):
  44. paddle_pos = self.canvas.coords(self.paddle.id)
  45. if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  46. if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  47. return True
  48. return False
  49. #---------------------------------------------------------------------------
  50. # Eigenschaften des Paddles definieren
  51. class Paddle:
  52. def __init__(self, canvas, color):
  53. self.canvas = canvas
  54. self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
  55. self.canvas.move(self.id, 200, 300)
  56. self.xspeed = 0
  57. self.canvas.bind_all('<KeyPress-Left>', self.move_left)
  58. self.canvas.bind_all('<KeyPress-Right>', self.move_right)
  59. def draw(self):
  60. self.canvas.move(self.id, self.xspeed, 0)
  61. pos = self.canvas.coords(self.id)
  62. if pos[0] <= 0:
  63. self.xspeed = 0
  64. if pos[2] >= 500:
  65. self.xspeed = 0
  66. def move_left(self, evt):
  67. self.xspeed = -5
  68. def move_right(self, evt):
  69. self.xspeed = 5
  70. #----------------------------------------------------------------------------
  71. # Spieloberflaeche
  72. tk = Tk()
  73. tk.title("Superfantastisches Ping-Pong-Spiel")
  74. canvas = Canvas(tk, width=500, height=400, bd=3, bg='pink')
  75. canvas.pack()
  76. label = canvas.create_text(10, 10, anchor=NW, text="Punktestand: 0")
  77. tk.update()
  78. paddle = Paddle(canvas, 'black')
  79. ball = Ball(canvas, 'white', 25, paddle)
  80. neustart_button = Button(tk, text="Neues Spiel", command=restart_program)
  81. neustart_button.pack()
  82. start_button = Button(tk, text="Spiel starten", command=tk.quit)
  83. start_button.pack()
  84. exit_button = Button(tk, text='Quit', command=tk.destroy)
  85. exit_button.pack()
  86. tk.mainloop()
  87. def button_action():
  88. anweisungs_label.config()
  89. #-------------------------------------------------------------------------
  90. # Namenseingabe
  91. def anzeige():
  92. def show_entry_fields():
  93. print("Name: %s" % (e1.get()))
  94. master = Tk()
  95. Label(master, text="Name").grid(row=0)
  96. e1 = Entry(master)
  97. e1.grid(row=0, column=1)
  98. Button(master, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
  99. mainloop()
  100. #--------------------------------------------------------------------------
  101. # Schleife fuer das fortlaufende Bewegen des Balles
  102. while ball.hit_bottom == False:
  103. ball.draw()
  104. paddle.draw()
  105. canvas.itemconfig(label, text="Punktestand: "+str(ball.score))
  106. tk.update_idletasks()
  107. tk.update()
  108. time.sleep(0.01)
  109. print "Punktestand: ", ball.score
  110. if ball.hit_bottom == True:
  111. anzeige()
  112. # tk=Tk()
  113. # tk.title("Highscore-Liste")
  114. # canvas = Canvas(tk, width=500, height=400, bd=2, bg="white")
  115. # canvas.pack()
  116. #------------------------------------------------------------------------------------------------------------------