Text Map Generation And Control

Lets make a simple game. There is a "box" in top right corner.
Idea is that world moves instead of player. Also few tips, how to create a random world.

' All coding by E.K.Virtanen. 
' Public Domain
'
' Here we do some basic stuff that is needed
DECLARE SUB MakeMap () ' Sub that creates a map
DECLARE SUB ScreenPrint () ' Sub that prints stuff needed on screen
DECLARE SUB KeyPress () ' checks where you want to go
DECLARE SUB Box () ' makes a "nice" box where you travell ;D
DECLARE SUB Winner () ' And if(when) you find that money, you win
RANDOMIZE TIMER ' you know this one
DIM SHARED Map(120, 120) AS INTEGER ' Main map of game
DIM SHARED x AS INTEGER, y AS INTEGER ' and some more...

x = 30 : y = 30 ' Starting location of player
SCREEN 17: WIDTH 80, 50 ' Screen resolution.

CALL MakeMap
CALL Box
DO ' This is the main loop of "game"
CALL ScreenPrint
CALL KeyPress
LOOP

' ****** When money is finded, you come here ********
SUB Winner

CLS:Color INT(RND * 15 + 1)
PRINT "Yippee, you did it"
SLEEP
END

END SUB

' ******* from here starts SUB that creates a map **********
SUB MakeMap
' It's just a one way to do it ;)

' first we fill whole map with land
FOR i = 1 TO 120
FOR i2 = 1 TO 120 : Map(i, i2) = 46 : NEXT i2
NEXT i

' And now we create sea around of it
FOR i = 1 TO 120
w = INT(RND * 4 + 1)
FOR ii = 1 TO 14 + w ' this way we got some shapes for shores
Map(i, ii) = 219 : Map(ii, i) = 219 : Map(i, 120 - ii) = 219 : Map(120 - ii, i) = 219
NEXT ii
NEXT i

' that money to search
DO
d1 = INT(RND * 75) + 1 : d2 = INT(RND * 75) + 1 : d1 = d1 + 15: d2 = d2 + 15
Loop Until Map(d1, d2) = 46 : Map(d1, d2) = 36

' Loop to create 1100 threes on map
FOR i = 1 TO 1100
DO
d1 = INT(RND * 120) + 1 ' x location of tree
d2 = INT(RND * 120) + 1 ' y location of tree
Loop Until Map(d1, d2) = 46 ' so we get those 1100 trees there ;D
Map(d1, d2) = 5 ' and this is our tree placed somewhere in map
NEXT i
END SUB

SUB Box
' this creates a "box" where we move
CLS:COLOR 1
FOR i = 1 TO 25 ' so box size is 25x25
LOCATE i, 1: PRINT CHR$(177):LOCATE i, 25: PRINT CHR$(177):LOCATE 1, i: PRINT CHR$(177):LOCATE 25, i: PRINT CHR$(177)
NEXT i
LOCATE 25, 9: PRINT "Game Map." ' some useless text bottom of box
END SUB

SUB ScreenPrint
' Ok, this one prints map on screen (in box area)
' I use IF's here because it's good way in this kind of short script

FOR i = 1 TO 23
FOR ii = 1 TO 23
g$ = CHR$(46) : c = 8 ' At first everything is ground and grey
IF Map(x - 12 + i, y - 12 + ii) = 5 THEN g$ = CHR$(5) : c = 2 ' If tree, then g$ = 5 and color is green
IF Map(x - 12 + i, y - 12 + ii) = 219 THEN g$ = CHR$(219) : c = 1 ' If water then...
IF Map(x - 12 + i, y - 12 + ii) = 36 THEN g$ = CHR$(36) : c = 14 ' ...and if it's money
LOCATE i + 1, ii + 1: COLOR c: PRINT g$ ' Here we print area (on box) what you see
NEXT ii
NEXT i

COLOR 11:LOCATE 13, 13: PRINT CHR$(2) ' Here we print you in middle of map

END SUB

SUB KeyPress ' This checks what you press

DO
i$ = INKEY$
LOOP UNTIL i$ <> ""
IF i$ = CHR$(55) OR i$ = CHR$(0) + CHR$(71) THEN x = x - 1: y = y - 1
IF i$ = CHR$(51) OR i$ = CHR$(0) + CHR$(81) THEN x = x + 1: y = y + 1
IF i$ = CHR$(49) OR i$ = CHR$(0) + CHR$(79) THEN x = x + 1: y = y - 1
IF i$ = CHR$(57) OR i$ = CHR$(0) + CHR$(73) THEN x = x - 1: y = y + 1
IF i$ = CHR$(56) OR i$ = CHR$(0) + CHR$(72) THEN x = x - 1
IF i$ = CHR$(52) OR i$ = CHR$(0) + CHR$(75) THEN y = y - 1
IF i$ = CHR$(50) OR i$ = CHR$(0) + CHR$(80) THEN x = x + 1
IF i$ = CHR$(54) OR i$ = CHR$(0) + CHR$(77) THEN y = y + 1
IF i$ = CHR$(27) OR Map(x,y) = 219 THEN END ' if ESC pressed then we end
IF Map(x,y) = 36 THEN CALL Winner ' if Money finded, we move on at SUB Winner
END SUB
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.