Microsoft Small Basic

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

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

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

While alive
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 > bggh-64 Then 'The man landed on the ground
Jump = ""
ManY = bggh-64 'Put the man at the right height
ElseIf ManY < 32 Then 'The man hit the roof
JumpCurentSpeed = .4
EndIf

If TestThis Then
'Falling off platforms
If ManY = pfy4-64 Then
If pf4[pfx] = "" Or pf4[pfx-1] = "" Or pf4[pfx-2] = "" Or pf4[pfx-3] = "" Or pf4[pfx-4] = "" Then
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
ElseIf ManY = pfy3-64 Then
If pf3[pfx] = "" Or pf3[pfx-1] = "" Or pf3[pfx-2] = "" Or pf3[pfx-3] = "" Or pf3[pfx-4] = "" Then
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
ElseIf ManY = pfy2-64 Then
If pf2[pfx] = "" Or pf2[pfx-1] = "" Or pf2[pfx-2] = "" Or pf2[pfx-3] = "" Or pf2[pfx-4] = "" Then
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
ElseIf ManY = pfy1-64 Then
If pf1[pfx] = "" Or pf1[pfx-1] = "" Or pf1[pfx-2] = "" Or pf1[pfx-3] = "" Or pf1[pfx-4] = "" Then
Jump = "True"
JumpCurentSpeed = JumpInc
EndIf
EndIf
EndIf
'Landing on platforms
If JumpCurentSpeed > 0 And ManY < pfy4-32 And ManY > pfy4-64 Then
If pf4[pfx] <> "" Or pf4[pfx-1] <> "" Or pf4[pfx-2] <> "" Or pf4[pfx-3] <> "" Or pf4[pfx-4] <> "" Then
Jump = ""
ManY = pfy4-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy3-32 And ManY > pfy3-64 Then
If pf3[pfx] <> "" Or pf3[pfx-1] <> "" Or pf3[pfx-2] <> "" Or pf3[pfx-3] <> "" Or pf3[pfx-4] <> "" Then
Jump = ""
ManY = pfy3-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy2-32 And ManY > pfy2-64 Then
If pf2[pfx] <> "" Or pf2[pfx-1] <> "" Or pf2[pfx-2] <> "" Or pf2[pfx-3] <> "" Or pf2[pfx-4] <> "" Then
Jump = ""
ManY = pfy2-64
EndIf
ElseIf JumpCurentSpeed > 0 And ManY < pfy1-32 And ManY > pfy1-64 Then
If pf1[pfx] <> "" Or pf1[pfx-1] <> "" Or pf1[pfx-2] <> "" Or pf1[pfx-3] <> "" Or pf1[pfx-4] <> "" Then
Jump = ""
ManY = pfy1-64
EndIf
EndIf

ManSlide = ManSlide + ManIncIs
bgx = bgx + bgInc
If bgx < -32 Then
bgx = bgx + 32
pfx = pfx + 1
ElseIf bgx > 0 Then
bgx = bgx - 32
pfx = pfx - 1
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
Shapes.Move(background,bgx,bgy)
Shapes.Move(roofline,bgx,bgy)
Shapes.Move(groundline,bgx,bggh)

For i = 1 To 30
pfxt = pfx-18+i
t = i * 32 - 160 + bgx
If pf1[pfxt] <> "" Then
Shapes.Move(brick[pf1[pfxt]],t,pfy1)
EndIf
If pf2[pfxt] <> "" Then
Shapes.Move(brick[pf2[pfxt]],t,pfy2)
EndIf
If pf3[pfxt] <> "" Then
Shapes.Move(brick[pf3[pfxt]],t,pfy3)
EndIf
If pf4[pfxt] <> "" Then
Shapes.Move(brick[pf4[pfxt]],t,pfy4)
EndIf
EndFor
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

Program.Delay(delay)
If debug Then
DebugData()
EndIf
EndWhile

Sub OnKeyDown
key = GraphicsWindow.LastKey
If key = "Left" Then
bgInc = 2
ManDir = 1
ManIncIs = ManInc
ElseIf key = "Right" Then
bgInc = -2
ManDir = 2
ManIncIs = ManInc
ElseIf key = "Space" And Jump <> "True" Then
Jump = "True"
JumpCurentSpeed = -JumpPower
ElseIf key = "Escape" Then
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 = 673 'Window Height
alive = "True" 'Player is alive
bgx = 0 'Backgound X position
bgInc = 0 'Background X increment
bgy = 0 'Background Y position
bggh = gh - 32 'Ground height
delay = 6
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 = bggh-64 'Man's position Y
ManInc = .1 'Walking appearance speed
JumpPower = 8 'Jump power
JumpInc = .2 'Jump Gravity
EndSub

Sub SetupPlayField
pfw = 100 'Playfield width
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
pf1 = "0=1;5=2;10=3;20=5;25=6;35=2;40=3;45=4;75=1;80=2;85=3;90=4;95=5;"
pf2 = "15=11;30=12;90=11;"
pf3 = "8=33;36=22;83=21;"
pf4 = "1=31;42=23;76=31;"
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!"
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"))
For i = 1 To 6
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)
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("pf1 = " + pf1)
TextWindow.WriteLine("pf2 = " + pf2)
TextWindow.WriteLine("pf3 = " + pf3)
TextWindow.WriteLine("pf4 = " + pf4)
TextWindow.WriteLine("")
EndSub