我正在开发一个涉及在圆圈中绘制随机像素的项目。我想提高像素的绘制速度。如何更改每秒帧数。我查看了一些示例,但我的代码仍然不起作用。我使用的是Python 3.2.3和pygame 1.9这是我的代码:
from pygame import*
from random import*
screen = display.set_mode((1000,800))
tick = time.Clock()
rand_spraypaint_xs = []
rand_spraypaint_ys = []
col1 = (0,0,0)
canvasRect = Rect(100,100,500,500)
tool = 'spraypaint'
draw.rect(screen,(0,255,0),canvasRect,0)
running = True
while running:
for e in event.get():
if e.type == QUIT:
running = False
mx,my = mouse.get_pos()
mb = mouse.get_pressed()
x = randint(mx-30,mx+30)
y = randint(my-30,my+30)
dist =(((mx - x)**2 + (my - y)**2)**0.5)
if dist <=30:
rand_spraypaint_xs.append(x)
rand_spraypaint_ys.append(y)
if canvasRect.collidepoint(mx,my):
if tool == 'spraypaint':
if mb[0]==1:
screen.set_at((rand_spraypaint_xs[-1], rand_spraypaint_ys[-1]),col1)
time.wait(1)
tick.tick(10000)
display.flip()
quit()
答案 0 :(得分:0)
你可以尝试只检查10帧的事件。:
count = 0
while running:
if count >= 10:
for e in event.get():
if e.type == QUIT:
running = False
else:
count += 1
答案 1 :(得分:0)
执行此操作的最佳方法是使用for循环。如果将for循环设置为for i in range(10)
,它将每帧执行10次操作:
例如:
from random import*
screen = display.set_mode((1000,800))
tick = time.Clock()
col1 = (0,0,0)
canvasRect = Rect(100,100,500,500)
draw.rect(screen,(0,255,0),canvasRect,0)
running = True
while running:
for e in event.get():
if e.type == QUIT:
running = False
mx,my = mouse.get_pos()
mb = mouse.get_pressed()
if mb[0]:
for i in range(10): #8 can be changed to whatever value you want
#to make it faster
p = randint(0, 30), randint(0, 30)
draw.circle(screen, (0, 0, 0), p, 1)
tick.tick(100)
display.flip()
quit()
祝你好运!