Shooting game model

Do you have something to share with us? A tip for newbies, perhaps? Post it here.
Locked
nixe
Newcomer
Posts: 21
Joined: Tue Aug 28, 2007 5:47 pm
Location: Haapavesi
Contact:

Shooting game model

Post by nixe »

I found old Enter-magazine, where I've wroted "Näin toimii CoolBasic" (how CoolBasic works) article in 2005 (nr 6). In article I wrote about CoolBasic backgrounds and programming basics, and ofcourse made an simple shooting game model. From that article I now took original code and made it little better, ie. enemies are now in type collection. I've tried to make code as simple and clear as it could be. Code is also commented, so that even those "special" phases can be realized correctly. Hopefully someone seems this helpful.

Code is using images, sounds and tilemaps that comes with CoolBasic, so I consider that you use it from CoolBasics program folder (c:\program files\coolbasic\, or whatever you have) at least in the beginning.

Player is moved by arrows and shooting from right ctrl button.

Code: Select all

SCREEN 800,600,0,1
FrameLimit 40

playerpoints = 0
font = LoadFont("Verdana",32,1,1)
SetFont font

// Create type collection for bullets
Type BULLETS
    Field obj
End Type

// Load media
Gosub LoadMedia

// Create type collection for enemies and make couble enemies
Type ENEMIES
    Field obj
    Field energy
End Type
Gosub MakeEnemies


/////// MAIN LOOP ///////
Repeat
    Gosub CheckButtons
    Gosub EnemyAI
    Gosub CheckBullets
    Gosub DrawGUI
        
    // Move camera with player
    CloneCameraPosition player
    
    // Check if background music has stopped and start it from beginning
    If SoundPlaying(backgroundmusic) = 0 Then
        backgroundmusic = PlaySound("Media\SK_Battle2.mp3", 40)
    EndIf
    
    DrawScreen 

Until EscapeKey()


/////// SUBPROGRAMS ///////
// Subprogram that loads all media what needed in game
LoadMedia:
    // Load tilemap and animate it
    map = LoadMap("Media\cdm2.til", "Media\Tileset.bmp")
    PlayObject map,0,0,0.5
    
    // Load bullet object graphics and hide object
    bulletobject = LoadObject("Media\bullet.bmp", 72)
    ShowObject bulletobject, OFF
    
    // Load player graphics and set collision recognizing between player and map
    player = LoadObject("Media\soldier.bmp", 72)
    SetupCollision player,map,2,4,2
    
    // Load enemy graphics and hide object
    enemyobject = LoadObject("Media\guy.bmp", 72)
    ShowObject enemyobject, OFF
    
    // Load sounds
    backgroundmusic = PlaySound("Media\SK_Battle2.mp3", 40)
    shot = LoadSound("Media\gun 1.wav")
    scream = LoadSound("Media\scream1.wav")
Return

// Subprogram that checks keyboard usage
CheckButtons:
    If UpKey() Then MoveObject player,4
    If DownKey() Then MoveObject player,-4
    If LeftKey() Then TurnObject player,5
    If RightKey() Then TurnObject player,-5    
    
    // Check if shooting button is hit and if it is 
    // then create bullet and collision recognizing to it
    If KeyHit(cbkeyrcontrol) Then
        PlaySound shot, 40
        blt.BULLETS = New(BULLETS)
        blt\obj = CloneObject(bulletobject)
        CloneObjectPosition blt\obj, player
        CloneObjectOrientation blt\obj, player
        MoveObject blt\obj,15
        SetupCollision blt\obj,map,2,4,1
        ResetObjectCollision blt\obj
    EndIf
Return

// Subprogram that made simple AI for enemies
EnemyAI:
    For enem.ENEMIES = Each ENEMIES
        // If enemy object can "see" player object, then point it to player
        // and start mobing towards
        If ObjectSight(enem\obj,player) Then
            PointObject enem\obj, player
            MoveObject enem\obj, 2
        EndIf
    Next enem
Return

// Subprogram that checks bullets and collisions of bullets
CheckBullets:
    For blt.BULLETS = Each BULLETS
        MoveObject blt\obj, 10
        For enem.ENEMIES = Each ENEMIES
            // Check if bullets hit to enemies
            If ObjectsOverlap(blt\obj, enem\obj, 2) Then
                deleteobj = 1
                enem\energy = enem\energy - 20
                playerpoints = playerpoints + 5
            EndIf
        Next enem
        bulcollisions = CountCollisions(blt\obj)
        For i = 1 To bulcollisions
            // Check if bullets have hit to walls
            If GetCollision(blt\obj, i) > 1 Then deleteobj = 1
        Next i
        If deleteobj = 1 Then
            DeleteObject blt\obj
            Delete blt
            deleteobj = 0
        EndIf
    Next blt
Return

// Subprogram that draws "GUI"
DrawGUI:
    DrawGame
    DrawToWorld ON
    // Check enemies
    For enem.ENEMIES = Each ENEMIES
        PosX = ObjectX(enem\obj) - (ObjectSizeX(enem\obj)/2)
        PosY = ObjectY(enem\obj) + ObjectSizeY(enem\obj)
        EnWidth = enem\energy / 3
        Color cbWhite
        Box PosX,PosY,33,6
        Color cbRed
        Box PosX+1,PosY-1,EnWidth-2,4
        If enem\energy <= 0 Then
            // If energy is empty then play scream sound and delete enemy type
            PlaySound scream,40
            DeleteObject enem\obj
            Delete enem
        EndIf
    Next enem
    DrawToWorld OFF
    
    // Write points to screen (first is shadow)
    Color cbBlack : Text 12,12, playerpoints
    Color cbWhite : Text 10,10, playerpoints
Return

// Subprogram that creates two enemies and their energy
MakeEnemies:
    enem.ENEMIES = New(ENEMIES)
    enem\obj = CloneObject(enemyobject)
    PositionObject enem\obj,0,-500
    SetupCollision enem\obj,map,2,4,1
    SetupCollision enem\obj,player,2,2,2
    SetupCollision player,enem\obj,2,2,2
    ResetObjectCollision enem\obj
    enem\energy = 100
    
    enem.ENEMIES = New(ENEMIES)
    enem\obj = CloneObject(enemyobject)
    PositionObject enem\obj,0,500
    SetupCollision enem\obj,map,2,4,1
    SetupCollision enem\obj,player,2,2,2
    SetupCollision player,enem\obj,2,2,2
    ResetObjectCollision enem\obj
    enem\energy = 100
Return
And screenshot...
Image
p33l0mp1 kuin k0ulul41n3n
JATothrim
Tech Developer
Tech Developer
Posts: 606
Joined: Tue Aug 28, 2007 6:46 pm
Location: Kuopio

Re: Shooting game model

Post by JATothrim »

What is this? A nixe come back! Welcome! :o (or this topic is just a cheat.. :roll: )
I never thought that YOU could wrote that example. Marvelous.
-On selkeästi impulsiivinen koodaaja joka...
ohjelmoi C++:lla rekursiivisesti instantioidun templaten, jonka jokainen instantiaatio instantioi sekundäärisen singleton-template-luokan, jonka jokainen instanssi käynistää säikeen tulostakseen 'jea'.
User avatar
esa94
Guru
Posts: 1855
Joined: Tue Sep 04, 2007 5:35 pm

Re: Shooting game model

Post by esa94 »

In this thread: Wonderful English \o/

Apart from that, fabulous.
nixe
Newcomer
Posts: 21
Joined: Tue Aug 28, 2007 5:47 pm
Location: Haapavesi
Contact:

Re: Shooting game model

Post by nixe »

JATothrim wrote:What is this? A nixe come back! Welcome! :o (or this topic is just a cheat.. :roll: )
I never thought that YOU could wrote that example. Marvelous.
Hahhah, even that I wan't to come back, I can't. There's just not enough hours in a day. :D And those few "freetime" hours I usually spend with my daughter (born in august) and wife :D Even I don't have time, I am waiting for new CB :P

I just found this old tutorial and wanted to share it "free". And about cheat, if it is me, who is cheat :?
p33l0mp1 kuin k0ulul41n3n
Pettis
The Evil Admin
The Evil Admin
Posts: 1044
Joined: Sun Aug 26, 2007 5:08 pm
Location: Rovaniemi
Contact:

Re: Shooting game model

Post by Pettis »

Locked this topic because of spam. For a some reason this topic seems to attract spam bots. If you want to discuss more about this topic, please contact any of moderators.
CB Repository .com - Sinne ne kaikki koodit menneet on!
MunVerkko - Ilmaisia foorumeita ja blogeja
Locked