Microsoft Small Basic

Program Listing: BXT756
' Soldier -- By Krueg -- June 2012
' ARROWS to move
' SPACE or UP to jump
' ESCAPE to quit

' Credit goes to GoToLoop, NaochanON, and litedev for helping to make this program better.
' Thank You for your suggestions!

debug = "unTrue"
SetupVariables()
SetupPlayField()
SetupWindow()
SetupImages()
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

While lives > 0 'This is the main loop
'---------------------------------------------------------------------------------Calculate the jump direction and speed
If Jump Then
ManY = ManY + JumpCurentSpeed 'Adjust ManY for the jump
If JumpCurentSpeed < JumpPower Then 'The man will only fall this fast
JumpCurentSpeed = JumpCurentSpeed + JumpInc 'Add for gravity
EndIf
EndIf
If ManY > gh-64 Then 'The man landed on the bottom of the screen
Jump = ""
ManY = gh-64 'Put the man at the right height
ElseIf ManY < 32 Then 'The man hit the roof
JumpCurentSpeed = .4
EndIf

'---------------------------------------------------------------------------------Falling off platforms, check for stars and spikes
If ManY = pfy5-64 And Jump = "" Then 'Check conditions of the bottom row of platforms
If (pf5[pfx] > 0 And pf5[pfx] < 50) Or (pf5[pfx-1] > 0 And pf5[pfx-1] < 50) Or (pf5[pfx-2] > 0 And pf5[pfx-2] < 50) Or (pf5[pfx-3] > 0 And pf5[pfx-3] < 50) Or (pf5[pfx-4] > 0 And pf5[pfx-4] < 50) Then
Else
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
If pf5[pfx] > 50 And pf5[pfx] < 70 Then 'Did the man hit a star?
Shapes.Move(star[pf5[pfx]],-32,pfy5)
pf5[pfx] = ""
Score = Score + 10
StarCount = StarCount - 1
'Sound.PlayClick()
EndIf
If pf5[pfx] > 70 And pf5[pfx] < 100 Then 'Did the man hit a spike?
HitSpike()
EndIf
ElseIf ManY = pfy4-64 And Jump = "" Then 'Check conditions of the fourth row of platforms
If (pf4[pfx] > 0 And pf4[pfx] < 50) Or (pf4[pfx-1] > 0 And pf4[pfx-1] < 50) Or (pf4[pfx-2] > 0 And pf4[pfx-2] < 50) Or (pf4[pfx-3] > 0 And pf4[pfx-3] < 50) Or (pf4[pfx-4] > 0 And pf4[pfx-4] < 50) Then
Else
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
If pf4[pfx] > 50 And pf4[pfx] < 70 Then 'Did the man hit a star?
Shapes.Move(star[pf4[pfx]],-32,pfy4)
pf4[pfx] = ""
Score = Score + 10
StarCount = StarCount - 1
'Sound.PlayClick()
EndIf
If pf4[pfx] > 70 And pf4[pfx] < 100 Then 'Did the man hit a spike?
HitSpike()
EndIf
ElseIf ManY = pfy3-64 And Jump = "" Then 'Check conditions of the third row of platforms
If (pf3[pfx] > 0 And pf3[pfx] < 50) Or (pf3[pfx-1] > 0 And pf3[pfx-1] < 50) Or (pf3[pfx-2] > 0 And pf3[pfx-2] < 50) Or (pf3[pfx-3] > 0 And pf3[pfx-3] < 50) Or (pf3[pfx-4] > 0 And pf3[pfx-4] < 50) Then
Else
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
If pf3[pfx] > 50 And pf3[pfx] < 70 Then 'Did the man hit a star?
Shapes.Move(star[pf3[pfx]],-32,pfy3)
pf3[pfx] = ""
Score = Score + 10
StarCount = StarCount - 1
'Sound.PlayClick()
EndIf
If pf3[pfx] > 70 And pf3[pfx] < 100 Then 'Did the man hit a spike?
HitSpike()
EndIf
ElseIf ManY = pfy2-64 And Jump = "" Then 'Check conditions of the second row of platforms
If (pf2[pfx] > 0 And pf2[pfx] < 50) Or (pf2[pfx-1] > 0 And pf2[pfx-1] < 50) Or (pf2[pfx-2] > 0 And pf2[pfx-2] < 50) Or (pf2[pfx-3] > 0 And pf2[pfx-3] < 50) Or (pf2[pfx-4] > 0 And pf2[pfx-4] < 50) Then
Else
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
If pf2[pfx] > 50 And pf2[pfx] < 70 Then 'Did the man hit a star?
Shapes.Move(star[pf2[pfx]],-32,pfy2)
pf2[pfx] = ""
Score = Score + 10
StarCount = StarCount - 1
'Sound.PlayClick()
EndIf
If pf2[pfx] > 70 And pf2[pfx] < 100 Then 'Did the man hit a spike?
HitSpike()
EndIf
ElseIf ManY = pfy1-64 And Jump = "" Then 'Check conditions of the first row of platforms
If (pf1[pfx] > 0 And pf1[pfx] < 50) Or (pf1[pfx-1] > 0 And pf1[pfx-1] < 50) Or (pf1[pfx-2] > 0 And pf1[pfx-2] < 50) Or (pf1[pfx-3] > 0 And pf1[pfx-3] < 50) Or (pf1[pfx-4] > 0 And pf1[pfx-4] < 50) Then
Else
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
If pf1[pfx] > 50 And pf1[pfx] < 70 Then 'Did the man hit a star?
Shapes.Move(star[pf1[pfx]],-32,pfy1)
pf1[pfx] = ""
Score = Score + 10
StarCount = StarCount - 1
'Sound.PlayClick()
EndIf
If pf1[pfx] > 70 And pf1[pfx] < 100 Then 'Did the man hit a spike?
HitSpike()
EndIf
EndIf

'---------------------------------------------------------------------------------Landing on platforms
If JumpCurentSpeed > 0 And ManY < pfy5-32 And ManY > pfy5-64 Then
If (pf5[pfx] > 0 And pf5[pfx] < 50) Or (pf5[pfx-1] > 0 And pf5[pfx-1] < 50) Or (pf5[pfx-2] > 0 And pf5[pfx-2] < 50) Or (pf5[pfx-3] > 0 And pf5[pfx-3] < 50) Or (pf5[pfx-4] > 0 And pf5[pfx-4] < 50) Then
Jump = ""
ManY = pfy5-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy4-32 And ManY > pfy4-64 Then
If (pf4[pfx] > 0 And pf4[pfx] < 50) Or (pf4[pfx-1] > 0 And pf4[pfx-1] < 50) Or (pf4[pfx-2] > 0 And pf4[pfx-2] < 50) Or (pf4[pfx-3] > 0 And pf4[pfx-3] < 50) Or (pf4[pfx-4] > 0 And pf4[pfx-4] < 50) Then
Jump = ""
ManY = pfy4-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy3-32 And ManY > pfy3-64 Then
If (pf3[pfx] > 0 And pf3[pfx] < 50) Or (pf3[pfx-1] > 0 And pf3[pfx-1] < 50) Or (pf3[pfx-2] > 0 And pf3[pfx-2] < 50) Or (pf3[pfx-3] > 0 And pf3[pfx-3] < 50) Or (pf3[pfx-4] > 0 And pf3[pfx-4] < 50) Then
Jump = ""
ManY = pfy3-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy2-32 And ManY > pfy2-64 Then
If (pf2[pfx] > 0 And pf2[pfx] < 50) Or (pf2[pfx-1] > 0 And pf2[pfx-1] < 50) Or (pf2[pfx-2] > 0 And pf2[pfx-2] < 50) Or (pf2[pfx-3] > 0 And pf2[pfx-3] < 50) Or (pf2[pfx-4] > 0 And pf2[pfx-4] < 50) Then
Jump = ""
ManY = pfy2-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy1-32 And ManY > pfy1-64 Then
If (pf1[pfx] > 0 And pf1[pfx] < 50) Or (pf1[pfx-1] > 0 And pf1[pfx-1] < 50) Or (pf1[pfx-2] > 0 And pf1[pfx-2] < 50) Or (pf1[pfx-3] > 0 And pf1[pfx-3] < 50) Or (pf1[pfx-4] > 0 And pf1[pfx-4] < 50) Then
Jump = ""
ManY = pfy1-64
EndIf
EndIf

'If pfx < 0 Then 'Routine to wrap the playfield.
' pfx = pfx + pfw - 25 'The first 25 steps in the array
'ElseIf pfx > pfw Then 'must match the last 25 steps
' pfx = pfx - pfw + 25 'in the SetupPlayField routine.
'EndIf
'---------------------------------------------------------------------------------Show playfield from array
For i = 1 To 30
pfxt = pfx-18+i
t = i * 32 - 160 + bgx
pftest = pf1[pfxt] 'Show playfield line 1
If pftest <> "" Then
If pftest < 50 Then
Shapes.Move(brick[pftest],t,pfy1)
ElseIf pftest > 50 and pftest < 70 Then
Shapes.Move(star[pftest],t,pfy1-32)
ElseIf pftest > 80 and pftest < 100 Then
Shapes.Move(spike[pftest],t,pfy1-32)
EndIf
EndIf
pftest = pf2[pfxt] 'Show playfield line 2
If pftest <> "" Then
If pftest < 50 Then
Shapes.Move(brick[pftest],t,pfy2)
ElseIf pftest > 50 and pftest < 70 Then
Shapes.Move(star[pftest],t,pfy2-32)
ElseIf pftest > 80 and pftest < 100 Then
Shapes.Move(spike[pftest],t,pfy2-32)
EndIf
EndIf
pftest = pf3[pfxt] 'Show playfield line 3
If pftest <> "" Then
If pftest < 50 Then
Shapes.Move(brick[pftest],t,pfy3)
ElseIf pftest > 50 and pftest < 70 Then
Shapes.Move(star[pftest],t,pfy3-32)
ElseIf pftest > 80 and pftest < 100 Then
Shapes.Move(spike[pftest],t,pfy3-32)
EndIf
EndIf
pftest = pf4[pfxt] 'Show playfield line 4
If pftest <> "" Then
If pftest < 50 Then
Shapes.Move(brick[pftest],t,pfy4)
ElseIf pftest > 50 and pftest < 70 Then
Shapes.Move(star[pftest],t,pfy4-32)
ElseIf pftest > 80 and pftest < 100 Then
Shapes.Move(spike[pftest],t,pfy4-32)
EndIf
EndIf
pftest = pf5[pfxt] 'Show playfield line 5 this is the ground
If pftest <> "" Then
If pftest < 50 Then
Shapes.Move(ground[pftest],t,pfy5)
ElseIf pftest > 50 and pftest < 70 Then
Shapes.Move(star[pftest],t,pfy5-32)
ElseIf pftest > 80 and pftest < 100 Then
Shapes.Move(spike[pftest],t,pfy5-32)
ElseIf pftest > 100 and pftest < 110 Then
Shapes.Move(icon[pftest],t,pfy5-32)
EndIf
EndIf
EndFor
'----------------------------------------------------------------------------Calculate and move background and roofline
bgx = bgx + bgInc 'Add the increment to the background X position
If bgx < - 32 Then 'Background images repeat every 32 pixels
bgx = bgx + 32
pfx = pfx + 1 'Move right on the playfield array
ElseIf bgx > 0 Then
bgx = bgx - 32
pfx = pfx - 1 'Move left on the playfield array
EndIf
Shapes.Move(background,bgx,bgy)
Shapes.Move(roofline,bgx,bgy)
'----------------------------------------------------------------------------Generate animation for the man
ManSlide = ManSlide + ManIncIs
If ManSlide > 4.9 Then
ManSlide = 1
EndIf
ManSlideFloor = Math.Floor(ManSlide)
If ManDirLast <> ManDir Or ManSlideFloorLast <> ManSlideFloor Then
Shapes.Move(Man[ManDir][ManSlideFloor],ManX,ManY)
Shapes.ShowShape(man[ManDir][ManSlideFloor])
Shapes.HideShape(man[ManDirLast][ManSlideFloorLast])
EndIf
If Jump Then
Shapes.Move(man[ManDir][ManSlideFloor],ManX,ManY)
EndIf
ManDirLast = ManDir
ManSlideFloorLast = ManSlideFloor

GraphicsWindow.Title = "RUN Little Soldier Dude, RUN! ---- Score = " + Score + " ---- Stars = " + StarCount + " ---- Lives = " + lives
While Clock.ElapsedMilliseconds < fps 'Wait here to create a consistant frames per second
EndWhile
fps = Clock.ElapsedMilliseconds + delay 'Calculate the time to wait for frames per second
If debug Then
DebugData()
EndIf
EndWhile
Sound.PlayMusic("O5 C8 C8 C8 O4 G8")

Sub HitSpike 'The man hit a spike routine
Jump = "True"
JumpCurentSpeed = -JumpPower / 2 'Give the man a half jump
lives = lives - 1 'Take away one life
EndSub

Sub OnKeyDown
key = GraphicsWindow.LastKey
If key = "Left" Then 'Left arrow was pressed
bgInc = 2 'Background increment to the right
ManDir = 1 'The man array for showing his left side
ManIncIs = ManInc 'The increment for the man walking animation
ElseIf key = "Right" Then 'Right arrow was pressed
bgInc = -2 'Background increment to the left
ManDir = 2 'The man array for showing his right side
ManIncIs = ManInc 'The increment for the man walking animation
ElseIf (key = "Up" or key = "Space") And Jump <> "True" Then
Jump = "True" 'Turn on the Jump flag
JumpCurentSpeed = -JumpPower 'Get the man momentum for the jump
ElseIf key = "Escape" Then 'Escape was pressed to quit
Program.End()
EndIf
EndSub

Sub OnKeyUp
key = GraphicsWindow.LastKey
If key = "Right" Or key = "Left" Then
bgInc = 0
ManIncIs = ""
EndIf
EndSub

Sub SetupVariables
gw = 800 'Window Width
gh = 672 'Window Height
lives = 5 'Players lives
bgx = 0 'Backgound X position
bgInc = 0 'Background X increment
bgy = 0 'Background Y position
'bggh = gh - 32 'Ground height
ManDir = 1 'Man direction 1=left 2=right
ManSlide = 1 'Man's slide number for walking animation
ManX = gw/2-16 'Man's position X, center on screen
ManY = gh-96 'Man's position Y
ManInc = .1 'Walking appearance speed
JumpPower = 8 'Jump power
JumpInc = .2 'Jump Gravity
Score = 0 'It's not a game without a score
StarCount = 0 'Stars collected
delay = 9 'Change this for game speed
EndSub

Sub SetupPlayField
pfx = 14 'Playfield position
pfy1 = 128 'Playfield line 1 Y position
pfy2 = 256 'Playfield line 2 Y position
pfy3 = 384 'Playfield line 3 Y position
pfy4 = 512 'Playfield line 4 Y position
pfy5 = 640 'Playfield line 5 Y position, the ground

level = 1 'Set the level here with plans to create more later
pfw = 100 'Playfield width -- not used yet
StarCount = 15
'-----------------------------------------------------------------------------------------------Platforms Array
'--------------------------These arrays correspond with 5 different levels of platforms and objects on the play field -- pf1 = "18=1", pf1 is the top row of platforms
'--------------------------The first number represents the position from the left on the playfield -- pf1 = "18=1", 18 is the position
'--------------------------The second number represents the object on that position -- pf1 = "18=1", 1 is the brown bricks
'--------------------------These are all 5 blocks wide These are 1 block wide These are 2 blocks wide
'--------------------------bricks[1-9] are brown bricks star[51-59] are blue stars icon[101] is the sign pointing right
'--------------------------bricks[11-19] are blue bricks star[61-69] are red stars icon[102] is the sign pointing left
'--------------------------bricks[21-29] are green bricks spike[81-89] are green spikes
'--------------------------bricks[31-39] are red bricks spike[91-99] are red spikes
'--------------------------ground[41-49] are ground images
'--------------------------The screen will show 25 blocks of the playfield and calculate 30 to push objects off the screen
'--------------------------Issues: do not let the same object show up twice on the screen it will blink, that's why there are 9 images, to avoid repeats
'--------------------------I chose this method of defining the whole array in one statement versus defining a whole lot of individual variables of the same array
pf1 = "18=1;20=65;23=2;25=66;28=3;30=67;33=4;35=68;48=11;53=12"
pf2 = "38=31;39=61;40=62;41=63;42=64;43=32"
pf3 = "28=21;30=82;31=83;32=84;33=22"
pf4 = "18=18;19=55;20=56;23=19;25=57;26=58;27=59"
pf5 = "0=41;5=42;10=43;9=101;15=44;20=45;21=91;22=92;23=93;24=94;25=46;30=47;35=48;40=41;45=42;50=43;55=44;56=51;57=52;58=53;59=54;60=45;65=46;70=47;75=48;80=42;85=43;90=44;91=102;95=45;100=46;"
EndSub

Sub SetupWindow
If debug Then
TextWindow.Left = gw + 20
TextWindow.Top = 10
TextWindow.Title = "Soldier Dude Debug."
EndIf
GraphicsWindow.Hide()
GraphicsWindow.Height = gh
GraphicsWindow.Width = gw
GraphicsWindow.Top = 5
GraphicsWindow.Left = 5
GraphicsWindow.Title = "RUN Little Soldier Dude, RUN! ---- Score = " + Score + " ---- Stars = " + StarCount + " ---- Lives = " + lives
GraphicsWindow.BackgroundColor = "Black"
EndSub

Sub SetupImages
'path = Program.Directory + "\"
path = "http://krueg.com/sb/soldier/"
background = Shapes.AddImage(ImageList.LoadImage(path + "background.png"))
Shapes.Move(background,0,32)
'groundline = Shapes.AddImage(ImageList.LoadImage(path + "groundline.png"))
'Shapes.Move(groundline,0,gh-32)
roofline = Shapes.AddImage(ImageList.LoadImage(path + "roofline.png"))
icon[101] = Shapes.AddImage(ImageList.LoadImage(path + "signright.png"))
icon[102] = Shapes.AddImage(ImageList.LoadImage(path + "signleft.png"))
Shapes.Move(icon[102],-100,pfy5-32)
For i = 1 To 9
brick[i] = Shapes.AddImage(ImageList.LoadImage(path + "5brickbrown.png"))
Shapes.Move(brick[i],-160,pfy1)
brick[i+10] = Shapes.AddImage(ImageList.LoadImage(path + "5brickblue.png"))
Shapes.Move(brick[i+10],-160,pfy2)
brick[i+20] = Shapes.AddImage(ImageList.LoadImage(path + "5brickgreen.png"))
Shapes.Move(brick[i+20],-160,pfy3)
brick[i+30] = Shapes.AddImage(ImageList.LoadImage(path + "5brickred.png"))
Shapes.Move(brick[i+30],-160,pfy4)
ground[i+40] = Shapes.AddImage(ImageList.LoadImage(path + "5ground.png"))
Shapes.Move(ground[i+40],-165,pfy5)
star[i+50] = Shapes.AddImage(ImageList.LoadImage(path + "starblue.png"))
Shapes.Move(star[i+50],-160,pfy5)
star[i+60] = Shapes.AddImage(ImageList.LoadImage(path + "starred.png"))
Shapes.Move(star[i+60],-160,pfy5)
spike[i+80] = Shapes.AddImage(ImageList.LoadImage(path + "spikegreen.png"))
Shapes.Move(spike[i+80],-160,pfy5)
spike[i+90] = Shapes.AddImage(ImageList.LoadImage(path + "spikered.png"))
Shapes.Move(spike[i+90],-160,pfy5)
EndFor
manl2 = ImageList.LoadImage(path + "man_l2.png")
manr2 = ImageList.LoadImage(path + "man_r2.png")
man[1][1] = Shapes.AddImage(ImageList.LoadImage(path + "man_l1.png"))
man[1][2] = Shapes.AddImage(manl2)
man[1][3] = Shapes.AddImage(ImageList.LoadImage(path + "man_l3.png"))
man[1][4] = Shapes.AddImage(manl2)
man[2][1] = Shapes.AddImage(ImageList.LoadImage(path + "man_r1.png"))
man[2][2] = Shapes.AddImage(manr2)
man[2][3] = Shapes.AddImage(ImageList.LoadImage(path + "man_r3.png"))
man[2][4] = Shapes.AddImage(manr2)
For i = 1 To 4
Shapes.Move(man[1][i],ManX,ManY)
Shapes.Zoom(man[1][i],2,2)
Shapes.HideShape(man[1][i])
Shapes.Move(man[2][i],ManX,ManY)
Shapes.Zoom(man[2][i],2,2)
Shapes.HideShape(man[2][i])
EndFor
GraphicsWindow.Show()
EndSub

Sub DebugData
TextWindow.Clear()
TextWindow.WriteLine("key = " + key)
TextWindow.WriteLine("bgInc = " + bgInc)
TextWindow.WriteLine("bgx = " + bgx)
TextWindow.WriteLine("pfx = " + pfx)
TextWindow.WriteLine("Jump = " + Jump)
TextWindow.WriteLine("ManX = " + ManX)
TextWindow.WriteLine("ManY = " + ManY)
TextWindow.WriteLine("JumpCurentSpeed = " + JumpCurentSpeed)
TextWindow.WriteLine("")
TextWindow.WriteLine("pf1 = " + pf1)
TextWindow.WriteLine("pf2 = " + pf2)
TextWindow.WriteLine("pf3 = " + pf3)
TextWindow.WriteLine("pf4 = " + pf4)
TextWindow.WriteLine("pf5 = " + pf5)
EndSub