如何调整仅在移动鼠标时有效的代码

时间:2019-04-27 14:52:03

标签: python pygame mouse

我的代码应该正确,但是,即使我没有设置鼠标,它也只有在移动鼠标时才起作用。 代码应绘制同心圆运动。

我什至尝试过阻塞鼠标,但仍然无法正常工作

import pygame
import math
import time
pygame.init()
screen = pygame.display.set_mode([800,600])
black = (0,0,0)
keep_going = True

white = (255,255,255)
freq = 10
num_circles = 0
radius = 0

while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False


        radius = radius + 1
        num_circles = math.ceil(radius / freq)
        screen.fill(white)

        radiusMax = num_circles * freq

        pace = freq / radiusMax


        for y in range(num_circles, 1, -1):

            radiusY = int(((pace * (num_circles - y)) + pace) * radiusMax) + (radius % freq)

            pygame.draw.circle(screen, black,(400, 300), radiusY, 1)

        pygame.display.update()           
pygame.quit()

我想知道为什么我会遇到这种错误,如何解决它,如果不可能的话,可能的解决方案。

1 个答案:

答案 0 :(得分:1)

引起此问题的原因是您执行了事件循环中的所有代码。仅当发生事件(例如鼠标移动(pygame.MOUSEMOTION)时,才执行事件循环中的代码。

在主循环中而不是在事件循环中进行绘制以解决问题:

while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False

    #<--
    #<--
    #while input() != "quit":
    #for x in range(1): #importante
    raggio = raggio + 1
    num_cerchi = math.ceil(raggio / freq)
    screen.fill(white)

    raggioMax = num_cerchi * freq

    passo = freq / raggioMax
    #time.sleep(5)

    for y in range(num_cerchi, 1, -1):
    # 1, -1

        raggioY = int(((passo * (num_cerchi - y)) + passo) * raggioMax) + (raggio % freq)

        pygame.draw.circle(screen, black,(400, 300), raggioY, 1)

        #raggio = raggio+1 #importante
    clock.tick(60)
    pygame.display.update()           
pygame.quit()