Image display in pygame wont work.

Started by
23 comments, last by Unknownforest 14 years, 4 months ago
""" mypygame.py
    draw an image on the screen"""

import pygame
pygame.init()

def main():
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Display an image")

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((96, 96, 0))

    mypygame = pygame.image.load("example.png")
    mypygame = mypygame.convert()

    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

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

        pygame.display.flip()

if __name__ == "__main__":
    main()

When I save and run the module, all I get is a black window. What am I doing wrong? BTW I'm using pygame 1.9.1 the version that's required for 2.5.4. [Edited by - Unknownforest on November 14, 2009 9:15:11 PM]
Advertisement
Quote:Original post by Unknownforest

mypygame = pygame.image.load("example.png")
mypygame = mypygame.convert()

...
screen.blit(background, (0, 0))
screen.blit(example, (96, 96))
...
pygame.display.flip()



Here is the relevant portion of yr code where the problem is. Yr line, screen.blit(example, (96, 96)) should be screen.blit(mypygame, (96, 96)). Right?

Check if this works.
It worked with small PNGs but when I went with a PNG that's 640x400 it didn't work.
Quote:Original post by Unknownforest
... but when I went with a PNG that's 640x400 it didn't work.

What do you mean by this? Can you please be more specific?
Quote:Original post by signal_
Quote:Original post by Unknownforest
... but when I went with a PNG that's 640x400 it didn't work.

What do you mean by this? Can you please be more specific?


The image won't show up in the window.
Okay. I just downloaded Pygame & Python. I wiped this comp one month ago so I didn't re-install it yet.

Try this:
import pygame, sys, ospygame.init()from pygame.locals import *def GetInput():    key = pygame.key.get_pressed()     for event in pygame.event.get():         if event.type == pygame.QUIT or key[K_ESCAPE]:             pygame.quit();            sys.exit() def main():    screen = pygame.display.set_mode((640, 480))        pygame.display.set_caption("Display an image")    background = pygame.Surface(screen.get_size())    background = background.convert()    background.fill((96, 96, 0))    img0 = pygame.image.load("0.png").convert()    img1 = pygame.image.load("1.png").convert()        clock = pygame.time.Clock()    keepGoing = True    imgX = 0;    imgY = 0;    directionX = 1;    directionY = 1;        while keepGoing:        clock.tick(30)        GetInput()        screen.blit(background, (0, 0))        screen.blit(img0, (0, 0))        screen.blit(img1, (imgX, imgY))        if imgX < 0 or imgX > (screen.get_width() - img1.get_width()):            directionX *= -1        if imgY < 0 or imgY > (screen.get_height() - img1.get_height()):            directionY *= -1        imgX += directionX        imgY += directionY        pygame.display.flip()if __name__ == "__main__":    main()


Some notes:

1. I used 2 images to make this program: img0 and img1. img0, for me, is 640x480 so it covers the screen. img1 is a 96x96 sprite that bounces around the screen. I added some simple movement and screen-edge collision detection.

2. Use 'source' tags when posting code; they preserve the formatting. You can just copy-n-paste mine....

3. Try this. It should work. If it doesn't something is wrong on yr end (I'm using the same Python and Pygame as you are).
 import pygame, sys, ospygame.init()from pygame.locals import *def GetInput():    key = pygame.key.get_pressed()     for event in pygame.event.get():         if event.type == pygame.QUIT or key[K_ESCAPE]:             pygame.quit();            sys.exit() def main():    screen = pygame.display.set_mode((640, 480))        pygame.display.set_caption("Display an image")    background = pygame.Surface(screen.get_size())    background = background.convert()    background.fill((96, 96, 0))    sky6 = pygame.image.load("6.png").convert()    sky4 = pygame.image.load("4.png").convert()        clock = pygame.time.Clock()    keepGoing = True    skyX = 0;    skyY = 0;    directionX = 1;    directionY = 1;        while keepGoing:        clock.tick(30)        GetInput()        screen.blit(background, (0, 0))        screen.blit(sky6, (0, 0))        screen.blit(sky4, (skyX, skyY))        if skyX < 0 or skyX > (screen.get_width() - sky4.get_width()):            directionX *= -1        if skyY < 0 or skyY > (screen.get_height() - sky4.get_height()):            directionY *= -1        sky4X += directionX        sky4Y += directionY        pygame.display.flip()if __name__ == "__main__":    main()


I use the approach of using sky6 as the background since it's 0, 0. And then I used sky4 for movement. Both are 96x96 .pngs. But all I get is.

Traceback (most recent call last):  File "C:\Documents and Settings\anonymous\Desktop\thatfolder\pygame.py", line 1, in <module>    import pygame, sys, os  File "C:\Documents and Settings\anonymous\Desktop\thatfolder\pygame.py", line 2, in <module>    pygame.init()AttributeError: 'module' object has no attribute 'init'


What wrong here? What did I do wrong in placing the pngs?
Are you sure you have exactly:

import pygame, sys, os
pygame.init()


at the start of your pygame.py? No weird spaces or anything else?
Quote:Original post by mattd
Are you sure you have exactly:

import pygame, sys, os
pygame.init()


at the start of your pygame.py? No weird spaces or anything else?


Yeah, OP, make sure you preserve formatting. Why don't you try typing or copy-pasting the code into IDLE. That way you can see any error messages that pop up. Maybe something is wrong with yr setup since the code is running fine on my pc.

So try inputting the code into IDLE line-by-line and post the results. Here is my result from the code that I typed up:

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.    ****************************************************************    Personal firewall software may warn about the connection IDLE    makes to its subprocess using this computer's internal loopback    interface.  This connection is not visible on any external    interface and no data is sent to or received from the Internet.    ****************************************************************    IDLE 1.2.4      ==== No Subprocess ====>>> import pygame, sys, os>>> >>> from pygame.locals import *>>> pygame.init()(6, 0)>>> screen = pygame.display.set_mode((640, 480))>>> pygame.display.set_caption("Display an image")>>> background = pygame.Surface(screen.get_size())>>> background = background.convert()>>> background.fill((96, 96, 0))<rect(0, 0, 640, 480)>>>> img0 = pygame.image.load("0.png").convert()>>> img1 = pygame.image.load("1.png").convert()>>> imgX = 0;>>> imgY = 0;>>> screen.blit(background, (0, 0))<rect(0, 0, 640, 480)>>>> screen.blit(img0, (0, 0))<rect(0, 0, 640, 480)>>>> screen.blit(img1, (imgX, imgY))<rect(0, 0, 96, 96)>>>> >>> pygame.display.flip()>>> pygame.quit();


Does yrs look like this? Post the result if it doesn't.

This way you can see what each line of the code does. IDLE basically will interpret each line of code you give it, kinda like stepping thru a program with a debugger.

Pay special attention to the line pygame.init() when you type it in to IDLE. If you receive
..., line 2, in <module>    pygame.init()AttributeError: 'module' object has no attribute 'init'

then that is troubling. It might mean that things are not setup correctly for you. Also, what operating system are you using? Is yr processor 32 or 64 bit?
This is what I did real quick. BTW I am running off of a 32 bit OS. Windows XP. OS X should be 64 bit I think. Since whenever the hell I get this up and running I'd like this build to actually run well in modern macs if there really is going to be a crap load of issues with mac os 9.2 and GameSprockets.

>>> import pygame, sys, os>>> >>> from pygame.locals import *>>> pygame.init()(6, 0)>>> screen = pygame.display.set_mode((640, 480))>>> pygame.display.set_caption("Display an image")>>> background = pygame.Surface(screen.get_size())>>> background.fill((96, 96, 0))<rect(0, 0, 640, 480)>>>> sky6 = pygame.image.load("sky6.png").convert()Traceback (most recent call last):  File "<pyshell#8>", line 1, in <module>    sky6 = pygame.image.load("sky6.png").convert()error: Couldn't open sky6.png>>> 


I guess I could give copy and pasting another shot. If this doesn't work I don't know what the hell I'm suppose to do about making a pygame app.

This topic is closed to new replies.

Advertisement