I can't get images to load.. I always get an error

here's my code so far:


import os, sys
import pygame
from pygame.locals import *

if not pygame.font: print "Warning, fonts disabled"


def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

class Paddle(pygame.sprite.Sprite):
    def __init__(self):
        self.image, self.rect = load_image('paddle.bmp')

class ball(pygame.sprite.Sprite):
    def __init__(self):
        self.image, self.rect = load_image('ball.bmp')
        screen = pygame.display.get_surface()
        self. area = screen.get_rect()
        self.rect.topleft = 10, 10
        self.y, self.x = 10, 8

    def move(self):
        newpos = self.rect.move((self.y, self.x))
        if self.rect.left < self.area.left:
            #point to player 1
            #new ball
        if self.rect.right > self.area.right:
            #point ot player 2
            #new ball
        if self.rect.top < self.area.top or \
           self.rect.bottom > self.area.bottom:
           self.y = -self.y
           newpos = self.rect.move((self.x, self.y))
        self.rect = newpos

def main():

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

    background = pygame.Surface((640,480))
    background = background.convert()

    if pygame.font:
        font = pygame.font.Font(None, 36)
        text = font.render("0:0", 1, (10,10,10))
        textpos = text.get_rect(centerx=background.get_width()/2)
        background.blit(text, textpos)

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

    clock = pygame.time.Clock()
    paddle = Paddle()
    ball = Ball()
    allsprites = pygame.sprite.RenderPlain((paddle, ball))

#main loop
    while 1:

        for event in pygame.event.get():
            if event.type == QUIT:
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
            elif event.type == KEYDOWN and event.key == K_UP:
                #move paddle up
            elif event.type == KEYDOWN and event.key == K_DOWN:
               # move paddle down


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

if __name__ == '__main__': main()

Recommended Answers

All 5 Replies

What error?

oops, forget to paste it at the bottom

Traceback (most recent call last):
File "C:\Python26\New folder\pongAttempt.py", line 106, in <module>
if __name__ == '__main__': main()
File "C:\Python26\New folder\pongAttempt.py", line 80, in main
paddle = Paddle()
File "C:\Python26\New folder\pongAttempt.py", line 29, in __init__
self.image, self.rect = load_image('paddle.bmp')
File "C:\Python26\New folder\pongAttempt.py", line 18, in load_image
raise SystemExit, message
SystemExit: Couldn't open data\paddle.bmp

Where are you placing your paddle.bmp file?

the img files and py file are all in the same folder


Im having the same problem. after three months with no reply, has anyone come up with a solution to this?

More specifically, the problem is that pygame wont load any images. none at all. please dont tell me to put the images in the same folder or to write out the entire path to the image. none of that works.

Ive tried this on mac and windows, using different versions of python and pygame. still nothing.

I have read the entire internet. yes, the entire internet. nothing.

To be honest, this is quite frustrating and i would appreciate any help

Be a part of the DaniWeb community

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