0

I have only been learning pygame for a few weeks mainly following different arcade style game tutorials. Have adapted a pong game and am trying to insert two delays, one when ball hits border (edge of screen behind paddle) then a reset and time delay before the ball starts moving again from the centre. This way the players can see where the ball contacted the edge of screen and then have all sprites reset to their original position before moving off again.

This is the current code:

class Paddle(pygame.sprite.Sprite):
    def reset(self):
        paddleA.rect.x = 20
        paddleA.rect.y = 200
        paddleB.rect.x = 670
        paddleB.rect.y = 200
class Ball(pygame.sprite.Sprite):
    def reset(self):  
        ball.rect.x = 345
        ball.rect.y = 195
        pygame.time.delay(1000)
        self.velocity = [randint(4,8), randint(-8,8)]
    def hit(self):
        self.velocity = 0
        pygame.time.delay(500)    
asl = pygame.sprite.Group()
asl.add(paddleA)
asl.add(paddleB)
asl.add(BorderA)        
asl.add(BorderB)
asl.add(ball)

while GameOn:
    
    asl.update()
    if pygame.sprite.collide_mask(ball, BorderA):
        scoreB+=1
        ball.hit()
        paddleA.reset()
        paddleB.reset()
        ball.reset()
    if pygame.sprite.collide_mask(ball, BorderB):
        scoreA+=1
        ball.hit()
        paddleA.reset()
        paddleB.reset()
        ball.reset()

My guess is there needs to be an update in there, but I have tried adding asl.update() and pygame.display.flip() in a couple of different spots to try and get it to reset the sprites before going to the second time.delay but it never works, the delay just lasts longer and the ball still moves as soon as it moves back to the centre.

  • Time delays like this lock the whole program up. What you should investigate is using `pygame.time.get_ticks()` for a time-stamp, and then check in your main-loop for when time_stamp + some_delay is in the past, using this as a trigger-point for the new/different operation. – Kingsley Sep 18 '20 at 00:08
  • 1
    Here's an [answer](https://stackoverflow.com/a/60822906/7675174) of mine that might help, it shows how to use a custom event to set a timer. So in your case on collision with the edge, you could set the timer, then when the timer event occurs, reset the positions. Providing a [mcve] makes it easier for the community to answer your question, you may also find it beneficial to read [How to Ask Questions](https://stackoverflow.com/questions/how-to-ask). – import random Sep 18 '20 at 07:07

0 Answers0