drawing a point on the screen every 17ms in Python?
Asked Answered
C

3

2

I managed to string together a script that receives commands from an iOS app setting velocity and a direction.

The thing is I do not have the actual device, so my app instead sends commands to a little python web socket server I built that uses tornado...

Essentially what I would ideally need is a way to:

Display a window Every 17ms, clear the window, read a global variable with x and y and draw a point or circle at x and y.

Is there a convenient way to do this so I can visually see what's going on?

If I can get something to draw a circle in a window every X ms, I can handle the rest.

What needs to be added:

-create a window
-create a timer
on timer callback: clear screen and draw a circle in the window.
Cum answered 1/3, 2014 at 14:19 Comment(3)
Can you post your script? It is difficult to talk about your code without a sample to discuss. Where do you want the window displayed? On the device? Can you enumerate the steps with more detail?Toxicity
@MylesBaker The python script runs on my PC, I need the script to display a circle on my PC, I run the script in a terminal window. Ideally, I would like to make a Window and draw in it.Cum
You need to choose a library for drawing. See here: #326800Toxicity
U
7

You should try using pygame for graphics work. First download pygame

Here is a sample code

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

HOPE THAT HELPS. Remember you need to download pygame first. You should also read up on pygame. It is really helpful.

Urdar answered 1/3, 2014 at 14:53 Comment(2)
as it happens, 17ms is almost 60 fps.Helaina
Done. It should be complete now.Urdar
H
2

You could use your terminal as a "window" and draw a "circle" in it. As a very simple (and unreliable) "timer", time.sleep() function could be used:

#!/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

To try it, save the code into random-walker.py and run it:

$ python random-walker.py

I don't know whether it works on Windows.

Helaina answered 1/3, 2014 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.