engine.py

Created by valmontechno

Created on March 08, 2024

1.88 KB


try:
    import os
    os.environ['KANDINSKY_OS_MODE'] = '0'
    os.environ['KANDINSKY_ZOOM_RATIO'] = '2'
    os.environ['ION_ENABLE_DEBUG'] = ''
    isPc = True
except: pass

import kandinsky as kd
from math import *
from lib_math import *

WIDTH = 320 // 5
HEIGHT = 222 // 5

def draw():
    for j in range(HEIGHT):
        for i in range(WIDTH):
            index = i + j*WIDTH
            color = pixelBuffer[index]
            if color != oldPixelBuffer[index]:
                for l in range(5):
                    for k in range(5):
                        kd.set_pixel(i*5+k, j*5+l, color)
                        oldPixelBuffer[index] = color

def fillScreen(color):
    global pixelBuffer
    pixelBuffer = [color] * (WIDTH * HEIGHT)

def putPixel(v, color):
    x = round(v.x)
    y = round(v.y)
    if 0 <= x < WIDTH and 0 <= y < HEIGHT:
        pixelBuffer[x + y*WIDTH] = color

def putTriangle(tri, color):
    def eq(p, a, b):
        return (a.x-p.x)*(b.y-p.y)-(a.y-p.y)*(b.x-p.x)

    xmin = min(tri.p1.x, tri.p2.x, tri.p3.x)
    xmax = max(tri.p1.x, tri.p2.x, tri.p3.x)
    ymin = min(tri.p1.y, tri.p2.y, tri.p3.y)
    ymax = max(tri.p1.y, tri.p2.y, tri.p3.y)

    for j in range(ymin, ymax +1):
        if 0 <= j < HEIGHT:
            for i in range(xmin, xmax +1):
                if 0 <= i < WIDTH:
                    pos = Vector2(i, j)
                    w1 = eq(pos, tri.p1, tri.p2)
                    w2 = eq(pos, tri.p2, tri.p3)
                    w3 = eq(pos, tri.p3, tri.p1)
                    if (w1 >= 0 and w2 >= 0 and w3 >= 0) or (-w1 >=0 and -w2 >= 0 and -w3 >= 0):
                        putPixel(pos, color)

pixelBuffer = []
fillScreen((0, 0, 0))
oldPixelBuffer = [None] * (WIDTH * HEIGHT)

for t in range(WIDTH):
    fillScreen((0, 0, 0))
    tri = Triangle(Vector2(100//5, 50//5), Vector2(300//5, 50//5), Vector2(t, 150//5))
    putTriangle(tri, (0, 0, 255))
    draw()