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?
Unfortunately It wont let me upload the sound files. I gotta set up a git hub
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()
Unfortunately It wont let me upload the sound files. I gotta set up a git hub