2016-05-26 8 views
0

ich eine Taste Funktion in PyGame haben, und hier ist der Code dafür:Wie machen Taste nicht haltbaren pygame

def button(msg, x, y, w, h, ic, ac, action = None): 
    global mousedown 
    mouse = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 

    if x + w > mouse[0] > x and y + h > mouse[1] > y: 
     pygame.draw.rect(gameDisplay, COLORS[ic], (x, y, w, h)) 
     if click[0] == 1 and action is not None: 
      mousedown = True 
      function = action() 
      return function 

Das Problem, das ich habe, ist, wenn ich die Taste gedrückt halten, die Funktion übergeben Der Knopf wird solange wiederholt, bis die Maus unklickt. Gibt es eine Möglichkeit, das Problem zu beheben, so dass der Benutzer den Knopf nur einmal drücken kann und den Knopf loslassen muss, um die Funktion zu wiederholen?

Antwort

1

Sie könnten nur einen not_press Zustand am Anfang der Funktion hinzufügen, die am Ende des button() Aufruf false fertig ist und der auf true gesetzt zurück, wenn pygame.MOUSEBUTTONUP ist registriert (dh wenn die Schaltfläche nicht geklickt wird). in der Hauptereignisschleife

def button(msg, x, y, w, h, ic, ac, action = None): 
    if not_press == True 
     global mousedown 
     mouse = pygame.mouse.get_pos() 
     click = pygame.mouse.get_pressed() 

     if x + w > mouse[0] > x and y + h > mouse[1] > y: 
      pygame.draw.rect(gameDisplay, COLORS[ic], (x, y, w, h)) 
      if click[0] == 1 and action is not None: 
       mousedown = True 
       function = action() 
       not_press = False #note this will only disable the button() if the code get's that far ... if you want to disable it no matter what just change the indent (see grayed comment below) 
       return function 
    #not_press = False # if put here will only allow the user to press the button once no matter if the content can be done or not 

und dann:

So etwas wie

for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
     quit() 
    elif event.type == pygame.MOUSEBUTTONUP: #This will check if you have unclicked 
     not_press = True # and only then reset the condition for the content of Button() to be done again 
0

Anstatt eine Schaltflächenfunktion zu erstellen, können Sie die integrierte Funktion des pygame-Moduls verwenden.

Dies ist die Hauptfunktion des Moduls, um die Mausklicks zu überprüfen.

pygame.MOUSEBUTTONDOWN

for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      quit() 
     elif event.type == pygame.MOUSEBUTTONDOWN: #This will check if you clicked the mouse 
      if event.button == 1: # left mouse button? #Checks if you did left click 

      #Do rest.... 
+0

Möchten Sie den Befehl bei jedem Spiel Schleife nicht, dass noch wiederholen, wenn die Taste gedrückt bleibt? – Sorade

+0

Was Sie gesagt haben, ist "Gibt es eine Möglichkeit, es zu beheben, so dass der Benutzer den Knopf nur einmal drücken kann, und loszulassen und zu wiederholen, um die Funktion zu wiederholen?" Also ja, wenn Sie die Taste loslassen, müssen Sie erneut darauf klicken, um die Funktion zu erhalten. –