How to move the object in squared path repeatedly?

Viewed 60

So basically what I want to happen here is to make my object move to the screen in all directions whenever it reaches the endpoint. For example, if I go -> then if I reach the endpoint, downward then if I reach the endpoint, <- then if I reach the endpoint then upward as it keeps repeating. Here's the code.

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)

Here is my expectation enter image description here

1 Answers

See How to make a circle move diagonally from corner to corner in pygame.

For a general approach, define a list pf corner points, the speed and the index of the next point in the list:

x, y = 430, 630
corner_points = [(430, 630), (30, 630), (30, 30), (430, 20)]
speed = 2
next_pos_index = 1

Move the object form point to point in the application loop:

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) 

Minimal example, based on your code:

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)
Related