为什么在这种情况下事件总是 pygame.MOUSEBUTTONDOWN?

Why is event always pygame.MOUSEBUTTONDOWN in this situation?

我正在进行一个大型学校项目的最后一部分。我正在创建一个 simon 游戏,您必须记住颜色并按正确顺序单击它们。

在我的方块动画闪烁后,我将 'usergottaclick' 设置为 true。

if other == True:
 #And if event = Mousebuttondown
            if usergottaclick == False:
                if mousex >= 50 and mousex <= 150 and mousey >= 110 and mousey <= 160:
                    randomnum = random.randint(1,4)
                    addtosimons()

                    for i in range (0, roundnum):

                        if simonslist[i] == 1:
                            #Flashes red square

                        if simonslist[i] == 2:
                            #Flashes blue square

                        if simonslist[i] == 3:
                            #Flashes green square

                        if simonslist[i] == 4:
                            #Flashes yellow square

                    addroundnum()
                    usergottaclick = True

这是当用户轮到点击方块时我的代码:

def usersturn():
#This code is under if event = pygame.MOUSEBUTTONDOWN
global usergottaclick
if usergottaclick == True:
    playerchoicelist = []
    for i in range (0, roundnum -1 ):

        if mousex >=200 and mousex <= 600 and mousey >= 0 and mousey <= 375:
            clicknum = 1
            playerchoicelist.append(clicknum)
            pygame.draw.rect(screen, REDLIGHT, [200, 0, 400, 375])
            pygame.display.update()
            pygame.time.delay(500)
            if playerchoicelist[i] != simonslist[i]:
                print("Wrong")
                changeusergottaclick()
                subtractroundnum()

        if mousex >=600 and mousex <= 1000 and mousey >= 0 and mousey <= 375:
            clicknum = 2
            playerchoicelist.append(clicknum)
            pygame.draw.rect(screen, BLUELIGHT, [600, 0, 400, 375])
            pygame.display.update()
            pygame.time.delay(500)
            if playerchoicelist[i] != simonslist[i]:
                print("Wrong")
                changeusergottaclick()
                subtractroundnum()

        if mousex >=200 and mousex <= 600 and mousey >= 375 and mousey <= 750:
            clicknum = 3
            playerchoicelist.append(clicknum)
            pygame.draw.rect(screen, GREENLIGHT, [200, 375, 400, 375])
            pygame.display.update()
            pygame.time.delay(500)
            if playerchoicelist[i] != simonslist[i]:
                print("Wrong")
                changeusergottaclick()
                subtractroundnum()


        if mousex >=600 and mousex <= 1000 and mousey >= 375 and mousey <= 750:
            clicknum = 4
            playerchoicelist.append(clicknum)
            pygame.draw.rect(screen, YELLOWLIGHT, [600, 375, 400, 375])
            pygame.display.update()
            pygame.time.delay(500)
            if playerchoicelist[i] != simonslist[i]:
                print("Wrong")
                changeusergottaclick()
                subtractroundnum()




        else:
            changeusergottaclick()

你需要注意的是,用户点击的方块被添加到玩家选择列表中。

但是当我 运行 这样做时,它的行为就好像事件总是 pygame.mousebuttondown 一样,只需在循环允许的范围内点击方块的次数(与 roundnum 一样多的次数)设置为).

我如何才能让他们一次只能点击一个方块,然后必须再次点击才能点击另一个方块?

非常感谢任何帮助的人,这是我的标记。

该代码似乎试图 "drive" 用户,而不是简单地在点击发生时做出反应。没有 roundnum 玩家鼠标事件,通常一次只有一个。处理每个事件。

在事件驱动程序中(即:所有 pygame 程序的 99%),将这些作为实时事件处理通常是个好主意,并使用系统时间来确定何时需要发生在未来。这允许代码不使用 pygame.time.delay(),并为用户界面提供更好的流程。

下面的代码绘制了您的 "simon" 个矩形,使用一个简单的列表列表数据结构来保存每个矩形、名称、颜色和上次单击时间。像这样将所有矩形数据保存在一个列表中允许程序循环执行每个按钮的 相同 代码的列表。更少的代码就是更少的错误机会,并使代码更简单、更清晰。每当您发现自己在复制代码块,然后进行简单的修改时,最好停下来想一想是否有更好的方法。保留 pygame Rect 对象还可以让您进行简单的碰撞检测。

因此按钮的时间字段(初始为零)用于确定矩形是否需要以底色或高亮色绘制。单击按钮时,当前时间存储在按钮中。当按钮被绘制到屏幕上时,如果它仍然是 LIGHT_TIME 毫秒,那么它会被突出显示。

import pygame

pygame.init()

WINDOW_WIDTH  = 1000
WINDOW_HEIGHT = 1000
FPS = 60 

RED         = ( 200,   0,   0 )
BLUE        = (   0,   0, 200 )
YELLOW      = ( 200, 200,   0 )
GREEN       = (   0, 200,   0 )
LIGHTRED    = ( 255,   0,   0 )
LIGHTBLUE   = (   0,   0, 255 )
LIGHTYELLOW = ( 255, 255,   0 )
LIGHTGREEN  = (   0, 255,   0 )

LIGHT_TIME   = 300 # milliseconds the button stays hghlighted for

# define the on-screen button areas & colour
#              name      base    pressed    click   rectangle
#                        colour  colour     time    
buttons = [  [ "red",    RED,    LIGHTRED,    0,    pygame.Rect( 200, 0, 400, 375 )   ],
             [ "blue",   BLUE,   LIGHTBLUE,   0,    pygame.Rect( 600, 0, 400, 375 )   ],
             [ "green",  GREEN,  LIGHTGREEN,  0,    pygame.Rect( 200, 375, 400, 375 ) ],
             [ "yellow", YELLOW, LIGHTYELLOW, 0,    pygame.Rect( 600, 375, 400, 375 ) ]  ]

# ---------- Main ----------

screen = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
clock  = pygame.time.Clock()
game_over = False
while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            mouse_pos = event.pos
            for i, b in enumerate( buttons ):                                 # for every button
                name, base_colour, pressed_colour, click_time, butt_rect = b  #   get the button's parts
                if ( butt_rect.collidepoint( mouse_pos ) ):                   #   was the mouse-click inside this?
                    print( "Button [" + name + "] pressed" )
                    buttons[i][3] = pygame.time.get_ticks()                   # update the button's click-time


    time_now = pygame.time.get_ticks()                                        # get the current time (in milliseconds)
    for b in buttons:                                                         # for every button
        name, base_colour, pressed_colour, click_time, butt_rect = b          #   get the button's parts
        if ( click_time > 0 and click_time + LIGHT_TIME > time_now ):         #   if the mouse-click was LIGHT_TIME ms before time-now
            colour = pressed_colour  # light-up colour                        #       colour the button light
        else:                                                                 #   otherwise,
            colour = base_colour     # base colour                            #       colour the button normally
        pygame.draw.rect( screen, colour, butt_rect, 0 )                      #   draw a filled rectangle in the colour, for the button


    pygame.display.update()
    clock.tick(FPS)

pygame.quit()