epic3d.py

Created by squarepoint

Created on December 09, 2022

2.67 KB

A full 3d wireframe engine! It lags quite a bit though :/ Move with sin-cos-tan-ln, turn with 6-*-/-(


from math import cos,sin,pi
from kandinsky import *
from time import *
from ion import *

MODEL=[]

def cube3d(x1,y1,z1,x2,y2,z2,c):
  MODEL.append(c)
  line3d(x1,y1,z1,x1,y2,z1)
  line3d(x1,y1,z1,x2,y1,z1)
  line3d(x2,y1,z1,x2,y2,z1)
  line3d(x1,y2,z1,x2,y2,z1)
  line3d(x1,y1,z2,x1,y2,z2)
  line3d(x1,y1,z2,x2,y1,z2)
  line3d(x2,y1,z2,x2,y2,z2)
  line3d(x1,y2,z2,x2,y2,z2)
  line3d(x1,y1,z1,x1,y1,z2)
  line3d(x2,y1,z1,x2,y1,z2)
  line3d(x1,y2,z1,x1,y2,z2)
  line3d(x2,y2,z1,x2,y2,z2)

def line3d(x1,y1,z1,x2,y2,z2):
  point3d(x1,y1,z1)
  point3d(x2,y2,z2)

def point3d(x,y,z):
  MODEL.append([x,-y,z])
  #y invert bc axis goes from up to down

def line(x1,y1,x2,y2,c):
  res=2
  w=x2-x1
  h=y2-y1
  if abs(w)>=abs(h)and not w==0:
    d=h/w
    for i in range(0,w,(w>0)*res*2-1):
      fill_rect(x1+i,y1+int(d*i+0.5),res*2,res,c)
  elif abs(h)>abs(w):
    d=w/h
    for i in range(0,h,(h>0)*res*2-1):
      fill_rect(x1+int(d*i+0.5),y1+i,res,res*2,c)

def render(camx,camy,camz,sinx,cosx,siny,cosy):
  fill_rect(0,0,320,222,(0,0,0))
  draw_string("When the calculator is sus",30,202,(255,255,255),(0,0,0))
  for j in range(0,len(MODEL),25):
    c=MODEL[j]
    for i in range(j+1,j+25,2):
      x1=MODEL[i][0]-camx
      y1=MODEL[i][1]-camy
      z1=MODEL[i][2]-camz
      x2=MODEL[i+1][0]-camx
      y2=MODEL[i+1][1]-camy
      z2=MODEL[i+1][2]-camz

      x1,z1=yturn(x1,z1,cosy,siny)
      x2,z2=yturn(x2,z2,cosy,siny)
      y1,z1=xturn(y1,z1,cosx,sinx)
      y2,z2=xturn(y2,z2,cosx,sinx)

      px1=int(200*x1/z1)
      py1=int(200*y1/z1)
      px2=int(200*x2/z2)
      py2=int(200*y2/z2)
      line(px1+160,py1+111,px2+160,py2+111,c)

def yturn(x,z,cosy,siny):
  return z*siny+x*cosy,z*cosy-x*siny

def xturn(y,z,cosx,sinx):
  return y*cosx-z*sinx,y*sinx+z*cosx

RED=(255,0,0)
BLU=(0,100,255)

cube3d(-150,-200,-50,-50,0,50,RED)
cube3d(150,-200,-50,50,0,50,RED)
cube3d(-150,0,-50,150,400,50,RED)
cube3d(-100,250,-100,100,350,-50,BLU)
cube3d(-100,100,50,100,350,100,RED)

camx=0
camy=0
camz=-800
grav=0
camyrot=0
camxrot=0

while True:
  camy+=grav
  grav+=1
  if camy>0:
    camy=0
    grav=0
  if keydown(KEY_OK)and camy==0:
    grav=-20
  if keydown(KEY_SIX):
    camyrot+=pi/45
  elif keydown(KEY_DIVISION):
    camyrot-=pi/45
  if keydown(KEY_LEFTPARENTHESIS):
    camxrot-=pi/45
  elif keydown(KEY_MULTIPLICATION):
    camxrot+=pi/45
  sinx=sin(camxrot)
  cosx=cos(camxrot)
  siny=sin(camyrot)
  cosy=cos(camyrot)
  if keydown(KEY_SINE):
    camz-=siny*20
    camx-=cosy*20
  elif keydown(KEY_TANGENT):
    camz+=siny*20
    camx+=cosy*20
  if keydown(KEY_LN):
    camz+=cosy*20
    camx-=siny*20
  elif keydown(KEY_COSINE):
    camz-=cosy*20
    camx+=siny*20
  render(camx,camy,camz,sinx,cosx,siny,cosy)

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.