Game closes once reaching 260 score

import pygame
pygame.init()
import math

## VARIABLES ##
running = True
windowHeight = 500
windowWidth = 500
window = pygame.display.set_mode((windowHeight, windowWidth))
clock = pygame.time.Clock()
score = 0
smiley = pygame.image.load("smiley.png")
smiley = pygame.transform.scale(smiley,(120,120))
bigsmiley = pygame.transform.scale(smiley,(200,200))
font = pygame.font.Font('Extra Blue.ttf', 26)
font_90 = pygame.font.Font('Extra Blue.ttf', 90)
font_70 = pygame.font.Font('Extra Blue.ttf', 70)
font_50 = pygame.font.Font('Extra Blue.ttf', 50)
gameState = 0
passiveScore = 0 
scoreIncrease = pygame.USEREVENT
pygame.time.set_timer(scoreIncrease, 1000)
clickStrength = 0
autoClicker = 0

stillCirclePos = [250,250]
stillCircleSize = 105
stillCircleColour = (0,0,0)

while running:

    mousePos = pygame.mouse.get_pos()
    mouseX, mouseY = pygame.mouse.get_pos()
    distance = math.sqrt( ((mousePos[0]-stillCirclePos[0])**2)+((mousePos[1]-stillCirclePos[1])**2))
    scoreDisplay = f"Points: {score}"
 ## MOUSE DETECTION ##
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
             print(mousePos)
             if gameState == 0 and mouseX >= (140) and mouseX <= (360) and mouseY >= (350) and mouseY <= (450):
                 print("adasdasd")
                 gameState = 1
                 print (gameState)

             if distance < stillCircleSize and gameState == 1:
                  score += 1 + 1*clickStrength
                  print(score)
            if gameState == 1 and mouseX >= (399) and mouseX <= (499) and mouseY >= (1) and mouseY <= (41):
               running = False
            if gameState == 1 and mouseX >= (0) and mouseX <= (130) and mouseY >= (430) and mouseY <= (500):
               gameState = 2
            if gameState == 2 and mouseX >= (399) and mouseX <= (499) and mouseY >= (1) and mouseY <= (41):
               gameState = 1
            if gameState == 2 and mouseX >= (430) and mouseX <= (475) and mouseY >= (130) and mouseY <= (175) and score >= 50:
               score -= 50
               clickStrength += 1
            if gameState == 2 and mouseX >= (430) and mouseX <= (475) and mouseY >= (190) and mouseY <= (235) and score >= 200:
               score -= 200
               autoClicker += 1


      ## PASSIVE SCORE ##
        elif event.type == scoreIncrease:
           passiveScore = 0 + 1*autoClicker
           score += passiveScore
       




    window.fill((255,255,255))

    ## MENU SCREEN ##
    if gameState == 0:
        window.fill((0,0,0))
        pygame.draw.rect(window, pygame.Color("white"), (140,350,220,100))
        playDisplay = "Play"
        title = "Smiley Clicker!"
        renderedPlay = font_90.render(playDisplay, 1, pygame.Color("black"))
        renderedTitle = font_70.render(title, 1, pygame.Color("white"))
        window.blit(renderedPlay, (170,350))
        window.blit(renderedTitle, (70, 50))
        window.blit(smiley,(190,190))

    ## MAIN GAME##
    if gameState == 1:

     renderedScore = font_50.render(scoreDisplay, 1, pygame.Color("black"))

     window.blit(renderedScore, (160,70))
     pygame.draw.circle(window, stillCircleColour, stillCirclePos, stillCircleSize)
     window.blit(bigsmiley, (150,150))
     pygame.draw.rect(window, pygame.Color("red"), (399,1,100,40))
     quit = "Quit"
     renderedQuit = font.render(quit, 1, pygame.Color("black"))
     window.blit(renderedQuit, (420,5))
     pygame.draw.rect(window, pygame.Color("green"),(0,430,130, 499))
     shop = "Shop"
     renderedShop = font_50.render(shop, 1, pygame.Color("black"))
     window.blit(renderedShop, (15,435))
     pygame.time.set_timer(score + passiveScore, 1000)

    ## SHOP SCREEN ##
    if gameState == 2:
       shopTitle = "SHOP"
       renderedShoptitle = font_90.render(shopTitle, 1, pygame.Color("black"))
       window.blit(renderedShoptitle, (175,20))
       pygame.draw.rect(window,pygame.Color("red"), (399,1,100,40))
       back = "Back"
       renderedBack = font.render(back, 1, pygame.Color("black"))
       window.blit(renderedBack, (420,5))
       upgrade1 = "Click Strength - 50 points"
       renderedUpgrade1 = font.render(upgrade1, 1, pygame.Color("black"))
       window.blit(renderedUpgrade1, (30,140))
       pygame.draw.rect(window, pygame.Color("green"),(430,130,70,45))
       buy1 = "Buy"
       renderedBuy1 = font_50.render(buy1, 1, pygame.Color("black"))
       window.blit(renderedBuy1, (430, 125))

       upgrade2 = "Autoclicker 1pps - 200 points"
       renderedUpgrade1 = font.render(upgrade2, 1, pygame.Color("black"))
       window.blit(renderedUpgrade1, (30,200))
       pygame.draw.rect(window, pygame.Color("green"),(430,190,70,45))
       buy2 = "Buy"
       renderedBuy2 = font_50.render(buy2, 1, pygame.Color("black"))
       window.blit(renderedBuy2, (430, 190))

       










    pygame.display.flip()
    clock.tick(60)


pygame.quit

What is causing this to happen? There is no given error message, the code will just close when the score reaches 260, or a bit above. The game does not close if the score reaches 260 using passive score. How do I fix this?

This may be a bug in pygame. If the event and event.type are printed out every iteration of the main loop. We will see stuff like

...
<Event(168-Unknown {})> 168
<Event(209-Unknown {})> 209
<Event(88-Unknown {})> 88
<Event(1025-MouseButtonDown {'pos': (266, 279), 'button': 1, 'touch': False, 'window': None})> 1025
(266, 279)
259
<Event(195-Unknown {})> 195
<Event(74-Unknown {})> 74
<Event(69-Unknown {})> 69
<Event(246-Unknown {})> 246
<Event(163-Unknown {})> 163
<Event(251-Unknown {})> 251
<Event(241-Unknown {})> 241
<Event(237-Unknown {})> 237
<Event(102-Unknown {})> 102
<Event(152-Unknown {})> 152
<Event(124-Unknown {})> 124
<Event(141-Unknown {})> 141
<Event(79-Unknown {})> 79
<Event(232-Unknown {})> 232
<Event(190-Unknown {})> 190
<Event(93-Unknown {})> 93
<Event(256-Quit {})> 256
<Event(174-Unknown {})> 174
<Event(40-Unknown {})> 40
...

The problem is the ID/number of these unknown events keep increasing with every click on the surface and, at some point, number 256 is reached which is the quit event of pygame, so the if-statement kick in and stop the game.

These two issues could be related

However, I am not sure as to why the value in the score affects the event’s ID/number. For example, if you set a breakpoint and then manually set score to 250, pygame will again stop again at the score of 259.

Something to note, clicking outside of the smiley or bigsmiley surface appears to not increase the unknown event’s ID/number. Another thing, when clicking become dragging, the unknown event will start to show up.

The formatting of the code is a little sketchy :sweat_smile:, but I doubt that it is contributing to the current behavior.

Nevermind, your

pygame.time.set_timer(score + passiveScore, 1000)

is the one sending that quit event :sweat_smile:

Remove it and it should stop sending unknown event and the quit event that close your game. set_timer’s first argument has to be event ID/type/number you want pygame to emit when the time (the 2nd argument) is up.

You already set up your user event that will trigger the passive score increase. That is this piece of code

scoreIncrease = pygame.USEREVENT
pygame.time.set_timer(scoreIncrease, 1000)

You also already handled this type of event in the loop.

Thanks so much, this worked!

And sorry about the sketchy formatting, haha :smiling_face_with_tear: