Advertisement
furas

Python - PyGame - text follows mouse position

Sep 2nd, 2022 (edited)
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | Gaming | 0 0
  1. # https://www.furas.pl
  2. # https://blog.furas.pl
  3.  
  4. import pygame
  5.  
  6. pygame.init()
  7.  
  8. screen = pygame.display.set_mode((800, 600))
  9.  
  10. font = pygame.font.SysFont(None, 30)
  11.  
  12. while True:
  13.     for event in pygame.event.get():
  14.         if event.type == pygame.QUIT:
  15.             pygame.quit()
  16.             exit()
  17.        
  18.     x, y = pygame.mouse.get_pos()
  19.    
  20.     text_X_image = font.render(f'X: {x}', True, (255,255,255))
  21.     text_X_rect  = text_X_image.get_rect()
  22.     text_X_rect.x = x + 10
  23.     text_X_rect.y = y + 10
  24.  
  25.     text_Y_image = font.render(f'Y: {y}', True, (255,255,255))
  26.     text_Y_rect  = text_Y_image.get_rect()
  27.     text_Y_rect.x = x + 10
  28.     text_Y_rect.y = y + 10 + 25
  29.  
  30.     # --- draw ---
  31.    
  32.     screen.fill((0,0,0))
  33.  
  34.     screen.blit(text_X_image, text_X_rect)
  35.     screen.blit(text_Y_image, text_Y_rect)
  36.    
  37.     pygame.draw.line(screen, (128, 127, 128), (0, y), (800, y))
  38.     pygame.draw.line(screen, (128, 127, 128), (x, 0), (x, 600))
  39.        
  40.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement