I can't get images to load.. I always get an error
here's my code so far:
#pong
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)
try:
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):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('paddle.bmp')
class ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__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
pass
if self.rect.right > self.area.right:
#point ot player 2
#new ball
pass
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():
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("Pong")
pygame.mouse.set_visible(0)
background = pygame.Surface((640,480))
background = background.convert()
background.fill((250,250,250))
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))
pygame.display.flip()
clock = pygame.time.Clock()
paddle = Paddle()
ball = Ball()
allsprites = pygame.sprite.RenderPlain((paddle, ball))
#main loop
while 1:
clock.tick(40)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == KEYDOWN and event.key == K_UP:
#move paddle up
pass
elif event.type == KEYDOWN and event.key == K_DOWN:
# move paddle down
pass
allsprites.update()
screen.blit(background, (0,0))
allsprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__': main()