Pygame project sound mixing issue...

Joined
Jul 20, 2023
Messages
56
Reaction score
2
Im making a tanks game. Left and Right arrow keys move the tank left and right and space bar + ENTER fire a round. I have a sound uploaded for both the tank moving and the sound of heavy artillery being fired. They both work individually but when I press fire while moving the fire does not sound. Only the moving sound continues. However I added a print statement to tell me if it detected the space bar hit and it definitely does every time. I can get two sounds to go at the same time by hitting the space bar and then moving but it doesnt work in the other way. Whats the dil?
Python:
import pygame as PG

# INITIALIZE PYGAME
PG.init()

# --------- S E T T I N G S --------------
# SET CONSTANTS
screen_size = SCREEN_WIDTH, SCREEN_HEIGHT = (1200, 600)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

background_image_file = 'assets/IMAGES/bg1.png'

# TANK SETTINGS
tank_image_file = 'assets/IMAGES/grey_tank_1.png'
tank_moving_sound_file = 'assets/SOUND/moving_tank_short_2.wav'
tank_fire_round_sound_file_1 = 'assets/SOUND/tank_short_shot.wav'
tank_fire_round_sound_file_2 = 'assets/SOUND/tank_shoots_1.wav'

# SET SCREEN MODE AND BACKGROUND IMAGE
screen = PG.display.set_mode(screen_size)
screen_rect = screen.get_rect()
bg_img = PG.image.load(background_image_file)
bg_img = PG.transform.scale(bg_img, screen_size).convert()
bg_img_rect = bg_img.get_rect()

# LOAD TANK SPRITE IMAGE
# RIGHT SIDE TANK (LEFT - FACING)
IMG_tank_L_facing = PG.image.load(tank_image_file).convert()
IMG_tank_L_facing = PG.transform.scale_by(IMG_tank_L_facing, .6)
IMG_tank_L_facing.set_colorkey(WHITE)
tank_L_facing_img_rect = IMG_tank_L_facing.get_rect()
# LEFT SIDE TANK (RIGHT - FACING)
IMG_tank_R_facing = PG.transform.flip(IMG_tank_L_facing, True, False)
IMG_tank_R_facing.set_colorkey(WHITE)
tank_R_facing_img_rect = IMG_tank_R_facing.get_rect()

# LOAD TANK SOUNDS
SOUND_tank_moving = PG.mixer.Sound(tank_moving_sound_file)
SOUND_fire_short_round = PG.mixer.Sound(tank_fire_round_sound_file_1)
SOUND_fire_long_round = PG.mixer.Sound(tank_fire_round_sound_file_2)

# SET CHANGING OBJECT ATTRIBUTES
moving = False
moving_direction = 0
pos_x = float(tank_R_facing_img_rect.x)

def update_screen():
    screen.blit(bg_img, (0, 0))
    screen.blit(IMG_tank_R_facing, tank_R_facing_img_rect)
    screen.blit(IMG_tank_L_facing, tank_L_facing_img_rect)

    PG.display.update()

# XXXXXXXXXXXXXXXXXXXXXXXXXX  G A M E   L O O P  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


def run_game():
    global moving, moving_direction, pos_x
    # POSITION TANKS ON SCREEN
    tank_R_facing_img_rect.topleft = (10, (SCREEN_HEIGHT / 2) + 60)
    tank_L_facing_img_rect.topright = (SCREEN_WIDTH - 10, tank_R_facing_img_rect.y)
    pos_x = float(tank_R_facing_img_rect.x)  # THIS IS TO KEEP THE 10pix SPACING FROM REVERTING BACK TO 0

    while True:
        # EVENT HANDLER
        for event in PG.event.get():
            # QUIT GAME
            if event.type == PG.QUIT or event.type == PG.KEYDOWN and event.key == PG.K_ESCAPE:
                exit()

            # KEY-DOWN EVENTS
            elif event.type == PG.KEYDOWN:
                match event.key:
                    # MOVE RIGHT
                    case PG.K_RIGHT:
                        moving = True
                        moving_direction = 1
                    # MOVE lEFT
                    case PG.K_LEFT:
                        moving = True
                        moving_direction = -1
                    # SPACE BAR - FIRE SHORT ROUND
                    case PG.K_SPACE:
                        print('SPACE')
                        SOUND_fire_short_round.play()
                    # ENTER - FIRE LONG ROUND
                    case PG.K_RETURN:
                        SOUND_fire_long_round.play()

            # KEY-UP ENENTS
            elif event.type == PG.KEYUP:
                # STOP MOVING
                if event.key == PG.K_LEFT or event.key == PG.K_RIGHT:
                    moving = False
                    moving_direction = 0

        # UPDATE OBJECT ATTRIBUTES
        if moving:
            pos_x += moving_direction * .2
            tank_R_facing_img_rect.x = pos_x
            SOUND_tank_moving.play()
        else:
            SOUND_tank_moving.stop()

        # UPDATE SCREEN
        update_screen()


if __name__ == '__main__':
    run_game()
bg1.png
grey_tank_1.png


Unfortunately It wont let me upload the sound files. I gotta set up a git hub
 
Joined
Dec 10, 2022
Messages
94
Reaction score
26
This works for me. You can also uncomment the channels if it doesn't work for you.

Python:
# Do the imports
import pygame
import os
from random import random

# Initiate pygame
pygame.init()

# Setup the window
pygame.display.set_mode((800,600))

# Path to executing script
path = os.path.realpath(os.path.dirname(__file__))

# Itialize pygame mixer
pygame.mixer.init(frequency=44100, size=-16, channels=1)

# Sound files
thunder = pygame.mixer.Sound(f'{path}/thunder.mp3')
rain = pygame.mixer.Sound(f'{path}/rain.mp3')

# Create the channels
# channel1 = pygame.mixer.Channel(0)
# channel2 = pygame.mixer.Channel(1)

# Play the rain in a continuous loop
# channel1.play(rain, loops=-1)

rain.play()
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False


    # Set a random number and compare
    # Play the file if number greater than or equal too random number
    if random() >= .9999:
        # channel2.play(thunder)
        # 6 second delay to allow file to play
        # pygame.time.delay(6000)
        thunder.play()
            

    pygame.display.update()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top