【发布时间】:2021-07-07 02:44:19
【问题描述】:
对于上下文,我试图在 python 中转换此代码,该代码在地球的我的世界地图上获取一个位置,从地图周围获取多个经线,并向用户发送一个与最近的经线不一致的图像,其中一条线在输入位置和经线位置
import discord
import os
from discord.ext import commands
import math
from PIL import Image, ImageDraw
from io import BytesIO
import time
eco = []
NearestLand = ''
NearestCoords = []
sa = 0
side = ''
spawnPoints = {
"Oceania Spawn": [16801, 2761],
"Antartica Spawn": [8178, 8661],
"Europe Spawn": [-386, -4782],
"Asia Spawn": [12808, -3192],
"Africa Spawn": [2420, 3738],
"North America Spawn": [-10288, -4852],
"South America Spawn": [-6487, 1360],
"Soviet Nexus": [16507,-6595],
"Ryvendor Warp-Pad":[9640,-2390],
"Gulag Warp-Pad":[11741,-4596],
"Soviet-Serbia Base Warp-Pad":[-10137,-5374],
"KGB HQ Warp-Pad":[12958,-5627],
}
client = commands.Bot(command_prefix = '!')
@client.event
async def on_ready():
print('Bot Started.')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="For Intruders"))
@client.command()
async def test(ctx):
await ctx.send('All systems functioninal comrade!')
@client.command()
async def envoy(ctx, x, y):
await ctx.send("Calculating Comrade!")
time.sleep(2)
lowest = 1000000000
eco = [x, y]
for i in range(0, 2):
eco[i] = int(eco[i])
print("Crate's Coordinates at X = {x}, Y = {y}".format(x=eco[0], y=eco[1]))
# print(eco)
# print(spawnPoints.get('Oceania'))
for i in spawnPoints:
z = math.dist(spawnPoints[i], eco)
# print("From {spawn}, ".format(spawn = str(i)) + str(z) + " blocks away.")
if lowest >= z:
lowest = z
# print("The lowest distance is at: " + str(lowest) + ", at "+ i)
NearestLand = i
NearestCoords = spawnPoints[i]
print("newNearDist: " + str(math.dist(spawnPoints[i], eco)))
await ctx.send("The nearest warp point is on {nl}, and the distance is {z}.".format
(nl=NearestLand, z=round(lowest, 2)))
print(NearestCoords)
xmap = Image.open("map.jpg")
draw = ImageDraw.Draw(xmap)
draw.line(
((int(NearestCoords[0]) + 21472) / 10,
(int(NearestCoords[1]) + 10735) / 10,
(int(x) + 21472) / 10,
(int(y) + 10735) / 10),
fill = (255, 0, 0),
width = 10)
draw.ellipse(
((((int(x) + 21472) / 10) - 25),
(((int(y) + 10735) / 10) - 25),
(((int(x) + 21472) / 10) + 25),
(((int(y) + 10735) / 10) + 25)),
fill = (255, 0, 0),
width = 25)
#draw.line(((42975 - x)/10), ((21471 - y)/10))
xmap.save("xmap.jpg")
print("Saved! Uploading...")
await ctx.send(file = discord.File("xmap.jpg"))
print("Uploaded!")
@client.command()
async def envoys(ctx, x, y):
await ctx.send("Calculating...")
time.sleep(2)
lowest = 1000000000
eco = [x, y]
for i in range(0, 2):
eco[i] = int(eco[i])
print("Crate's Coordinates at X = {x}, Y = {y}".format(x=eco[0], y=eco[1]))
# print(eco)
# print(spawnPoints.get('Oceania'))
for i in spawnPoints:
z = math.dist(spawnPoints[i], eco)
# print("From {spawn}, ".format(spawn = str(i)) + str(z) + " blocks away.")
if lowest >= z:
lowest = z
# print("The lowest distance is at: " + str(lowest) + ", at "+ i)
NearestLand = i
NearestCoords = spawnPoints[i]
print("newNearDist: " + str(math.dist(spawnPoints[i], eco)))
await ctx.send("The nearest warp point is on {nl}, and the distance is {z}.".format
(nl=NearestLand, z=round(lowest, 2)))
print(NearestCoords)
xmap = Image.open("map.jpg")
draw = ImageDraw.Draw(xmap)
draw.line(
((int(NearestCoords[0]) + 21472) / 10,
(int(NearestCoords[1]) + 10735) / 10,
(int(x) + 21472) / 10,
(int(y) + 10735) / 10),
fill = (255, 0, 0),
width = 10)
draw.ellipse(
((((int(x) + 21472) / 10) - 25),
(((int(y) + 10735) / 10) - 25),
(((int(x) + 21472) / 10) + 25),
(((int(y) + 10735) / 10) + 25)),
fill = (255, 0, 0),
width = 25)
#draw.line(((42975 - x)/10), ((21471 - y)/10))
xmap.save("xmap.jpg")
print("Saved! Uploading...")
await ctx.send(file = discord.File("xmap.jpg"))
print("Uploaded!")
client.run(os.getenv('TOKEN'))
进入节点,这是我目前所拥有的:
const Discord = require('discord.js');
const fs = require('file-system');
require('dotenv').config();
const client = new Discord.Client();
const { prefix } = require('config.json');
client.commands = new Discord.Collection();
client.on('ready', () => console.log('The Bot is ready!'));
client.user.setActivity("For Intruders", { type: "WATCHING" });
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.login(process.env.TOKEN)
我难过的是如何计算位置与输入位置之间的距离 这是我的 config.json。
{
"prefix": "!",
"spawnPoints": {
"Oceania Spawn": "16801, 2761",
"Antartica Spawn": "8178, 8661",
"Europe Spawn": "-386, -4782",
"Asia Spawn": "12808, -3192",
"Africa Spawn": "2420, 3738",
"North America Spawn": "-10288, -4852",
"South America Spawn": "-6487, 1360",
"Soviet Nexus": "16507,-6595",
"Ryvendor Warp-Pad": "9640,-2390",
"Gulag Warp-Pad": "11741,-4596",
"Soviet-Serbia Base Warp-Pad": "-10137,-5374",
"KGB HQ Warp-Pad": "12958,-5627",
},
}
提前致谢!
【问题讨论】:
-
我投票结束这个问题,因为“如何计算位置与输入位置之间的距离”是一道数学题,而不是编程题。尝试将
distance between points formula放入搜索引擎,或专门搜索math.stackexchange.com。 (我也不明白你为什么说“x、y、z 坐标”,但显示的数据每点只有两个坐标。) -
为清晰起见进行了编辑
标签: python node.js discord.js minecraft