Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to move the object in squared path repeatedly

I’ll try to rephrase this question again..
So basically what I want to happen here is to make my object move to the screen in all direction whenever it reach the end point.. 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading