Main forum > Tutorials

Shoot Em Up - Video

<< < (8/8)

aroldo:
Hello GLB community!

I decided to do some improvements in this great tutorial made by Gernot (Kitty Hello), so I did changes to it as follows:

* Added Sound
* Images
The Blaster GLB project,  image and sound files are attached in the zip file below.
Just unzip to the directory of your choice.

Enjoy!!!

Here is the code.

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

// A SHOT type. It's just the position in this sample
// Ein Schuss-TYPE. Hier ist nur die Position von Bedeutung
TYPE SHOT
x
y
ENDTYPE

TYPE FOE
x
y=10
speed=3
amour=4
ENDTYPE

TYPE BOOM
x
y
time
ENDTYPE
// Variables

// a SHOT for adding new ones later
// ein SHOT um später neue hinzuzufügen
LOCAL newshot AS SHOT
LOCAL newbug  AS FOE
LOCAL player_x, player_y

// Image Sizes
LOCAL player_w, player_h
LOCAL lazer_w, lazer_h
LOCAL shot_w, shot_h
LOCAL bug_w, bug_h
LOCAL boom_w, boom_h


LOCAL delay
LOCAL time, nextbug, didhit

LOCAL screenW, screenH
// Audio
GLOBAL vol = 1.0 // 0.0 low - 1.0 loud
GLOBAL pan = 0 // -1.0 left, 0 center, 1.0 right

// The SHOTs array. Here the shots get inserted
// das SHOTs Feld. Hier kommen die Schüsse rein
LOCAL shots[] AS SHOT
LOCAL bugs[] AS FOE
LOCAL poof[] AS BOOM

//Sound Files
LOADSOUND "Media/sounds/blow.wav", 0, 4 // shot
LOADSOUND "Media/sounds/explosion.wav", 1, 2 // explosion

// Images
LOADSPRITE "Media/images/lazer.png", 0
LOADSPRITE "Media/images/shot.png", 1
LOADSPRITE "Media/images/bug.png",  2
LOADSPRITE "Media/images/boom.png", 3

GETSPRITESIZE 0, lazer_w, lazer_h // lazer
GETSPRITESIZE 1, shot_w, shot_h // shot
GETSPRITESIZE 2, bug_w, bug_h // bug
GETSPRITESIZE 3, boom_w, boom_h // boom


GETSCREENSIZE screenW, screenH

// Set player X and Y position on the screen
player_x = screenW/2
player_y = screenH/3

WHILE TRUE
// Player Movement
// Spieler Bewegung
INC player_x, MOUSEAXIS(0)
// Limit to borders
// Auf den Rand begrenzen
player_x=MAX(MIN(player_x,screenW),0)

// Space bar and some time since last shot?
// Leertaste und etwas Zeit seit letztem Schuss vergangen?
IF MOUSEAXIS(3) AND delay<GETTIMERALL() // Prepare a shot
// Schuss vorbereiten
newshot.x = player_x+lazer_w/2-shot_w/2 // center shot on the cannon
newshot.y = player_y
// And stuff into the array
// Und in's Feld reinpacken
DIMPUSH shots[], newshot

play(0) // Play shooting sound!

// Get a time when the next shot can be fired
// Zeit festlegen, wann der nächste Schuss geladen ist
delay = GETTIMERALL() + 100
ENDIF


time = GETTIMERALL()
IF time>nextbug
nextbug = time + 500
LOCAL b AS FOE
DIMPUSH bugs[], b
ENDIF

FOREACH bug IN bugs[]
INC bug.x, bug.speed
IF bug.x<0 OR bug.x>screenW
INC bug.y, 32
bug.speed = -bug.speed
ENDIF
didhit=FALSE
FOREACH pop IN shots[]
IF BOXCOLL(pop.x, pop.y, 8,8, bug.x, bug.y, 32,32)
didhit=TRUE

play(1) // Play explosion sound

DELETE pop // continue to the end of the FOREACH loop
ENDIF
NEXT
IF didhit
LOCAL p AS BOOM
p.x = bug.x
p.y = bug.y-boom_h/2+bug_h/2
p.time = time
DIMPUSH poof[], p
DELETE bug
ENDIF
DRAWSPRITE 2, bug.x, bug.y // DRAW the Bug
NEXT


FOREACH p IN poof[]
LOCAL pos = (time - p.time) / 1000 // 0 ... 1
IF pos > 1 THEN DELETE p
ALPHAMODE 1-pos
// ******************************************************************

// IF you prefer the rotation effect coment DRAWSPRITE line
// and  uncomment the ROTOZOOMSPRITE line below.

// ROTOZOOMSPRITE 3, p.x, p.y, pos*1000, 1+pos*3 // DRAW Explosion
DRAWSPRITE 3, p.x,p.y // DRAW Explosion


// ******************************************************************
NEXT
ALPHAMODE 0



// Loop through all shots
// Durch alle Schüsse laufen
FOREACH pop IN shots[]
// Move shot up
// Schuss hoch bewegen
DEC pop.y, 5
// If shot reaches upper screen, remove it and continue
// Wenn Schuss am oberen Rand, dann entfernen und continue
IF pop.y<0 THEN DELETE pop

// Draw the shot
// Schuss zeichnen

DRAWSPRITE 1, pop.x, pop.y // DRAW the shot
NEXT

// Draw "player"
// "Spieler" zeichnen
DRAWSPRITE 0, player_x, player_y

SHOWSCREEN
WEND
// ------------------------------------------------------------- //
// ---  PLAY  ---
// ------------------------------------------------------------- //
FUNCTION play: ch
PLAYSOUND (ch, pan, vol) // channel, pan, vol
ENDFUNCTION // PLAY


--- End code ---

[attachment deleted by admin]

Navigation

[0] Message Index

[*] Previous page

Go to full version