1
\$\begingroup\$

I am trying to make some games using classes. But I got a problem when trying to make the ping pong game. I made 2 paddles and a ball but I just do not understand how should I make the collision between the paddles and the ball using classes. Sorry if my code is kind of unorganized and thank you for your help and patience

import pygame
from sys import exit 

pygame.init()

#Screen details
screen_width=1000
screen_height=700
screen=pygame.display.set_mode((screen_width,screen_height))


class Paddle():
    
    def __init__(self, x,y,width,height,color):
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.color=color
        
    def drawing(self):
        display=pygame.display.get_surface()
        pygame.draw.rect(display,(self.color),(self.x,self.y,self.width,self.height),0)
        
        
    def moving1(self):
        self.k=pygame.key.get_pressed()
        if self.k[pygame.K_UP]:
            self.y-=1
        if self.k[pygame.K_DOWN]:
            self.y+=1
    
    def moving2(self):
        self.k=pygame.key.get_pressed()
        if self.k[pygame.K_p]:
            self.y-=1
        if self.k[pygame.K_u]:
            self.y+=1
            
            
class Ball():
    
    def __init__(self,posx,posy,radius,color,speed):
        self.posx=posx
        self.posy=posy
        self.radius=radius
        self.color=color
        self.speed=speed
        
    def drawing(self):
        display=pygame.display.get_surface()
        pygame.draw.circle(display,(self.color),(self.posx,self.posy),self.radius,0)
        
        
    def moving(self):
        self.posx-=self.speed

        #I have to continue here. Not finished
    
    
        
paddle1=Paddle(0,130,50,100,(255,255,255))
paddle2=Paddle(980,350,50,100,(255,255,255))
ball=Ball(500,350,10,(255,255,255),1)
                

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
            
    screen.fill((0,0,0))
    
    paddle1.drawing()
    paddle2.drawing()
    paddle1.moving1()
    paddle2.moving2()
    ball.drawing()
    ball.moving()
            
    pygame.display.update()
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

There are a couple of ways you can detect collisions in Pygame, the easiest in this case, I think, is to use Rect.colliderect().

To use .colliderect() you first need some rects. In the case of the paddles that's easy, because your initialization parameters of x, y, width, height is a rect. To get the rect for the circle you can use a line similar to:

ballRect = pygame.draw.circle(display,(self.color),(self.posx,self.posy),self.radius,0)

Now that you have the three rects, in your ball class test for collisions:

def collideTest(self):
    bump = self.ballRect.colliderectlist([paddle1.rect, paddle2.rect])
    if bump >= 0:
        #bump holds either 0 or 1 indicating which paddle was collided with

So, now a couple of tips.

Classes should only handle the things which pertain to the class itself. They shouldn't be checking for keypresses, for example. Keypresses and other input should be checked for in your main game loop. The objects should keep track of things like their own position, appearance, movement, etc.

A class's methods should, again, pertain to the class. Your paddles and your ball, for example, should know how to draw themselves, so yeah, you should have a "drawing" method. (I usually call mine either .draw() or .show().)

As I already indicated above, it should be your ball object which test for the collision, the reason being that it is the ball which must react to the collision. You should also add a couple of rects at the sides of the display which represent the goals. The ball object should determine whether it has collided with a paddle or a goal and react appropriately, either by reflecting off the paddle or by updating the appropriate score.

You can add a scoreboard class which keeps track of and displays the score for both players.

Good luck with it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .