May-12-2023, 08:02 PM
(This post was last modified: May-12-2023, 08:02 PM by deanhystad.)
A laser beam is not a bullet. They are fundamentally different. You should make a special class for the laser beam instead of adding a laser update method to bullet. That way you can let pygame groups take care of all the updating.
I did this in my original post with a line. This is how it would be done with an image.
https://stackoverflow.com/questions/6640...eturn-none
When I saw the error message I assumed the problem was caused by play() returning None, but I did not know why, nor find why, that might happen. The pygame mixer documentation says the sound will not play if there is no channel available, but I could not find where it said it will return None when this happens.
The short answer is that you'll have to check the return value from play() before you try to set_volume() (or you can catch the exception).
I did this in my original post with a line. This is how it would be done with an image.
import pygame class Beam(pygame.sprite.Sprite): beam = pygame.image.load("beam.png") width = beam.get_width() def __init__(self, x, y, speed=20): super().__init__() self.rect = pygame.rect.Rect(x - self.width / 2, y, self.width, 0) self.speed = speed self.height = 600 - y def update(self): self.rect.height += self.speed if self.rect.height >= self.height: self.kill() else: # Scale beam image to approppriate size for sprite image self.image = pygame.transform.scale(self.beam, (self.rect.width, self.rect.height)) pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((200, 400)) beams = pygame.sprite.Group() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: beams.add(Beam(*event.pos)) screen.fill("black") beams.update() beams.draw(screen) pygame.display.flip() clock.tick(30)About your shooting sound volume problem, I found this:
https://stackoverflow.com/questions/6640...eturn-none
When I saw the error message I assumed the problem was caused by play() returning None, but I did not know why, nor find why, that might happen. The pygame mixer documentation says the sound will not play if there is no channel available, but I could not find where it said it will return None when this happens.
The short answer is that you'll have to check the return value from play() before you try to set_volume() (or you can catch the exception).