Microsoft Small Basic
Program Listing:
Embed this in your website
<object id='sbapp' data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='640' height='480'> <param name='source' value='http://smallbasic.com/program/ClientBin/SBWeb.xap'/> <param name='onError' value='onSilverlightError' /> <param name='background' value='white' /> <param name='minRuntimeVersion' value='3.0.40624.0' /> <param name='autoUpgrade' value='true' /> <param name='initParams' value='programId=CWC005' /> </object>
playerX
=
0
'initial player location
playerY
=
0
maze
[
playerX
+
":"
+
playerY
]
=
"·"
'set the square of the maze at the player location to be empty so that it will always draw the first time through
GraphicsWindow
.
FontSize
=
24
'increase/decrease this to make the squares bigger/smaller
GraphicsWindow
.
KeyUp
=
Move
'move when the player releases the key so that they have to press it once for each movement
Move
(
)
'call move to draw the game for the first time otherwise screen will be blank until they press a key
Sub
Move
newX
=
playerX
'make a copy of the player's current location
newY
=
playerY
If
(
GraphicsWindow
.
LastKey
=
"Up"
)
Then
'check if the player is moving and if so change the copy of the location accordingly
newY
=
newY
-
1
ElseIf
(
GraphicsWindow
.
LastKey
=
"Down"
)
Then
newY
=
newY
+
1
ElseIf
(
GraphicsWindow
.
LastKey
=
"Left"
)
Then
newX
=
newX
-
1
ElseIf
(
GraphicsWindow
.
LastKey
=
"Right"
)
Then
newX
=
newX
+
1
EndIf
location
=
newX
+
":"
+
newY
'set location to the new position
CheckMazeAtLocation
(
)
'the square of maze at this location might not have been created yet
If
(
maze
[
location
]
<>
"■"
)
Then
'only update position and draw if they're not trying to move into a wall
If
(
maze
[
location
]
=
"☆"
)
Then
'if the player walked onto a star...
score
=
score
+
1
'give them a point
maze
[
location
]
=
"·"
'remove the star and place an empty floor here instead
EndIf
playerX
=
newX
'update the player location
playerY
=
newY
For
y
=
0
To
17
'draw a section of the maze that is 18 x 18 tiles in size
For
x
=
0
to
17
location
=
(
x
-
9
+
playerX
)
+
":"
+
(
y
-
9
+
playerY
)
'turn x and y into a location relative to the player
CheckMazeAtLocation
(
)
'the part of the maze at this location might not have been created yet
GraphicsWindow
.
BrushColor
=
"MidnightBlue"
'clearing each square individually flickers less than a GraphicsWindow.Clear()
GraphicsWindow
.
FillRectangle
(
x
*
GraphicsWindow
.
FontSize
,
y
*
GraphicsWindow
.
FontSize
,
GraphicsWindow
.
FontSize
,
GraphicsWindow
.
FontSize
)
GraphicsWindow
.
BrushColor
=
"PaleGreen"
'draw the maze square at this location
GraphicsWindow
.
DrawText
(
x
*
GraphicsWindow
.
FontSize
,
y
*
GraphicsWindow
.
FontSize
,
maze
[
location
]
)
EndFor
EndFor
GraphicsWindow
.
DrawText
(
9
*
GraphicsWindow
.
FontSize
,
9
*
GraphicsWindow
.
FontSize
,
"☻"
)
'draw the player
GraphicsWindow
.
Title
=
"Score: "
+
score
'draw the score in the title bar
EndIf
EndSub
Sub
CheckMazeAtLocation
'check if the maze is empty at this location and if it is then randomly pick a tile to place here
If
(
Array
.
ContainsIndex
(
maze
,
location
)
=
"False"
and
Math
.
GetRandomNumber
(
100
)
=
1
)
Then
maze
[
location
]
=
"☆"
'star - increases your score
ElseIf
(
Array
.
ContainsIndex
(
maze
,
location
)
=
"False"
and
Math
.
GetRandomNumber
(
4
)
=
1
)
Then
maze
[
location
]
=
"■"
'wall - a square you can't move through
ElseIf
(
Array
.
ContainsIndex
(
maze
,
location
)
=
"False"
)
Then
maze
[
location
]
=
"·"
'floor - a square you can move over
EndIf
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.