开发者

How can I have varied KMOD status' for a single key in Pygame?

Here's what I've got so far:

for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_LEFT:
            mods = pygame.key.get_mods()
            if mods and KMOD_SHIFT:
                movei = -5
            if mods and KMOD_CTRL:
                movei = -20
            else:
                movei = -10

The problem is it seems to only pick up one or the other (KMOD_SHIFT or KMDO_CTRL) ALL THE TIME, not selectively. So it doesn'开发者_开发百科t matter which modifier I press (shift, alt, ctrl etc.) the effect is still the same.

The effect I'm going for is that the onscreen character can creep, run or walk, respectively.

Thanks in advance.


You're using the logical and operator, while you really need the bit-wise operator &. Instead of

if mods and KMOD_SHIFT:

you want

if mods & KMOD_SHIFT:

The logical and will return the value of the second operand as long as the first one has a true value (in this case, not equal to 0). The & operator will perform a bit-wise AND operation and will therefore return a non-zero value (logically interpreted as true) only if some of the bits from the KMOD constant are on in the mods variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜