【问题标题】:Printing Pyautogui Command in pygame在 pygame 中打印 Pyautogui 命令
【发布时间】:2020-10-11 12:18:07
【问题描述】:

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

【问题讨论】:

    标签: macos pygame python-3.7 pyautogui


    【解决方案1】:

    首先,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() 将元组更改为要在屏幕上打印的字符串。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-07
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多