Skip to main content
added the text to somewhere relevant. ; edited tags
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

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()




  '''Hello ! 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()




  '''Hello ! 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'''         

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()
added 4 characters in body
Source Link
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()




  '''Hello ! I am trying to make some games using classes. But I got a problem when
trying to make the snakeping 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()




  '''Hello ! I am trying to make some games using classes. But I got a problem when
trying to make the snake 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()




  '''Hello ! 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'''         
Source Link

Using collision with classes in pygame

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()




  '''Hello ! I am trying to make some games using classes. But I got a problem when
trying to make the snake 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'''