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.
self.pressedkeys[self.key] = True
self.pressedkeys[self.key] = False
def key_action(self):
for self.key, is_pressed in self.pressedkeys.items():
if is_pressed:
print("x")
if self.key == QtCore.Qt.Key_D:
self.game.newplayer.move_forward()
self.update()
elif self.key == QtCore.Qt.Key_W:
self.game.newplayer.jump_up() #calling here for jump function!
self.update()
def jump_up(self):
grav = 5
self.item.moveBy(0, self.jumpacc)
while self.get_ylocation() < 0: #while rect is not touching the floor move the rect towards floor
self.item.moveBy(0, grav)
def keyPressEvent(self, QKeyEvent):
self.key = QKeyEvent.key()
self.pressedkeys[self.key] = True
def keyReleaseEvent(self, QKeyEvent):
self.key = QKeyEvent.key()
self.pressedkeys[self.key] = False
def key_action(self):
for self.key, is_pressed in self.pressedkeys.items():
if is_pressed:
print("x")
if self.key == QtCore.Qt.Key_D:
self.game.newplayer.move_forward()
self.update()
elif self.key == QtCore.Qt.Key_W:
self.game.newplayer.jump_up() #calling here for jump function!
self.update()
def jump_up(self):
grav = 5
self.item.moveBy(0, self.jumpacc)
while self.get_ylocation() < 0: #while rect is not touching the floor move the rect towards floor
self.item.moveBy(0, grav)
To copy to clipboard, switch view to plain text mode
Bookmarks