[英]Pygame Mouse Clicked Does Not Update
The function start_menu() leads to run_instructions() after a button press. 按下按钮后,函数start_menu()会导致run_instructions()。 In run_instructions() once a user clicks the mouse again it should go to another function however I think the click from the previous function carries on and automatically triggers click[0] to = 1 despite the fact no one has clicked anything.
在run_instructions()中,一旦用户再次单击鼠标,它应该转到另一个函数,但是我认为来自上一个函数的单击会继续进行并自动触发click [0] = 1,尽管实际上没有人单击任何东西。
def run_instructions():
clicked = False
while clicked == False:
click = pygame.mouse.get_pressed()
board.blit(instructions,[0,0])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if click[0] == 1:
create_environment()
clicked = True
def start_menu():
global menu
while menu == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 125 + 172 > mouse[0] > 150 and 448 + 69 > mouse[1] > 448 and click[0] == 1:
menu = False
run_instructions()
break
Is there anyway to make click[0] update or reset it to 0 when it enters run_instructions(). 无论如何,当click [0]进入run_instructions()时,是否可以对其进行更新或将其重置为0。 I've tried using pygame.MOUSEBUTTONDOWN but it gives the same problem.
我尝试使用pygame.MOUSEBUTTONDOWN,但是它也有同样的问题。
Thanks. 谢谢。
Checking for pygame.MOUSEBUTTONDOWN
events in the event loop is the right solution. 在事件循环中检查
pygame.MOUSEBUTTONDOWN
事件是正确的解决方案。 pygame.mouse.get_pressed
is problematic here because it only tells you if a mouse button is currently being held down and not if the button was clicked once. pygame.mouse.get_pressed
在这里有问题,因为它仅告诉您当前是否按住了鼠标按钮,而不告诉您是否单击过一次按钮。
Here's a working example. 这是一个工作示例。 I use a
pygame.Rect
as a very simple button in the first scene on which you have to click to get to the run_instructions
function. 我在第一个场景中使用
pygame.Rect
作为一个非常简单的按钮,您必须单击该按钮才能进入run_instructions
函数。 In the next scene (with the different background color) press the mouse button a second time and it'll print 'create_environment'. 在下一个场景(背景颜色不同)中,再次按下鼠标按钮,它将打印“ create_environment”。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
BG_COLOR = pygame.Color('gray12')
BG_COLOR2 = pygame.Color(50, 90, 120)
def run_instructions():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
print('create_environment')
screen.fill(BG_COLOR2)
pygame.display.flip()
clock.tick(30)
def start_menu():
button_rect = pygame.Rect(40, 100, 80, 50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# If the button collides with the mouse position.
if button_rect.collidepoint(event.pos):
run_instructions()
screen.fill(BG_COLOR)
pygame.draw.rect(screen, (90, 200, 50), button_rect)
pygame.display.flip()
clock.tick(30)
start_menu()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.