Reputation: 93
I am making a space shooter game which has a planets in the background. I've decided to draw some planets in the background and when i move to the right the planets should move to the left. That is for the player to feel the spaceship is moving around the space. However i could have done it for one planet only. When try to apply the other planets in one class it is constantly changing to the other planet.
lanetdic = {'planets':[]}
imagestoload = ['Feza/graphs/sprites/saturne.png']
for i in imagestoload:
img = pg.image.load(i).convert_alpha()
planetdic['planets'].append(img)
this is to load the sprite. and in the below i created a class for the planets.
class Planets(pg.sprite.Group):
def __init__(self):
pg.sprite.Group.__init__(self)
self.frame = 0
self.image = planetdic['planets'][self.frame]
self.rect = self.image.get_rect()
self.rect.center = (500+100*self.frame,HEIGHT/2)
self.pos = vec(500,HEIGHT/2)
self.vel = vec(0,0)
self.acc = vec(0,0)
def update(self):
#self.acc = vec(0,0)
self.frame = (self.frame + 1)%len(planetdic['planets'])
Maybe it is not sensible to create a class for a planet but i couldnt have find anotherway if there is please tell me.
if we get to the point again. In the below i made a for loop to load images. and used again the same for loop
planetdic = {'planets':[]}
imagestoload = ['Feza/graphs/sprites/saturne.png','Feza/graphs/sprites/jupiter.png','Feza/graphs/sprites/venus.png','Feza/graphs/sprites/uranus.png','Feza/graphs/sprites/neptune.png']
for i in imagestoload:
img = pg.image.load(i).convert_alpha()
planetdic['planets'].append(img)
When i apply multi images it changes one to other in miliseconds how can i prevent this happen. I just want to show every planets in the background and make them move.
Upvotes: 1
Views: 485
Reputation: 210909
How to draw multiple sprites in one class
You do not do that. However, you can create multiple instance objects of a class.
The image (Surface) must be a parameter of the constructor of the class Planet
. The Planet
class is a subcalss of sprite.Sprite
, not sprite.Group
:
class Planet(pg.sprite.Sprite):
def __init__(self, image):
pg.sprite.Group.__init__(self)
self.frame = 0
self.image = image
self.rect = self.image.get_rect()
self.rect.center = (500+100*self.frame,HEIGHT/2)
self.pos = vec(500,HEIGHT/2)
self.vel = vec(0,0)
self.acc = vec(0,0)
Create a new instance object of the Planet
class for each planet:
planets = pg.sprite.Group()
imagestoload = ['Feza/graphs/sprites/saturne.png','Feza/graphs/sprites/jupiter.png','Feza/graphs/sprites/venus.png','Feza/graphs/sprites/uranus.png','Feza/graphs/sprites/neptune.png']
for filepath in imagestoload:
img = pg.image.load(filepath).convert_alpha()
planet = Planet(img)
planets.add(planet)
Upvotes: 1