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.

connect_the_clicks.py 1.3KB

2 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import pygame
  2. # Festlegung der Konstanten
  3. WIDTH = 320
  4. HEIGHT = 240
  5. SIZE = (WIDTH, HEIGHT)
  6. BLACK = (0, 0, 0)
  7. RED = (255, 0, 0)
  8. # Hauptfunktion mit Standardstruktur eines Pygame
  9. def main():
  10. screen = init_game()
  11. game_loop(screen)
  12. exit_game()
  13. # Initialisierung von Pygame
  14. def init_game():
  15. global clicks
  16. clicks = []
  17. pygame.init()
  18. return pygame.display.set_mode(SIZE)
  19. # Game-Loop
  20. def game_loop(screen):
  21. while True:
  22. if event_handling() == False:
  23. break
  24. if update_game() == False:
  25. break
  26. draw_game(screen)
  27. # Beenden von Pygame
  28. def exit_game():
  29. pygame.quit()
  30. # Event-Behandlung
  31. def event_handling():
  32. global clicks
  33. for event in pygame.event.get():
  34. if event.type == pygame.QUIT:
  35. return False
  36. if event.type == pygame.MOUSEBUTTONUP:
  37. pos = pygame.mouse.get_pos()
  38. clicks.append(pos)
  39. return True
  40. # Aktualisierung des Spiels
  41. def update_game():
  42. return True
  43. # Zeichnen des Spiels
  44. def draw_game(screen):
  45. screen.fill(BLACK)
  46. last_pos = None
  47. for pos in clicks:
  48. if last_pos is not None:
  49. pygame.draw.line(screen, RED, last_pos, pos, 2)
  50. last_pos = pos
  51. pygame.display.flip()
  52. # Start des Programms
  53. main()