So I am making an idle clicker game in Pygame. I wanted to add a shop system where if you click on a cursor, you can buy a cursor to click for you. So here is what I wrote:
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie_rct.collidepoint(event.pos):
points_from_clicking += 1
if cursor_rct.collidepoint(event.pos) and final_score >= 10:
cursors += 1
final_score -= 10
What happens was that instead of decreasing the player's score upon purchasing, the amount of points the player has will only increase.
Full code:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 640))
clock = pygame.time.Clock()
mouse_pos = pygame.mouse.get_pos()
points_from_clicking = 0
cursors = 0
start_time = pygame.time.get_ticks()
game_active = False
font = pygame.font.Font("PixelType.ttf", 75)
#code for displaying score
def display_score():
global points
global present
global final_score
present = (int(pygame.time.get_ticks() / 1000) - int(start_time / 1000)) * (cursors * 1)
final_score = present + points_from_clicking
score = font.render(f"{final_score}", False, (0, 0, 0))
score_rct = score.get_rect(center = (320, 60))
screen.blit(score, score_rct)
backround = pygame.image.load("graphics/backround.png").convert_alpha()
backround_rct = backround.get_rect(topleft = (0, 0))
cookie = pygame.image.load("graphics/cockie.png").convert_alpha()
cookie_rct = cookie.get_rect(center = (320, 320))
cursor = pygame.image.load("graphics/cursor.png").convert_alpha()
cursor_rct = cursor.get_rect(center = (320, 580))
#gameloop
run = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if game_active:
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie_rct.collidepoint(event.pos):
points_from_clicking += 1
if cursor_rct.collidepoint(event.pos) and final_score >= 10:
cursors += 1
final_score -= 10
else:
#code to start game
if event.type == pygame.MOUSEBUTTONDOWN:
cursors = 0
points = 0
present = 0
final_score = 0
game_active = True
if game_active:
screen.blit(backround, backround_rct)
screen.blit(cookie, cookie_rct)
screen.blit(cursor,cursor_rct)
display_score()
else:
#Main menu
screen.fill((255, 255, 255))
pygame.display.update()
clock.tick(60)