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.

analog_clock.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import pygame
  2. import math
  3. import datetime
  4. # Festlegung der Konstanten
  5. WIDTH = 320
  6. HEIGHT = 240
  7. SIZE = (WIDTH, HEIGHT)
  8. DISTANCE = 10
  9. CIRCLE_RADIUS = HEIGHT//2 - DISTANCE
  10. NUMBER_RADIUS = CIRCLE_RADIUS * 0.9
  11. HOURS_LENGTH = NUMBER_RADIUS * 0.6
  12. MINUTES_LENGTH = NUMBER_RADIUS * 0.8
  13. SECONDS_LENGTH = NUMBER_RADIUS * 0.9
  14. FONT_SIZE = 20
  15. FONT_NAME = None # None = default font
  16. BLACK = (0, 0, 0)
  17. WHITE = (255, 255, 255)
  18. RED = (255, 0, 0)
  19. FPS = 1
  20. # Hauptfunktion mit Standardstruktur eines Pygame
  21. def main():
  22. screen = init_game()
  23. game_loop(screen)
  24. exit_game()
  25. # Initialisierung von Pygame
  26. def init_game():
  27. global clock
  28. global font
  29. pygame.init()
  30. clock = pygame.time.Clock()
  31. font = pygame.font.Font(FONT_NAME, FONT_SIZE)
  32. return pygame.display.set_mode(SIZE)
  33. # Game-Loop
  34. def game_loop(screen):
  35. while True:
  36. if event_handling() == False:
  37. break
  38. if update_game() == False:
  39. break
  40. draw_game(screen)
  41. clock.tick(FPS)
  42. # Beenden von Pygame
  43. def exit_game():
  44. pygame.quit()
  45. # Event-Behandlung
  46. def event_handling():
  47. for event in pygame.event.get():
  48. if event.type == pygame.QUIT:
  49. return False
  50. return True
  51. # Aktualisierung des Spiels
  52. def update_game():
  53. global hours_angle
  54. global minuten_angle
  55. global seconds_angle
  56. now = datetime.datetime.now()
  57. hours = now.hour % 12
  58. minutes = now.minute
  59. seconds = now.second
  60. hours_angle = (hours * 60 + minutes) / 720 * 2 * math.pi
  61. minuten_angle = minutes / 60 * 2 * math.pi
  62. seconds_angle = seconds / 60 * 2 * math.pi
  63. return True
  64. # Zeichnen des Spiels
  65. def draw_game(screen):
  66. screen.fill(BLACK)
  67. position = (WIDTH//2, HEIGHT//2)
  68. draw_dial(screen, position)
  69. draw_hands(screen, position)
  70. pygame.display.flip()
  71. # Zeichnen des Ziffernblatts
  72. def draw_dial(screen, position):
  73. global font
  74. # Rand
  75. pygame.draw.circle(screen, WHITE, position, CIRCLE_RADIUS, 1)
  76. # Ziffern
  77. for i in range(12):
  78. angle = i * (2 * math.pi) / 12
  79. y = math.cos(angle) * NUMBER_RADIUS * -1
  80. x = math.sin(angle) * NUMBER_RADIUS
  81. text = font.render(str(i or 12), True, WHITE)
  82. text_rect = text.get_rect(center=(WIDTH // 2 + x, HEIGHT // 2 + y))
  83. screen.blit(text, text_rect)
  84. # Zeichnen der Zeiger
  85. def draw_hands(screen, position):
  86. end_pos_hours = (position[0] + math.sin(hours_angle) * HOURS_LENGTH, position[1] + math.cos(hours_angle) * HOURS_LENGTH * -1)
  87. pygame.draw.line(screen, WHITE, position, end_pos_hours, 4)
  88. end_pos_minutes = (position[0] + math.sin(minuten_angle) * MINUTES_LENGTH, position[1] + math.cos(minuten_angle) * MINUTES_LENGTH * -1)
  89. pygame.draw.line(screen, WHITE, position, end_pos_minutes, 2)
  90. end_pos_seconds = (position[0] + math.sin(seconds_angle) * SECONDS_LENGTH, position[1] + math.cos(seconds_angle) * SECONDS_LENGTH * -1)
  91. pygame.draw.line(screen, RED, position, end_pos_seconds, 1)
  92. # Start des Programms
  93. main()