0

I want to store the final pong position result into a variable after running my timer_event call for one second. I'm not sure how to do so though. Here are my functions

def get_final_position(self):

    return (pong.rect.centerx, pong.rect.centery)

def hard_AI_mode(self, pong, paddle):

    initial_pong_position = (pong.rect.centerx, pong.rect.centery)
    pygame.time.set_timer(self.get_final_position(), 1000)

    final_pong_position = ???
turtlefish12
  • 241
  • 3
  • 12
  • Have you thought about creating a 'pong' object that stores the location? That could be shared amongst different functions. otherwise you could try using a 'global' variable, but that is generally discouraged. (a pong object would then have getter & setter functions to correspond with actions) – boethius Nov 30 '17 at 17:16
  • Please try to improve the quality of your questions. They should be easy to understand for the responders and future readers with similar problems. Provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) and describe your goals and issues precisely. – skrx Nov 30 '17 at 17:20
  • 1
    Also, check out how [`pygame.time.set_timer`](https://stackoverflow.com/a/15056742/6220679) should be used. You have to pass a `USEREVENT` to it, check in the event loop if this `USEREVENT` has been added to the queue and then call your function inside of the event loop. – skrx Nov 30 '17 at 17:59

2 Answers2

1

As skrx said you use set_timer in wrong way.

It has to send event which you have to catch in mainloop and execute your function.

import pygame

# --- constants ---

AFTER_SECOND = pygame.USEREVENT + 1

# --- functions ---

def get_final_position():
    print('one second later')

    # stop event AFTER_SECOND
    pygame.time.set_timer(AFTER_SECOND, 0)

    return pong_rect.center

# --- main ---

pygame.init()
screen = pygame.display.set_mode((400, 300))

# - objects -

pong_rect = pygame.Rect(0, 0, 10, 10)

# - start -

print('start game')

initial_pong_position = pong_rect.center
print('initial:', initial_pong_position)

# repeat sending event AFTER_SECOND every 1000ms
pygame.time.set_timer(AFTER_SECOND, 1000)

# --- mainloop ---

clock = pygame.time.Clock()
is_running = True

while is_running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            is_running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                is_running = False

        elif event.type == AFTER_SECOND:
            final_pong_position = get_final_position()
            print('initial:', initial_pong_position)
            print('  final:', final_pong_position)

    screen.fill((0,0,0))
    pygame.display.update()
    clock.tick(25)

pygame.quit()
furas
  • 134,197
  • 12
  • 106
  • 148
0

In this case, you should first create a sprite for pong if you dont have it already, there are several guides on the web. Inside the "pong" sprite class, place a pos variable and a vel variable.

Pos = (x, y) # x and y being position pong needs to be
Vel = (0, 0) # we will change this later

In the pong sprites update function, add self.pos to self.vel and set self.rect.center to pos When a key is pressed, we change vel to (x, y) # x is left/right, y is up/down When a key is up, we change vel back to (0, 0) Inside get_final_position, set centerx = pong.pos[1] and centery = pong.pos[2] and then return centerx and centery

I hope this is the response you are looking for.

Edit

Alright, thats easy, just create a varible called dt and set it to clock.tick(FPS_You_are_using) / 1000 define it before the game loop starts. after, when in get_final_position, set a variable called timer, and then make a loop that subtracts dt from timer, once timer = 0, exit that loop and then do the rest

Mercury Platinum
  • 1,549
  • 1
  • 15
  • 28
  • you should see [previous question](https://stackoverflow.com/questions/47568136/how-to-get-the-initial-and-final-position-of-ball-after-one-second). Main problem is to get value after one second. – furas Nov 30 '17 at 20:40