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.

04_walker.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. CIRCLE_RADIUS = 10
  9. # Hauptfunktion mit Standardstruktur eines Pygame
  10. def main():
  11. screen = init_game()
  12. game_loop(screen)
  13. exit_game()
  14. # Initialisierung von Pygame
  15. def init_game():
  16. global position
  17. position = (WIDTH // 2, HEIGHT // 2)
  18. pygame.init()
  19. return pygame.display.set_mode(SIZE)
  20. # Game-Loop
  21. def game_loop(screen):
  22. while True:
  23. if event_handling() == False:
  24. break
  25. if update_game() == False:
  26. break
  27. draw_game(screen)
  28. # Beenden von Pygame
  29. def exit_game():
  30. pygame.quit()
  31. # Behandlung der Events
  32. def event_handling():
  33. global position
  34. for event in pygame.event.get():
  35. if event.type == pygame.QUIT:
  36. return False
  37. if event.type == pygame.KEYDOWN:
  38. if event.key == pygame.K_UP:
  39. position = (position[0], position[1] - 1)
  40. if event.key == pygame.K_DOWN:
  41. position = (position[0], position[1] + 1)
  42. if event.key == pygame.K_LEFT:
  43. position = (position[0] - 1, position[1])
  44. if event.key == pygame.K_RIGHT:
  45. position = (position[0] + 1, position[1])
  46. return True
  47. # Aktualisierung des Spiels
  48. def update_game():
  49. return True
  50. # Zeichnen des Spiels
  51. def draw_game(screen):
  52. screen.fill(BLACK)
  53. pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS)
  54. pygame.display.flip()
  55. # Start des Programms
  56. main()