Guest User

Untitled

a guest
Feb 25th, 2024
2,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import time
  2. import random
  3. import pygame
  4.  
  5. # Initialize Pygame
  6. pygame.init()
  7.  
  8. # Get native screen resolution and aspect ratio
  9. info = pygame.display.Info()
  10. width, height = info.current_w, info.current_h
  11. screen = pygame.display.set_mode((width, height))
  12. pygame.display.set_caption("Screensaver Clock")
  13.  
  14. # Font and text size
  15. font = pygame.font.SysFont("Arial", 72, bold=True)
  16.  
  17. # Rainbow color list
  18. rainbow_colors = [
  19. (255, 0, 0), (255, 165, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255), (75, 0, 130), (238, 130, 238)
  20. ]
  21.  
  22. # Color change index
  23. color_index = 0
  24.  
  25. # Clock position and movement variables
  26. x, y = random.randint(0, width - 100), random.randint(0, height - 100)
  27. x_vel, y_vel = random.uniform(-0.5, 0.5), random.uniform(-0.5, 0.5)
  28.  
  29. # Flag to control display mode (clock or date)
  30. is_clock_mode = True
  31.  
  32. running = True
  33. while running:
  34. # Handle events (excluding mouse movement)
  35. for event in pygame.event.get():
  36. if event.type == pygame.QUIT:
  37. running = False
  38. if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  39. running = False
  40. if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  41. is_clock_mode = not is_clock_mode
  42.  
  43. # Update time or date based on display mode
  44. if is_clock_mode:
  45. current_time = time.strftime("%H:%M:%S")
  46. else:
  47. current_date = time.strftime("%d/%m/%Y")
  48.  
  49. # Render text
  50. if is_clock_mode:
  51. text = font.render(current_time, True, rainbow_colors[color_index])
  52. else:
  53. text = font.render(current_date, True, rainbow_colors[color_index])
  54. text_rect = text.get_rect()
  55.  
  56. # Update position
  57. x += x_vel
  58. y += y_vel
  59.  
  60. # Check for edge collisions and bounce with color change
  61. if x < 0:
  62. x = 0
  63. x_vel *= -1
  64. color_index = (color_index + 1) % len(rainbow_colors)
  65. elif x + text_rect.width > width:
  66. x = width - text_rect.width
  67. x_vel *= -1
  68. color_index = (color_index + 1) % len(rainbow_colors)
  69.  
  70. if y < 0:
  71. y = 0
  72. y_vel *= -1
  73. color_index = (color_index + 1) % len(rainbow_colors)
  74. elif y + text_rect.height > height:
  75. y = height - text_rect.height
  76. y_vel *= -1
  77. color_index = (color_index + 1) % len(rainbow_colors)
  78.  
  79. # Fill screen with black
  80. screen.fill((0, 0, 0))
  81.  
  82. # Draw text
  83. screen.blit(text, (x, y))
  84.  
  85. # Update display
  86. pygame.display.flip()
  87.  
  88. # Hide mouse cursor
  89. pygame.mouse.set_visible(False)
  90.  
  91. # Quit Pygame
  92. pygame.quit()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment