So I am making an idle clicker game (which in no shape, way, or form is related to snake), and I wanted to add a cursor that when clicked, would add +1 points every second. Here is the code for how the cursor increases points:
def display_score():
global points
present = (int(pygame.time.get_ticks() / 1000) - int(start_time / 1000)) * (cursors * 1)
points += present
#print(present)
score = font.render(f"{points}", False, (0, 0, 0))
score_rct = score.get_rect(center = (320, 60))
screen.blit(score, score_rct)
Then I tried making it so that it costs 10 points to buy a cursor. Here is what I wrote:
if event.type == pygame.MOUSEBUTTONDOWN:
if cursor_rct.collidepoint(event.pos) and points >= 10:
points -= 10
cursors += 1
Instead of buying 1 cursor and then stopping, the game kept buying more cursors for no apparent reason.
Full code:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 640))
clock = pygame.time.Clock()
mouse_pos = pygame.mouse.get_pos()
points = 0
cursors = 0
start_time = pygame.time.get_ticks()
font = pygame.font.Font("PixelType.ttf", 75)
#text = font.render("h", False, (0, 0, 0))
#text_rct = text.get_rect(center = (320, 60))
def display_score():
global points
present = (int(pygame.time.get_ticks() / 1000) - int(start_time / 1000)) * (cursors * 1)
points += present
#print(present)
score = font.render(f"{points}", 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))
run = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie_rct.collidepoint(event.pos):
points += 1
if cursor_rct.collidepoint(event.pos) and points >= 10:
points -= 10
cursors += 1
screen.blit(backround, backround_rct)
screen.blit(cookie, cookie_rct)
screen.blit(cursor,cursor_rct)
display_score()
pygame.display.update()
clock.tick(60)