Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BallShoot - A bare bones shooter game easy to expand on.
#1
Here's a bare-bones shooter game example that I *think* is easy to learn from and to expand on.  It's pretty simple.  Shoot the balls, don't let any pass you or touch you.  Move the piece at the bottom LEFT/RIGHT using the arrow keys, SPACE shoots.  ESC quits.   If a ball hits you or you let one pass the game is over.

Nothing much of a game, but it was a lazy Saturday and I just wanted to code something.  Maybe some can make use of it.  It would be easy to expand this little shooter.

- Dav

Note: Get the lastest ballshoot v8 HERE

Code: (Select All)
'=============
'ballshoot.bas
'=============
'A bare-bones small shooter game that you can expand on.
'Coded by Dav, AUG/2024 for QB64PE v3.13

'Move player using arrow keys - SPACE bar shoots.
'Shoot the balls. Don't let any hit you or pass you by.

'This is a basic shooter game that is easy enough to
'learn from and expand on so you can make your own game,
'I used balls as an enemy because I had a ball SUB handy,
'and it had a colorful effect.  I used it for the enemy
'as well has for the shots.  I also used an alpha screen
'clearing method using LINE instead of CLS.  This gave
'all the moving pieces a neat trailing/fade effect.

'Game is over if a ball hits you our you let one pass.

'There are many ways to exand this into something better.
'If you do make more out of it, please share it also.


Randomize Timer

balls = 6 'number of balls at one time
shots = 6 'number of shots allowed on screen

Dim ballx(1 To balls) 'ball x pos
Dim bally(1 To balls) 'ball y pos
Dim balld(1 To balls) 'ball direction
Dim ballr(1 To balls) 'ball radius

Dim shotx(1 To shots) 'shot x pos
Dim shoty(1 To shots) 'shot y pos
Dim shota(1 To shots) 'flag if shot is active


Screen _NewImage(640, 480, 32)
_FullScreen _SquarePixels

'set game defaults
score = 0 'score is zero
shotcount = 0 'no active shots
playerx = _Width / 2 'x pos of player
playery = 475 'y pos of player (this stays same)

'generate random ball info
For i = 1 To balls
    ballx(i) = Int(Rnd * _Width) + 1 'x pos
    bally(i) = Int(Rnd * (_Height / 4)) + 1 'y pos
    balld(i) = Int(Rnd * 3) - 1 'moving direction
    ballr(i) = 10 + Int(Rnd * 20) 'radius
Next

'=== main game loop ===

Do

    'this make the trailing fade effect on screen (cooler than doing CLS)
    Line (0, 0)-(_Width, _Height), _RGBA(0, 0, 0, 75), BF

    'add some screen sparklers
    Line (Rnd * _Width, Rnd * _Height)-Step(Rnd * 3, Rnd * 3), _RGBA(255, 255, 255, Rnd * 200), BF

    'handle all balls
    For i = 1 To balls

        'randomly change ball direction by making a -1, 0 or 1 to add to x pos , sometimes.
        If Rnd < .1 Then balld(i) = Int(Rnd * 3) - 1

        'update ball x/y pos
        ballx(i) = ballx(i) + balld(i) 'new ball x pos, with randomly generated direction
        bally(i) = bally(i) + 1 'drift ball y pos down...

        'make sure ball stays in bounds
        If ballx(i) < ballr(i) Then ballx(i) = ballr(i)
        If ballx(i) > (_Width - ballr(i)) Then ballx(i) = (_Width - ballr(i))

        'if you missed shooting a ball and it goes off screen, end game
        If bally(i) > _Height Then End

        'finally draw ball....
        glowball ballx(i), bally(i), ballr(i), 255, 255, 255, 255

    Next

    'draw player along bottom of screen
    Line (playerx - 5, playery - 5)-(playerx + 5, playery + 5), _RGB(255, Rnd * 125, Rnd * 128), BF
    Line (playerx - 5, playery - 5)-(playerx + 5, playery + 5), _RGB(0, 0, Rnd * 255), B

    'handle all active shots...
    For i = 1 To shotcount
        If shota(i) Then
            'draw the shot
            glowball shotx(i), shoty(i), 3, 255, 0, 0, 255
            shoty(i) = shoty(i) - 5 'make shot go up
            'if shot goes up off screen...
            If shoty(i) < 0 Then shota(i) = 0 'set shot active flag off
        End If
    Next

    'left/right arrows move player
    If _KeyDown(19200) Then playerx = playerx - 5 'left arrow, move left
    If _KeyDown(19712) Then playerx = playerx + 5 'right arrow, move right

    'make sure player doesn't go off screen
    If playerx < 0 Then
        playerx = 0
    ElseIf playerx > (_Width - 1) Then
        playerx = (_Width - 1)
    End If

    'Spacebar fires a shot
    If _KeyDown(32) Then
        'look for first inactive shot to shoot
        For i = 1 To UBound(shota)
            'if this one is free, and lastshot time passed...
            If shota(i) = 0 And Timer - lastshot > .2 Then
                shotx(i) = playerx 'give it our x/y
                shoty(i) = playery
                shota(i) = 1 'mark it as active
                'update shot count
                If i > shotcount Then shotcount = i
                lastshot = Timer 'mark time of this shot....
                'Note: I'm using shot timer to prevent too rapid fire of shots
                Exit For
            End If
        Next
    End If

    'see if shot hits a ball
    For i = 1 To balls
        For j = 1 To shotcount
            If shota(j) Then
                If Abs(ballx(i) - shotx(j)) < ballr(i) And Abs(bally(i) - shoty(j)) < ballr(i) Then
                    'flash a circle around hit ball
                    Circle (ballx(i), bally(i)), ballr(i), _RGB(255, 255, 255)
                    'Line (0, 0)-(_Width, _Height), _RGBA(255, 255, 255, 10), BF
                    shota(j) = 0 'set shot active flag off
                    bally(i) = 1 'give ball new y pos (top)
                    ballx(i) = Int(Rnd * _Width) + 1 'give ball a new x pos
                    ballr(i) = 10 + Int(Rnd * 20) 'give ball new radius
                    score = score + 1 'update score
                End If
            End If
        Next
    Next

    'if a ball hits player, end game
    For i = 1 To balls
        If Abs(ballx(i) - playerx) < ballr(i) And Abs(bally(i) - playery) < ballr(i) Then End
    Next

    'show score
    Locate 1, 1: Print "Score: "; score;

    _Display

    _Limit 30

Loop Until _KeyDown(27) 'ESC quits

End

Sub glowball (x, y, size, r, g, b, a)
    t = Timer
    For y2 = y - size To y + size
        For x2 = x - size To x + size
            If Sqr((x2 - x) ^ 2 + (y2 - y) ^ 2) <= size Then
                clr = (size - (Sqr((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y)))) / size
                noise = Int(Rnd * 50)
                r = Sin(6.005 * t) * size - y2 + size + 255 * 2
                g = Sin(3.001 * t) * size - x2 + size + 255
                b = Sin(2.001 * x2 / size + t + y2 / size) * r + 255
                t = t + .00195
                PSet (x2, y2), _RGBA(clr * r - noise, clr * g - noise, clr * b - noise, a)
            End If
        Next
    Next
End Sub

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
BallShoot - A bare bones shooter game easy to expand on. - by Dav - 08-24-2024, 11:46 PM



Users browsing this thread: 5 Guest(s)