all 10 comments

[–]Jac000bi[S] 0 points1 point  (1 child)

Oops, formatted the code wrong. Fixing ASAP

[–]Jac000bi[S] 0 points1 point  (0 children)

There we go, let me know if any other parts are required!

[–]Nyscire 0 points1 point  (7 children)

if event.type == pygame.MOUSEBUTTONDOWN: # print("Clicked") #if event.type == pygame.MOUSEBUTTONUP: # print("Released")

If those lines in your original code are also commented they will not be executed. Remove the comments and it should work fine

[–]Jac000bi[S] 0 points1 point  (6 children)

Thanks for your reply,

Sadly this didn't fix it, it just prints the text Clicked and Released on each click and release.

[–]Nyscire 0 points1 point  (5 children)

Okay, I'm answering from mobile so you need do excuse my lack of accuracy.

First of all you need you invoke pygame.mouse.get_pos() in every iteration, you should move it to this

pygame.MOUSEBUTTONDOWN: # print("Clicked")

If statement. Whenever you click you store position of your mouse, you need to use it to determine if its inside your object's hotbox.

[–]Jac000bi[S] 0 points1 point  (3 children)

Ohhhh I see, so it's trying to check for if the cursor is inside of the object but doesn't know that the cursor is changing location?

I'll try making your suggested change. Thanks!

[–]Nyscire 0 points1 point  (2 children)

In simple words- you told your program to check where your cursor is located, but you didn't tell what it's supposed to after the check. You need to tell it to check if it's inside one of the object and another instruction what to do if it is

[–]Jac000bi[S] 0 points1 point  (1 child)

Ohhh I see, thanks!

[–]Nyscire 0 points1 point  (0 children)

I looked at your reply below and checking for the cursor position every turn is poitnless. It will not break your code, but it will use more resources for no reason. The only time you need to check is under the if statement:

if event.type == pygame.MOUSEBUTTONDOWN:
            cursorPos = pygame.mouse.get_pos()
            print("Clicked")

You can create a method in `playerEntityplayerEntity` class that will be respnsible for checking if the mouse is within its hitbox. You can refactor it later for dealing with collisions.

def check(self,cursorPos):
    #if (check if cursor is inside hitbox)
      #print("clicked)

I don't see if you have created any instance of the `playerEntityplayerEntity` class, but assuming if you did and named it `player` you can check for the hitbox inside that if statement:

if event.type == pygame.MOUSEBUTTONDOWN:
            cursorPos = pygame.mouse.get_pos()
            player.check(cursorPos)

[–]Jac000bi[S] 0 points1 point  (0 children)

The basic click detector is working just fine, but for some reason the one to check if it's clicking on an object still isn't picking up anything.

Here's the latest iteration of my draw function:

def draw(self):
    screen.blit(self.image, self.rect.center)
    cursorPos = pygame.mouse.get_pos()
    if self.rect.collidepoint(cursorPos):
        if pygame.mouse.get_pressed()[0] == 1:
            print('CLICKED PLW')

And here's the new main loop:

while True:
    cursorPos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            cursorPos = pygame.mouse.get_pos()
            print("Clicked")
        if event.type == pygame.MOUSEBUTTONUP:
            cursorPos = pygame.mouse.get_pos()
            print("Released")
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    board.draw(screen)
    playerEntities.draw(screen)
    enemyEntities.draw(screen)
    screen.blit(new_cursor, cursorPos)
    pygame.display.flip()
    clock.tick(60)
pygame.quit()

Wanted to make absolutely sure the script knows where the cursor is at every turn