cube_move.py

Created by antarctus

Created on December 01, 2022

2.05 KB

Script de manipulation 3D fait à partir de de la demo 3D de squarepoint

Utilisez les flèches. Et si possible Omega en logiciel de calculette.


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


MODEL=[]
COLORS=[(255,0,0),(255,255,0),(0,255,0),(0,255,255),(0,0,255),(255,0,255)]

def cube3d(x1,y1,z1,x2,y2,z2):
  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])

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

try:
  line=draw_line
except:
  pass


def render(cosx,sinx,cosy,siny,cosz,sinz):
  fill_rect(0,0,320,200,(0,0,0))
  for i in range(0,len(MODEL),2):
    x1,y1,z1=MODEL[i]
    x2,y2,z2=MODEL[i+1]

    #Rotations
    y1,z1=yturn(y1,z1,cosx,sinx)
    y2,z2=yturn(y2,z2,cosx,sinx)
    x1,z1=yturn(x1,z1,cosy,siny)
    x2,z2=yturn(x2,z2,cosy,siny)
    x1,y1=zturn(x1,y1,cosz,sinz)
    x2,y2=zturn(x2,y2,cosz,sinz)

    #Projections
    px1=int(x1*100/(z1+300))
    py1=int(y1*100/(z1+300))
    px2=int(x2*100/(z2+300))
    py2=int(y2*100/(z2+300))
    line(px1+160,py1+100,px2+160,py2+100,COLORS[i//2%6])

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

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

def zturn(x,y,cosz,sinz):
  return x*cosz-y*sinz,x*sinz+y*cosz

cube3d(-100,-100,-100,100,100,100)

xrot=0
yrot=0
zrot=0

fill_rect(0,0,320,222,(0,)*3)
draw_string("Full engine coming soon...",30,202,(255,255,255),(0,0,0))
while True:
  xrot+=pi/90*(keydown(KEY_UP)-keydown(KEY_DOWN))
  yrot+=pi/90*(keydown(KEY_LEFT)-keydown(KEY_RIGHT))

#  wait_vblank()
  render(cos(xrot),sin(xrot),cos(yrot),sin(yrot),cos(zrot),sin(zrot))
  sleep(0.01)

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.