python - 如何连接不和谐和pygame?
问题描述
所以几天前我看了一个人制作“不和谐游戏”的视频。他通过制作终端游戏然后将其转移到不和谐来做到这一点。但它是用 Java 编写的,所以我开始搜索并查看是否可以在 python 中做类似的事情。虽然我可以用一个终端游戏然后转移它,但我想看看我是否可以合并 pygame 和 discord 在 discord 中制作一个 pygame。我搜索了谷歌并找到了这个答案。我想过使用,但我想问是否有其他方法。
这是表单的链接:Pygame with discord bot
它使用线程来同时运行(discord API 和 pygame)。
import discord, random, pygame, time, asyncio
import random # just for fun
from threading import Thread
### better to set them as a global variable
client = discord.Client()
pygame.init() # put these in the beginning
gameDisplay = pygame.display.set_mode((500, 500))
white = (255,255,255)
clock = pygame.time.Clock()
green = (0,255,0)
red = (255,0,0)
black = (0,0,0)
###
#client.send_message()
@client.event
async def on_message(message):
# do what you want to do...
pass
async def send_message(msg):
await client.send_message(client.get_channel('197481285852069898'), msg) # send a message, you can use channel id or any channel instance
def run_gui(): # for running the gui
gameDisplay.fill(white) # initialize the screen white
current_color = red
my_rect = pygame.draw.rect(gameDisplay, current_color, (50,50,50,50)) # draw a rect and save the rect
while 1: # pygame mainloop
pygame.display.update() # update the screen
for event in pygame.event.get(): # proper way of retrieving events
if event.type == pygame.MOUSEBUTTONDOWN: # check if the event is right clicked on mouse
mouse = pygame.mouse.get_pos()
if my_rect.collidepoint(mouse): # see if it pressed the rect
# do stuff if the button is pressed...
current_color = red
# send a random message maybe?
choosen = random.choice(['hello','hi','I am a bot','wassup','I luv u <3'])
asyncio.ensure_future(send_message( msg=choosen)) # since discord.py uses asyncio
else:
# do stuff if it's not pressed...
current_color = green
# refill the screen white
gameDisplay.fill(white)
# redraw the rect
my_rect = pygame.draw.rect(gameDisplay, current_color, (50,50,50,50))
clock.tick(60) # 60 fps
def run_bot(): # for running the bot
client.run('token') # run the bot
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
Thread(target=run_bot).start() # start thread the run the bot
run_gui() # run the gui
解决方案
将更多静态类型的游戏制作成 Discord 机器人非常容易,但是由于速率限制(您可以在每个指定的时间段内发出多少请求),将“节奏更快”的游戏制作成 Discord 机器人非常困难。
在您的示例中,原始线程中的那个人只想在他在游戏中单击时发送消息。不要将完整的游戏制作成 Discord 机器人。
如果你想做一些“快节奏”的东西——这意味着界面每秒更新多次,你可能不走运,因为 Discord 不允许这样做。
推荐阅读
- docker - 如何为非 docker Jenkins master 创建 docker slave 构建
- php - 提交表单按钮后重定向到 2 个不同的页面
- python - 预期 2 个值得到一个函数
- cassandra-3.0 - 如何在 Embedded-Cassandra 中获取特定版本的 Cassandra
- sql - 为“stat”多次指定了“30”列
- java - 为什么即使启用了 Wi-Fi 呼叫,方法 telephonyManager.getVoiceNetworkType() 也会返回 Network_LTE 的值?
- c# - AWS Lambda 函数在 .Net 核心中调用自身
- typescript - 打字稿什么是更好的方法类或接口
- asp.net - 如何从 ajax 选项卡容器中获取客户端 ID
- mysql - mysql中只有一条记录的“limit 10”和“limit 100”有什么区别吗?