문제

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