1

So, I have been working on this game called Space Invader. Everything is doing great except for one thing which is the sound. As in the below code, I've imported winsound module and it is not doing well. When I fire the bullet it makes the fire noise which I wanted but the problem is that I've also added background music, and whenever I hit fire it stops the background music and starts making fire noises until it hits the target. I want background music to run continuously. I've also seen many Youtube videos but couldn't get any help. Can anyone tell me the mistake which I've made in the below code?

import turtle
import random
import math
import winsound

score = 0
highscore = 0
targets = []
live_remaining = 3

screen = turtle.Screen()
screen.title("Space War")
screen.bgcolor("Black")
screen.setup(width=800, height=770)
screen.bgpic("Space.gif")
screen.addshape("Spaceship.gif")
screen.addshape("Missile.gif")
screen.addshape("ufo.gif")
screen.addshape("gameover.gif")
screen.addshape("youwin.gif")
screen.tracer(0)
winsound.PlaySound("bgmusic.wav", winsound.SND_ASYNC)

spaceship = turtle.Turtle()
spaceship.speed(0)
spaceship.shape("Spaceship.gif")
spaceship.penup()
spaceship.ht()
spaceship.goto(0, -320)
spaceship.st()
spaceship.direction = "stop"
spaceshipspeed = 15

num_of_targets = 50

for i in range(num_of_targets):
    targets.append(turtle.Turtle())

target_start_x = -250
target_start_y = 275
target_number = 0

for target in targets:
    target.shape("ufo.gif")
    target.penup()
    target.speed(0)
    x=target_start_x + (50 * target_number)
    y=target_start_y
    target.goto(x, y)
    target_number += 1 
    if target_number == 10:
        target_start_y -= 20
        target_number = 0
targetspeed = 0.5

fires=turtle.Turtle()
fires.shape("Missile.gif")
fires.penup()
fires.ht()
fires.speed(0)
fires.goto(0, -340)
fires.direction = "stop"
firesspeed=3

fire = turtle.Turtle()
fire.shape("Missile.gif")
fire.penup()
fire.ht()
fire.speed(0)
fire.goto(0, -340)
fire.direction = "stop"
firespeed = 4
firestate = "ready"

sb=turtle.Turtle()
sb.shape("square")
sb.color("white")
sb.ht()
sb.penup()
sb.goto(0,320)
sb.write("Score: 0 | High Score: 0", align="center", font=("courier", 30, "bold"))

life = turtle.Turtle()
life.shape("square")
life.ht()
life.penup()
life.goto(-350,-350)
life.color("white")
life.write("Life remaining: 3", font=("courier",10,"bold"))


def go_left():
    x = spaceship.xcor()
    x -= spaceshipspeed
    if x < -340:
        x = -340
    spaceship.setx(x)


def go_right():
    x = spaceship.xcor()
    x += spaceshipspeed
    if x > 340:
        x = 340
    spaceship.setx(x)


def go_forward():
    global firestate
    if firestate == "ready":
        firestate = "fire"
        x = spaceship.xcor()
        y = spaceship.ycor()
        fire.setposition(x, y)
        fire.st() 
        winsound.PlaySound("fire.wav", winsound.SND_ASYNC)


def move():
    pass


screen.listen() 
screen.onkeypress(go_left, "a")    
screen.onkeypress(go_right, "d")
screen.onkeypress(go_forward, "space")  

while True:
    screen.update()
   
    for target in targets:
        x = target.xcor()
        x += targetspeed
        target.setx(x)

        if target.xcor() > 340:
            targetspeed *= -1
            for t in targets:    
                y = t.ycor()
                y -= 20
                t.sety(y)

        if target.xcor() < -340:
            targetspeed *= -1
            for t in targets:
                y = t.ycor()
                y -= 20
                t.sety(y)

        if fire.distance(target) < 20:
            fire.direction = "stop"
            fire.ht()
            fire.clear()
            firestate = "ready"
            fire.goto(10000000, 1000000)            
            target.goto(0, 10000000) 
            score += 10
            winsound.PlaySound("explosion.wav", winsound.SND_ASYNC)

            if score > highscore:
                highscore = score
            sb.clear()
            sb.write("Score: {} | High Score: {}".format(score,highscore), align="center", 
font=("courier", 30, "bold"))

            if score == 500:
                screen.clear()
                screen.bgcolor("black")
                gf = turtle.Turtle()
                gf.shape("youwin.gif")
                gf.goto(0, 0)
                winsound.PlaySound("gamewin.wav",winsound.SND_ASYNC)

        if spaceship.distance(target) < 60:
            spaceship.goto(0, -320)
            target.goto(-320, 300)
            live_remaining -= 1
            winsound.PlaySound("explosion.wav",winsound.SND_ASYNC)

            life.clear()
            life.write("Life remaining: {}".format(live_remaining), font=("courier",10,"bold"))

            if live_remaining == 0:
                screen.clear()
                screen.bgcolor("black")
                gover=turtle.Turtle()
                gover.shape("gameover.gif")
                gover.goto(0, 0)
                winsound.PlaySound("gamelose.wav", winsound.SND_ASYNC)  
                replay = input("Press r to retry",)

    y = fire.ycor()
    y += firespeed
    fire.sety(y)
    if fire.ycor() > 300:
        fire.ht()
        firestate = "ready"  

    move()
screen.mainloop()
accdias
  • 5,160
  • 3
  • 19
  • 31
HaRrY
  • 13
  • 3
  • Maybe adding the flag [`SND_NOSTOP`](https://docs.python.org/3/library/winsound.html#winsound.SND_NOSTOP) would help? – Rafael-WO Sep 29 '21 at 11:02
  • Nope, by doing this it's playing music but causing game to stop display – HaRrY Sep 30 '21 at 13:22
  • But did you pass both, `SND_ASYNC` and `SND_NOSTOP`? Else it might help to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (without all the GIFs etc., just a screen and music) so that other people can try out the code. – Rafael-WO Oct 01 '21 at 07:44
  • Nope, it didn't help. – HaRrY Oct 20 '21 at 17:11

0 Answers0