2016-12-28 9 views
0

나는 게임을 만들고있어 너무 민감 게임은 토큰을 살 수있는 상점을 가지고 있으며, 버튼을 클릭하면 파이 게임 클릭

가 대기하는 기능입니다 .. 마우스가 미친 간다를 구입 마우스 클릭. 여기

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) 

는 함수가 호출되는 경우입니다 누를에 버튼을-누르지에서 상태를 변경하면 event.MOUSEBUTTONDOWN 한 번만 생성

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

사용 event.MOUSEBUTTONDOWN 대신 pygame.mouse.get_pressed() 때문이다. 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() 
+0

변경된 예 - 이제 3 개의 버튼이 있습니다. – furas