如何调整仅在我移动鼠标时才有效的代码
How adjust code that works only if I move the 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()
我想知道为什么我会遇到这种错误,如何解决,如果不可能,可能的解决方案。
问题是因为你在事件循环中执行了所有代码。事件循环中的代码仅在事件发生时执行,例如鼠标移动 (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()
我的代码应该是正确的,但它只有在我移动鼠标时才起作用,即使我什至没有设置鼠标也是如此。 代码应该绘制移动的同心圆。
我什至尝试过屏蔽鼠标,但还是不行
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()
我想知道为什么我会遇到这种错误,如何解决,如果不可能,可能的解决方案。
问题是因为你在事件循环中执行了所有代码。事件循环中的代码仅在事件发生时执行,例如鼠标移动 (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()