hello, me again. When ever I try to add pictures it WONT LET ME ADD THEM. i download them, type all the code, and no image shows up at all. Can someone tell me how to correctly do it. do i have to define the code that imports pictures?

Recommended Answers

All 5 Replies

Pictures imported to pygame need to be in .png format. If the pictures don't show up in .png format then something is wrong with the code you're using to show the pictures.

Pygame can load quite a number of image formats. Here is an example ...

# experiments with module pygame
# free from: http://www.pygame.org/
# load and display an image using pygame

import pygame as pg

# initialize pygame
pg.init()

# pick an image you have (.bmp  .jpg  .png  .gif)
# if the image file is not in the working folder,
# use the full pathname like "C:/Images/gifs/Pooh.gif"
image_file = "Pooh.gif"

# RGB color tuple used by pygame
white = (255, 255, 255)

# create a 300x300 white screen
screen = pg.display.set_mode((300,300),  pg.RESIZABLE )
screen.fill(white)

# load the image from a file
image = pg.image.load(image_file)

# draw image, position the image ulc at x=50, y=20
screen.blit(image, (50, 20))

# nothing gets displayed until one updates the screen
pg.display.flip()

# start event loop and wait until
# the user clicks on the window corner x
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit

thanks, man. I am going to try that code out with a pic. From my understanding, you type the name of the file its in, like in your example, image = pg.image.load(image_file). most tut i went to only listed small snippets of code that proved of no use and would not work. now i rly understand how you do it. hopefully, it will work and may take some understanding of your above code, vegaseat

def Myfunc():
print "Stuff"
#This is test code from your example code you gave me, i'm loading
#an image of a mars mountain photographed by NASA.


#import modules
import pygame
from pygame.locals import *
import pygame as pg
#initialize pygame
pg.init()

#make the screen
screen_width = 640
screen_height = 480

screen = pygame.display.set_mode((screen_width, screen_height))

#the image
image_file = "mars.jpg"

#white screen
white = (255, 255, 255)

#load the image from the file
image = pg.image.load(image_file)

#draw the image, pos it
#IMPORTANT: when i run screen.blit(50, 20)) it says 
#its a syntax error on the third parenthesis. how do i fix it?
#oddly, when i remove third parenthesis, it runs but no image/screen
#shows up. how
#were you able to run it?
#i dont know about the rest of the code though...
screen.blit(image, (50, 20))


#update screen with pg.display.flip()
pg.display.flip()

#start event loop and wait until user clicks the X button
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit

EDIT: i added image, to the blit func and found that was the problem... but nothing shows up. no image. no screen. HELP!

Why don't you just run the sample code I gave you with the image_file = "mars.jpg" and make sure you have the image file in the working directory or give it the full path.

Avoid the crap you added to the start of the code!!!

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.