BASIC

Author Topic: Bouncing Bullets  (Read 3223 times)

Offline quick2code

  • Mc. Print
  • *
  • Posts: 28
    • View Profile
Bouncing Bullets
« on: 2008-Oct-29 »
This is the first demo that I'm actually proud of. I know it can be optimized much better with arrays and such. I did the math but I don't really understand it. Sorry if this post is trivial.


Code: GLBasic [Select]
// --------------------------------- //
// Project: Ammo Types
// Start: Tuesday, October 28, 2008
// IDE Version: 5.360

GLOBAL LocBulletY
GLOBAL ScreenX, ScreenY
GLOBAL fontX, fontY, bullSizeX, bullSizeY

ScreenX=640
ScreenY=480

//Bullets starting point
LocBulletY=(ScreenY/2)

SETSCREEN ScreenX, ScreenY, 0
LIMITFPS 60

SETTRANSPARENCY RGB(199, 200, 255)

LOADFONT "FirstFont.png", 1
SETFONT 1
GETFONTSIZE fontX, fontY

//Put string in variable and get its length
Message1$ = "Left/Right Control moves bullets"
wordLen1=LEN(Message1$)

Message2$ = "Space temporarily lowers bounce"
wordLen2=LEN(Message2$)

LOADSPRITE "Bullet.bmp", 1
GETSPRITESIZE 1, bullSizeX, bullSizeY

//While Escape key is not pressed
WHILE KEY(01) = FALSE

        PRINT Message1$, (ScreenX/2)-(fontX*wordLen1)/2, 100
        PRINT Message2$, (ScreenX/2)-(fontX*wordLen2)/2, (100+fontY)


        bulletBounce()

SHOWSCREEN

WEND

END



FUNCTION bulletBounce:

GLOBAL bulletDir = 1
GLOBAL bounceLimit

//Move bullet
INC LocBulletY,bulletDir

bounceLimit=TRUE

//Set limit ball can bounce
IF bulletDir>20
        bounceLimit=FALSE
        //Space key lowers bounce
        IF KEY(57)
        bulletDir=bulletDir-5
        ENDIF
ENDIF

//Make bullet bounce
IF bounceLimit
        bulletDir=bulletDir+.5
ENDIF

//If bullet goes past top or bottom, reverse
IF LocBulletY>ScreenY

        bulletDir = bulletDir*-1

        ELSEIF LocBulletY<0

                bulletDir = bulletDir*-1

ENDIF

LOCAL bulletStart=20 //Starting position of bullets
GLOBAL bulletSpace=0 //Spacing increment



FOR x = 20 TO 200 STEP bulletStart //Draw several bullets


//Initialize bullet\
DRAWSPRITE 1, (ScreenX/2-(bullSizeX/2)*bulletStart)+(x+bulletSpace), LocBulletY

//Left and Right Control moves all bullets
//Beyond that, don&#39;t ask
IF KEY(157)

        IF bulletSpace<(ScreenX/2-(bullSizeX)*bulletStart)+(bullSizeX/2*bulletStart/2)
                bulletSpace=bulletSpace+1
        ENDIF

        ELSEIF KEY(29)
                IF bulletSpace>-(ScreenX/2-(bullSizeX/2)*bulletStart/2)+(bullSizeX/2*bulletStart/2)
                        bulletSpace=bulletSpace-1
                ENDIF
ENDIF


NEXT


ENDFUNCTION
 
« Last Edit: 2008-Oct-29 by quick2code »