1
    if keyboard.h and start == False:
        start = True
        print("hello?")
        dotty_move()
    if keyboard.h and start == True:
        start = False
        print("bye")


    dot1.pos = x, y
    dot2.pos = xi, yi


def dotty_move():
    global speed, speedi, start
    if start == True:
        animate(dot1, duration = speedi, pos = (xi, 0), on_finished = dot_move())
        animate(dot2, duration = speed, pos = (x, 0), on_finished = dot_move())
def dot_move():
    global speed, speedi, start, x, y, xi, yi, dot1
    if start == True:
        animate(dot1, pos=(0, 425), on_finished = dotty_move())
        print("test")
        animate(dot2, duration = speed, pos = (x, 425), on_finished = dotty_move())


dot_move()

Hello! Everything before the dotty_move() subroutine (I have really bad names for my subroutines haha) is in the update() subroutine.

I am trying to have the input of the key "h" only happen once, but it seems to input 3 times if I press it for a split second. I think this is because it's in the update class, but I couldn't find another way to check for the key press.

This code is meant to make two dots in pre-set x coordinates move up and down over and over with a defined speed whenever you press "h". If you press "h" again it is supposed to stop this. The reason I think this isn't working at the moment is because of the issue I stated earlier: it's turning on and off so it never does anything.

But I don't know. If anyone can help it, would be really appreciated. Thanks :)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Whi_24
  • 41
  • 4
  • In order to get help, you will need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – alec_djinn Sep 29 '22 at 17:03

1 Answers1

0

The issue is here:

if keyboard.h and start == False:
    start = True
    print("hello?")
    dotty_move()
if keyboard.h and start == True:
    start = False
    print("bye")

Execute this code in your mind:

  • Is keyboard.h is pressed and start = false (yes it is!)
    • start := true

And then straight away immediately after:

  • Is keyboard.h is pressed and start = true (yes, we just set it above!)
    • start := false

So as soon as start is set, it becomes un-set.

Probably just an "else-if" (elif:) will fix it.

if keyboard.h and start == False:
    start = True
    print("hello?")
    dotty_move()
elif keyboard.h and start == True:
    start = False
    print("bye")

Because only one of the logical expressions will match.

You may also have timing related issues, where your program is running so fast, it still turns off straight after turning on. To handle this, maybe measure the time between on/off with something like pygame.time.get_ticks().

next_click_time = 0   # time [h] can be pressed again

# ...

if keyboard.h:
    time_now = pygame.time.get_ticks()
    if ( time_now > next_click_time ):
        next_click_time = time_now + 300   # milliseconds in the future
        start = not start                  # invert start
        if ( start ):
            # turning on
            print("hello?")
            dotty_move()
        else:
            # turning off
            print("end")
Kingsley
  • 14,398
  • 5
  • 31
  • 53