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()
My pygame code is running horribly slow, how can I fix it?
Try adding pygame.display.update()
to the end of your while loop
I've had a similar issue running Pygame before :))
Why is my platform game in PyGame suddenly so slow?
Do not load the image in your main loop. Define (load) it outside of the loop and then use it via variable. This is your problem, because it loads the image (in this case FPS = 60) times per second from your disk.
Why is my pygame program gradually getting slower the longer it runs?
The issue is caused by the fact, that the containers (lists) self.dark_blocks
and self.color_blocks
are refilled in every frame, but you missed to clear them. Hence the number of elements int the containers is continuously growing. Since the content of this lists is drawn in every frame, the performance drops. You can't see this effect, because the elements of the lists are drawn on top of each other.
Clear self.dark_blocks
and self.color_blocks
at the begin of room_0.draw
, right after tile_rects
is cleared:
class room_0():
# [...]
def draw(self, window):
global tile_rects
tile_rects = []
self.dark_blocks = []
self.color_blocks = []
# [...]
Related Topics
Django: How to Serve Media/Stylesheets and Link to Them Within Templates
Calling Custom Functions from Python Using Rpy2
Convert Backward Slash to Forward Slash in Python
In Python Can One Implement Mixin Behavior Without Using Inheritance
How to Validate a Date String Format in Python
Performing a Getattr() Style Lookup in a Django Template
Open File in a Relative Location in Python
Putting an If-Elif-Else Statement on One Line
Opencv Videocapture and Error: (-215:Assertion Failed) !_Src.Empty() in Function 'Cv::Cvtcolor'
Using a Django Variable in a CSS File
Fama MACbeth Regression in Python (Pandas or Statsmodels)
What Is the "Sys.Stdout.Write()" Equivalent in Ruby
How to Exchange Keys with Values in a Dictionary
Typeerror: Can't Use a String Pattern on a Bytes-Like Object in Re.Findall()
Efficiently Convert Uneven List of Lists to Minimal Containing Array Padded with Nan