【问题标题】:Why is event always pygame.MOUSEBUTTONDOWN in this situation?为什么在这种情况下事件总是 pygame.MOUSEBUTTONDOWN ?
【发布时间】:2020-01-12 16:03:40
【问题描述】:

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

在我的方块动画闪烁后,我将 '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()

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

但是当我运行它时,它的行为就好像 event 总是 pygame.mousebuttondown ,并且只是在循环允许的范围内单击正方形(与 roundnum 设置的一样多)。

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

非常感谢帮助的人,这是为了我的标记。

【问题讨论】:

    标签: python loops pygame click


    【解决方案1】:

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

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

    下面的代码绘制了你的“simon”矩形,使用一个简单的列表数据结构来保存每个矩形,它的名称、颜色和最后一次单击的时间。将所有矩形数据保存在这样的列表中允许程序循环遍历列表,为每个按钮执行 same 代码。更少的代码是更少的错误机会,并使代码更简单、更干净。每当您发现自己在复制代码块,然后进行简单的修改时,最好停下来想一想是否有更好的方法。保留一个 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()
    

    【讨论】:

    • 哇。我很感动。我的 barley 老师教了我任何关于 pygame 的知识,所以这就是为什么我的代码如此冗长且看起来很有趣的原因。你刚刚保存了我的成绩。上帝保佑你的灵魂。
    猜你喜欢
    • 2013-04-23
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多