bernams
bernams

Reputation: 3

AttributeError: 'Player' object has no attribute 'collidepoint'

im currently learning to program in python (first language) and im trying to create a clicker game, however, while doing the click function i got this error:

line 73, in <module>
    if player.collidepoint(event.pos):
AttributeError: 'Player' object has no attribute 'collidepoint'

it seems that my "player" object (the clickable object) doesnt have a rect ? but after looking it up for hours i could not find an answer

this is my game code

import pygame
from os import path

img_dir = path.join(path.dirname(__file__), "img")

width = 500
height = 600
fps = 30

# Cores 
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0 ,0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

# Iniciar o game

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Trojan Clicker")
clock = pygame.time.Clock()


font_name = pygame.font.match_font("arial")
def draw_text(surf, text, size, x , y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, white)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((75, 75))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = width / 2
        self.rect.bottom = height / 2
        self.speedx = 0

    def update(self):
        self.speedx = 0





all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)


clicks = 0
# Loop
running = True
while running:
    # Fps
    clock.tick(fps)
    # Eventos
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
                # 1 is the left mouse button, 2 is middle, 3 is right.
                if event.button == 1:
                    # `event.pos` is the mouse position.
                    if player.collidepoint(event.pos):
                        # Increment the number.
                        number += 1        



    # Updates
    all_sprites.update()


    # Draw / render X
    screen.fill(black)
    all_sprites.draw(screen)
    draw_text(screen, str(clicks), 18, width / 2, 10)

    # Depois de desenhar tudo, "flip" o display
    pygame.display.flip()

pygame.quit()    

Some comments are in portuguese btw, sorry about that

Thanks in advance for everyone who helps

Upvotes: 0

Views: 2146

Answers (1)

smoggers
smoggers

Reputation: 3192

Change the line

if player.collidepoint(event.pos):

to

if player.rect.collidepoint(event.pos):

Upvotes: 1

Related Questions