在 pygame 中打印 Pyautogui 命令

Printing Pyautogui Command in pygame

我目前正在尝试编写一个 pyautogui/pygame 代码来跟踪鼠标位置并将电线打印在 pygame window 上。这是我目前所拥有的:

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()

当我 运行 此代码时,我得到错误:

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

如你所见,变量POS包含命令;我想要在我的 pygame 屏幕上打印命令输出。有其他选择还是我只是看错了?我已经搜索了互联网,在这里提问通常是我最后的选择,所以:任何 help/criticism 不胜感激。

首先,如果 POS 不是静态变量,您应该将其名称小写,因为大写字母用于静态变量。接下来,您尝试打印一个点类型变量,因为 pyautogui.position() returns 鼠标坐标。您应该尝试将其转换为字符串:POS = str(POS)

首先,POS不是一个字符串,如果你想连续改变鼠标位置我建议你这样做:

POS = pyautogui.position

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

POS存储仓位的函数,添加()时执行,所以每次循环都会执行,随着仓位的变化

tuple() 将 Point(x=771, y=168) 更改为 (771, 168)

str() 将元组更改为要在屏幕上打印的字符串。