animgalax.py

Created by schraf

Created on April 17, 2026

1.41 KB


from math import log, sin, cos, pi
from kandinsky import fill_rect, get_pixel, set_pixel
from ion import keydown
import ion


COUL = (0,0,0), (222,20,16), (255,166,0), (255,243,206)
W, H = 320, 222

_state = [1]
def rng_seed(s):
    _state[0] = s & 0xFFFFFFFF
def rng():
    x = _state[0]
    x ^= (x << 13) & 0xFFFFFFFF
    x ^= (x >> 17) & 0xFFFFFFFF
    x ^= (x << 5)  & 0xFFFFFFFF
    _state[0] = x & 0xFFFFFFFF
    return _state[0] / 4294967296.0

N_POINTS = 800
DROTAT   = 2 * pi / 900
SCALE    = 12

def draw_galaxy(rotat):
    rng_seed(1)
    for _ in range(N_POINTS):
        r = rng()
        dist  = log(r + 4.54e-5)
        angle = 2 * pi * rng()

        u = 6 * dist * sin(angle)
        v = 5 * dist * cos(angle)

        cd = cos(3 * dist + rotat)
        sd = sin(3 * dist + rotat)
        x =  u * cd + v * sd
        y = -u * sd + v * cd
        y += rng() * 3 - 1.5

        xt = 1.4 * x + 0.6 * y
        yt = 0.2 * x + 0.8 * y

        px = int(160 + xt * SCALE)
        py = int(111 + yt * SCALE)

        if 0 <= px < W and 0 <= py < H:
            cur = COUL.index(get_pixel(px, py))
            if cur < 3:
                set_pixel(px, py, COUL[cur + 1])

fill_rect(0, 0, W, H, COUL[0])
rotat = 0.0

while True:
    fill_rect(0, 0, W, H, COUL[0])
    draw_galaxy(rotat)
    rotat += DROTAT
    if rotat >= 2 * pi: rotat = 0.0
    if keydown(ion.KEY_HOME) or keydown(ion.KEY_BACK):
        break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.