I am new to coding games using pygame so I thought of making a cookie clicker type of game for my first project. My problem is that I can press the cookie and it gives me cookies when pressed but I cannot press the BUY picture that I have on screen. It's like it doesn't register the click. Please help me I have searched for 2 hours I can'ẗ find a solution anywhere. Here's my code:
# import the pygame module
import pygame
pygame.init()
# Define a function for what will happen if the player buys an item (not done)
def shop():
if buy_image.get_rect().collidepoint(pos):
times_clicked -= 15
# Start with no clicks
times_clicked = 0
background_colour = ("white")
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
pygame.display.set_caption("Cookies")
screen.fill(background_colour)
# create a font object.
# 1st parameter is the font file
# which is present in pygame.
# 2nd parameter is size of the font
font = pygame.font.SysFont("arial",32, True)
fullscreen = False
# Variable to keep our game loop running
running = True
clock = pygame.time.Clock()
# game loop
while running:
# Amount of cookies here
text = font.render(f"Cookies: {times_clicked}", True, "orange")
# create a rectangular object for the
# text surface object
textRect = text.get_rect()
# set the center of the rectangular object.
textRect.center = (880, 100)
# Shop pic
shop_text = pygame.image.load("shop.png")
shop_text = pygame.transform.scale(shop_text, (400, 100))
# Cursor (shop)
cursor_image = pygame.image.load("cursor.png")
cursor_image = pygame.transform.scale(cursor_image, (100, 100))
# Cookie pic
image = pygame.image.load("cookie.jpeg")
image = pygame.transform.scale(image, (800, 800))
# "BUY" pic that doesn't work
buy_image = pygame.image.load("buy.jpeg")
buy_image = pygame.transform.scale(buy_image, (100, 80))
for event in pygame.event.get():
screen.fill(background_colour)
screen.blit(image, (100, 60))
screen.blit(text, textRect)
screen.blit(shop_text, (1250, 20))
screen.blit(cursor_image, (1550, 120))
screen.blit(buy_image, (1400, 130))
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the pos postions of the mouse click
pos = pygame.mouse.get_pos()
# Check if cookie picture is pressed
if image.get_rect().collidepoint(pos):
times_clicked += 1
# Check if BUY picture is pressed
shop()
# Check for QUIT event
if event.type == pygame.QUIT:
running = False
# Update the display using flip
pygame.display.flip()
clock.tick(60)