Super-TYPEs

Previous topic - Next topic

Kitty Hello

With the next update (soon, soon!) you will be able to write a shooting spaceship as easy as this:

Code (glbasic) Select
// --------------------------------- //
// Project: Blaster
// Start: Thursday, April 05, 2007
// IDE Version: 4.145

// A SHOT type. It's just the position in this sample
TYPE SHOT
x
y
ENDTYPE

// a SHOT for adding new ones later
LOCAL newshot AS SHOT

// The SHOTs array. Here the shots get inserted
LOCAL shots[] AS SHOT

playerx = 320
WHILE TRUE
// Player Movement
INC playerx, KEY(205)-KEY(203)
// Limit to borders
playerx=MAX(MIN(playerx,600),40)

// Space bar and some time since last shot?
IF KEY(57) AND delay // Prepare a shot
newshot.x = playerx
newshot.y = 400
// And stuff into the array
DIMPUSH shots[], newshot
// Get a time when the next shot can be fired
delay = GETTIMERALL() + 100
ENDIF

// Loop through all shots
FOREACH pop IN shots[]
// Move shot up
DEC pop.y, 1
// If shot reaches upper screen, remove it and continue
IF pop.y<0 THEN DELETE pop

// Draw the shot
PRINT "|", pop.x, pop.y
NEXT

// Draw "player"
PRINT "A", playerx, 400

// some status
PRINT "Shots: "+BOUNDS(shots[], 0), 0,0
SHOWSCREEN
WEND

Lazarus


bigsofty

"FOREACH"... yay... no more BOUNDS in my FOR loops ;)
"DIMPUSH"... array as a stack?
"DELETE" ... pop off of stack?


Looks very cool, types are turning out to be REALLY powerful in GLBasic!
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

bigsofty

These are great... cut my code in half and its now much more readable! :)
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)