【发布时间】:2017-08-03 15:13:14
【问题描述】:
我正在使用 pygame 在 python 中为我的游戏制作开始菜单,我创建了一个函数来检测鼠标是否按下了每个按钮的位置,如果是,它会更改分配给所述的布尔值按钮。但是布尔值没有改变。
这是我的代码:
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()
“...”是我在代码中进行的跳转,仅显示与问题相关的部分。
位置检测确实有效,我使用“打印”进行了调试。
-- 更新--
意外删除了定义按钮的部分。
当我将代码恢复到原始状态时,我也犯了一些错别字。
【问题讨论】:
-
button定义在哪里? -
第二个
if statement在checkSelection。您在mouse_x和event_g上有错字 -
另外,您似乎将布尔值传递给
checkSelection()函数,但没有将任何参数作为参数? -
修正了错别字并添加了定义按钮的部分。