easiest way to plot x,y graphically during run-time?

Nick Craig-Wood nick at craig-wood.com
Thu Jun 4 10:29:42 CEST 2009


Esmail <ebonak at hotmail.com> wrote:
>  Scott David Daniels wrote:
> > Esmail wrote:
> >> ... Tk seems a bit more complex .. but I really don't know much about
> >> it and its interface with Python to make any sort of judgments as
> >> to which option would be better.
> > 
> > This should look pretty easy:
> 
>  Thanks Scott for taking the time to share this code with me, it
>  will give me something to study. I'm not sure if I'd say it looks
>  easy (but then again I am not very familiar with Tk :-)

Here is a demo with pygame...

import pygame
from pygame.locals import *
from random import randrange

size = width, height = 640, 480
background_colour = 0x00, 0x00, 0x00
foreground_colour = 0x51, 0xd0, 0x3c

def main():
    pygame.init()
    screen = pygame.display.set_mode(size, 0, 32)
    pygame.display.set_caption("A test of Pygame - press Up and Down")
    pygame.mouse.set_visible(0)
    clock = pygame.time.Clock()
    dots = [ (randrange(width), randrange(height)) for _ in range(100) ]
    radius = 10
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == QUIT:
                raise SystemExit(0)
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    raise SystemExit(0)
                elif event.key == K_UP:
                    radius += 1
                elif event.key == K_DOWN:
                    radius -= 1
        screen.fill(background_colour)
        for dot in dots:
            pygame.draw.circle(screen, foreground_colour, dot, radius, 1)
        dots = [ (dot[0]+randrange(-1,2), dot[1]+randrange(-1,2)) for dot in dots ]
        pygame.display.flip()

if __name__ == "__main__":
    main()

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list