Вопрос

I have a program in pygame that takes mouse input and keyboard input like follows:

for event in events:
    if event.key == pygame.K_LEFT:
            for k in other:
                k.move(archerspeed, 0)
                k.draw(k.x, k.y)
            for k in arrows:
                Arrow().draw(k[0], k[1])
    if event.key == pygame.K_RIGHT:
            for k in other:
                k.move(-archerspeed, 0)
                k.draw(k.x, k.y)
            for k in arrows:
                Arrow().draw(k[0], k[1])
    if event.key == pygame.K_UP:
            for k in other:
                k.move(0, archerspeed)
                k.draw(k.x, k.y)
            for k in arrows:
                Arrow().draw(k[0], k[1])
    if event.key == pygame.K_DOWN:
            for k in other:
                k.move(0, -archerspeed)
                k.draw(k.x, k.y)
            for k in arrows:
                Arrow().draw(k[0], k[1])

if pygame.mouse.get_pressed()[0] == 1 and time.time() - arrowtime > 1:
    myarcher.shoot(myarcher.x, myarcher.y)
    arrowtime = time.time()

Everything works fine in this game, and my object responds to the keyboard events, until the mouse starts moving. While the mouse is moving, no matter how hard I bang my keyboard, nothing happens.

Why is this? Is there any way to prevent this from occurring?

Это было полезно?

Решение

What is happening is that your mouse events are completely halting your events. I suggest filtering them out using list comprehension beforehand:

try:
    events = [event for event in events if event.key >= 273 and event.key <= 276]
except AttributeError:
    continue

This makes sure that the keys are one of the arrow keys. Otherwise, remove it from the list of events.

Hope this helps!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top