Jamie Lin
Jamie Lin

Reputation: 61

Pygame lags when two players are implemented

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.

Upvotes: 6

Views: 293

Answers (1)

Alexey Astahov
Alexey Astahov

Reputation: 203

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

Upvotes: 2

Related Questions