从起始坐标和速度得到结束坐标
Getting end coordinate from starting coordinate and velocity
所以我正在 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, (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()
我会说将以下函数添加到 Ball
class:
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 的 return 值,那么你可以让你的机器人直接移动到那里,无论是缓慢的方式还是瞬间的方式。
如果你想获得更快的解决方案,你需要使用一些物理方程,但在我看来,这已经足够快了(它会很快到达那里)。此外,您也可以通过将速度想象为一个向量来使用三角函数来获得答案(然后您可以获得相对于 y 轴的运动角度,然后使用 trig 获得最终长度,直到 y 到达边界。重复那,直到你到达 y 轴边界,你可以更有效地计算你的 x(尽管,再一次,很可能没有必要这样做,因为它应该 运行 足够快)。
所以我正在 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, (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()
我会说将以下函数添加到 Ball
class:
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 的 return 值,那么你可以让你的机器人直接移动到那里,无论是缓慢的方式还是瞬间的方式。 如果你想获得更快的解决方案,你需要使用一些物理方程,但在我看来,这已经足够快了(它会很快到达那里)。此外,您也可以通过将速度想象为一个向量来使用三角函数来获得答案(然后您可以获得相对于 y 轴的运动角度,然后使用 trig 获得最终长度,直到 y 到达边界。重复那,直到你到达 y 轴边界,你可以更有效地计算你的 x(尽管,再一次,很可能没有必要这样做,因为它应该 运行 足够快)。