0

I'm new to python and I can't find any answers online, whenever I move my Player (turtle) it stops the Enemy (Genshape, turtle). Can anyone help with my code?

These are my functions for movement, left and right:

def move_left():
    if player.xcor() == 100:
        winsound.Beep(200, 100)
        player.setx(0)
    elif player.xcor() == 0:
        winsound.Beep(200, 100)
        player.setx(-100)


def move_right():
    if player.xcor() == -100:
        player.setx(0)
        winsound.Beep(200, 100)
    elif player.xcor() == 0:
        winsound.Beep(200, 100)
        player.setx(100)

This is my code for the looping statement:

generate_shapes()
screen.listen()
screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")

while True:
    Genshape.clear()
    screen.update()
    Genshape.sety(Genshape.ycor() - 60)
    if Genshape.ycor() < -352:
        generate_shapes()
turtle.done()

Screenshot:

screenshot

If you need it, here's the full code on GitHub: https://github.com/googlez-bit/simpleGame.git

I want to remove the loop interruption whenever I move.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
JL S
  • 1
  • Are you using `tracer` and have you looked at, for example, [How to move multiple turtles at the same time?](https://stackoverflow.com/questions/40050438/how-to-move-multiple-turtles-at-the-same-time)? All relevant code should be in the question itself as a [mcve]. Thanks – ggorlen Oct 03 '22 at 02:46

1 Answers1

0

winsound.Beep is synchronous. If you beep for 100ms, your code will block for 100ms. That's a leftover from the early early days of Windows, when the speaker was directly connected to an I/O register, and the Beep API had to generate the sound by setting the register in real time at the proper frequency.

You need to find a different sound library or API. If you can generate your beep in a short wave file, you can use:

    winsound.PlaySound( sound, winsound.SND_ASYNC )
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30