Pygame lags when two players are implemented
Asked Answered
R

1

6

I have just started playing around with pygame, and have just come across a problem - when I make my game for 2 players, the second character always lags. Here is my code.

import pygame, sys
from pygame.locals import *

pygame.init()

clock = pygame.time.Clock()

background_img = pygame.image.load('Data/background.jpg')
size = background_img.get_size()

pygame.mixer.init()                         
pygame.mixer.music.load('Data/song.wav')   
pygame.mixer.music.set_volume(0.7)          
pygame.mixer.music.play(-1)    

dot_img = pygame.image.load('Data/dot.png')
dotx = 0
doty = 0
dotx_speed = 0
doty_speed = 0

circle_img = pygame.image.load('Data/circle.png')
circlex = 0
circley = 0
circlex_speed = 0
circley_speed = 0

display = pygame.display.set_mode(size)

pygame.display.set_caption('Game')

while 1: 
  for event in pygame.event.get():
      if event.type == pygame.QUIT:
          pygame.quit()
          sys.exit()

      elif event.type == pygame.KEYDOWN:
          if event.key == pygame.K_LEFT:
            dotx_speed = -10
          elif event.key == pygame.K_RIGHT:
            dotx_speed = 10
          elif event.key == pygame.K_UP:
            doty_speed = -10
          elif event.key == pygame.K_DOWN:
            doty_speed = 10
          elif event.key == pygame.K_a:
            circlex_speed = -10
          elif event.key == pygame.K_d:
            circlex_speed = 10
          elif event.key == pygame.K_w:
            circley_speed = -10
          elif event.key == pygame.L.s:
            circley_speed = 10            

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            dotx_speed = 0
        elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            doty_speed = 0
        elif event.key == pygame.K_a or event.key == pygame.K_d:
            circlex_speed = 0
        elif event.key == pygame.K_w or event.key == pygame.K_s:
            circley_speed = 0              

  dotx += dotx_speed
  doty += doty_speed

  circlex += circlex_speed
  circley += circley_speed    

  display.blit(background_img,(0,0))
  display.blit(dot_img,(dotx,doty))
  display.blit(circle_img,(circlex,circley))

  pygame.display.update()
  clock.tick(100)

I am not that well versed with pygame, or python for that matter, so please forgive my sloppy code. Any help is appreciated.

Raposa answered 21/1, 2016 at 12:47 Comment(8)
I'd be inclined to say that it's because (what I'm assuming is the first player) the dot has priority in the elif event.type == pygame.KEYDOWN section. So the circle can only get input when none of the dot control-keys are being held down.Towline
Not sure how you'd fix this, as I'm fairly certain simultaneous key-presses (ignoring modifiers like SHIFT etc.) cannot be detected. Changing if .. elif to if .. if might improve things slightly because you could detect multiple keys in the same loop.Towline
if ... if shouldn't change it because event.key can have only one value. Code looks OK.Bowdlerize
who is the second character? Second character have lags even you didn't changed position of first charachter? Can you provide a bit more information?Kohler
your code works for me - but I tested it without pygame.mixerBowdlerize
btw: you should have pygame.K_s instead of pygame.L.sBowdlerize
I don't think that the priorities were a problem, but nevertheless, I will check that out. And sorry for not checking my work - pygame.L_s is an obvious mistake. Thanks!Raposa
Code works perfectly for me too (with some random images and a sound file). What happens if you change the control keys?Prophecy
K
2

Firs of all event handler and calculations in one flow is bad practice. Because your calculations may be not as fast as you want (100 fps in your example) For example, check resolution of your images.

Also you have too many if-else statements (it is not a mistake in your case). You can replace it with dicts.

Make your frame rate more realistic (60).

Read A Newbie Guide to pygame, there are some mistakes in your code, for example using pygame.image.load('foo.png') with the .convert() method to "to get any kind of speed out of your blits".

Kohler answered 21/1, 2016 at 13:19 Comment(3)
Thanks for all the input. I have changed the frame rate, and it seems to work a bit better, but after a while, the circle stops, and the entire program becomes unresponsive. (I have to force quit the application.) In response to your earlier comment, the second character is just an image, and even if I haven't moved dot, circle still lags after I move it. I will read the Newbie Guide to Pygame. Thanks a lot.Raposa
@JamieLin - code looks OK. maybe problem is in hardware (keyboard) or operation system.Bowdlerize
You probably mean "using pygame.image.load('foo.png') *with .convert() ...*" ;)Prophecy

© 2022 - 2024 — McMap. All rights reserved.