[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Creating several instances of same image
Hi Folks,
Im attempting to write a little space invaders type game.
Im having problems working out how to get my aliens displayed onto the
screen and then being able to manipulate them.
I want the game to look like the original space invaders.
Here's the relevant code:
>class Alien(pygame.sprite.Sprite):
> """ aliens only move as far left/right and the most left/right alien
can move """
> def __init__(self):
> pygame.sprite.Sprite.__init__(self)
> self.image, self.rect = loadImg('alien.png')
># Now create some Aliens()
> alienDict = {}
> for alien in range(1,13):
> alienDict[alien] = Alien()
Im thinking that Im doing this in completely the wrong way.
Could anyone point me in the right direction to get 12 aliens on the screen
(4 x 3).
Ive looked at the code from the Aliens demo included with pygame, but thats
different from what I want to do.
Also, I appreciate that my problem is probably as much python related as
pygame related, but Ive been doing python for about 4 months and want to
apply what I've learnt to pygame.
Ive attached the whole code in case that helps.
Many thanks
Nick.
import pygame
from pygame.locals import *
import sys
# image sizes:
# defender.png 40 x 40
# alien.png 30 x 20
# laser.png 6 x 20
screenSize = (640,480)
black = (0,0,0)
def loadImg(img):
image = pygame.image.load(img).convert()
return image, image.get_rect()
class Defender(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = loadImg('defender.png')
self.rect.move_ip(0,440)
def moves(self, keyPressed):
xvalue = 5
if keyPressed == 'K_RIGHT' and self.rect.right < 640:
self.rect.move_ip(xvalue,0)
elif keyPressed == 'K_LEFT' and self.rect.left > 0:
self.rect.move_ip(-xvalue,0)
else:
pass
class Alien(pygame.sprite.Sprite):
""" aliens only move as far left/right and the most left/right alien can move """
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = loadImg('alien.png')
class Laser(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = loadImg('laser.png')
def main():
screen = pygame.display.set_mode(screenSize)
pygame.display.set_caption('Invaders')
background = pygame.Surface(screen.get_size()).convert()
background.fill(black)
screen.blit(background, (0,0))
pygame.key.set_repeat(15,15)
defender = Defender()
laser = Laser()
# Aliens
alienDict = {}
for alien in range(1,13):
alienDict[alien] = Alien()
print alienDict[alien].image
print alienDict
allsprites = pygame.sprite.RenderPlain((defender)) # + laser/aliens
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
defender.moves('K_RIGHT')
if event.key == K_LEFT:
defender.moves('K_LEFT')
screen.blit(background, (0,0))
allsprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
main()