VirtualSquares
VirtualSquares

Reputation: 125

Colliderect function not working in Pygame

So I am making a pong game in pygame while implementing oop. My problem is the colliderect function is giving me the error " ball_hitter object has no attribute 'colliderect'". I've used the colliderect function before but it's not working for me this time.

Code:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Pong With Classes!")
#Variables
red = (255,0,0)
blue = (0,0,255)
black = (0,0,0)
clock = pygame.time.Clock()
#End of Variables
class ball_hitter:
    def __init__(self,x,y,length,width,color):
        self.x = x
        self.y = y
        self.length = length
        self.width = width
        self.color = color
        self.left = False
        self.right = False
    def draw_ball_hitter(self):
        screen.fill(black)
        pygame.draw.rect(screen,self.color,(self.x,self.y,self.length,self.width))
#Paddle creation
paddle = ball_hitter(250,400,130,20,red)
#End  of Paddle Creation
class thing_thats_being_hit:
    def __init__(self,x,y,color):
        self.x = x
        self.y = y
        self.radius = 27
        self.color = color
        self.speed_x = -4
        self.speed_y = 4
    def draw_googlyball(self):
        pygame.draw.circle(screen,self.color,(self.x,self.y),self.radius)
    def move(self):
        self.x += self.speed_x
        self.y += self.speed_y
#Ball Creation
ball = thing_thats_being_hit(200,50,blue)
#End of Ball Creation

#Paddle Collision
if paddle.colliderect(ball):
    print("It Worked!")
#End Of Paddle Collision

ball.move()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                paddle.left = True
            if event.key == pygame.K_RIGHT:
                paddle.right = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                paddle.left = False
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                paddle.right = False
    paddle.draw_ball_hitter()
    ball.draw_googlyball()
    if paddle.left == True:
        paddle.x = paddle.x - 2
    if paddle.right == True:
        paddle.x = paddle.x + 2
    if ball.x <= 0:
        ball.speed_x*=-1
    if ball.x >= 500:
        ball.speed_x *= -1
    if ball.y >= 500:
        pygame.quit()
    ball.move()
    pygame.display.update()
    clock.tick(100)

I would appreciate it if someone could give me a fixed code and explain is to me.

Upvotes: 0

Views: 65

Answers (1)

Rabbid76
Rabbid76

Reputation: 210928

See How do I detect collision in pygame?. colliderect is a method of pygame.Rect. Therefore you need to create pygame.Rect objects. Also you have to do the collision test in the application loop instead of before the application loop:

while True:
    # [...]

    paddle_rect = pygame.Rect(paddle.x, paddle.y, paddle.width, paddle. length)
    ball_rect = pygame.Rect(ball.x - ball.radius, ball.y - ball.radius,
                            ball.radius * 2, ball.radius * 2)
    
    if paddle_rect.colliderect(ball_rect):
        print("It Worked!")

Upvotes: 1

Related Questions