簡體   English   中英

如何獲得鼠標點擊

[英]How to get mouse clicks

我正在用Pygame編寫一個小游戲,我不確定如何獲得鼠標點擊。 我已經弄清楚了一點,但說實話,我沒有最好的時間。

這就是我所擁有的:

import pygame

pygame.init()

def menu():
    title_font = pygame.font.SysFont("monospace",64)
    button_font = pygame.font.SysFont("monospace", 30)
    screen_size = (1280,950)
    screen = pygame.display.set_mode(screen_size)

    background = (255,255,255)
    screen.fill(background)
    play_button = pygame.Rect(250,600,200,100)
    quit_button = pygame.Rect(850,600,200,100)
    controls_button = pygame.Rect(550,600,200,100)
    pygame.draw.rect(screen, (0,255,0), play_button)
    pygame.draw.rect(screen, (255,0,0), quit_button)
    pygame.draw.rect(screen, (255,229,0), controls_button)
    title = title_font.render("Fightastic!", 1, (0,0,0))
    screen.blit(title, (450,300))
    play_text = button_font.render("START",1,(0,0,0))
    screen.blit(play_text, (310,635))
    quit_text = button_font.render("QUIT",1,(0,0,0))
    screen.blit(quit_text, (910,635))
    controls_text = button_font.render("CONTROLS",1,(0,0,0))
    screen.blit(controls_text, (580,635))
    buttons = [play_button, quit_button, controls_button]

    while True:

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                exit()

        mouse_cursor = pygame.mouse.get_pos()
        mouse_pressed = pygame.mouse.get_pressed()

        option = 0
        for i in range(len(buttons)):
            if buttons[i].collidepoint( mouse_cursor):
                option = i+1

        if option == 1:
            print ("YO I GOT CLICKED")
        elif option == 2:
            print ("CLICKED MY DUDE")
        elif option == 3:
            quit()

    pygame.display.update()

menu()

游戲的菜單是唯一需要點擊的部分,這就是我所展示的全部內容。

謝謝!

你已經有pygame.mouse.get_pressed()所以用它來檢查點擊了哪個按鈕。

    option = 0

    if mouse_pressed[0]: # check if left button was clicked
        for i, but in enumerate(buttons, 1): # get button and its number
            if but.collidepoint(mouse_cursor): # check collision
                option = i # remember button number
                break      # no need to check other buttons

但是使用pygame.mouse.get_pressed()會產生一個問題 - 它會一直按住鼠標按鈕返回True (所以看起來每秒都有很多次點擊)所以如果你更改屏幕上的按鈕,新按鈕就會一樣然后它會自動點擊新按鈕。 最好使用event.type == pygame.MOUSEBUTTONDOWN ,它只創建一次點擊。

while True:

    option = 0

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            for i, but in enumerate(buttons, 1):
                if but.collidepoint(event.pos):
                    option = i
                    break # no need to check other buttons

    if option == 1:
        print ("YO I GOT CLICKED")
    elif option == 2:
        print ("CLICKED MY DUDE")
    elif option == 3:
        quit()

    pygame.display.update()

BTW:

您可以保留帶回調的按鈕(函數名稱不帶()

    buttons = [
        (play_button, play_function),
        (controls_button, control_function),
        (quit_button, quit_function),
    ]

然后你可以直接調用function / callback(using () )而無需option

            if event.type == pygame.MOUSEBUTTONDOWN:
                for rect, callback in buttons:
                    if rect.collidepoint(event.pos):
                        callback() # execute function
                        break # no need to check other buttons

完整的工作代碼

import pygame

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 950

WHITE = (255, 255, 255)

# --- functions --- (lower_case_names)

def play_function():
    print("YO I GOT CLICKED")

def controls_function():
    print("CLICKED MY DUDE")

def quit_function():
    pygame.quit()
    quit()

def menu(screen):

    # - init -

    title_font = pygame.font.SysFont("monospace", 64)
    button_font = pygame.font.SysFont("monospace", 30)

    # - objects -

    play_button = pygame.Rect(250,600,200,100)
    quit_button = pygame.Rect(850,600,200,100)
    controls_button = pygame.Rect(550,600,200,100)

    buttons = [
        (play_button, play_function),
        (controls_button, controls_function),
        (quit_button, quit_function),
    ]

    # - draws -

    screen.fill(WHITE)

    title = title_font.render("Fightastic!", 1, (0,0,0))
    screen.blit(title, (450,300))


    pygame.draw.rect(screen, (0,255,0), play_button)
    play_text = button_font.render("START",1,(0,0,0))
    screen.blit(play_text, (310,635))

    pygame.draw.rect(screen, (255,0,0), quit_button)
    quit_text = button_font.render("QUIT",1,(0,0,0))
    screen.blit(quit_text, (910,635))

    pygame.draw.rect(screen, (255,229,0), controls_button)
    controls_text = button_font.render("CONTROLS",1,(0,0,0))
    screen.blit(controls_text, (580,635))

    pygame.display.update()

    # - mainloop -

    clock = pygame.time.Clock()

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                for rect, callback in buttons:
                    if rect.collidepoint(event.pos):
                        callback() # execute function
                        break # no need to check other buttons

        clock.tick(5) # 5 FPS - to slow down game and use less CPU

# --- main ---

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

menu(screen)

使用鼠標輸入實際上相對容易使用,您可以用很少的代碼編寫強大的交互性。

我拿了你給的代碼並做了一些調整和修復。

  • 縮進行pygame.display.update()

  • 實現了完整的鼠標點擊檢測。

  • 修改了使用option的方式和列表buttons的順序,以便按鈕按從左到右的順序排列。

主要的補充是if語句中的mouse_pressed[0] == 1部分。 這將獲取鼠標按鈕列表(左,右,中)和一個int 0或1,指示是否按下它們。 因此,代碼檢查是否按下了左按鈕。

這是代碼:

import pygame

pygame.init()

def menu():
    title_font = pygame.font.SysFont("monospace",64)
    button_font = pygame.font.SysFont("monospace", 30)
    screen_size = (1280,950)
    screen = pygame.display.set_mode(screen_size)

    background = (255,255,255)
    screen.fill(background)
    play_button = pygame.Rect(250,600,200,100)
    quit_button = pygame.Rect(850,600,200,100)
    controls_button = pygame.Rect(550,600,200,100)
    pygame.draw.rect(screen, (0,255,0), play_button)
    pygame.draw.rect(screen, (255,0,0), quit_button)
    pygame.draw.rect(screen, (255,229,0), controls_button)
    title = title_font.render("Fightastic!", 1, (0,0,0))
    screen.blit(title, (450,300))
    play_text = button_font.render("START",1,(0,0,0))
    screen.blit(play_text, (310,635))
    quit_text = button_font.render("QUIT",1,(0,0,0))
    screen.blit(quit_text, (910,635))
    controls_text = button_font.render("CONTROLS",1,(0,0,0))
    screen.blit(controls_text, (580,635))
    buttons = [play_button, controls_button, quit_button]

    while True:

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                exit()

        mouse_cursor = pygame.mouse.get_pos()
        mouse_pressed = pygame.mouse.get_pressed()

        option = -1

        for i in range(len(buttons)):
            if buttons[i].collidepoint( mouse_cursor ) and mouse_pressed[0] == 1:
                option = i

        if option == 0:
            print ("YO I GOT CLICKED")
        elif option == 1:
            print ("CLICKED MY DUDE")
        elif option == 2:
            quit()

        pygame.display.update()

menu()

我希望這有用,如果您有任何其他問題,請隨時在下面發表評論!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM