2016-03-25 47 views
0

我试图在我的游戏中创建一个商店,但我遇到了pygame.mouse.get_pressed()的问题。当用户点击一个按钮时,程序认为他们多次点击它,因此使用的用户数量比用户预期的要多。我想添加一个延迟,以便游戏不会这样做。我想1点击代表他们购买一个对象。我曾尝试降低该窗口的帧率,但结果相同。这是我现在的代码。如何在pygame中添加延迟点击鼠标?

这是发生所有鼠标动作的地方。

def button(x, y, w, h, ic, ac, action = None): 
    global paused 

    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, ac, (x, y, w, h)) 

     if click[0] == 1 and action == Game: 
      Game() 
     if click[0] == 1 and action == quitgame: 
      sys.exit() 
     if click[0] == 1 and action == None: 
      paused = False 
     if click[0] == 1 and action == StartScreen: 
      save() 
      StartScreen() 
     if click[0] == 1 and action == LootScreen: 
      LootScreen() 
     if click[0] == 1 and action == open_common_drop: 
      open_common_drop() 
     if click[0] == 1 and action == open_rare_drop: 
      open_rare_drop() 

    else: 
     pygame.draw.rect(gameDisplay, ic, (x, y, w, h)) 

这是战利品商店目前所在的地方。

def LootScreen(): 
    global current_drops 

    loot = True 

    while loot: 
     for event in pygame.event.get(): 

      if event.type == pygame.QUIT: 
       save() 

       pygame.quit() 
       sys.exit() 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_t: 
        open_common_drop() 
       elif event.key == pygame.K_y: 
        open_rare_drop() 

       if event.key == pygame.K_ESCAPE: 
        StartScreen() 

     gameDisplay.fill(gray) 
     title('Loot Chests!') 

     button(400, 150, 260, 50, blue, bright_blue, open_common_drop) 
     button(695, 150, 260, 50, red, bright_red, open_rare_drop) 
     button(display_width * 0.42, display_height/1.15, 255, 50, red, bright_red, StartScreen) 

     game_display_text('Open Common Chest (T)', 407, 165) 
     game_display_text('Open Rare Chest (Y)', 725, 165) 
     game_display_text('You Got: %s' % current_drops, 50, display_height/2) 
     game_display_text('Credits: %.2f' % player_loot_data['credit_count'], 15, 15) 
     game_display_text('Main Menu', display_width * 0.47, display_height/1.13) 

     game_display_text('Janus\': %s' % player_loot_data['loot_data_one'] , 950, 500) 
     game_display_text('Peace of Minds: %s' % player_loot_data['loot_data_two'], 950, 535) 

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

回答

0

我会建议一个定时布尔开关。这是一个应该提供帮助的例子。

import time 

boolswitch = False 
timer = 0.0 
clock = time.time() 
x = time 

while True: 
    if timer > 0.25: 
     boolswitch = True 
     timer = 0.0 
    else: 
     x = time.time() 
     timer += (x-clock) 
     clock = x 

    if boolswitch: 
     click = pygame.mouse.get_pressed() 
     if click[0] == 1: 
      boolswitch = False 
      clock = time.time() 
+0

我试图将其纳入我的按钮(),但它导致我的游戏黑屏,并变得没有反应。 –

+0

这是因为它是一个没有中断条件的while循环。这仅仅是定时布尔开关如何工作的一个例子。你必须将逻辑运用到游戏的while循环中。 – oxrock

+0

我会试着弄清楚如何为我的游戏做这个工作。我会让你知道它是否有效。 –

0

您需要在按钮功能中使用bool开关。在这里,我以一种应该工作的方式重新开发了这个功能。

def button(x, y, w, h, ic, ac, action = None, held): 
    global paused 

    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, ac, (x, y, w, h)) 

     if click[0] == 1: 
      if held == False: 
       if action == Game: 
        Game() 
       elif action == quitgame: 
        sys.exit() 
       elif action == None: 
        paused = False 
       elif action == StartScreen: 
        save() 
        StartScreen() 
       elif action == LootScreen: 
        LootScreen() 
       elif action == open_common_drop: 
        open_common_drop() 
       elif action == open_rare_drop: 
        open_rare_drop() 
      held = True 
     else: 
      held = False 

    else: 
     pygame.draw.rect(gameDisplay, ic, (x, y, w, h)) 
    return held 

正如你看到的,有一个新的变量held添加到函数来处理按钮是否滞留与否。这里已经拿到并返回,因此每次调用该函数时都不会重置。因此,让我告诉你为什么我这样写,以及这个逻辑是如何工作在其核心的。

import pygame 
# 
display = pygame.display.set_mode((800,600)) 
clock = pygame.time.Clock() 
# 
held = False # Variable to handle if mouse button is held 
# 
RUNNING = True 
while RUNNING: 
    clock.tick(60) 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      RUNNING = False 
    # 
    button = pygame.mouse.get_pressed() # Get mouse state 
    if button[0]: # Check if left mouse button is pressed 
     if held == False: # Check if button is held down 
      print(True) # If button is not held down, print true 
     held = True # Set held eqaual to true for next iteration 
    else: # If left mouse button is not pressed 
     held = False # held is set to false, alowing event to happen again 
    # 
    display.fill((0,0,0)) 
    # 
    pygame.display.flip() 
# 
pygame.quit() 

以上是一个非常简单的独立程序,它还实现了所监视的变量以监视是否保持鼠标按钮。在这个程序中,变量held首先被声明为False,因为没有按下鼠标。然后,在主循环中,调用pygame.mouse.get_pressed()来获取鼠标输入,然后立即检查鼠标左键。如果按下鼠标左键,将会发生held的检查。如果它是错误的,程序将打印True。然后将held设置为True以进行下一次迭代。如果没有按下鼠标左键,else:语句将触发,并将held重置为默认的False状态。