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.
The code runs slow because it is a heap of unstructured code (this is the cleaned up version) that is constantly loading external resources, reading events and mouse data. Structure your code so that slow things like reading from files happen ONCE. Likewise reading keys, events, etc. not once for each block.
Pygame uses the Simple DirectMedia Layer (SDL) library, with the intention of allowing real-time computer game development without the low-level mechanics of the C programming language and its derivatives.
pygame is a Python wrapper for the SDL library, which stands for Simple DirectMedia Layer. SDL provides cross-platform access to your system's underlying multimedia hardware components, such as sound, video, mouse, keyboard, and joystick.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With