NOGDUS $1670.00 has been donated to NOGDUS!
July 20, 2017, 11:38:29 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Programming Starfields in QuickBASIC  (Read 1492 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: April 21, 2008, 07:49:44 PM »

Programming Starfields in QuickBASIC

Today I'm going to teach you how to make those cool moving starfields that you see in space shooter games!

Okay, let's get started.

To start off I am going to explain what, a Starfield means to us (game programmers).
A Starfield is, for our purposes, an array of objects that may be rendered to the screen.
Most Starfields come in 2 flavors: Verticle and Horizontal Starfields.

Here is a simple data structure for making a simple horizontal starfield:
Code:
' a simple data type to hold our star information
TYPE StarType
X AS INTEGER
Y AS INTEGER
XV AS INTEGER
END TYPE

Now, the X and Y are obviously the position of the star, but what is the XV variable for?
Simple. XV is the velocity of the star's movement along the X axis.
If we were making a Vertical starfield, we would use YV. Understand? Good.

Okay, we've got a data structure, now we need to create our array of stars.

Code:
DIM SHARED Stars(100) AS StarType

SUB InitStars ()
FOR INDEX = 0 TO 99
' position the stars randomly on the screen
Stars.X = INT(RND * 319)
Stars.Y = INT(RND * 199)

' set maximum velocity of 8 ppf, minimum of 2
Stars.XV = 3 * INT(RND * 3) + 2
NEXT
' note: ppf is pixels per frame
END SUB

What we did was set each of the 100 stars to a random position and set the velocity to a random number between 2 and 8.
how is the velocity set to that? you ask. well, its really simple if you look closely:

3 * INT(RND * 3) + 2 gives a 'random number' from 2 to 8.

INT(RND * 3) gives a number from 0 to 2

3 * 0 + 2 = 2
3 * 1 + 2 = 5
3 * 2 + 2 = 8

the only possible settings for the 'random number' are 2, 5 and 8.


Now, we need to be in graphics mode to draw starfields...I usually use SCREEN 13, but this time I'm going to be using SCREEN 7.

Code:
SUB ProgInit ()
' enter 320x200x16 graphics mode, and create 3 virtual 'pages' of video memory
' set page #2 to be active, #1 and #3 are inactive.
SCREEN 7, 0, 1, 0
CLS
END SUB

Lets create the main loop that will move our stars (this would be your game's main loop)

Code:
SUB ProgMain ()
done = FALSE
InitStars
DO
CLS
K$ = INKEY$
KEYPRESS$ = RIGHT$(K$, 1)
SELECT CASE KEYPRESS$ 
CASE CHR$(27)
done = TRUE
END SELECT

MoveStars
DrawStars
' copy active virtual page to page being viewed
PCOPY 1, 0
LOOP UNTIL done = TRUE
END SUB

Time for me to show you how to move those stars. First, let me explain how it works.
The way to move the stars, is to update the position by adding the velocity to each of the star's positions.
We don't want to have all the stars go offscreen forever, so we must make them 'reset' to simulate an infinite universe of stars.

Code:
SUB MoveStars ()
' for each star
FOR INDEX = 0 TO 99
' add the velocity to the position of the star
Stars.X = Stars.X + Stars.XV

' if the star moves past the right edge of the screen
' reset the star to the left edge of the screen
IF Stars.X > 319 THEN Stars.X = 0
NEXT
END SUB

well, it's time to draw those stars! For this example, we'll simply draw pixels at the position of each star. You could, however use bitmaps that look like stars, or anything, really. I've made some cool warp effects using starfield code. Smiley

Code:
SUB DrawStars ()
'' for each star
FOR INDEX = 0 TO 99
' draw a single pixel
PSET (Stars.X, Stars.Y)
NEXT
END SUB

Here is the whole source file for you to copy and save so you can instantly see the results.

Code:
'
' STARS.BAS
' (C) 2004, CCPS Solutions
' All rights reserved.
' http://www.ccpssolutions.com

' You are free to use this source in any way,
' provided you give credit where it is due.
' Thanks. :)

' declare all variables as INTEGERs
DEFINT A-Z

' some useful constants
CONST FALSE = 0
CONST TRUE = NOT FALSE

' create our star data structure
TYPE StarType
X AS INTEGER
Y AS INTEGER
XV AS INTEGER
END TYPE

' create our array of stars
DIM SHARED Stars(100) AS StarType

ProgInit
ProgMain

' return to text mode and quit
SCREEN 0
WIDTH 80
SYSTEM

DEFINT A-Z
SUB DrawStars ()
FOR INDEX = 0 TO 99
PSET (Stars.X, Stars.Y)
NEXT
END SUB

DEFINT A-Z
SUB MoveStars ()
FOR INDEX = 0 TO 99
Stars.X = Stars.X + Stars.XV
IF Stars.X > 319 THEN Stars.X = 0
NEXT
END SUB

DEFINT A-Z
SUB InitStars ()
FOR INDEX = 0 TO 99
' position the stars randomly on the screen
Stars.X = INT(RND * 319)
Stars.Y = INT(RND * 199)
' set maximum velocity of 8 ppf, minimum of 2
Stars.XV = 3 * INT(RND * 3) + 2
NEXT
' note: ppf is pixels per frame
END SUB

DEFINT A-Z
SUB ProgInit ()
' enter 320x200x16 graphics mode, and create 3 virtual 'pages' of video memory
' set page #2 to be active, #1 and #3 are inactive.
SCREEN 7, 0, 1, 0
CLS
END SUB

DEFINT A-Z
SUB ProgMain ()
done = FALSE
InitStars
DO
CLS
K$ = INKEY$
KEYPRESS$ = RIGHT$(K$, 1)
SELECT CASE KEYPRESS$ 
CASE CHR$(27)
done = TRUE
END SELECT

MoveStars
DrawStars
PCOPY 1, 0 ' copy active virtual page to page being viewed
LOOP UNTIL done = TRUE
END SUB

Well, after reading all that (you did read it didn't you?) you should be able to make your own cool space games!
Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!