background_image_filename = "sushiplate.jpg"
sprite_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(sprite_image_filename)

# The x coordinate of our sprite
x = 0.

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        
        screen.blit(background, (0, 0))
        screen.blit(sprite, (x, 100))
        
        x += 10.
        
        # If the image goes off the end of the screen, move it back
        if x > 640.:
            x -= 640.
            
        pygame.display.update()

In the above code I'M trying to understand the two blit lines. I was unable to simply click go to definition. When I searched the pygame documentation I found blit belonging to the Surface module. But I'M trying to understand how it's using the parameters (x, 100). I couldn't find an example or a list of how it works in the pygame documentation. Thanks.

Dani AI

Generated

Short clarification and a few practical tips that build on 's answer and the snippet posted by .

Surface.blit has a couple of useful details beyond "it draws one Surface onto another": its typical signature is roughly Surface.blit(source, dest, area=None, special_flags=0) and it returns a Rect describing the destination area that was actually changed. The dest argument can be a (x,y) pair or a Rect; the optional area lets you copy only a sub-rectangle of the source (handy for sprite sheets). There is also a special_flags parameter for blend modes when you need additive or multiply blending.

Performance and transparency notes: convert images with convert() to match the display format for faster blits; use convert_alpha() only when you need per-pixel alpha (PNG). For simple colorkey transparency, use surface.set_colorkey(color, pygame.RLEACCEL) to enable RLE acceleration. The order of blits matters (draw background before sprites). Use the Rect returned by blit and pass it to pygame.display.update(rect) to do partial screen updates instead of refreshing the whole screen.

A couple of practical fixes for the original code: the blit calls and the display update should be done once per frame, after event handling — not inside the event loop. Also avoid incrementing position by a fixed number each iteration without capping the frame rate; use pygame.time.Clock() and compute movement as speed * dt so movement is consistent across machines. Example pattern:

clock = pygame.time.Clock()
speed = 180.0  # pixels per second
xpos = 0.0
while True:
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            raise SystemExit
    dt = clock.tick(60) / 1000.0
    xpos += speed * dt
    dirty = screen.blit(bg_surface, bg_rect)
    dirty = screen.blit(player_surface, (int(xpos), 100))
    pygame.display.update(dirty)

Quick tips: use sprite.get_rect() and screen.get_width() for correct wrap logic; cast float positions to int() when passing to blit; and pre-scale or cache transformed surfaces (rather than scaling each frame) when performance is an issue.

Recommended Answers

All 2 Replies

The 'blit' loads a image object to the window surface.

In your example 'blit' loads the 'background' image to the window starting at cordinates 0 in x and 0 in y.

screen.blit(background, (0, 0))

The second 'blit' loads the 'sprite' image to the screen, with a fixed cordinate of 100 in 'y', and uses a variable 'x' to define the cordinate on x, that is incremented by 10 every cycle.

screen.blit(sprite, (x, 100))
x += 10

Hope it helps,

Cheers and Happy coding

Yes it did, thanks.

The 'blit' loads a image object to the window surface.

In your example 'blit' loads the 'background' image to the window starting at cordinates 0 in x and 0 in y.

screen.blit(background, (0, 0))

The second 'blit' loads the 'sprite' image to the screen, with a fixed cordinate of 100 in 'y', and uses a variable 'x' to define the cordinate on x, that is incremented by 10 every cycle.

screen.blit(sprite, (x, 100))
x += 10

Hope it helps,

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.