Why is my simple pygmag lagging behind?

I am making a simple python game using pygame and after I added the cannon toggle function the game started to lag. I have no idea why he is falling behind. I tried to reboot but it didn't work. The code is really short, so it might just be my computer, but if there is anything that can help get it up and running faster, please let me know. Here is the code:

import sys, pygame, pygame.mixer
from pygame.locals import *

pygame.init()

size = width, height = 600, 400

screen = pygame.display.set_mode(size)

pygame.display.set_caption('Blue Screen of Death')

#variables
x = 100
y = 200
gun_type = "gun1"
gun = pygame.image.load("gun1.png")
gun = pygame.transform.scale(gun,(500,250))
gun_sound = pygame.mixer.Sound("gun_sound.wav")
clock = pygame.time.Clock()

while 1:
  mx, my = pygame.mouse.get_pos()
  for event in pygame.event.get():
    if event.type == pygame.QUIT:sys.exit()

    elif event.type == KEYDOWN and event.key == K_ESCAPE:
      sys.exit()
    elif event.type == MOUSEBUTTONDOWN:
      gun_sound.play()
    elif event.type == KEYDOWN and event.key == K_1:
      gun = pygame.image.load("gun1.png")
      gun = pygame.transform.scale(gun,(500,250))
      gun_type = "gun2"
    elif event.type == KEYDOWN and event.key == K_2:
      gun = pygame.image.load("gun2.png")
      gun = pygame.transform.scale(gun,(500,250))
      gun_type = "gun2"
    elif event.type == KEYDOWN and event.key == K_TAB:
    if gun_type == "gun2":
      gun_type = "gun2_aimed"
    elif gun_type == "gun2_aimed":
      gun_type = "gun2"
    elif gun_type == "gun2_aimed":
      gun = pygame.image.load("gun2_aimed.png")
      gun = pygame.transform.scale(gun,(500,250))



  #frames per second
  clock.tick(60)

  hallway = pygame.image.load("hallway.png")
  hallway = pygame.transform.scale(hallway,(600,400))
  screen.blit(hallway,(0,0))

  screen.blit(gun,(mx-100,y))

  pygame.display.flip()

      

Thank you for your help.

+3


source to share


3 answers


You can try to load your cannon images before the while loop and keep a link to them so you don't have to load the image on the fly every time.



+2


source


Don't call pygame.image.load

from an event handler.



Instead, call it on all your resources at startup and just disable the one you are using.

+1


source


This is probably the most important thing you can learn in Pygame

.

As I have for many years we had problems with the delay Pygame

. I was upset and almost switched to Pyglet

. My game only ran at 9 frames per second.

Then I found some documents Pygame

on my computer. He had some advice from David Clarke and suggested adding .convert_alpha()

at the end of all loads of images Pygame

. This increased the frame rate to 32!

Here's the website:

https://www.pygame.org/docs/tut/newbieguide.html

I always create a function to do this for me, so I don't have to type '.convert_alpha ()' many times:

def loadify(imgname):
    return pygame.image.load(imgname).convert_alpha()

      

Just replace pygame.image.load(

with loadify(

when using this function.

Have fun with Pygame!

+1


source







All Articles