0

I am trying to move a line across a distance in a specified amount of time. The basic code so far is something like:

    ticks = 0
    aud_time_ms = int(self.AUD_MAN.get_aud_length(selected_aud,milleseconds=True))
    selected_img_width = self.get_img_size(selected_img)[1]

    while self.RUNNING:
        # start with filling screen
        self.WIN.fill(WHITE)

        # blit audio wave images
        self.WIN.blit(ref_wave_img,ref_wave_rect)

        # blit text
        self.WIN.blit(controls_text_surf,(10,0))

        # blit moving line
        pg.draw.rect(self.WIN,RED,self.LINE_RECT)

        if(self.AUD_FINISHED):
            self.WIN.blit(aud_finished_text_surf,(100,100))

        if(self.PLAYING_AUD):
            self.move_ref_aud_line(self.LINE_DX,ticks)
        

        # check events
        for event in pg.event.get():

            if event.type == aud_finished_event:
                self.AUD_FINISHED = True

            # check key presses
            elif event.type == pg.KEYDOWN:
                # check if key pressed == "r", then we record
                if event.key == pg.K_r:
                    # reset the audio
                    self.AUD_FINISHED = False
                    self.PLAYING_AUD = True
                    pg.time.set_timer(aud_finished_event,aud_time_ms)

                    self.move_ref_aud_line(dx=self.LINE_DX,ticks=ticks)

                    print("R")
                # check if key press == "p", then we play the audio
                elif event.key == pg.K_p:
                    # reset the audio
                    self.AUD_FINISHED = False
                    pg.time.set_timer(aud_finished_event,aud_time_ms)

                    print("P")
            


            # if event is quit, quit
            if event.type == pg.QUIT:
                self.RUNNING = False


        # update the surf
        pg.display.update()
        self.CLOCK.tick(30)
        ticks = self.CLOCK.tick(60)
    
    pg.quit()

Basically, I want my self.LINE_RECT to move at velocity self.LINE_DX a distance selected_img_width in a time length aud_time_ms.

How can I do this, so that a line would move across an image fully in say, 2 seconds, when pressing "p"?

Steak
  • 386
  • 2
  • 10
  • 2
    if you runs with 30 FPS ( `CLOCK.tick(30)` ) then you need 60 moves/loops (`2seconds * 30FPS`) and you may divide distance by `60` - `dx = distance_x/60`, `dy = distance_y/60` and add in every loop `x + dx` , `y += dy`. But it wouild be better to keep `x,y` as float instead of integer. – furas Apr 22 '21 at 00:19
  • 1
    you may get current time and set `stop_move = current_time + 2000` (2s) and later in every loop check if new current time is smaller then `stop_move`. When it smaller then you move object, When it bigger then you don't move – furas Apr 22 '21 at 00:23
  • 1
    if you want to move also after releasing key `P` then you shouldn't move it inside `for event` loop. Key `P` should only set `stop_move = ...` (and eventually `rect_is_moving = True`) and rest should be done outside `for event` loop. `if rect_is_moving and current_time < stop_move: ...` – furas Apr 22 '21 at 00:26
  • 1
    the same way you can use `start_move = current_time + 2000` to start with delay. – furas Apr 22 '21 at 00:33
  • Thank you! I did exactly that, works like a charm! – Steak Apr 22 '21 at 21:50

0 Answers0