Microsoft Small Basic

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

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

gw = 640 'Window width
gh = 480 'Window Height
gh32 = gh - 32 'Top of the brick line on the bottom

md = 1 'Man direction 1=left 2=right
z = 1 'Man's slide number for walking animation

IncX = 2 'Background speed X
IncY = 1 'Background speed Y, no real use yet
ManX = gw/2-16 'Man's position X, center on screen
ManY = gh32-64 'Man's position Y
IncZ = .1 'Walking appearance speed
Jp = 8 'Jump power
IncJy = .2 'Jump Gravity

Delay = 10
play = "True"
debug = "True"

SetupWindow()
SetupImages()

Main:
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

While play

If jump Then
ManY = ManY + Jy 'Adjust ManY for the jump
If Jy < Jp Then 'The man will only fall this fast
Jy = Jy + IncJy 'Add for gravity
EndIf
EndIf
If ManY > gh32-64 Then 'The man landed the brick line
jump = ""
ManY = gh32-64 'Put the man at the right height
EndIf

x = x + dx
y = y + dy
z = z + dz

If x < -32 Then
x = x + 32
ElseIf x > 0 Then
x = x -32
EndIf

If y < -32 Then
y = y + 32
ElseIf y > 0 Then
y = y -32
EndIf

Shapes.Move(background x,y)
Shapes.Move(brickline x,gh32)

If z > 4.9 Then
z = 1
EndIf
zInt = Math.Floor(z)

If mdlast <> md Or mslast <> zInt Or jump Then
Shapes.Move(man[md][zInt],ManX,ManY)
Shapes.ShowShape(man[md][zInt])
Shapes.HideShape(man[mdlast][mslast])
EndIf

mdlast = md
mslast = zInt

If debug Then
DebugData()
EndIf

Program.Delay(Delay)

EndWhile

Sub OnKeyDown

key = GraphicsWindow.LastKey

If key = "Left" Then
dx = IncX
dz = IncZ
md = 1
ElseIf key = "Right" Then
dx = -IncX
dz = IncZ
md = 2
ElseIf key = "Space" and jump <> "True" Then
jump = "True"
Jy = -Jp
ElseIf key = "Up" Then
dy = IncY
ElseIf key = "Down" Then
dy = -IncY
ElseIf key = "Escape" Then
Program.End()
EndIf

EndSub

Sub OnKeyUp

key = GraphicsWindow.LastKey

If key = "Right" Or key = "Left" Then
dx = ""
dz = ""
ElseIf key = "Up" Or key = "Down" Then
dy = ""
EndIf

EndSub

Sub SetupWindow
If debug Then
TextWindow.Left = gw + 20
TextWindow.Top = 10
EndIf
GraphicsWindow.Hide()
GraphicsWindow.Height = gh
GraphicsWindow.Width = gw
GraphicsWindow.Top = 5
GraphicsWindow.Left = 5
GraphicsWindow.BackgroundColor = "Black"
EndSub

Sub SetupImages
'path = Program.Directory + "\"
path = "http://krueg.com/sb/samples/"
manl2 = ImageList.LoadImage(path + "man_l2.png")
manr2 = ImageList.LoadImage(path + "man_r2.png")
background = Shapes.AddImage(ImageList.LoadImage(path + "background.png"))
brickline = Shapes.AddImage(ImageList.LoadImage(path + "brickline.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)
Shapes.Move(brickline, -32,gh-32)
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("X = " + x + " Y = " + y)
TextWindow.WriteLine("DirectionX = " + dx)
TextWindow.WriteLine("DirectionY = " + dy)
TextWindow.WriteLine("Jump = " + jump)
TextWindow.WriteLine("ManX = " + ManX + " ManY = " + ManY)
TextWindow.WriteLine("MD = " + md + " Z = " + z)
EndSub