Orbiting+planet

When using sprites and the sprites are overlapping then the last sprite added will be on top. Knowing this we can move a sprite on top of others by 1) removing it with the REMOVESPRITE command, and 2) adding it back with the ADDSPRITE command. It will now be on top of other sprites, because it is the last sprite added.

The following code snippet demonstrates this, by making a ball (planet) orbit around another larger ball (sun). Switching the topmost sprite is done in the SELECT CASE block.

code format="qbasic" nomainwin WindowWidth = 400 WindowHeight = 400

'define constants rad = 90 / asn(1) angle = 0 long = 140 'longer radius of the orbit short = 24 'shorter radius sunR = 60 'radius of sun planetR = 16 'radius of planet

open "Sprites" for graphics_nsb as #main
 * 1) main, "trapclose [quit]"

'find centre of the graphics area
 * 1) main, "down; home; posxy centreX centreY"

'draw sun
 * 1) main, "place "; sunR; " "; sunR
 * 2) main, "color black; backcolor black"
 * 3) main, "circlefilled "; sunR
 * 4) main, "place 0 "; sunR*2; "; boxfilled "; sunR*2; " "; sunR*4
 * 5) main, "place "; sunR; " "; sunR*3
 * 6) main, "color yellow; backcolor yellow"
 * 7) main, "circlefilled "; sunR
 * 8) main, "getbmp sun 0 0 "; sunR*2; " "; sunR*4
 * 9) main, "fill white"

'draw planet
 * 1) main, "place "; planetR; " "; planetR
 * 2) main, "color black; backcolor black"
 * 3) main, "circlefilled "; planetR
 * 4) main, "place 0 "; planetR*2; "; boxfilled "; planetR*2; " "; planetR*4
 * 5) main, "place "; planetR; " "; planetR*3
 * 6) main, "color darkgreen; backcolor darkgreen"
 * 7) main, "circlefilled "; planetR
 * 8) main, "getbmp planet 0 0 "; planetR*2; " "; planetR*4
 * 9) main, "fill blue"
 * 10) main, "getbmp bg 0 0 10 10"
 * 11) main, "background bg"
 * 12) main, "addsprite sun sun; addsprite planet planet"

sunX = centreX - sunR sunY = centreY - sunR
 * 1) main, "spritexy sun "; sunX; " "; sunY

timer 40, [timerLoop] [timerLoop] 'if the planet is at the left-most or right-most position 'switch the order of the sprites select case angle case 180 'put sun back to its place case 0 end select
 * 1) main, "removesprite sun; addsprite sun sun"
 * 1) main, "spritexy sun "; sunX; " "; sunY
 * 1) main, "removesprite planet; addsprite planet planet"

'calculate planet's position alpha = angle/rad planetX = cos(alpha)*long + centreX - planetR planetY = sin(alpha)*short + centreY - planetR

'calculate planet's relative size for additional 3D look z = planetY-centreY size = 100+z size = size + size mod 2 'hopefully takes care of flickering
 * 1) main, "spritescale planet "; size


 * 1) main, "spritexy planet "; planetX; " "; planetY
 * 2) main, "drawsprites"

'increase angle for the next time through the loop angle = angle+3 'if full circle... if angle >= 360 then angle = 0 wait

[quit] timer 0 unloadbmp "sun" unloadbmp "planet" unloadbmp "bg" close #main end code


 * 1) Edit: added a routine to change the planets scale for additional 3-D looks.