2014-03-01 28 views
1

我設法將從iOS應用程序設置速度和方向接收命令的腳本串起來。在Python中每17ms在屏幕上繪製一個點?

的事情是我沒有實際的設備,所以我的應用程序,而不是發送命令到一個小蟒蛇網絡套接字服務器我建立一個使用龍捲風......

基本上我會非常需要的是一種方法, :

顯示一個窗口 每隔17ms,清空窗口,用x和y讀取一個全局變量,並在x和y處繪製一個點或一個圓。

有沒有一個方便的方法來做到這一點,所以我可以直觀地看到發生了什麼?

如果我可以在每個X毫秒內在窗口中畫一個圓,我可以處理其餘的問題。

什麼需要添加:

-create a window 
-create a timer 
on timer callback: clear screen and draw a circle in the window. 
+0

你可以發佈你的腳本?沒有樣本討論就很難談論你的代碼。你想在哪裏顯示窗口?在設備上?你能列舉更詳細的步驟嗎? –

+0

@MylesBaker python腳本在我的電腦上運行,我需要腳本在我的電腦上顯示一個圓圈,我在終端窗口中運行腳本。理想情況下,我想製作一個窗口並繪製它。 – jmasterx

+0

您需要選擇一個繪圖庫。請看這裏:http://stackoverflow.com/questions/326300/python-best-library-for-drawing –

回答

5

你應該嘗試使用pygame的圖形工作。 首先下載pygame的

這裏是一個示例代碼

import pygame,sys 
from pygame import * 

WIDTH = 480 
HEIGHT = 480 
WHITE = (255,255,255) #RGB 
BLACK = (0,0,0) #RGB 

pygame.init() 
screen = display.set_mode((WIDTH,HEIGHT),0,32) 
display.set_caption("Name of Application") 
screen.fill(WHITE) 
timer = pygame.time.Clock() 
pos_on_screen, radius = (50, 50), 20  
while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
    timer.tick(60) #60 times per second you can do the math for 17 ms 
    draw.circle(screen, BLACK, pos_on_screen, radius) 
    display.update() 

希望幫助。記住你需要先下載pygame。 你也應該閱讀pygame。這真的很有幫助。

+0

碰巧,17ms幾乎是60 fps。 – jfs

+0

完成。它現在應該是完整的。 – sshashank124

0

你可以使用你的終端作爲「窗口」並在其中畫一個「圓圈」。作爲一個非常簡單的(和不可靠的)「計時器」,time.sleep()函數可用於:

#!/usr/bin/env python 
"""Print red circle walking randomly in the terminal.""" 
import random 
import time 
from blessings import Terminal # $ pip install blessings colorama 
import colorama; colorama.init() # for Windows support (not tested) 

directions = [(-1, -1), (-1, 0), (-1, 1), 
       (0, -1),   (0, 1), 
       (1, -1), (1, 0), (1, 1)] 
t = Terminal() 
with t.fullscreen(), t.hidden_cursor(): 
    cur_y, cur_x = t.height // 2, t.width // 2 # center of the screen 
    nsteps = min(cur_y, cur_x)**2 # average distance for random walker: sqrt(N) 
    for _ in range(nsteps): 
     y, x = random.choice(directions) 
     cur_y += y; cur_x += x # update current coordinates 
     print(t.move(cur_y, cur_x) + 
       t.bold_red(u'\N{BLACK CIRCLE}')) # draw circle 
     time.sleep(6 * 0.017) # it may sleep both less and more time 
     print(t.clear) # clear screen 

要嘗試它,代碼保存到random-walker.py並運行它:

$ python random-walker.py 

我不知道無論它在Windows上工作。