Can someone tell me how to display an image with pygame. By the way I am using python 3.x.

Dani AI

Generated

Good catch by : the error comes from the destination argument you pass to blit. It must be a single position (tuple) or a Rect, not two separate integers, and the surface itself is not callable. The docs spell this out: you pass an (x, y) for the topleft or a Rect whose topleft is used. Using a Rect also makes later movement easier. (nea.pygame.org)

A couple of quality-of-life tips for and future readers:

  • After loading a PNG, call convert_alpha() once to match the display format and keep per-pixel transparency. It draws faster and avoids odd blending. You can also check format support with pygame.image.get_extended(). (pygame.org)
  • Prefer a relative path (e.g., an assets/ folder) instead of an absolute C:/... path so your code runs on any machine.
  • Update the window once per frame inside your loop. For simple scenes, pygame.display.flip() is fine; pygame.display.update(...) is useful when you intentionally update only small regions on software displays. Either way, call it once per frame. (nea.pygame.org)

Minimal draw loop you can drop in after you have screen, clock, image, and image_rect:

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

    screen.fill((0, 0, 0))
    screen.blit(image, image_rect)  # image_rect = image.get_rect(topleft=(50, 100))
    pygame.display.flip()
    clock.tick(60)

Finally, to ’s point from 2010: today’s pygame works on modern Python 3 and has for years (pygame 2.x is the current line). If you are starting fresh now, install a recent pygame on Python 3 and you are good to go. (pygame.org)

Recommended Answers

All 5 Replies

I thought pygame didn't work with 3.X?
If it does, it should still follow the same instructions.
1) Load image (pygame.image.load(image))
2) Blit onto display screen (screen_variable.blit(image, image.coordinates))
3) Update display (pygame.display.update())

sorry 3.0
but when I do this

import pygame, sys, time, random
from pygame.locals import *

# set up pygame
pygame.init()
mainClock = pygame.time.Clock()

# set up the window
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Sprites and Sound')
# set up the colors
BLACK = (0, 0, 0)

# set up the block data structure
pi = pygame.image.load('C:/python31/include/player.ss.png')

windowSurface(pi.blit(pi,20,20))
pygame.display.update()

it keeps giving this back

Traceback (most recent call last):
  File "C:/Python31/7.py", line 19, in <module>
    windowSurface(pi.blit(pi,25,25))
TypeError: invalid destination position for blit

I don't understand why it isn't recognizing any of the coordinates I try

I guess it is

windowSurface.blit(pi, 25, 25)

or

windowSurface.blit(pi, (25, 25))

but i never used pygame.

It's the second one

windowSurface.blit(pi,(50,100))

thanks for someone who dosen't use pygame your pretty good with it

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.