0

I am currently trying to make a pyautogui/pygame code that tracks the mouse location and prints the cords on a pygame window. Here is what I have so far:

import pygame
import pygame.freetype  
import pyautogui
import time

pygame.init()
POS = pyautogui.position()
screen = pygame.display.set_mode((800, 600))
GAME_FONT = pygame.freetype.Font("/Users/user/Desktop/Calibri.ttf", 24)
running = True

while running:
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
          running = False

screen.fill((255, 255, 255))
while True:
    time.sleep(0.4)
    GAME_FONT.render_to(screen, (40, 350), POS, (0, 0, 0))
pass

pygame.display.flip()

pygame.quit()

When I run this code I get the error:

TypeError: Expected a Unicode or LATIN1 (bytes) string for text: got type Point

As you can see, the variable POS contains the command; I would like the command output printed on my pygame screen. Is there an alternative or am I just looking at this wrong? I have scoured the internet and asking a question here is usually my last resort so: Any help/criticism is appreciated.

Triobro3
  • 21
  • 3

2 Answers2

0

first if POS is not a static variable, you should put its name in lowercase since uppercase us used for static ones. Next, you are trying to print a point type variable since pyautogui.position() returns the mouse coordinates. You should try casting it into a string: POS = str(POS)

NuMa
  • 588
  • 2
  • 7
0

First of all, POS is not a string, if you want continuous changes for the mouse position i recommend you do the following:

POS = pyautogui.position

GAME_FONT.render_to(screen, (40, 350), str(tuple(POS())), (0, 0, 0))

POS stores the function for the position, and it is executed when () is added, so it will get executed each loop, as the position changes.

the tuple() changes Point(x=771, y=168) to (771, 168)

the str() changes the tuple into string to be printed on the screen.

Anonymous
  • 528
  • 3
  • 17