NaaLaa

Full Version: Some spinning balls
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Remember something very similar from n6, some balls spinning around in space.

Code:
' balls.n7
' --------

include "list.n7"

#win32

set window "Balls", 640, 480
set redraw off

balls = List()
for z = -2 to 2  for y = -2 to 2  for x = -2 to 2  balls.Add(Ball(x, y, z))

additiveMode = false
a1 = 0; a2 = 0; az = 0
while not keydown(KEY_ESCAPE, true)
    if keydown(KEY_SPACE, true)  additiveMode = not additiveMode
    a1 = (a1 + 0.02)%(PI*2)
    a2 = (a2 + 0.013)%(PI*2)
    az = (az + 0.009)%(PI*2)
    for i = 0 to balls.size - 1
        ball = balls[i]
        ball.px = ball.x; ball.py = ball.y; ball.pz = ball.z
        x = ball.px*cos(a1) - ball.py*sin(a1); y = ball.py*cos(a1) + ball.px*sin(a1)
        ball.px = x; ball.py = y
        z = ball.pz*cos(a2) - ball.py*sin(a2); y = ball.py*cos(a2) + ball.pz*sin(a2)
        ball.pz = z; ball.py = y
        ball.pz = ball.pz + 5.5 + sin(az)*0.5
        ball.px = 320 + 200*ball.px/ball.pz; ball.py = 240 + 200*ball.py/ball.pz
    next
    balls.SortByField("pz", balls.DESCENDING)
    set color 0, 0, 0, 64
    cls
    set additive additiveMode
    for i = 0 to balls.size - 1
        s = 100/balls[i].pz
        intens = 255 - (balls[i].pz - 1)*50
        set color intens/2, intens/2, intens/2
        draw ellipse balls[i].px, balls[i].py, s, s, true
        set color intens*0.75, intens*0.75, intens*0.75
        draw ellipse balls[i].px - s*0.4, balls[i].py - s*0.4, s*0.25, s*0.25, true
    next
    set additive false
    set color 255, 255, 255
    set caret width(primary)/2, 480 - fheight()*2
    center "Press spacebar to toggle additive drawing"
    
    redraw
    fwait 60
wend

function Ball(x, y, z)
    return [x: x, y: y, z: z, px: 0, py: 0, pz: 0]
endfunc
Whoa! "additive" is very bright! I am hoping that it is just visible light rays... If it's gamma rays you better hope that I do not get angry... Moo Ha Ha Ha...

Cool demo...
(03-03-2024, 02:02 AM)johnno56 Wrote: [ -> ]Whoa! "additive" is very bright! I am hoping that it is just visible light rays... If it's gamma rays you better hope that I do not get angry... Moo Ha Ha Ha...

Cool demo...

I always picture Dr. Evil from Austin Powers when I read your "Moo Ha Ha Ha ..." Smile

https://youtu.be/7edeOEuXdMU?si=Vyj4cndlLnK78Us7
You know of uncle Evil? Cool...
(03-02-2024, 07:24 PM)Marcus Wrote: [ -> ]Remember something very similar from n6, some balls spinning around in space.
...

3D BALLS PUZZLE
Make 3D Square 3x3
Input 0 to 26
You only have 10 attempts to solve this puzzle.

Example
14,17,19,20,22,23,24,25,26
Find other solutions !

Hint : Start with number 0

It gave me an idea to make a 3D Balls Puzzle game based on your code. Big Grin

Code:
'=====================================
' 3D BALLS PUZZLE
' Make 3D Square 3x3
' Input 0 to 26
'
' Example : 14,17,19,20,22,23,24,25,26
' Find other solutions !
'
' Reference (Marcus)
' - balls.n7
'======================================


'-------------------
'  INITIALIZATION
'-------------------
#win32
include "list.n7"
include "sfx.n7"
visible balls = List()
visible sfx = SFX() 'create an sfx instance

set window "3D BALLS PUZZLE", 600/2, 400/2,false,3
set redraw off

'color definition
visible black_alpha     = [0,0,0,64]
visible black           = [0,0,0]
visible white           = [255,255,255]
visible green           = [0,168,0]
visible red             = [255,0,0]

Setup()

temp = 0
try = 1

'-------------
'  MAIN LOOP
'-------------
do
    'Prepare Instruction
    set color green
    set caret 5,5;wln "Make 3D square 3x3"; wln "Input 0 to 26" ; wln "Attempt "+try
    set caret 5,height(primary)-50; wln "99 to clear screen"; wln "55 to quit"
    Sound(0.1,261.63*3);Sound(0.02,329.63*2)
   
    'choose balls
    set caret 5,50; temp = rln(2,TYPE_NUMBER)
    if temp>=0 and temp<=26 then ChooseBalls(temp)
    if temp = 99 then
        try = 0
        set color black; cls
    endif
    if temp = 55 end
    if try%10= 0 then
        set color green; set caret 5,100; wln "Try again !"
        try = 1
        temp = rln()
        set color black; cls
    else
        try = try + 1
    endif
    redraw; wait 2
   
    'clear input
    set color black; draw rect 5,50,40,20,1; draw rect 68,38,20,10,1;redraw
loop

'------------
' FUNCTIONS
'------------
function Ball(x, y, z)
    return [x: x, y: y, z: z, px: 0, py: 0, pz: 0]
endfunc

function Setup()
    'Balls in 3 x 3 x 3
    for z = -1 to 1 
        for y = -1 to 1 
            for x = -1 to 1 
                balls.Add(Ball(x, y, z))
            next
        next
    next

    'angles
    a1 = 5.76
    a2 = 3.76

    'calculate balls' coordinate
    for i = 0 to 26
        ball = balls[i]
       
        ball.px = ball.x
        ball.py = ball.y
        ball.pz = ball.z
       
        x = ball.px*cos(a1) - ball.py*sin(a1)
        y = ball.py*cos(a1) + ball.px*sin(a1)
       
        ball.px = x
        ball.py = y
       
        z = ball.pz*cos(a2) - ball.py*sin(a2)
        y = ball.py*cos(a2) + ball.pz*sin(a2)
       
        ball.pz = z
        ball.py = y
       
        ball.pz = ball.pz + 5.5
        ball.px = width(primary)/2+ 200*ball.px/ball.pz +70
        ball.py = height(primary)/2 + 200*ball.py/ball.pz
    next
   
    balls.SortByField("pz", balls.DESCENDING)
endfunc

function ChooseBalls(i)
    'radius of each ball
    s = 100/balls[i].pz
   
    'Draw 3D balls
    set color red; draw ellipse balls[i].px, balls[i].py, s, s,1
    set color white; draw ellipse balls[i].px, balls[i].py, s, s
    set caret balls[i].px, balls[i].py; wln i
       
    'Draw little dot of light reflection
    set color white; draw ellipse balls[i].px - s*0.4, balls[i].py - s*0.4, s*0.25, s*0.25,1
endfunc

function Sound(b,x)
    'SquareWave(duration,freq,volume)
    mytune = sfx.SquareWave(b,x,0.05)
    play sound mytune
    wait 50
    free sound mytune 'release sound from memory
endfunc

Edit
- added sound effect
- limit attempts to solve : 10 times only