微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从起始坐标和速度获取结束坐标

如何解决从起始坐标和速度获取结束坐标

所以我正在用 python (pygame) 创建一个小乒乓球游戏,我正在尝试为它创建一个机器人...... 如果你有球速度: veLocity = [10,5] 球 x: x = 300 和他们: y = 300 是否有可能计算球的去向(也知道球弹跳的边缘在哪里) 到目前为止,我的游戏代码https://pastebin.com/eQRZedqs

import pygame
import sys
 
 
SIZE = (1000,600)
FRAMES = 60
win = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
 
 
class Player:
    def __init__(self,x,y):
        self.size = (15,120)
        self.x = x
        self.y = y
 
    def draw(self,surface):
        pygame.draw.rect(surface,(255,255,255),pygame.Rect((self.x,self.y),self.size))
 
 
class Ball:
    def __init__(self):
        self.x = SIZE[0] // 2
        self.y = SIZE[1] // 2
        self.vel = [-5,0]
 
    def move(self):
        self.x += self.vel[0]
        self.y += self.vel[1]
        if self.y <= 20 or self.y >= SIZE[1] - 20:
            self.vel[1] *= -1
        if (self.x <= 45 and bot.y < self.y < bot.y + bot.size[1]) or (self.x >= SIZE[0] - 45 and p.y < self.y < p.y + p.size[1]):
            self.vel[0] *= -1
 
    def draw(self,surface):
        pygame.draw.circle(surface,(self.x,10)
 
 
class Bot(Player):
    def __init__(self,y):
        super().__init__(x,y)
        self.ball_vel = b.vel
        self.ball_x = b.x
        self.ball_y = b.y
 
 
def draw_screen(surface):
    surface.fill((0,0))
    p.draw(surface)
    b.draw(surface)
    bot.draw(surface)
    b.move()
    pygame.display.update()
 
 
def key_handler():
    keys = pygame.key.get_pressed()
 
    if (keys[pygame.K_UP] or keys[pygame.K_w]) and p.y >= 20:
        p.y -= 5
    elif (keys[pygame.K_DOWN] or keys[pygame.K_s]) and p.y + p.size[1] <= SIZE[1] - 20:
        p.y += 5
 
 
def main_loop():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        key_handler()
 
        draw_screen(win)
 
        clock.tick(FRAMES)
 
 
if __name__ == "__main__":
    pygame.init()
    p = Player(SIZE[0] - 45,SIZE[1] // 2 - 60)
    b = Ball()
    bot = Bot(20,SIZE[1] // 2 - 60)
    main_loop()

解决方法

我想说将以下函数添加到 Ball 类:

class Ball:
    def __init__(self):
        self.x = SIZE[0] // 2
        self.y = SIZE[1] // 2
        self.vel = [-5,0]
 
    def move(self):
        self.x += self.vel[0]
        self.y += self.vel[1]
        if self.y <= 20 or self.y >= SIZE[1] - 20:
            self.vel[1] *= -1
        if (self.x <= 45 and bot.y < self.y < bot.y + bot.size[1]) or (self.x >= SIZE[0] - 45 and p.y < self.y < p.y + p.size[1]):
            self.vel[0] *= -1
 
    def draw(self,surface):
        pygame.draw.circle(surface,(255,255,255),(self.x,self.y),10)

    # projecting to see in which y the ball will end up (returns the y coordinate)
    def project(self):
        # setting up local variables as to not change the actual position of the ball
        x = self.x
        y = self.y
        vel = self.vel
        # doing practically the same thing as move but with the local x and y
        while x > 45 and x < SIZE[0] - 45:
                x += vel[0]
                y += vel[1]
                if y <= 20 or y >= SIZE[1] - 20:
                    vel[1] *= -1
        return y

既然你有 y 的返回值,那么你可以让你的机器人直接移动到那里,无论是缓慢的还是即时的。 如果你想获得更快的解决方案,你需要使用一些物理方程,但在我看来,这已经足够快了(它会很快到达那里)。此外,您也可以使用三角学来获得答案,将速度想象成一个向量(然后您可以获得相对于 y 轴的运动角度,然后使用三角函数来获得最终长度,直到 y 到达边界。重复直到到达 y 轴边界,您才能更有效地计算 x(不过,再一次,很可能没有必要这样做,因为它应该运行得足够快)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。