2014-03-01 25 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上工作。