TGC Codebase Backup



Basic 2d Starfield Using Types by Steve Fash

4th Dec 2005 9:43
Summary

A Basic 2d starfield in DBP using types..Reasonably commented so you can see what's what !



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `-----------------------------------------------------------------------------------------------------------
`Basic 2d starfield in DBPro By Fash - Converted From an original I wrote in Blitz
`Mainly written to test out basic type usage in DBP
`-----------------------------------------------------------------------------------------------------------
set window on:sync rate 60:sync on  `set window and sync rate etc
set display mode 800,600,16         `set display mode
global maxstars=50                  `max stars is our total number of stars
gosub setup_stars                   `setup our stars and their original location
repeat                              `repeat
   do                               `do the following loop
      cls                           `clear screen to avoid tearing
      gosub update_stars            `update our stars
      sync                          `sync our screen to see changes
   loop                             `loop back to the do
until escapekey()=1                 `until escape is pressed
end                                 `if it is then end the program
`-----------------------------------------------------------------------------------------------------------
setup_stars:
type stars                          `define our custom type
field x,y,speed                     `and it's fields
endtype                             `end of type
dim star(maxstars) as stars         `decide how many instances of the type we need
for a=0 to maxstars                 `fill the following variables "maxstars" amount of times
   star(a).x=rnd(800)               `random x position on the screen
   star(a).y=rnd (600)              `random y position on the screen
   star(a).speed=rnd(5)             `random speed
next                                `repeat and fill variable until we have filled them all
return                              `return from this subroutine and continue the program
`-----------------------------------------------------------------------------------------------------------
update_stars:
for a=0 to maxstars                 `repeat for all stars
   circle star(a).x,star(a).y,1     `plot a circle at each stars x and y position
   star(a).x=star(a).x-star(a).speed`move each star type by it's own star speed
   if star(a).x < 0                 `if it goes off the screen (position 0)
      star(a).x=800                 `redraw it again at the other side
   endif                            `if not, don't bother
next                                `repeat the loop until all stars (as per maxstars) have been done
return                              `return from this subroutine
`-----------------------------------------------------------------------------------------------------------