Stoat
Stoat

Reputation: 13

How do I detect a click on an image thats being toggeled by another image

So i was messing around in computer science and starting actually getting some decent progress (for me) on a project that managed to last longer than 2 days. I just want some help on what im doing wrong. heres my code its like some fnaf spin off that im planning to add some watermelon enemy to. Im just confused on how to do this click detection

 import pygame, random, time
import pygame_textinput
import prompts

pygame.init()
textinput = pygame_textinput.TextInputVisualizer()

font = pygame.font.SysFont("Comicsansms", 55)

display = pygame.display.set_mode((575, 375))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()

pygame.key.set_repeat(200, 25)

room = pygame.image.load("assets/images/room.png")
dark = pygame.image.load("assets/images/dark.png")
light = pygame.image.load("assets/images/light.png")
mel = pygame.image.load("assets/images/waterelo.png")
tablet = pygame.image.load("assets/images/3.png")
cam1 = pygame.image.load("assets/images/winner1.png")

def wait(x):
  time.sleep(x)

def insideimage(pos, rsurf):
    refrect = rsurf.get_rect().move((100, 100))
    pickedcol = display.get_at(pos)
    return refrect.collidepoint(pos)


q = False
flash = False 
while True:
    display.fill((225, 225, 225))
    display.blit(room, (0, 0))
    events = pygame.event.get()
    textinput.update(events)
    
    for event in events:
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.KEYDOWN:
          if event.key == pygame.K_q:
            flash = not flash
          if event.key == pygame.K_SPACE:
            q = not q
        elif event.type == pygame.MOUSEBUTTONDOWN and q == True:
          if cam1.rect.collidepoint(event.pos):
            print("hi")

    if q == True:
      display.blit(tablet, (-75, -75))
      display.blit(cam1, (450, 240))
    elif flash == True:
      display.blit(light, (70, 60))
    else:
      display.blit(dark, (80, 55))

    pygame.display.update()
    clock.tick(30)
##if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:

Upvotes: 1

Views: 38

Answers (1)

Rabbid76
Rabbid76

Reputation: 210889

See How do I detect collision in pygame?. A pygame.Surface has no rect attribute. Use get_rect() to get a rectangle with the size of the image and set the position with keyword arguments:

elif event.type == pygame.MOUSEBUTTONDOWN and q == True:
       
    cam1_rect = cam1.get_rect(topleft = (450, 240))   
    if cam1_rect .collidepoint(event.pos):
        print("hi")

Upvotes: 1

Related Questions