2014-01-09 16 views
0

我正在處理涉及在圓圈中繪製隨機像素的項目。我想提高像素的繪製速度。我如何改變每秒的幀數。我看了一些例子,但我的代碼仍然無法正常工作。我使用Python 3.2.3和1.9的Pygame這是我的代碼:使用Python繪圖時每秒的幀數

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

你如何繪圖 - PyGame,PyQt,PyOpenGL?根據框架,它可能會非常不同? – 2014-01-09 04:47:04

+0

我使用pygame繪圖 – user3175999

+0

我建議使用[Short Self Contained Correct Example](http://sscce.org)來給代碼提供更多的上下文。 – 2014-01-09 05:17:07

回答

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() 

祝你好運!

0

你可以嘗試只檢查過事件10幀:

count = 0 
while running: 
    if count >= 10: 
     for e in event.get(): 
      if e.type == QUIT: 
       running = False 
    else: 
     count += 1