Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mini Space Invaders
#3
And now with TYPE variables...

Code: (Select All)
_Title "Pete's Mini Space Invaders"
Width 40, 20: _Font 16
Type invaders
    bit As Integer
    TopRow As Integer
    RowHeight As Integer
    terminate As Integer
    trail As Integer
    ms As Single
    lag As Single
    xbase As Integer
    BaseActive As Integer
    zenith As Integer
    l As Integer
    r As Integer
    z1 As Single
    z2 As Single
    yfire As Integer
    xfire As Integer
    outcome As Integer
    score As _Integer64
    array As String
End Type
Dim si As invaders
si.array = "A A A A A A A A A A A A": For i = 0 To 4: a$(i) = si.array: Next
si.bit = 1
si.TopRow = 4
si.RowHeight = 5
si.terminate = _Height
si.trail = _Width \ 2 - Len(si.array) \ 2
si.ms = .05
si.lag = .35
si.xbase = _Width \ 2
Refresh si, a$()
Do
    ScoreKeeper si
    If si.yfire <> 0 And Abs(si.z2 - Timer) > si.ms Then MissileAction si, a$()
    poll si
    If Abs(si.z1 - Timer) > si.lag Then MoveAliens si, a$()
    If si.BaseActive Then BaseAction si
    If si.outcome Then finish si: _Delay 5: Exit Do
Loop

Sub ScoreKeeper (si As invaders)
    info$ = Space$(_Width)
    ' x$ = "Altitude:" + Str$(_Height * 1000 - (si.TopRow - 1 + si.RowHeight) * 1000) + " Ft  Score:" + Str$(si.score)
    ' i = _Width \ 2 - Len(x$) \ 2
    ' Mid$(info$, i) = x$
    ' Locate 1, 1: Print info$;
    Locate 1, 2: Print "Altitude:"; _Height * 1000 - (si.TopRow - 1 + si.RowHeight) * 1000; "Ft    ";
    Locate 1, _Width - 15: Print "Score:"; si.score;
End Sub

Sub poll (si As invaders)
    Static restrict
    Select Case _KeyHit
        Case 19200: If si.xbase > 1 And restrict = 0 Then si.xbase = si.xbase - 1: restrict = 1: si.BaseActive = -1
        Case 19712: If si.xbase < _Width And restrict = 0 Then si.xbase = si.xbase + 1: restrict = 1: si.BaseActive = -1
        Case -19200, -19712: restrict = 0: _KeyClear: si.BaseActive = 0
        Case 32: If si.yfire = 0 Then si.BaseActive = 1
    End Select
End Sub

Sub MissileAction (si As invaders, a$())
    si.z2 = Timer
    If Screen(si.yfire, si.xfire) = 24 Then Locate si.yfire, si.xfire: Print " ";
    si.yfire = si.yfire - 1
    If Screen(si.yfire, si.xfire) = 65 Then
        RowCheck = 0: si.l = 0: si.r = 0: si.score = si.score + 2500
        Locate si.yfire, si.xfire: Print Chr$(15);: _Delay .1: Locate si.yfire, si.xfire: Print " ";
        Mid$(a$(si.yfire - si.TopRow), si.xfire - si.trail + 1, 1) = " ": si.yfire = 0: Sound 1000, .5
        For i = 4 To 0 Step -1
            If Len(LTrim$(a$(i))) And RowCheck = 0 Then si.RowHeight = i + 1: RowCheck = 1
            If Len(RTrim$(a$(i))) > si.r Then si.r = Len(RTrim$(a$(i)))
            If Len(LTrim$(a$(i))) > si.l Then si.l = Len(LTrim$(a$(i)))
        Next
        si.r = Len(si.array) - si.r: si.l = Len(si.array) - si.l
        Refresh si, a$()
        If LTrim$(a$(0) + a$(1) + a$(2) + a$(3) + a$(4)) = "" Then si.outcome = 1 ' You won!
    Else
        If si.yfire < si.zenith Then si.yfire = 0 Else Locate si.yfire, si.xfire: Print Chr$(24);
    End If
End Sub

Sub MoveAliens (si As invaders, a$())
    si.z1 = Timer
    If si.TopRow - 1 + si.RowHeight = si.terminate Then
        si.outcome = -1
    Else
        If si.trail + Len(si.array) - si.r > _Width And si.bit = 1 Or si.trail = 1 - si.l And si.bit = -1 Then
            si.bit = -si.bit: si.TopRow = si.TopRow + 1: si.lag = si.lag - .02
            If si.lag < .1 Then si.lag = .1: si.ms = .025
        Else
            si.trail = si.trail + si.bit
        End If
    End If
    Refresh si, a$()
    Sound 500, .1
End Sub

Sub Refresh (si As invaders, a$())
    View Print si.TopRow - 1 To si.TopRow + si.RowHeight - 1: si.zenith = 0: Cls 2
    For i = 0 To si.RowHeight - 1
        Locate si.TopRow + i, si.trail + si.l
        Print RTrim$(Mid$(a$(i), si.l + 1));
        If Len(LTrim$(a$(i))) And si.zenith = 0 Then si.zenith = si.TopRow + i
    Next
    View Print
    Locate _Height, si.xbase: Print Chr$(234);
End Sub

Sub BaseAction (si As invaders)
    Select Case si.BaseActive
        Case -1
            Locate _Height, 1: Print Space$(_Width);
            Locate _Height, si.xbase: Print Chr$(234);
        Case 1
            si.yfire = _Height: si.xfire = si.xbase
    End Select
    si.BaseActive = 0
End Sub

Sub finish (si As invaders)
    Select Case si.outcome
        Case -1
            Locate 3, 2: Print "GAME OVER - YOU LOST!"
        Case 1
            ScoreKeeper si: Locate 3, 2: Print "GAME OVER - YOU WON!"
    End Select
End Sub

This got me thinking how using TYPE variables lengthens the code of a short routine, but if you were to substantially add to a project, it would pay off in the end.

I wonder if we have coders here who are so meticulous they try or actually achieve separate type variables so only those needed in a sub get passed to the sub? The IDE will give warnings if you pass variables that are not used, but this is not the case when using TYPE variables.

Pete
Reply


Messages In This Thread
Mini Space Invaders - by Pete - 09-11-2025, 08:40 PM
RE: Mini Space Invaders - by Pete - 09-12-2025, 02:03 AM
RE: Mini Space Invaders - by Pete - 09-12-2025, 06:38 AM
RE: Mini Space Invaders - by Pete - 09-12-2025, 09:17 AM
RE: Mini Space Invaders - by Pete - 09-12-2025, 11:20 PM
RE: Mini Space Invaders - by Pete - 09-13-2025, 09:39 PM
RE: Mini Space Invaders - by Pete - 09-18-2025, 07:13 PM
RE: Mini Space Invaders - by Pete - 09-19-2025, 12:01 AM
RE: Mini Space Invaders - by bplus - 09-20-2025, 12:01 AM
RE: Mini Space Invaders - by NakedApe - 09-20-2025, 12:31 AM
RE: Mini Space Invaders - by Pete - 09-20-2025, 07:06 AM
RE: Mini Space Invaders - by bplus - 09-20-2025, 01:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Space Ship Game aadityap0901 4 824 12-26-2024, 02:13 AM
Last Post: bplus
  The Space Globe NakedApe 4 1,079 09-04-2024, 10:49 PM
Last Post: bplus
  In the spirit of Terry's Tutorials - GUARDIAN Alien Space Ship Game Pete 24 4,329 10-18-2022, 05:16 PM
Last Post: Pete
  Space Lander james2464 22 4,222 10-01-2022, 01:23 AM
Last Post: TerryRitchie
  Space(d) Invaders! Cobalt 2 1,034 07-28-2022, 02:24 AM
Last Post: madscijr

Forum Jump:


Users browsing this thread: 1 Guest(s)