Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Long post

I can not seem to get my collisions to work in this game of Pong. Along with the collisions, I would really like some help adding physics to the game (ball hits from NE and bounces back at SW)

I have included some of my code for the game. I am having a few issues that could use some debugging. The ball occasionally goes through the paddles, and occasionally gets an invisible wall at aprox. X= 40px.
All help is greatly appreciated.
Thanks in advance


Python
# Set the width and height of the screen [width, height]
                        size = (700, 500)
                        screen = pygame.display.set_mode(size)
                        done = False
                        #Create a class named sprite to use for the paddles and the ball.
                        class Sprite():
                            def __init__(self,x,y,width,height,color):
                         
                                self.x = x
                         
                                self.y = y
                         
                                self.width = width
                         
                                self.height = height
                                
                                self.color= (255,255,255)
                        #attirbute for drawing the sprite(s) to the screen
                            def render(self):
                                pygame.draw.rect(screen,self.color,(self.x,self.y,self.width,self.height))
                         #Create the sprites       
                        Paddle1 = Sprite(50,175,25,150,WHITE])
                        Paddle2 = Sprite(650,175,25,150,WHITE)
                        Ball = Sprite(300,250,25,25,WHITE)
                        #Variables for moving the paddles
                        moveY1,moveY2 = 0,0

This is all of the code that I have for collisions with the ball.
Python
#Commands for the moving ball
    if Ball.y >= 500 or Ball.y <= 0 :
        Ball_move_y *= -1
        Ball_move_x *= -1
    if Ball.x >= 700 or Ball.x <= 0:
        Ball_move_x *= -1
        Ball_move_y *= -1
#Collisions for balls and paddles
    if Ball.x &g= Paddle1.x and Ball.y >= Ball.y and Ball.x <= (Paddle1.x + -50) and Ball.y <= (Paddle1.y + -50):
        Ball_move_x *= -1
        Ball_move_y *= -1
    elif (Ball.x + -50) >= Paddle2.x and (Paddle2.y+ -50) >= Paddle2.y and (Ball.x+ -50) <= (Paddle2.x + -50) and (Ball.y+ -50) <= (Paddle2.y + -50):
        Ball_move_x *= -1
        Ball_move_y*= -1

This is for moving the actual ball, after it has been drawn to the screen (deleted drawing since it is irrelevant to the question).
Python
#Move the ball
Ball.x += Ball_move_x
Ball.y += Ball_move_y

# --- Limit to 60 frames per second
clock.tick(60)

This is the end of the code, minus the quit statement. I'm not sure if the frames per second is relevant to this code.

What I have tried:

I have tried adding a different collision detector, but that seems to make things worse.

After fixing what was pointed out in solutions 1 and 2, my boundaries are working better, but I can not get my paddles to hit my ball.
Posted
Updated 2-Jun-16 9:53am
v2

Quote:
if Ball.y >= 500 or Ball.y <= 0 :
Ball_move_y *= -1
Ball_move_x *= -1

Inverting the x-speed of the ball when it hits a vertical wall is a physical error.

There are other (similar) flaws in your code.
 
Share this answer
 
Comments
Patrice T 1-Jun-16 18:14pm    
5ed
Sergey Alexandrovich Kryukov 1-Jun-16 19:14pm    
Ha-ha, a 5.
—SA
Hugget.N 2-Jun-16 15:35pm    
Thanks for pointing that out, that mistake was because I thought that was how you add physics to the game, and then I forgot to remove it.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

To test your code, either you spy your code waiting for each condition to append and you check that reactions are correct.
otherwise, you set all conditions one by one and call your code to see if it is correct or not.
I agree with flaw spotted in Solution 1.
Another flaw, when the ball hit the border of the field or get outside of the field, you reverse the moving direction, but you forget to put the ball back inside the field. If you unlucky, you can fall into a trap and stuck outside the field, continuously reversing ball direction.
 
Share this answer
 
Comments
Hugget.N 2-Jun-16 15:37pm    
Same with the above mistake, I thought it had to do with physics and never removed it. I will give that a shot here shortly. As for the debugger, I will try that out later tonight.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900