如何重复移动平方路径中的对象?
How to move the object in squared path repeatedly?
所以基本上我想要在这里发生的是让我的对象在到达端点时向各个方向移动到屏幕。例如,如果我去 ->
然后如果我到达终点,downward
然后如果我到达终点,<-
然后如果我到达终点然后 upward
因为它保持重复。这是代码。
import pygame
#Initialize Game
pygame.init()
#Create a screen (width,height)
screen = pygame.display.set_mode((550,725))
#Title and icon
pygame.display.set_caption('Bounce')
icon = pygame.image.load('assets/ball.png')
court = pygame.image.load('assets/court.png')
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
def ball(x,y):
screen.blit(icon,(x,y))
def court_(x,y):
screen.blit(court,(x,y))
x = 430
y = 630
direction = "right" #Directions
running = True
while running:
#background
screen.fill((255,175,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if direction == "right":
x -= 2
if direction == "left":
x += 2
if direction == "up":
y -= 2
if direction == "down":
y += 2
if x <= 30:
direction = "up"
if y <= 30:
direction = "left"
if x >= 430:
direction = "down"
if y == 630:
direction = "right"
court_(0,0)
ball(x,y)
pygame.display.update()
clock.tick(60)
这是我的期望
参见 。
对于一般方法,定义一个角点列表,列表中下一个点的速度和索引:
x, y = 430, 630
corner_points = [(430, 630), (30, 630), (30, 30), (430, 20)]
speed = 2
next_pos_index = 1
在应用程序循环中将对象形式点对点移动:
circle_dir = pygame.math.Vector2(corner_points[next_pos_index]) - (x, y)
if circle_dir.length() < speed:
x, y = corner_points[next_pos_index]
next_pos_index = (next_pos_index + 1) % len(corner_points)
else:
circle_dir.scale_to_length(speed)
new_pos = pygame.math.Vector2(x, y) + circle_dir
x, y = (new_pos.x, new_pos.y)
基于您的代码的最小示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((550,725))
clock = pygame.time.Clock()
x, y = 430, 630
corner_points = [(430, 630), (30, 630), (30, 30), (430, 20)]
speed = 2
next_pos_index = 1
def move(x, y, speed, points, i):
circle_dir = pygame.math.Vector2(points[i]) - (x, y)
if circle_dir.length() < speed:
x, y = points[i]
i = (i + 1) % len(points)
else:
circle_dir.scale_to_length(speed)
new_pos = pygame.math.Vector2(x, y) + circle_dir
x, y = (new_pos.x, new_pos.y)
return x, y, i
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
x, y, next_pos_index = move(x, y, speed, corner_points, next_pos_index)
screen.fill((255,175,0))
pygame.draw.circle(screen, "blue", (x, y), 20)
pygame.display.update()
clock.tick(60)
所以基本上我想要在这里发生的是让我的对象在到达端点时向各个方向移动到屏幕。例如,如果我去 ->
然后如果我到达终点,downward
然后如果我到达终点,<-
然后如果我到达终点然后 upward
因为它保持重复。这是代码。
import pygame
#Initialize Game
pygame.init()
#Create a screen (width,height)
screen = pygame.display.set_mode((550,725))
#Title and icon
pygame.display.set_caption('Bounce')
icon = pygame.image.load('assets/ball.png')
court = pygame.image.load('assets/court.png')
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
def ball(x,y):
screen.blit(icon,(x,y))
def court_(x,y):
screen.blit(court,(x,y))
x = 430
y = 630
direction = "right" #Directions
running = True
while running:
#background
screen.fill((255,175,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if direction == "right":
x -= 2
if direction == "left":
x += 2
if direction == "up":
y -= 2
if direction == "down":
y += 2
if x <= 30:
direction = "up"
if y <= 30:
direction = "left"
if x >= 430:
direction = "down"
if y == 630:
direction = "right"
court_(0,0)
ball(x,y)
pygame.display.update()
clock.tick(60)
这是我的期望
参见
对于一般方法,定义一个角点列表,列表中下一个点的速度和索引:
x, y = 430, 630
corner_points = [(430, 630), (30, 630), (30, 30), (430, 20)]
speed = 2
next_pos_index = 1
在应用程序循环中将对象形式点对点移动:
circle_dir = pygame.math.Vector2(corner_points[next_pos_index]) - (x, y)
if circle_dir.length() < speed:
x, y = corner_points[next_pos_index]
next_pos_index = (next_pos_index + 1) % len(corner_points)
else:
circle_dir.scale_to_length(speed)
new_pos = pygame.math.Vector2(x, y) + circle_dir
x, y = (new_pos.x, new_pos.y)
基于您的代码的最小示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((550,725))
clock = pygame.time.Clock()
x, y = 430, 630
corner_points = [(430, 630), (30, 630), (30, 30), (430, 20)]
speed = 2
next_pos_index = 1
def move(x, y, speed, points, i):
circle_dir = pygame.math.Vector2(points[i]) - (x, y)
if circle_dir.length() < speed:
x, y = points[i]
i = (i + 1) % len(points)
else:
circle_dir.scale_to_length(speed)
new_pos = pygame.math.Vector2(x, y) + circle_dir
x, y = (new_pos.x, new_pos.y)
return x, y, i
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
x, y, next_pos_index = move(x, y, speed, corner_points, next_pos_index)
screen.fill((255,175,0))
pygame.draw.circle(screen, "blue", (x, y), 20)
pygame.display.update()
clock.tick(60)