首页 > 解决方案 > 从起始坐标和速度获取结束坐标

问题描述

所以我正在用python(pygame)创建一个小乒乓球游戏,我正在尝试为它创建一个机器人......如果你有球速度: velocity = [10, 5] 球x: x = 300 和y: 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, (255, 255, 255), (self.x, self.y), 10)
 
 
class Bot(Player):
    def __init__(self, x, 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, 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()

标签: pythonpygamecoordinatespong

解决方案


我会说将以下函数添加到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 轴的运动角度,然后使用 trig 获得最终长度,直到 y 到达边界。重复那,直到你到达 y 轴边界,你可以更有效地计算你的 x (尽管,再一次,很可能不需要这样做,因为它应该运行得足够快)。


推荐阅读