Source Code: Simple Shooting Demo
rating: 0+x

Description

This is a simple shooting demo that uses an array to manage bullets.

Instructions

Use the left and right arrow keys to fire.

Source Code

SetFPS 60

Type TObj
    x#
    y#
    xSpeed#
    ySpeed#
EndType

Dim player As TObj
player.x# = GetScreenWidth() / 2
player.y# = GetScreenHeight() / 2

Dim bullets(1) As TObj

Do
    If LeftKey()
        cell = GetFreeCell( bullets() )
        bullets(cell).x# = player.x# - 15
        bullets(cell).y# = player.y#
        bullets(cell).xSpeed# = -5
    EndIf

    If RightKey()
        cell = GetFreeCell ( bullets() )
        bullets(cell).x# = player.x# + 15
        bullets(cell).y# = player.y# 
        bullets(cell).xSpeed# = 5
    EndIf

    FlushKeys

    For i = 1 to GetArrayElements( bullets().TObj, 1 )
        If bullets(i).x# < 0 Then FreeCell bullets().TObj, i
        If bullets(i).y# > GetScreenWidth() Then FreeCell bullets().TObj, i
    Next

    For i = 1 to GetArrayElements( bullets().TObj, 1 )
        bullets(i).x# = bullets(i).x# + bullets(i).xSpeed#
        bullets(i).y# = bullets(i).y# + bullets(i).ySpeed#
    Next    

    Cls 0
        For i = 1 to GetArrayElements( bullets().TObj, 1 )
            Circle bullets(i).x#, bullets(i).y#, 5, False
        Next

        Circle player.x#, player.y#, 10, False
    Sync
Loop

Related Pages

Categories: Beginner Source Code : Source Code