I am trying to make QGraphicsItem jump with keys.

Here is code which reads pressed key from user (this is working):

I don't really know how I should create the jump_up() function here, this code won't work, because while -loops iterates so fast it goes
straight to the floor.

Qt Code:
  1. def keyPressEvent(self, QKeyEvent):
  2. self.key = QKeyEvent.key()
  3. self.pressedkeys[self.key] = True
  4.  
  5. def keyReleaseEvent(self, QKeyEvent):
  6. self.key = QKeyEvent.key()
  7. self.pressedkeys[self.key] = False
  8.  
  9. def key_action(self):
  10. for self.key, is_pressed in self.pressedkeys.items():
  11.  
  12. if is_pressed:
  13. print("x")
  14. if self.key == QtCore.Qt.Key_D:
  15.  
  16. self.game.newplayer.move_forward()
  17. self.update()
  18. elif self.key == QtCore.Qt.Key_W:
  19. self.game.newplayer.jump_up() #calling here for jump function!
  20. self.update()
  21.  
  22. def jump_up(self):
  23. grav = 5
  24. self.item.moveBy(0, self.jumpacc)
  25. while self.get_ylocation() < 0: #while rect is not touching the floor move the rect towards floor
  26. self.item.moveBy(0, grav)
To copy to clipboard, switch view to plain text mode