python - Pygame shape rotation -


i'm working on little game , i'm having trouble rotating 1 of shapes when key pressed. have managed rotate shape when key pressed continues rotate (90 degrees) rather staying in new position. have feeling in type of loop cant seem out of loop. hope can help. relevant code located @ sixth line bottom (elif keys[k_d])

class player(pygame.sprite.sprite):  def __init__(self, screen):     self.screen = screen     self.image = pygame.image.load(os.path.join('shape.png'))     self.rect = self.image.get_rect()     self.rect.x = 10     self.rect.y = 540     #you don't need define self.width because self.rect.width     #same self.height     self.speed = 10  def update(self):     keys = pygame.key.get_pressed()            if keys[k_left]:         self.rect.left -= self.speed         if self.rect.left < 0: #the left edge             self.rect.left = 0     elif keys[k_right]:         self.rect.right += self.speed         if self.rect.right > window_width:             self.rect.right = window_width     elif keys[k_up]:         self.rect.top -= self.speed         if self.rect.top < 0:             self.rect.top = 0     elif keys[k_down]:         self.rect.bottom += self.speed         if self.rect.bottom > window_height:             self.rect.bottom = window_height     ***#the code below shapes rotation***.             elif keys[k_d]:         self.image = pygame.transform.rotate(self.image, 90)     def draw(self, screen=none):     if screen none:         screen = self.screen      screen.blit(self.image, (self.rect.x, self.rect.y)) 

as far see it, calling update every frame.

pygame.keys.get_pressed() returns "d" pressed every frame if "d" pressed. better work event check pygame.keydown , keyup events. fired once key pressed , once released.

i suggest add keymap main program class' constructor (the

def __init__(self): 

part).

i add d-key example, others add on own.

self.keymap = {pygame.k_d: false} 

then in main loop check events:

while running:     event in pygame.event.get():         if event.type == pygame.keydown:             if event.key in self.keymap:                 self.keymap[event.key] = true          if event.type == pygame.keyup:             if event.key in self.keymap:                 self.keymap[event.key] = false 

keydown fired once when key pressed , doesn't return true every frame while key being held down. sets variable in main class true can use in other classes.

keyup fired once when release key. resets variable in keymap false, program ready deal next keydown event of key.

check variable in player's update method:

if main.keymap[k_d] == true:     self.image = pygame.transform.rotate(self.image, 90) 

is of use you?

//edit: make clear again , point out problem in code lies:

pygame.keys.get_pressed 

returns true every frame if key still pressed, therefore rotation performed in every frame while key held down.

pygame.keydown 

and

pygame.keyup 

are fired once when key pressed or released, don't return true second time, if keep pressing key.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -