1

A program where you click your mouse then, a box appears from point 1 to 2 and box one moves to box 2

the "update" in the while loop does not seem to be updating the screen when the box moves

the pertaining code (not all):

frames = 0
speed = 20

def distance(x, y, x1, y1):
    distance_ = x - x1, y - y1
    return distance_

while True:
    frames += 1

    if box_two_x is not None:
        pygame.draw.rect(win, (255, 0, 255), (box_two_x, box_two_y, 13, 13))

    while distance(box_one_x, box_one_y, box_two_x, box_two_y)[1] != 0:
        frames += 1
        if frames % speed == 0:
            box_one_y += 1
            pygame.display.update()

if I need to provide more context as to the code or the problem feel free to ask.

p.s. what I've tried

  • moving the update before the if statement
  • calling a function to update the screen
  • putting the whole code after the second while statement in a function
  • drawing the square in the if statement (in the while loop)
3
  • Try pygame.display.flip() Commented Jun 25, 2021 at 5:29
  • I have tried that before, any specific place? Commented Jun 25, 2021 at 5:33
  • 2
    Do not try to control the game with a loop inside the application loop! Commented Jun 25, 2021 at 5:33

1 Answer 1

2

Do not try to control the game with a loop inside the application loop. Use the application loop:

frames = 0
speed = 20

def distance(x, y, x1, y1):
    distance_ = x - x1, y - y1
    return distance_

while True:
    frames += 1

    if box_two_x is not None:
        pygame.draw.rect(win, (255, 0, 255), (box_two_x, box_two_y, 13, 13))

    if distance(box_one_x, box_one_y, box_two_x, box_two_y)[1] != 0:
        if frames % speed == 0:
            box_one_y += 1
            
    pygame.display.update()

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

clock = pygame.time.Clock()

def distance(x, y, x1, y1):
    distance_ = x - x1, y - y1
    return distance_

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

    if distance(box_one_x, box_one_y, box_two_x, box_two_y)[1] != 0:
        box_one_y += 1

    win.fill(0)

    # [...]

    if box_two_x is not None:
        pygame.draw.rect(win, (255, 0, 255), (box_two_x, box_two_y, 13, 13))

    pygame.display.update()
    clock.tick(60)

The typical PyGame application loop has to:

Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely brilliant, and worked; but just one question. Why did you remove my raise SystemExit? come on you are just no fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.