Advertisement
LockdateforGHS

Pygame.py

Apr 2nd, 2023
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Define colors
  5. BLACK = (0, 0, 0)
  6. WHITE = (255, 255, 255)
  7. RED = (255, 0, 0)
  8.  
  9. # Set the width and height of the screen
  10. WIDTH = 600
  11. HEIGHT = 400
  12.  
  13. # Initialize Pygame
  14. pygame.init()
  15.  
  16. # Set the font for displaying text
  17. font = pygame.font.SysFont(None, 48)
  18.  
  19. # Create the screen
  20. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  21.  
  22. # Set the title of the game window
  23. pygame.display.set_caption("Clicker Aim Trainer")
  24.  
  25. # Create a clock to control the game's frame rate
  26. clock = pygame.time.Clock()
  27.  
  28. # Set the initial score and target position
  29. score = 0
  30. target_pos = (0, 0)
  31.  
  32. # Define a function to generate a new target
  33. def new_target():
  34.     global target_pos
  35.     target_pos = (random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50))
  36.  
  37. # Define a function to display the score
  38. def display_score():
  39.     score_text = font.render(f"Score: {score}", True, WHITE)
  40.     screen.blit(score_text, (10, 10))
  41.  
  42. # Define a function to draw the target
  43. def draw_target():
  44.     pygame.draw.circle(screen, RED, target_pos, 25)
  45.  
  46. # Define a function to handle mouse clicks
  47. def handle_click(pos):
  48.     global score
  49.     if abs(pos[0] - target_pos[0]) <= 25 and abs(pos[1] - target_pos[1]) <= 25:
  50.         score += 1
  51.         new_target()
  52.  
  53. # Set up the game loop
  54. done = False
  55. while not done:
  56.     # Handle events
  57.     for event in pygame.event.get():
  58.         if event.type == pygame.QUIT:
  59.             done = True
  60.         elif event.type == pygame.MOUSEBUTTONDOWN:
  61.             handle_click(pygame.mouse.get_pos())
  62.  
  63.     # Fill the screen with black
  64.     screen.fill(BLACK)
  65.  
  66.     # Generate a new target if necessary
  67.     if target_pos == (0, 0):
  68.         new_target()
  69.  
  70.     # Draw the target and display the score
  71.     draw_target()
  72.     display_score()
  73.  
  74.     # Update the screen
  75.     pygame.display.flip()
  76.  
  77.     # Limit the game's frame rate
  78.     clock.tick(60)
  79.  
  80. # Quit Pygame
  81. pygame.quit()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement