0

我希望在之前单击鼠标后运行某个循环。我将一个名为 mouse_clicked 的变量初始化为 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)

谢谢!奥马尔

4

2 回答 2

2

回答太快后编辑

移动你的循环:

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。

于 2013-07-16T10:09:44.510 回答
1

看起来您的第二个循环甚至没有开始:您将 mouse_pressed 启动为 False。所以,

while mouse_pressed

必然会在循环开始之前停止循环。希望这可以帮助!

于 2013-07-16T10:11:38.020 回答