I'm trying to make a cookie clicker clone, but my code can't detect when the grandma button is pressed.
Here is my code:
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Cookie Clicker')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
crashed = False
cookie = pygame.image.load('cookie.png')
grandma = pygame.image.load('grandma.png')
cookies = 0
def car(x,y):
gameDisplay.blit(cookie, (x,y))
def grandshop(x,y):
gameDisplay.blit(grandma, (xx,yy))
x = 0
y = 0
xx = 450
yy = 20
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y postions of the mouse click
mouse_pos = pygame.mouse.get_pos()
if cookie.get_rect().collidepoint(mouse_pos):
if event.button == 1:
cookies += 1
pygame.display.set_caption(f'Cookies: {cookies}')
else:
break
if grandma.get_rect().collidepoint(mouse_pos):
print("It actually worked! :)")
gameDisplay.fill(white)
car(x,y)
grandshop(xx,yy)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
I had expected it to print out "It actually worked! :)" when the grandma image was clicked.