Source Code: Gunna Die
rating: 0+x

Description

Gunna Die is a simple arcade game.

Instructions

Use the arrow keys to defend your ship (circle) in the center from the incoming asteroids.

Source Code

SetFPS 60

LoadFont "Verdana", 1, 20, 0

Global sw = GetScreenWidth()
Global sh = GetScreenHeight()

Constant UP     = 1
Constant DOWN     = 2
Constant    LEFT    = 3
Constant RIGHT = 4

Constant BULLET_RADIUS = 5
Constant BULLET_SPEED = 5
Constant SHIP_RADIUS = 20

Constant MAIN_GAME = 1
Constant GAME_OVER = 2
Constant TITLE = 3

Type TObj
    x#
    y#
    xSpeed#
    ySpeed#
    alv
EndType

Dim bullets(4) As TObj
Dim asteroids(10) As TObj

Global interval = 250
Global score = 0
Global gameState = TITLE_SCREEN

Do

    Select gameState
        Case MAIN_GAME

            UpdateBullets()
            UpdateAsteroids()
            BulletAstCollision()
            ShipAstCollision()
            CreateAsteroid()    

            If Upkey() Then FireBullet( UP )
            If DownKey() Then FireBullet( DOWN )
            If LeftKey() Then FireBullet( LEFT )
            If RightKey() Then FireBullet( RIGHT )
            FlushKeys
    EndSelect

    Cls 0
        Select gameState
            Case TITLE_SCREEN
                CenterText sw/2, sh/2 -10, "Gunna Die"
                CenterText sw/2, sh/2 + 10, "(Press Space)"
                If SpaceKey() Then gameState = MAIN_GAME

            Case MAIN_GAME
                Print "Score: " + Str$( score )
                Circle sw/2, sh/2, SHIP_RADIUS, False
                DrawBullets()
                DrawAsteroids()
            Case GAME_OVER

                CenterText sw/2, sh/2, "Game Over"
        EndSelect

    Sync
Loop

PSub BulletAstCollision()
    For x = 1 to GetArrayElements( bullets(), 1 )
        For y = 1 to GetArrayElements( asteroids(), 1 )
            If bullets(x).alv And asteroids(y).alv
                If CirclesIntersect(bullets(x).x#, bullets(x).y#, BULLET_RADIUS, asteroids(y).x#, asteroids(y).y#, BULLET_RADIUS * 2)
                    asteroids(y).alv = False
                    bullets(x).alv = False
                    score = score + 50
                EndIf
            EndIf    
        Next
    Next
EndPSub        

PSub CreateAsteroid()
    If RndRange(1,interval) < 5
        Dec(interval)
        If interval < 1 Then interval = 1

        cell = 0
        For i = 1 to GetArrayElements( asteroids(), 1 )
            If asteroids(i).alv = False Then cell = i
        Next

        If cell
            asteroids(cell).alv = True
            asteroids(cell).x# = sw / 2
            asteroids(cell).y# = sh / 2
            asteroids(cell).xSpeed# = 0
            asteroids(cell).ySpeed# = 0

            dir = RndRange(1,4)

            Select dir
                Case UP
                    asteroids(cell).y# = sh
                    asteroids(cell).ySpeed# = -BULLET_SPEED
                Case DOWN
                    asteroids(cell).y# = 0
                    asteroids(cell).ySpeed# = BULLET_SPEED
                Case LEFT
                    asteroids(cell).x# = sw
                    asteroids(cell).xSpeed# = -BULLET_SPEED
                Case RIGHT
                    asteroids(cell).x# = 0
                    asteroids(cell).xSpeed# = BULLET_SPEED
            EndSelect
        EndIf
    EndIf
EndPSub

PSub DrawBullets()
    For i = 1 to GetArrayElements( bullets(), 1 )
        If bullets(i).alv Then Circle bullets(i).x#, bullets(i).y#, BULLET_RADIUS, False
    Next
EndPSub

PSub DrawAsteroids()
    For i = 1 to GetArrayElements( asteroids(), 1 )
        If asteroids(i).alv Then Circle asteroids(i).x#, asteroids(i).y#, BULLET_RADIUS * 2, False
    Next
EndPSub

PSub FireBullet( dir )
    cell = 0
    For i = 1 to GetArrayElements( bullets(), 1 )
        If bullets(i).alv = False Then cell = i
    Next

    If cell
        bullets(cell).alv = True
        bullets(cell).x# = sw / 2
        bullets(cell).y# = sh / 2
        bullets(cell).xSpeed# = 0
        bullets(cell).ySpeed# = 0

        Select dir
            Case UP
                bullets(cell).y# = sh/2 - (BULLET_RADIUS + SHIP_RADIUS)
                bullets(cell).ySpeed# = -BULLET_SPEED
            Case DOWN
                bullets(cell).y# = sh/2 + (BULLET_RADIUS + SHIP_RADIUS)
                bullets(cell).ySpeed# = BULLET_SPEED
            Case LEFT
                bullets(cell).x# = sw/2 - (BULLET_RADIUS + SHIP_RADIUS)
                bullets(cell).xSpeed# = -BULLET_SPEED
            Case RIGHT
                bullets(cell).x# = sw/2 + (BULLET_RADIUS + SHIP_RADIUS)
                bullets(cell).xSpeed# = BULLET_SPEED
        EndSelect
    EndIf

EndPSub

PSub ShipAstCollision()
    For i = 1 to GetArrayElements( asteroids(), 1)
        If CirclesIntersect( sw/2, sh/2, SHIP_RADIUS, asteroids(i).x#, asteroids(i).y#, BULLET_RADIUS * 2)
            gameState = GAME_OVER
        EndIf
    Next
EndPSub    

PSub UpdateAsteroids()
    For i = 1 to GetArrayElements( asteroids(), 1 ) 
        If asteroids(i).alv
            asteroids(i).x# = asteroids(i).x# + asteroids(i).xSpeed#
            asteroids(i).y# = asteroids(i).y# + asteroids(i).ySpeed#
        EndIf
    Next
EndPSub

PSub UpdateBullets()
    For i = 1 to GetArrayElements( bullets(), 1 ) 
        If bullets(i).alv
            If bullets(i).x# < 0 Then bullets(i).alv = False
            If bullets(i).x# > sw Then bullets(i).alv = False
            If bullets(i).y# < 0 Then bullets(i).alv = False
            If bullets(i).y# > sh Then bullets(i).alv = False
        EndIf

        If bullets(i).alv
            bullets(i).x# = bullets(i).x# + bullets(i).xSpeed#
            bullets(i).y# = bullets(i).y# + bullets(i).ySpeed#
        EndIf
    Next
EndPSub

Related Pages

Categories: Game Source Code : Source Code