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.
dot
has priority in theelif event.type == pygame.KEYDOWN
section. So thecircle
can only get input when none of thedot
control-keys are being held down. – TowlineSHIFT
etc.) cannot be detected. Changingif .. elif
toif .. if
might improve things slightly because you could detect multiple keys in the same loop. – Towlineif ... if
shouldn't change it becauseevent.key
can have only one value. Code looks OK. – Bowdlerizepygame.mixer
– Bowdlerizepygame.K_s
instead ofpygame.L.s
– Bowdlerize