Pygame is running slow

The game is running slow because you load the START_Image in every frame. pygame.image.load is a very expensive operation, because it has to read the images from the data store. Load START_Image once at startup

surface  = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()#for fps
#caption
pygame.display.set_caption('Survival Island')

START_Image = pygame.image.load('START_Image.png').convert()

Do not call pygame.display.update() more than once in the main application loop. pygame.display.update() removes the events from the queue, thus you’ll get each event just once. Get the list of events once (events = pygame.event.get()) and pass the list of events to the functions:

while running:
    # [...]
    events = pygame.event.get()
    for event in events:
        # [...]
        
    if play == True:
        playGame(events)
    if canQuitOnStart == True:
        quitOnStart(events)

Further more, draw the scene in the application loop, rather the event loop. It is sufficient to do 1 single pygame.display.update() after drawing the entire scene.

The button click condition is wrong. It has to be:

if mousex > 550 and mousey > 350 and mousex <590 and mousey <390:

if  415 < mousex < 415+70 and 190 < mousey < 190+30:

Anyway I recommend to use pygame.Rect and collidepoint:

if pygame.Rect(415,190,70,30).collidepoint(mousex, mousey):

See the example:

#packages
import pygame
import sys
from sys import exit
#initialization
pygame.init()
#display surf
width = 600
height = 400
surface  = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()#for fps
#caption
pygame.display.set_caption('Survival Island')

START_Image = pygame.image.load('START_Image.png').convert()
    
#variables
mousex = 0
mousey = 0
#booleans
play = True #entered playmode
canQuitOnStart = True     #game can be quitted on start
drawStartScreen = True #start screen drawed
running = True # game is running
#definitions
def quitOnStart(events):     #quitting the game
    #can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(550,350,40,40))]
    global mousex,mousey,running
    for event in events:
       if event.type == pygame.MOUSEBUTTONDOWN: #quit on pressing x on start screen
            if mousex > 550 and mousey > 350 and mousex <590 and mousey <390:
                print('Exit1')
                running = False

def drawStart():      #drawing start menu
    surface.blit(START_Image,(0,0))

def playGame(events):
    #play on clicking on "play"
    global mousex,mousey,canQuitOnStart,drawStartScreen
    for event in events:
        if event.type == pygame.MOUSEBUTTONDOWN:
           if pygame.Rect(415,190,70,30).collidepoint(mousex, mousey): # can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(415,190,70,30))]
                canQuitOnStart = False
                drawStartScreen = False
    surface.fill((0,0,0))
    if drawStartScreen == True:
        drawStart()
    #pygame.draw.rect(surface, (255, 0, 0), (415,190,70,30))
    pygame.display.update()

def main():
    global canQuitOnStart, play, running, mousex, mousey
    #main loop
    while running:
        #get mouse position
        mousex,mousey = pygame.mouse.get_pos()
        # fps is 60
        clock.tick(120)
        # quit button event
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                running = False
        # main function
        if play == True:
            playGame(events)
        if canQuitOnStart == True:
            quitOnStart(events)

if __name__ == '__main__':
    main()

Leave a Comment