首页 > 解决方案 > 在 pygame 中打印 Pyautogui 命令

问题描述

我目前正在尝试制作一个 pyautogui/pygame 代码来跟踪鼠标位置并在 pygame 窗口上打印绳索。这是我到目前为止所拥有的:

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 屏幕上打印命令输出。有没有其他选择,或者我只是看错了?我在网上搜索过,在这里问一个问题通常是我最后的手段,所以:感谢任何帮助/批评。

标签: macospygamepython-3.7pyautogui

解决方案


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)


推荐阅读