我正在尝试让我的代码使用 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 的新手,所以如果您有任何清理或建议,我将不胜感激!谢谢!
喵喔喔
相关分类