background_image_filename = 'sushiplate.jpg'
sprice_image_filename = 'fugu.png'

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

background = pygame.image.load(background_image_filename).convert()
sprite = pygame.image.load(sprice_image_filename)

# our clock object
clock = pygame.time.Clock()

x1 = 0.
x2 = 0.

# speed in pixels per second
speed = 250.

frame_no = 0

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
            
    screen.blit(background, (0, 0))
    screen.blit(sprite, (x1, 50))
    screen.blit(sprite, (x2, 250))
    
    time_passed = clock.tick(30)
    time_passed_seconds = time_passed / 1000.0
    
    distance_moved = time_passed_seconds * speed
    x1 += distance_moved
    
    if (frame_no % 5) == 0:
        distance_moved = time_passed_seconds * speed
        x2 += distance_moved * 5
        
    # if the image goes of the end of the screen, move it back
    if x1 > 640.:
        x1 -= 640.
    if x2 > 640.:
        x2 -= 640.
        
    pygame.display.update()
    frame_no += 1

Could someone please explain to me what going on in the if statement. I don't understand why there is a duplicate line of code in the if statement that was also just a few lines back. The variable frame_no is also getting me. It's already 0 so the remainder of 0 \ 5 is always going to be 0. Thanks for any and all replies.

Dani AI

Generated

Short version for : the if is simply choosing when the second sprite gets updated — it only moves on every fifth frame. is right that the frame counter is incremented later in the loop, so the test will be true on frames 0, 5, 10, 15, etc. The duplicated calculation of the per-frame distance inside the if is redundant in this exact loop: it recomputes the same value and can be removed without changing behavior.

Why it looks odd: x1 is moved every loop, while x2 is moved less often but by five times the per-frame amount when it does move. With a fixed tick of 30fps that gives identical average speeds — for example, with speed=250 and dt ≈ 1/30s you get ~8.33 px per frame; x1 moves ~8.33 each frame, x2 moves ~41.67 once every five frames, so after five frames both have advanced ~41.67. The visible difference is smooth motion (x1) vs. a stepped jump (x2).

Practical fixes and tips:

  • Compute the elapsed time once per loop and reuse it; avoid recalculating the same value inside conditionals.
  • If you want x2 to actually be faster and smoother, update it every frame using a scaled speed (so it gets speed*5*dt each frame) instead of stepping every N frames.
  • If you deliberately want stepped motion, base the step on accumulated real time (or pygame timers) rather than a frame counter so behavior stays consistent when the frame rate changes.
  • Cast positions to integers when blitting to avoid sub-pixel artifacts: use integer coordinates for the blit call.

Small naming change helps readability: call frame_no something like frame_count or keep a separate elapsed_for_x2 timer so the intent is obvious.

The if statements are no repeated, theres one if for each sprite.

The if of line 42 moves the second sprite (x2) every 5 frames, or being said every 5 movements of the first sprite (x1)

The frame counter is incremented on line 52.

Cheers and Happy coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.