Beispiele und Musterlösungen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

billard.py 2.6KB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import pygame
  2. # Festlegung der Konstanten
  3. WIDTH = 320
  4. HEIGHT = 240
  5. SIZE = (WIDTH, HEIGHT)
  6. FPS = 30
  7. BLACK = (0, 0, 0)
  8. GREEN = (0, 255, 0)
  9. RED = (255, 0, 0)
  10. CIRCLE_RADIUS = 5
  11. MAX_SPEED = 30
  12. FRICTION = 0.97
  13. # Hauptfunktion mit Standardstruktur eines Pygame
  14. def main():
  15. screen = init_game()
  16. game_loop(screen)
  17. exit_game()
  18. # Initialisierung von Pygame
  19. def init_game():
  20. global position
  21. global speed
  22. global clock
  23. global pushed
  24. position = (WIDTH // 2, HEIGHT // 2)
  25. speed = (0.0, 0.0)
  26. clock = pygame.time.Clock()
  27. pushed = False
  28. pygame.init()
  29. return pygame.display.set_mode(SIZE)
  30. # Game-Loop
  31. def game_loop(screen):
  32. while True:
  33. if event_handling() == False:
  34. break
  35. if update_game() == False:
  36. break
  37. draw_game(screen)
  38. clock.tick(FPS)
  39. # Beenden von Pygame
  40. def exit_game():
  41. pygame.quit()
  42. # Event-Behandlung
  43. def event_handling():
  44. for event in pygame.event.get():
  45. if event.type == pygame.QUIT:
  46. return False
  47. if not pushed and event.type == pygame.MOUSEBUTTONUP:
  48. button_pushed()
  49. return True
  50. def button_pushed():
  51. global speed, pushed
  52. mouse_pos = pygame.mouse.get_pos()
  53. delta_x = mouse_pos[0] - position[0]
  54. delta_y = mouse_pos[1] - position[1]
  55. speed = (MAX_SPEED * delta_x / WIDTH, MAX_SPEED * delta_y / HEIGHT)
  56. pushed = True
  57. # Aktualisierung des Spiels
  58. def update_game():
  59. if pushed:
  60. calc_new_position()
  61. calc_new_speed()
  62. return True
  63. def calc_new_speed():
  64. global speed, pushed
  65. speed = (speed[0] * FRICTION, speed[1] * FRICTION)
  66. if abs(speed[0]) < 0.1 and abs(speed[1]) < 0.1:
  67. pushed = False
  68. speed = (0.0, 0.0)
  69. def calc_new_position():
  70. global speed, position
  71. position = (position[0] + speed[0], position[1] + speed[1])
  72. if position[0] < CIRCLE_RADIUS or position[0] + CIRCLE_RADIUS >= WIDTH:
  73. speed = (-speed[0], speed[1])
  74. position = (position[0] + speed[0], position[1] + speed[1])
  75. if position[1] < CIRCLE_RADIUS or position[1] + CIRCLE_RADIUS >= HEIGHT:
  76. speed = (speed[0], -speed[1])
  77. position = (position[0] + speed[0], position[1] + speed[1])
  78. # Zeichnen des Spiels
  79. def draw_game(screen):
  80. screen.fill(GREEN)
  81. pygame.draw.circle(screen, BLACK, position, CIRCLE_RADIUS)
  82. if not pushed:
  83. mouse_pos = pygame.mouse.get_pos()
  84. if mouse_pos[0] > 0 and mouse_pos[0] < WIDTH-1 and mouse_pos[1] > 0 and mouse_pos[1] < HEIGHT-1:
  85. pygame.draw.line(screen, RED, position, mouse_pos, 2)
  86. pygame.display.flip()
  87. # Start des Programms
  88. main()