A
A
Asking2015-02-25 10:40:10
Python
Asking, 2015-02-25 10:40:10

How to make two simultaneous actions on an object?

I have an object, I can move it, and when I press the spacebar, another object appears and flies up, but as soon as I press the spacebar, the main object stops and I need to press the movement keys again to move it.
Is it possible to somehow make the object not stop after pressing the spacebar, and the second object also starts moving. Here is the event definition code:

for ev in pygame.event.get():
       
        if ev.type == pygame.QUIT:
            main_while = False
        
        if ev.type == pygame.KEYDOWN:
            if ev.key == pygame.K_RIGHT:
                if hero.x < 420:
                    hero.x += 0.5
            if ev.key == pygame.K_LEFT:
                if hero.x > 3:
                    hero.x -= 0.5
            if ev.key == pygame.K_SPACE:
                if not bul_push:
                    bullet.x = hero.x + 30
                    bullet.y = hero.y + 10
                    bul_push = True

Thank you in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
localghost, 2015-02-25
@localghost

General approach: by pressing the key, do not change the position, but the speed of the object ( += vel). And when they release the key, change it back (-= vel).
Are you by any chance not taking An Introduction to Interactive Programming in Python right now?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question