TGC Codebase Backup



Particle Manipulation Sample by Anonymous Coder

21st Jun 2004 4:51
Summary

How to manipulate particles.



Description

I'm new to DarkBASIC so my code is probably very unoptimized but not bad for having it for 1 day eh :p
This shows how to change colors and such on particles through user input.
v1.1
Added ability to randomly generate a color to use on particles
Added ability to gradually change a user specified color with the left/right arrow keys
Help menu is now only shown when the user presses "H"

P.S. if theres any way that i can shorten the code let me know because right now its rather long ;)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Project: Particles
REM Created: 6/20/2004 5:37:19 PM
REM
REM ***** Main Source File *****
REM Coded by Jakal
REM feel free to use any piece of this code as your own :)
REM ****************************

`sync stuff
sync on
sync rate 60

`variables
gravity#=0
`default color change = red
color$="red"
red=255
blue=255
green=255

`pre-loop stuff to make particles and set color and position
make particles 1,0,10,10
color particles 1, R#, G#, B#
position particles 1, 0, -10, 0
`set the background color for the default camera to black
color backdrop black
`start main loop
do
`instructions on how to work everything
print "Newbie particle system"
print "Press H for Help on how to use program"



`set a default for particle emission to reset to after user has let go of "stop emission" button
set particle emissions 1, 200

`slowly increase gravity
if upkey() = 1
   gravity#=gravity#+0.5
   set particle gravity 1, gravity#
endif

`slowly decrease gravity
if downkey() = 1
   gravity#=gravity#-0.5
   set particle gravity 1, gravity#
endif

`stop emitting particles while "spacebar" is held down
if spacekey() = 1
   set particle emissions 1, 0
endif

`scancodes for the letters r, g, and b
rem  19 = r
rem  34 = g
rem  48 = b

`if scancode for r is detected switch color active color changer to red
if keystate(19)=1 then color$="red"
`same as above except changing to green
if keystate(34)=1 then color$="green"
`yet again except changing to blue
if keystate(48)=1 then color$="blue"

`decrease color depending on which color change is active
while leftkey()=1
if color$="red"
red=red-1
color particles 1, red, green, blue
endif
if color$="green"
green=green-1
color particles 1, red, green, blue
endif
if color$="blue"
blue=blue-1
color particles 1, red, green, blue
endif
sync
endwhile

`increase color change depending on which one is active
while rightkey()=1
if color$="red"
red=red+1
color particles 1, red, green, blue
endif
if color$="green"
green=green+1
color particles 1, red, green, blue
endif
if color$="blue"
blue=blue+1
color particles 1, red, green, blue
endif
sync
endwhile

`choose a random color for particles if enter key is pressed
if returnkey()=1 then color particles 1, rnd(255), rnd(255), rnd(255)

if keystate(35)=1 then HelpFunc()

sync
loop

`function to show commands
function HelpFunc()
`clear screen then show commands
cls
print "Use the up and down keys to increase/decrease Gravity"
print "Press Spacebar to stop emitting particles"
print "Press R then use left/right to increase/decrease RED color"
print "Press G then use left/right to increase/decrease GREEN color"
print "Press B then use left/right to increase/decrease BLUE color"
print "Whenever the enter key is pressed a random particle color will be chosen"
print "When you are done press any key to go back to the pretty particles :)"
`wait for user to press a key before resuming the program
sync
wait key
`clear the screen then hop out of the function
cls
endfunction