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.

Jamie Lin
  • 61
  • 2
  • 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. – SiHa Jan 21 '16 at 12:57
  • 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. – SiHa Jan 21 '16 at 13:03
  • `if ... if` shouldn't change it because `event.key` can have only one value. Code looks OK. – furas Jan 21 '16 at 13:15
  • 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? – Alexey Astahov Jan 21 '16 at 13:24
  • your code works for me - but I tested it without `pygame.mixer` – furas Jan 21 '16 at 13:34
  • btw: you should have `pygame.K_s` instead of `pygame.L.s` – furas Jan 21 '16 at 13:36
  • 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! – Jamie Lin Jan 21 '16 at 13:55
  • Code works perfectly for me too (with some random images and a sound file). What happens if you change the control keys? – elegent Jan 21 '16 at 20:41

1 Answers1

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".

Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
Alexey Astahov
  • 203
  • 1
  • 7
  • 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. – Jamie Lin Jan 21 '16 at 13:52
  • @JamieLin - code looks OK. maybe problem is in hardware (keyboard) or operation system. – furas Jan 21 '16 at 14:34
  • 1
    You probably mean "*using pygame.image.load('foo.png') *with* .convert() ...*" ;) – elegent Jan 21 '16 at 20:36