Pygame点击过于敏感

时间:2016-12-28 10:48:24

标签: python pygame

我正在制作游戏,游戏中有一个商店,您可以购买代币,如果您点击按钮购买它们,鼠标就会疯狂..

这是一个等待鼠标点击的功能。

def button(text, x, y, width, height, inactive_color, active_color, action):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
    pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
    if click[0] == 1:
        if action == "buy_slowdown":
            slowdown_powerup += 1

else:
    pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))

text_to_button(text, black, x, y, width, height)

这是调用函数的地方:

def shop():
shop = True
while shop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    gameDisplay.fill(white)
    # BUY SLOWDOWN TOKENS

    buyslowdownlabel = shopfont.render("Slowdown Tokens", 1, blue)

    slowdown_string_label = smallfont.render(str(slowdown_powerup), 1, black)

    gameDisplay.blit(buyslowdownlabel, [340, 190])

    pygame.draw.rect(gameDisplay, grey, [380, 235, 75, 75])

    gameDisplay.blit(slowdown_string_label, [410.5, 255.5])

    button("Buy", 380, 320, 100, 70, dark_yellow, yellow, "buy_slowdown")

    pygame.display.update()

1 个答案:

答案 0 :(得分:1)

使用event.MOUSEBUTTONDOWN代替pygame.mouse.get_pressed(),因为当按钮从未按下状态变为按下状态时,event.MOUSEBUTTONDOWN仅创建一次。当你按下时pygame.mouse.get_pressed()始终是True - 因为计算机比你快,所以当你点击按钮时它可以检查pygame.mouse.get_pressed()数千次。

#!/usr/bin/env python3


import pygame

# --- constants --- (UPPER_CASE names)

WHITE = (255,255,255)
BLACK = (  0,  0,  0)
DARK_YELLOW = (200,200,  0)
YELLOW = (255,255, 0)

# --- functions ---

def button_create(text, rect, inactive_color, active_color, action):

    font = pygame.font.Font(None, 40)

    button_rect = pygame.Rect(rect)

    text_buy = font.render(text, True, BLACK)
    text_buy_rect = text_buy.get_rect(center=button_rect.center)

    return [text_buy, text_buy_rect, button_rect, inactive_color, active_color, action, False]


def button_check(info, event):

    text, text_rect, rect, inactive_color, active_color, action, hover = info

    if event.type == pygame.MOUSEMOTION:
        # hover = True/False   
        info[-1] = rect.collidepoint(event.pos)

    elif event.type == pygame.MOUSEBUTTONDOWN:
        if hover and action:      
            action()


def button_draw(screen, info):

    text, text_rect, rect, inactive_color, active_color, action, hover = info

    if hover:
        color = active_color
    else:
        color = inactive_color

    pygame.draw.rect(screen, color, rect)
    screen.blit(text, text_rect)

# ---

def buy_slowdown(number=1):
    global slowdown_powerup

    slowdown_powerup += number
    print('slowdown_powerup:', slowdown_powerup)

def buy_1():
    buy_slowdown(1)

def buy_10():
    buy_slowdown(10)

def buy_100():
    buy_slowdown(100)

# --- main ---

# - init -

pygame.init()
screen = pygame.display.set_mode((800,600))
screen_rect = screen.get_rect()

# - objects -

slowdown_powerup = 0

button_1 = button_create("+1", (380, 235, 75, 75), DARK_YELLOW, YELLOW, buy_1)
button_2 = button_create("+10", (480, 235, 75, 75), DARK_YELLOW, YELLOW, buy_10)
button_3 = button_create("+100", (580, 235, 75, 75), DARK_YELLOW, YELLOW, buy_100)

# - mainloop -

shop = True

while shop:

    # - events -
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            shop = False

        button_check(button_1, event)
        button_check(button_2, event)
        button_check(button_3, event)

    # --- draws ---

    screen.fill(WHITE)

    button_draw(screen, button_1)
    button_draw(screen, button_2)
    button_draw(screen, button_3)

    pygame.display.update()

# - end -

pygame.quit()