如何在pygame中有两个key_input方法?

时间:2012-05-22 23:56:29

标签: python pygame

我发现我只能有一种键输入方法。贝娄我有两种方法(movementprite和movementspritevert)我发现我只能有一个工作。如果我同时启动两个,则顶部运行,第二个不执行任何操作。有没有解决的办法?难道我做错了什么?

原创计划

----运动档案------

    import pygame
    import os, sys
    from itertools import *
    from oryxsprites import *
    from oryxdisplay import *
    spritex = 320
    spritey = 320

    screen = pygame.display.set_mode((640, 640))
    def movementsprite():   

        global spritex

        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 running = False
             elif event.type == pygame.KEYDOWN:

                 if event.key == pygame.K_RIGHT:
                     if spritex == 576:
                         spritex += 0
                     else:
                         spritex += 32 

                 elif event.key == pygame.K_LEFT:              
                     if spritex == 32:
                         spritex -= 0
                     else:
                         spritex -= 32

        return spritex
    def movementspritevert():   

        global spritey

        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 running = False
             elif event.type == pygame.KEYDOWN:

                 if event.key == pygame.K_DOWN:
                     if spritey == 576:
                         spritey += 0
                     else:
                         spritey += 32 

                 elif event.key == pygame.K_UP:              
                     if spritey == 32:
                         spritey -= 0
                     else:
                         spritey -= 32

        return spritey

-------主文件------]

running = True

while running:

    backgroundmain()
    pygame.display.set_caption('OryxGame')
    #pygame.display.set_icon(grasstile)
    movementsprite()
    movementspritevert()

    pygame.display.flip()


    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False
             pygame.quit()

------显示文件----

def backgroundmain():
    spritex = movementsprite()
    spritey = movementspritevert()
    backdrop = pygame.Rect(0, 0, 640, 640) 
    screen.fill((50,50,50))
    playingfeildwidth = (32, 608)
    playingfeildheight = (32, 608)

    screen.blit(warrior1, (spritex, spritey))

------可能的解决方案-------------

- 主文件----

 while running:

    backgroundmain()
    pygame.display.set_caption('OryxGame')
    #pygame.display.set_icon(grasstile)
    movementsprite(events)
    movementspritevert(events)

    pygame.display.flip()


    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False
             pygame.quit()

-----显示文件-----

def backgroundmain():
    events = pygame.event.get()
    spritex = movementsprite(events)
    spritey = movementspritevert(events)
    backdrop = pygame.Rect(0, 0, 640, 640) 
    screen.fill((50,50,50))
    playingfeildwidth = (32, 608)
    playingfeildheight = (32, 608)

------密钥输入文件-----------

spritex = 320
spritey = 320
screen = pygame.display.set_mode((640, 640))
def movementsprite(events):  
    global spritex
    keys = pygame.key.get_pressed()

    for event in events:
         if event.type == pygame.QUIT:
             running = False
         elif event.type == pygame.KEYDOWN:

             if event.key == pygame.K_RIGHT:
                 if spritex == 576:
                     spritex += 0
                 else:
                     spritex += 32 

             elif event.key == pygame.K_LEFT:              
                 if spritex == 32:
                     spritex -= 0
                 else:
                     spritex -= 32

    return spritex
def movementspritevert(events):   
    global spritey
    keys = pygame.key.get_pressed()

    for event in events:
         if event.type == pygame.QUIT:
             running = False
         elif event.type == pygame.KEYDOWN:

             if event.key == pygame.K_DOWN:
                 if spritey == 576:
                     spritey += 0
                 else:
                     spritey += 32 

             elif event.key == pygame.K_UP:              
                 if spritey == 32:
                     spritey -= 0
                 else:
                     spritey -= 32

    return spritey

--- ---错误

Traceback (most recent call last):
  File "C:\Users\Eric\Dropbox\oryxgame\oryxgame.py", line 12, in <module>
    backgroundmain()
  File "C:\Users\Eric\Dropbox\oryxgame\oryxdisplay.py", line 11, in backgroundmain
    spritey = movementspritevert(events)
TypeError: movementspritevert() takes no arguments (1 given)

---- Full Code ----

1 个答案:

答案 0 :(得分:3)

问题是event.get()从队列中删除了事件(参见docs)。因此,当您的第二个函数使用event.get()时,队列中不会有任何事件,除非它们能够在调用之间的极短时间间隔内滑入。

要查看此问题,请在第一个函数中调用 print event 后放置for event in pygame.event.get(),然后在第二个函数中尝试。

解决这个问题的一种方法是

    events = pygame.event.get()
    function1(events)
    function2(events)

然后在events上使用for循环。


编辑以阐明如何实施

由于在初始化pygame之前调用了pygame.event.get(),因此您的代码可能会抛出错误。你可以使用类似的东西,

def backgroundmain():
    events = pygame.event.get()                #save events
    spritex = movementsprite(events)           #added events arg
    spritey = movementspritevert(events)       #added events arg
    backdrop = pygame.Rect(0, 0, 640, 640) 
    screen.fill((50,50,50))
    playingfeildwidth = (32, 608)
    playingfeildheight = (32, 608)

    screen.blit(warrior1, (spritex, spritey))

然后,从中更改运动功能:

def movementspritevert():   

    #Do stuff ...

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False
         elif event.type == pygame.KEYDOWN:

到此:

def movementspritevert(events):   

    #Do stuff ...

    for event in events:
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:

关键是,每次使用event.get()时,它都会删除事件,因此您的下一个调用将不会获得相同的事件。基本上,如果您希望所有移动功能都使用一个按键,则将事件保存为变量并将其传递给函数。