0

我发现我只能有一种按键输入方法。贝娄我有两种方法(movementsprite和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)

----完整代码----

4

1 回答 1

3

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

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

解决此问题的一种方法是

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

然后使用 for 循环events代替。


编辑:澄清如何实施

您的代码可能会抛出错误,因为pygame.event.get()在初始化 pygame. 你可以使用类似的东西,

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() 时,它都会删除事件,所以你的下一次调用不会得到相同的事件。基本上,如果您希望所有运动功能都使用一个按键,则将事件保存为变量并将其传递给函数。

于 2012-05-23T02:03:31.470 回答