Created
October 23, 2024 08:57
-
-
Save jul/16f12b9f45229665d2ca1c2e4747710d to your computer and use it in GitHub Desktop.
making animation with the ghostscript interpreter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf8 -*- | |
from subprocess import Popen, PIPE, STDOUT | |
import sys, os | |
from time import sleep | |
import select | |
from random import randint | |
MAXX=512 | |
MAXY=512 | |
BX=350 | |
BY=350 | |
gs = Popen(['gs', f"-g{MAXX}x{MAXY}"], | |
stdin=PIPE, | |
stdout=PIPE, | |
stderr=PIPE, | |
bufsize=-1, | |
) | |
os.set_blocking(gs.stdout.fileno(), False) | |
os.set_blocking(gs.stdin.fileno(), False) | |
os.set_blocking(gs.stderr.fileno(), False) | |
def puts(s): | |
s += "\n" | |
select.select([], [gs.stdin], []) | |
gs.stdin.write(s.encode("latin1")) | |
gs.stdin.flush() | |
try: | |
back = gs.stderr.read().decode("latin1") | |
if back: | |
print(back) | |
except: pass | |
vx= randint(-5,5) | |
vy= randint(-5,5) | |
x=250 | |
y=250 | |
r = 10 | |
puts("newpath") | |
def draw_oval_at(x,y, r): | |
global vx, vy, MAXY, MAXX | |
puts("newpath") | |
puts(f"{x} {y} {r} 0 360 arc closepath") | |
puts("0.7 setgray fill") | |
puts("stroke") | |
puts("1 setlinewidth") | |
while True: | |
puts(f"newpath {r} {r} moveto {BX-r} {r} lineto closepath stroke") | |
puts(f"newpath {r} {r} moveto {r} {BY-r} lineto closepath stroke") | |
puts(f"newpath {BX-r} {r} moveto {BX-r} {BY-r} lineto closepath stroke") | |
puts(f"newpath {r} {BY-r} moveto {BX-r} {BY-r} lineto closepath stroke") | |
if not r < x +vx < (BX -r): | |
vx = -vx | |
if not r < y +vy < (BY -r): | |
vy = -vy | |
x+=vx | |
y+=vy | |
draw_oval_at(x, y, r) | |
sleep(.01) | |
puts("erasepage") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment