My first code!

Blitz3D Forums/Blitz3D Beginners Area/My first code!

Flynn(Posted 2003) [#1]
Okay, go easy... (How do you do the black window thing to denote code)?


This is after a day or so, and I'm kind of proud of it. :)

It's just a simple player ship moves left and right (with 'Z' and 'X', fires a single bullet once every key-press of 'M' and plays a sound when it fires.

Feel free to criticise. I want to know if it can be made more efficient. I'm *very* pleased with sussing out how to use a flag variable to control the bullet's appearance, etc.

But there's a weird bug... Once the player has fired once, the bullet sound just keeps playing over and over at about one second pause interval. I can't see, from the code, why that would happen. Could this be another B+ bug? :)

All help appreciated. :D

Flynn.

-----
gfxplayer=LoadImage("graphics\player.bmp")
gfxlaser=LoadImage("graphics\laser.bmp")

;load and define sounds
soundlaser=LoadSound("sounds\soundlaser.wav")

;define player ship x coordinates and bullet coordinates
playerpos=320
las1=320
las2=390

;scale image up a bit
ScaleImage gfxplayer,1,1.5

;set bullet flag
bulletflag=0

;control loop
While Not KeyHit(1)


Cls

DrawImage gfxplayer, playerpos,420

;move ship left
If KeyDown(44) And playerpos>16 Then playerpos=playerpos-4

;move ship right
If KeyDown(45) And playerpos<590 Then playerpos=playerpos+4

;shoot condition
If KeyHit (50) And bulletflag=0 Then
PlaySound soundlaser
bulletflag=1
las1=playerpos+14
EndIf

;move laser
If bulletflag=1 Then
DrawImage gfxlaser,las1,las2
If las2>16 Then las2=las2-8
If las2<25 Then
las2=390
bulletflag=0
EndIf
EndIf

Flip

Wend

-----


keyboard(Posted 2003) [#2]
A Game Loop :)


Okay, go easy... (How do you do the black window thing to denote code)?


go to the <home> tab above, then look for the <FAQ> link on the top menu bar, and you will find useful stuff, like the bb codes to format your post.

its a great idea to format your code as you write, here is a common method.

If bulletflag=1 Then 
    DrawImage gfxlaser,las1,las2 
    If las2>16 Then las2=las2-8 
    If las2<25 Then 
        las2=390 
        bulletflag=0 
    EndIf 
EndIf



notice how the IF and the ENDIF that belongs to it are above one another? With a lot of loops and nested loops, that is an easy way to keep track of things.


Flynn(Posted 2003) [#3]
Thanks, keyboard. Again. :)

I solved the problem with the repeating sound. B+ doesn't seem to like wav files that were originally saved as loops (I used Reason 2.0).

Weird.