Pygame:在鼠标cli之后运行一个循环

2024-04-27 00:43:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在鼠标被点击后运行一个循环。 我启动了一个名为mouse_ched的变量为false,然后在单击鼠标后将其更改为True。然而,这似乎并不能让事情在以后继续下去。 我的代码是:

import sys, pygame

size = width, height = 320, 240
screen = pygame.display.set_mode(size)

running = True
mouse_pressed = False

while running:
    while mouse_pressed:
        rect = pygame.Rect(10, 20, 30, 30)
        pygame.draw.rect(screen, (255,0,0), rect)
        pygame.display.flip()

        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pressed = True
            if event.type == pygame.QUIT:
                running = False    
sys.exit(0)

谢谢! 奥马尔


Tags: recteventfalsetruesizeifdisplaysys
2条回答

看起来你的第二个循环甚至没有启动:你启动的鼠标按下错误。因此

while mouse_pressed

一定会在循环开始之前停止循环。 希望这有帮助!在

在回答过快后编辑了

移动你的循环:

while running:
    rect = pygame.Rect(10, 20, 30, 30)
    pygame.draw.rect(screen, (255,0,0), rect)
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pressed = True
        if event.type == pygame.QUIT:
            running = False   

    while mouse_pressed:
        # do your stuff
        mouse_pressed = False

在您的版本中,整个循环永远不会启动,因为mouse_pressed被初始化为False。在

相关问题 更多 >