Pygame点击太敏感 [英] Pygame click is too sensitive

查看:71
本文介绍了Pygame点击太敏感的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a game and the game has a shop which you can buy tokens and if you click the button to buy them the mouse goes crazy..

这是一个等待鼠标点击的函数.

This is a function which waits for the mouse click.

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)

这里是调用函​​数的地方:

Here is where the function is called:

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()

推荐答案

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

Use event.MOUSEBUTTONDOWN instead of pygame.mouse.get_pressed() because event.MOUSEBUTTONDOWN is created only once when button changes state from not-pressed into pressed. pygame.mouse.get_pressed() is True all the time when you keep pressed - and because computer is faster than you then it can check pygame.mouse.get_pressed() thousands time when you click button.

#!/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()

这篇关于Pygame点击太敏感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆