I'm trying to write a program where the image for the 'enemy' I have changes once its health goes to 0. I have different classes for each element, but I have this under this class.
class Enemy(pygame.sprite.Sprite):
def __init__(self, gs=None):
pygame.sprite.Sprite.__init__(self)
...initialization stuff...
self.image = pygame.image.load("enemy.png") #enemy image
self.rect = self.image.get_rectangle()
self.hp = 40 #initial health
def damage(self):
if self.rect.colliderect(self.user.rect): #collision/how the health goes down
self.hp = self.hp - 5
Now here's the part I'm curious about, I want to load a new image that replaces the old image I have for this enemy. Can I do/add (in the damage function)
if(self.hp == 0):
self.image = pygame.image.load("dead.png")
Will this replace it? Or just load another image on top of it?
Let me know what I'm missing, thank you!
You should load all your images into your init method when you create the object and then you can do the assigning/changing later using lists. Here is an example:
class Enemy(pygame.sprite.Sprite):
def __init__(self, gs=None):
pygame.sprite.Sprite.__init__(self)
self.images = [pygame.image.load("enemy.png"), pygame.image.load("dead.png")]
self.current_image = self.images[0]
self.rect = self.current_image.get_rectangle()
Then later you can do:
if(self.hp == 0):
self.current_image = self.images[1]
This will in fact, as per your concern, replace the current image instead of just drawing over it. Hope that helps.
Related
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.
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)
I´m working on improving my pythonic coding and want to know if there is a way to always point to an attribute's function in the same class (including "updating" its values)
the simplified code looks like this:
class xyz:
def __init__(self, widht, height):
self.image = pygame.Surface((width, height))
self.rect = self.image.get_rect()
obj = xyz(42, 42)
obj.image = pygame.Surface((some_other_width, some_other_height))
# obj.rect would still be of size 42x42 and not the new values
since the image is varying in size, changing very often and my class has to have a attribute named rect with the size of the curent image is there maybe a magic method that could the work of the update_rect() function (tried playing around with self__getattribute()__ a bit but that didn´t work)
def update_rect(self):
self.rect.width, self.rect.height = self.image.get_width(), self.image.get_height()
As jonrsharpe suggested in a comment, you can use a property. But make image a property, not rect, like this:
import pygame
class xyz(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
self._image = pygame.Surface((width, height))
self.rect = self.image.get_rect()
#property
def image(self):
return self._image
#image.setter
def image(self, value):
self._image = value
self.rect = self._image.get_rect(topleft=self.rect.topleft)
obj = xyz(42, 42)
obj.rect.topleft = (300, 300)
print(obj.rect.width) # prints 42
print(obj.rect.x) # prints 300
obj.image = pygame.Surface((100, 100))
print(obj.rect.width) # prints 100
print(obj.rect.x) # prints 300
This way, you can also keep the position that is usually stored in the rect attribute.
This is a the class of a sprite that goes left and right on the screen, when it hits the boundaries, it makes a "boing" sound and goes the opposite direction, everything works perfectly fine except for there is no boing sound when it hits the edge
class MySprite(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("vegetables.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.rect.left = 0
self.rect.top = 167
self.__direction = 10
def update(self):
self.rect.left += self.__direction
sound = pygame.mixer.Sound("Bounce.mp3")
sound.set_volume(0.5)
if (self.rect.left < 0) or (self.rect.right > screen.get_width()):
sound.play()
self.__direction = -self.__direction
If you want the class to play its own sound, just load it like any attribute on __init__.
class MySprite(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("vegetables.gif")
self.image = self.image.convert()
self.sound = pygame.mixer.Sound("Bounce.mp3") #like this
self.rect = self.image.get_rect()
self.rect.left = 0
self.rect.top = 167
self.__direction = 10
Then whenever it's correct to do so, just call self.sound.play().
def update(self):
self.rect.left += self.__direction
if (self.rect.left < 0) or (self.rect.right > screen.get_width()):
self.sound.play() #as seen here
self.__direction = -self.__direction
For whatever it's worth - if you're going to do it in this way (have the sprite play its own sounds, etc), I would recommend loading them beforehand and then passing them as arguments (perhaps default arguments to avoid errors), such that each instance of the class might call a unique sound if need be.
So in your code prior to these classes, one could do something like:
JumpSound = pygame.Mixer.Sound("jump.wav")
BonkSound = pygame.Mixer.Sound("bonk.wav")
#etc etc etc...
...and then later on, pass the sounds as arguments:
class MySprite(pygame.sprite.Sprite):
def __init__(self, jumpsound, bonksound):
#...your other code precedes...
self.jumpsound = jumpsound
self.bonksound = bonksound
#...your other code continues...
myHero = MySprite(JumpSound, BonkSound)
The names are a little bit lousy b/c they are the same barring the CamelCasing, but forgetting that, this is probably a much cleaner approach. You can set your volume on the sounds way before they are passed into the sprites, along with whatever other changes you feel are necessary before the sprite gets ahold of them.
I am trying to create a pacman game with pygame but I have run into a few problems. It it saying that the 'Food' has no image.
This is my pacman game [code redacted].
The problem is this area here something is wrong and it tells me that food has no attribute image
class Food(pygame.sprite.Sprite):
def __init__(self,x,y,color):
pygame.sprite.Sprite.__init__(self)
pygame.image = pygame.Surface([7,7])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
def update (self,player):
collidePlayer = pygame.sprite.spritecollide(self,player,False)
if collidePlayer:
food.remove
Removing all the irrelevant bits, do you see the difference between the following Sprite subclasses' __init__ methods?
class Wall(pygame.sprite.Sprite):
def __init__(self,x,y,width,height, color):#For when the walls are set up later
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height]) # <-------------
self.image.fill(color)
class player(pygame.sprite.Sprite):
def __init__ (self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([13,13]) # <-------------
self.image.fill(white)
class Food(pygame.sprite.Sprite)
def __init__(self,x,y,color):
pygame.sprite.Sprite.__init__(self)
pygame.image = pygame.Surface([7,7]) # <----- this one is different!
self.image.fill(color)
The reason you're getting an error saying that self doesn't have an image attribute is because you didn't set self.image, you stored the image in the pygame module itself.
PS: The lines which look like
food.remove
seem suspicious to me. If remove is a method, it'd be called by food.remove(), and food.remove wouldn't do anything.
Good day,
I have like 15 images I need to be buttons. I have buttons working with a Box() (Box - looks like this)
class Box(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((35, 30))
self.image = self.image.convert()
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.centerx = 25
self.rect.centery = 505
self.dx = 10
self.dy = 10
I am trying to make the buttons work with image sprites. So I attempted to copy the class style of the box and do the same for my Icons.. code looks like this...
class Icons(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/airbrushIC.gif").convert()
self.rect = self.image.get_rect()
self.rect.x = 25
self.rect.y = 550
the code in the main()
rect = image.get_rect()
rect.x = 25
rect.y = 550
ic1 = Icons((screen.get_rect().x, screen.get_rect().y))
screen.blit(ic1.image, ic1.rect)
pygame.display.update()
This code produces a positional (accepts 1 argument but 2 are there) error or an image is not referenced error (inside the Icon class).
I'm unsure if this is the right way to go about this anyways.. I know for sure that I need to load all the images (as sprites)... store them in an array... and then have my mouse check if it is clicking one of the items in the array using a for loop.
Thanks.
You are trying to pass an argument into Icons(), but your __init__() method takes no arguments. If you wanted to pass those onto the Sprite() constructor, then you probably wanted something like:
class Icons(pygame.sprite.Sprite):
def __init__(self, *args):
pygame.sprite.Sprite.__init__(self, *args)
...
This accepts any number of extra arguments (*args) using the star operator, then passes them into the sprite constructor.