How do i get my shape to move? I want it to move using the while statement provided

Viewed 39
pygame.draw.circle(screen, btms3, (250, 187.5), 125, 2)
pygame.display.update()

x=10
y=10
running = 1

while running:
  if x <=10:
    hmove = 1
  elif x >= 350:
   hmove = -1
  if hmove == 1:
    x += 1
  elif hmove == -1:
    x += -1

How do i make it do as the title says? i do have the pygame flip and display update nd things like that but i could not show as i didnt want to have a super long code.

1 Answers

You have to move the object in the application loop and you have to redraw the scene in every frame.Change the center coordinates of the circle in the loop and draw the circle at its new location in each frame:

import pygame

pygame.init()
screen = pygame.display.set_mode((360, 360))
clock = pygame.time.Clock()
x, y, hmove = 10, 10, 1

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    x += hmove
    if x >= 350 or x <= 10:
        hmove *= -1

    screen.fill((0, 0, 0))
    pygame.draw.circle(screen, "red", (x, y), 10, 2) 
    pygame.display.update()
    clock.tick(100)

pygame.quit()
exit()

The typical PyGame application loop has to:

Related