简体   繁体   English

Pygame:检查是否按下按钮

[英]Pygame: Checking if button is pressed

I'm working on the start menu for my game in python using pygame, I made a function that detects if the mouse pressed on the position of each button and if it does, it changes the value of a boolean asigned to said button. 我正在使用pygame在python中为我的游戏创建开始菜单,我创建了一个函数,该函数可检测鼠标是否按下了每个按钮的位置,如果按下,它将更改分配给所述按钮的布尔值。 However the boolean values are not changing. 但是,布尔值没有改变。

Here is my code: 这是我的代码:

import pygame
import sys
import sqlite3 as lite

# questions
option_1 =  option_2 =  option_3 =  option_4 = ''

# misc
initial_pos = 220
event_i = False
event_g = False
event_t = False
event_e = False

...

# Button Settings
button = pygame.image.load("src/trans-button.png")
button = pygame.transform.scale(button, (display_width - 84, 70))

button_start = button_tutorial = button_top10 = button_salir = button

# Game Functions

def checkSelection():
    # We get the mouse coordinates
    mouse_x, mouse_y = pygame.mouse.get_pos()

    # Then proceed to evaluate it's position in relation with the buttons

    if(mouse_x >= 42 and mouse_x <= button.get_width() + 42 and mouse_y >= initial_pos and mouse_y <= initial_pos + button.get_height()):
        event_i = True
    if(mouse_x >= 42 and mouse_x <= button.get_width() + 42 and mouse_y >= initial_pos + button.get_height() + 25 and mouse_y <= initial_pos + button.get_height()*2 + 25):
        event_g = True
    if(mouse_x >= 42 and mouse_x <= button.get_width() + 42 and mouse_y >= initial_pos + button.get_height()*2 + 50 and mouse_y <= initial_pos + button.get_height()*3 + 50):
        event_t = True
    if(mouse_x >= 42 and mouse_x <= button.get_width() + 42 and mouse_y >= initial_pos + button.get_height()*3 + 75 and mouse_y <= initial_pos + button.get_height()*4 + 75):
        event_e = True

...

# Game Loop
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            checkSelection()
            checkSelection()

    if event_i:
        print 'Begin'
    if event_e:
        print 'It should exit'
        crashed = True

    window.blit(background, (0,0))
    drawAll()
    pygame.display.update()
    clock.tick(60)

#End Game
pygame.quit()
quit()
sys.exit()

The '...' are jumps I made in the code to show you only the parts related to the issues. 我在代码中所做的“ ...”跳转仅向您显示与问题相关的部分。

The position detection does work, I checked it using 'print' for debbuging. 位置检测确实有效,我使用“打印”对它进行了调试。

-- Update -- -更新-

Accidentally deleted the section where the button was defined. 意外删除了定义按钮的部分。
Also a few typos I made when I returned the code to it's original state. 当我将代码恢复为原始状态时,我也做了一些错别字。

The problem is that the variables in the checkSelection() function are local variables. 问题在于checkSelection()函数中的变量是局部变量。 You need to declare the variables as global in the function: 您需要在函数中将变量声明为global变量:

def checkSelection():
    global event_i
    global event_t
    # ...

The rules on local vs. global variables in Python are neatly summarised at the official Python Programming FAQ. 在Python官方编程常见问题解答中巧妙地总结了Python中局部变量与全局变量的规则。

PS It's not a good idea to use global variables inside functions. PS在函数内部使用全局变量不是一个好主意。 I would recommend to define a function that checks only one button area and returns the result, which can be assigned to the (global) variable: 我建议定义一个仅检查一个按钮区域并返回结果的函数,该函数可以分配给(全局)变量:

def checkSelection(x, y):
    mouse_x, mouse_y = pygame.mouse.get_pos()
    return x <= mouse_x <= x + button.get_width() and
           y <= mouse_y <= y + button.get_height()

...
event_i = checkSelection(42, 25)
event_t = checkSelection(42, 50)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM