Added gravity, dynamic creation of dots, and different colors.
EDIT: Also, no limit to FPS, and timer-based movement.
` DOT-EXAMPLE, v2.0
` by Martin Enderleit
` modified by AWESOMENESS
` further modification by ME. :P
` Feel free to modify and experiment. :)
` Set the screen updating to manual, and at MAX frames per second.
sync on : sync rate 0
` Set the display mode to 800x600, 32-Bit.
` Change to another bit-depth (16/24) if you can't run 32-bit.
set display mode 800, 600, 32
` Set the title of the window.
set window title "Dot Example v2.0"
` Create a User Defined Type, describing position, velocity, and color.
type MYDOT_INFO
x as float ` X-Position
y as float ` Y-Position
xv as float ` X-Velocity
yv as float ` Y-Velocity
col as dword ` Color
endtype
` Initialize the Dot functions.
InitDots()
` Setup some timing variables, so we can control the animation speed.
global difTime as integer
global oldTime as integer
oldTime = timer()
` The MAIN Loop.
do
` Update the timer.
difTime = timer() - oldTime
oldTime = timer()
` Update the "simulation".
n = rnd(100)
if n > 0 then AddDot()
UpdateDots()
` Clear the screen, draw the dots, and then update(sync) the screen.
cls 0
DrawDots()
ink rgb(255, 255, 0), 0
text 0, 0, "FPS: " + str$(screen fps())
sync
loop
` Initializes the MyDot array as a dynamic array. (It can grow and shrink in size)
` Also sets up a global variable for the number of dots we currently have.
function InitDots()
dim MyDot() as MYDOT_INFO
global NumDots as integer
NumDots = 0
endfunction
` Add a dot to the "simulation".
function AddDot()
` Increase the current number of Dots.
inc NumDots, 1
` Add a dot entry to the dynamic Array.
array insert at bottom MyDot()
` Set initial values.
MyDot().x = rnd(screen width()-1)
MyDot().y = 0.0
MyDot().yv = 0.0
MyDot().xv = 0.0
c = rnd(200)
MyDot().col = rgb(c+55, c+55, c+55)
endfunction
` Remove a dot from the "simulation".
function DeleteDot(d as integer)
if d >= 0 and d < NumDots
array delete element MyDot(), d
dec NumDots, 1
endif
endfunction
` This function updates the position of each dot in the Array.
` If a dot reaches the bottom of the screen it is moved back up to the top.
function UpdateDots()
if NumDots > 0
for i=0 to NumDots-1
inc MyDot(i).y, MyDot(i).yv * difTime ` Update the position.
inc MyDot(i).yv, 0.0001 * difTime ` Add gravity to the Y-Velocity.
if MyDot(i).y > screen height()-1
` If the dot reaches the bottom, remove it from the "simulation".
DeleteDot(i)
` Decrease "i" by 1 so that we don't miss an entry or go out of bounds.
dec i, 1
endif
next i
endif
endfunction
` This function draws all the dots to the screen at their current positions.
function DrawDots()
if NumDots > 0
lock pixels ` This locks the screen-memory and allows us to draw faster.
for i=0 to NumDots-1
dot MyDot(i).x, MyDot(i).y, MyDot(i).col
next i
unlock pixels ` Unlock the memory after we are done drawing.
endif
endfunction