PDA

View Full Version : Jumping with QGraphicsItem[Python]



Nor1
15th March 2019, 15:18
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.



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)

ChrisW67
16th March 2019, 22:52
What should jump_up() do? Are you expecting a smooth transition over time up the screen followed by one downward?