亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Threading 同時運行 discord 客戶端和 pygame?

如何使用 Threading 同時運行 discord 客戶端和 pygame?

慕姐8265434 2023-04-18 15:25:12
我正在嘗試讓我的代碼使用 discord api 從 discord 獲取消息,并使用 pygame 將其放在黑屏上,并在中心顯示該消息。get_message() 和 main_window() 函數都單獨工作,但是當我將它與 Threading 放在一起時,get_message() 似乎不起作用。我的代碼import discordimport pygamefrom threading import Threadclient = 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 = Truescreen.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 的新手,所以如果您有任何清理或建議,我將不勝感激!謝謝!
查看完整描述

1 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

可能是在單獨的線程中呈現文本導致了問題。根據 pygame font?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()


查看完整回答
反對 回復 2023-04-18
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號