gpt4 book ai didi

python - 如何使用线程同时运行不和谐客户端和 pygame?

转载 作者:行者123 更新时间:2023-12-04 09:16:55 25 4
gpt4 key购买 nike

我试图让我的代码使用 discord api 从 discord 中获取消息,并使用 pygame 将其放在黑屏上,并在中心显示所述消息。 get_message() 和 main_window() 函数都单独工作,但是当我将它与 Threading 放在一起时,get_message() 似乎不起作用。
我的代码

import discord
import pygame
from threading import Thread

client = discord.Client()
new_message = "Potato"

pygame.font.init()
font = pygame.font.Font(None, 45)
color = (255, 255, 255)
txt = font.render(new_message, True, color)

def main():
t1 = Thread(target=main_window())
t3 = Thread(target=get_message())
t3.start()
t1.start()

def main_window():
global new_message
info = pygame.display.Info()
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.FULLSCREEN)
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
done = False

while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True

screen.fill((30, 30, 30))
screen.blit(txt, txt.get_rect(center=screen_rect.center))

pygame.display.flip()
clock.tick(30)

def get_message():

@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
if message.author == client.user or message.author.id == MY_USER_ID:
return
if message.channel.id == MY_CHANNEL_ID:
if message.content != " ":
global new_message
global txt
new_message = message.content
txt = font.render(new_message, True, color)

client.run("MY_ACCESS_KEY")

if name == 'main':
pygame.init()
main()
我对 Python 很陌生,所以如果您有任何清理或建议,我将不胜感激!谢谢!

最佳答案

在单独的线程中呈现文本可能会导致问题。根据pygame字体render()文档,该方法不是线程安全的。
我通过移动 font.render() 修改了您的代码调用以及与 pygame 相关的所有内容进入 main_window()功能。在 pygame 事件循环的每次传递中,它检查是否全局 new_message已更改并呈现文本(如果已更改)。
编辑:
我刚刚注意到,当您创建线程时,您错误地指定了函数。应该是 Thread(target=myFunc) (引用函数)而不是 Thread(target=myFunc()) (调用函数并传递结果)。

import discord
import pygame
from threading import Thread

client = discord.Client()
new_message = "Potato"

color = (255, 255, 255)

def main():
t1 = Thread(target=main_window) # no parentheses on function
t3 = Thread(target=get_message)
t1.start()
t3.start()

def main_window():
pygame.init()
pygame.font.init()

font = pygame.font.Font(None, 45)
info = pygame.display.Info()
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.FULLSCREEN)
screen_rect = screen.get_rect()
clock = pygame.time.Clock()

last_message = new_message # inital message
txt = font.render(new_message, True, color) # renter initial text

done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True

if new_message != last_message: # message was changed by other thread
last_message = new_message
txt = font.render(new_message, True, color) # re-render text

screen.fill((30, 30, 30))
screen.blit(txt, txt.get_rect(center=screen_rect.center))

pygame.display.flip()
clock.tick(30)

def get_message():

@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
if message.author == client.user or message.author.id == MY_USER_ID:
return
if message.channel.id == MY_CHANNEL_ID:
if message.content != " ":
global new_message
new_message = message.content

client.run("MY_ACCESS_KEY")

if name == 'main':
main()

关于python - 如何使用线程同时运行不和谐客户端和 pygame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63177650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com