main_3d.py

Created by valmontechno

Created on June 12, 2024

4.1 KB


try:
    import os
    os.environ['KANDINSKY_ZOOM_RATIO'] = '2'
    isPc = True
except:
    isPc = False

from v_math import *
from graphics_3d import *
import graphics_engine as gh
import kandinsky as kd
from time import monotonic
from ion import *

SPEED = 1
ROTATION_SPEED = 1

mesh = [
    Edge3(Vector3(0, 0, 2), Vector3(1, 0, 2)),
    Edge3(Vector3(1, 0, 2), Vector3(1, 1, 2)),
    Edge3(Vector3(1, 1, 2), Vector3(0, 1, 2)),
    Edge3(Vector3(0, 1, 2), Vector3(0, 0, 2)),
    Edge3(Vector3(0, 0, 3), Vector3(1, 0, 3)),
    Edge3(Vector3(1, 0, 3), Vector3(1, 1, 3)),
    Edge3(Vector3(1, 1, 3), Vector3(0, 1, 3)),
    Edge3(Vector3(0, 1, 3), Vector3(0, 0, 3)),
    Edge3(Vector3(0, 0, 2), Vector3(0, 0, 3)),
    Edge3(Vector3(1, 0, 2), Vector3(1, 0, 3)),
    Edge3(Vector3(1, 1, 2), Vector3(1, 1, 3)),
    Edge3(Vector3(0, 1, 2), Vector3(0, 1, 3)),
]

camera = Camera(Vector3(3.37, 1, -3.46), -0.08, 0.56, 5)

showPos = True
gh.TOP = 18 * showPos

def drawCameraPosition():
    gh.logNumber(camera.position.x, Vector2(0, 0), '#ff0000')
    gh.logNumber(camera.position.y, Vector2(60, 0), '#00ff00')
    gh.logNumber(camera.position.z, Vector2(120, 0), '#0000ff')
    gh.logNumber(camera.pitch, Vector2(180, 0), '#aa0000')
    gh.logNumber(camera.yaw, Vector2(240, 0), '#00aa00')
drawCameraPosition()

deltaTime = 0
lastTime = monotonic()
firstFrame = True

while True:

    if any((firstFrame, keydown(KEY_DOWN), keydown(KEY_UP), keydown(KEY_LEFT), keydown(KEY_RIGHT), keydown(KEY_MINUS), keydown(KEY_PLUS), keydown(KEY_ALPHA), keydown(KEY_LEFTPARENTHESIS), keydown(KEY_RIGHTPARENTHESIS))):
        firstFrame = False

        if keydown(KEY_ALPHA):
            showPos = not showPos
            gh.TOP = 18 * showPos
            kd.fill_rect(0, 0, gh.WIDTH, gh.HEIGHT, '#ffffff')
            if showPos:
                drawCameraPosition()
        else:
            kd.fill_rect(0, gh.TOP, gh.WIDTH, gh.HEIGHT, '#ffffff')

        if keydown(KEY_OK) or keydown(KEY_SHIFT):
            if keydown(KEY_DOWN):
                camera.pitch = max(-QUARTER_CIRCLE, camera.pitch - ROTATION_SPEED * deltaTime)
            if keydown(KEY_UP):
                camera.pitch = min(QUARTER_CIRCLE, camera.pitch + ROTATION_SPEED * deltaTime)
            if keydown(KEY_LEFT):
                camera.yaw = (camera.yaw + ROTATION_SPEED * deltaTime) % CIRCLE
            if keydown(KEY_RIGHT):
                camera.yaw = (camera.yaw - ROTATION_SPEED * deltaTime) % CIRCLE
            if showPos:
                gh.logNumber(camera.pitch, Vector2(180, 0), '#aa0000')
                gh.logNumber(camera.yaw, Vector2(240, 0), '#00aa00')

        else:
            if keydown(KEY_DOWN):
                camera.position = camera.position.sub(camera.getForward().mulK(SPEED * deltaTime))
            if keydown(KEY_UP):
                camera.position = camera.position.add(camera.getForward().mulK(SPEED * deltaTime))
            if keydown(KEY_LEFT):
                camera.position = camera.position.sub(camera.getRight().mulK(SPEED * deltaTime))
            if keydown(KEY_RIGHT):
                camera.position = camera.position.add(camera.getRight().mulK(SPEED * deltaTime))
            if showPos:
                gh.logNumber(camera.position.x, Vector2(0, 0), '#ff0000')
                gh.logNumber(camera.position.z, Vector2(120, 0), '#0000ff')

        n = keydown(KEY_PLUS) - keydown(KEY_MINUS)
        if n:
            camera.position.y += n * SPEED * deltaTime
            if showPos:
                gh.logNumber(camera.position.y, Vector2(60, 0), '#00ff00')

        n = keydown(KEY_RIGHTPARENTHESIS) - keydown(KEY_LEFTPARENTHESIS)
        if n:
            camera.focalLenth = max(1, camera.focalLenth + n * SPEED * deltaTime)

        drawMesh(mesh, '#00ffff', camera)

        drawMesh([Edge3(Vector3(0, 0, 0), Vector3(1, 0, 0))], '#ff0000', camera)
        drawMesh([Edge3(Vector3(0, 0, 0), Vector3(0, 1, 0))], '#00ff00', camera)
        drawMesh([Edge3(Vector3(0, 0, 0), Vector3(0, 0, 1))], '#0000ff', camera)

        while keydown(KEY_ALPHA): pass

    elif isPc:
        kd.get_pixel(0, 0)

    time = monotonic()
    deltaTime = time - lastTime
    lastTime = time

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.