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

皮耶罗·卡米索托

我正在完成一个庞大的学校项目。我正在创建一个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()

您需要关注的是用户单击的正方形已添加到playerchoicelist的事实。

但是,当我运行此命令时,它的行为似乎是event始终是pygame.mousebuttondown,并且在循环允许的次数范围内,只要单击正方形就可以了(单击Roundnum设置的次数)。

我如何获得它以便他们一次只能单击一个正方形,然后必须再次单击才能单击另一个?

非常感谢任何帮助的人,这是我的荣幸。

金斯利

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

在事件驱动程序(即,所有pygame程序中的99%)中,通常将它们作为实时事件进行处理,并利用系统时间来确定将来何时需要发生。这样就可以不使用代码pygame.time.delay(),并为用户界面提供了更好的流程。

下面的代码使用一个简单的list-of-lists数据结构来绘制每个矩形,名称,颜色和上次单击的时间,以绘制“ simon”矩形。将所有矩形数据保存在这样的列表中,可使程序在列表上循环,为每个按钮执行相同的代码。更少的代码可以减少出错的机会,并使代码更简单,更简洁。每当您发现自己复制代码块,然后进行简单的修改时,最好停一会儿,想知道是否有更好的方法。保留pygameRect对象还可以使您进行简单的碰撞检测。

因此,button的时间字段(最初为零)用于确定是否需要用基色或高亮色绘制矩形。单击该按钮时,当前时间存储在该按钮中。然后将按钮绘制到屏幕上时,如果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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在这种情况下x = 44?

为什么在这种情况下使用BufferedReader?

在这种情况下,为什么Python比C ++快?

为什么在这种情况下UniquelyReferencedNonObjC返回false?

为什么std :: forward在这种情况下无用

为什么在这种情况下转发参考无效?

为什么在这种情况下使用str()?

为什么在这种情况下打印为假?

为什么在这种情况下“ this”是全局/窗口对象?

为什么在这种情况下无法合并接口?

为什么在这种情况下事件处理程序不起作用?

为什么在这种情况下会有UnboundLocalError?

为什么在这种情况下Array的元素是可选的?

如何在通过检查第一个pygame.MOUSEBUTTONDOWN开始的循环中检测第二个pygame.MOUSEBUTTONDOWN

为什么在这种情况下使用memset

pygame.MOUSEBUTTONDOWN未注册

为什么我的pygame.MOUSEBUTTONDOWN无法正常工作?

为什么在这种情况下引用模板参数?

MOUSEBUTTONDOWN事件没有响应

pygame,如何使圆圈消失并与MOUSEBUTTONDOWN一起出现?

PyGame:MOUSEBUTTONDOWN事件的问题

为什么在这种情况下fzf失败

为什么在这种情况下,andmap返回#t?

在这种情况下,为什么 GetExternalLoginInfoAsync() 返回 null?

为什么在这种情况下创建循环?

为什么在这种情况下 cout 不会溢出?

Pygame - 在 MOUSEBUTTONDOWN 事件中讓子彈點亮屏幕

為什麼 MOUSEBUTTONDOWN 在 Pygame 中不起作用?

为什么在这种情况下需要引用/借用?