python - Pygame - maintain playing speed of game when keys are pressed -
i've got space invaders style game works okay when press key move player's ship, aliens slow down until key released. because there's more code being run when key pressed if there isn't (obviously). here's code that's run when press key
keys = pygame.key.get_pressed() if keys[pygame.k_up]: objship.move(1) elif keys[pygame.k_right]: objship.move(2) elif keys[pygame.k_down]: objship.move(3) elif keys[pygame.k_left]: objship.move(4)
which calls following code
def move(self, d): self.direction = d if self.direction == 1: self.image = pygame.image.load("shipu.png").convert() if self.yco >= 0: self.yco -= 1 if self.xco >= 884: self.xco = 860
is there way of way of equalising speed of aliens doesn't involve putting wait command (or empty loop or whatever) else statement act make-work delay?
i can put code here it's bit lengthy @ moment thought i'd try without incase there's obvious i'm missing.
self.image = pygame.image.load("shipu.png").convert()
you're reloading ships image every single time moves. don't this.
loading image returns surface, store surface in ship's object, , render surface @ ship's coordinates every frame, never having load image again.
loading files slow, , why you're seeing such dramatic slowdown.
considering have multiple graphics different movement states, load images @ once, when create ship, , store resulting surfaces in separate variables, or dictionary. when need swap between graphics, swap out surfaces needed one.
whatever do, load images once!
the slow down still going happen when change way suggested, @ least imperceptible. out movement on time, need use 'delta time'. basing distance moved off render time.
Comments
Post a Comment