Post by Trooper on Oct 2, 2004 23:02:15 GMT -5
Okay, I'm posting a text only version of my Toadder
program for three reasons:
1) So people can see that even simple text games can
be made to look great.
2) Someone mentioned the sharing of code, so I am
contributing my work.
3) And in contributing my work, I hope others will adopt
my coding style. Please note the non-spaghetti code,
specifically, no GOTO's and no END's (except one to
finish the program before the subroutines.
The code is below. If you want a copy with proper
indenting (much easier to read and work with), you
can get it here:
games.andrewhoyer.com/toadder/toadder10a.gb
The lengthy code follows. Due to 10k limit, comments
have been removed so use above link for more info.
Enjoy!
Trooper
program for three reasons:
1) So people can see that even simple text games can
be made to look great.
2) Someone mentioned the sharing of code, so I am
contributing my work.
3) And in contributing my work, I hope others will adopt
my coding style. Please note the non-spaghetti code,
specifically, no GOTO's and no END's (except one to
finish the program before the subroutines.
The code is below. If you want a copy with proper
indenting (much easier to read and work with), you
can get it here:
games.andrewhoyer.com/toadder/toadder10a.gb
The lengthy code follows. Due to 10k limit, comments
have been removed so use above link for more info.
' ******************************
' * TOADDER v1.0a *
' * Written by: Trooper *
' * trooper23@shaw.ca *
' * October 2004 *
' ******************************
' Set screen mode.
textmode (TEXT_BUFFERED)
SetTextScroll (false) ' Do not scroll text
' Maximum and minimum across for cars
const X_MAX = 15
const X_MIN = 1
dim i,j
dim flag
dim x,y
dim mx,my
dim ox,oy
dim lives
lives = 3
dim levelend
dim crashed
dim makemove
dim numberofcars ' Will be level x 3
' Arrays to hold data on cars
dim carx(100)
dim cary(100)
dim cardirection(100)
dim caractive(100)
' Timing variables
dim OldTick, TextSpeed, CurrentTick, TimeTick, MoveText
TextSpeed = 150
dim continuegame
continuegame = true
dim level
dim score
dim key$ ' Stores current move
dim toad$
toad$= "*"
' Set text size to game setting
resizetext (17, 12)
' Each new game starts here
while continuegame
score = 0
lives = 3
level = 1
while lives > 0
' Set starting Toad position
x=8 : y=10
levelend = false
makemove = false
crashed = false
numberofcars = (level * 3) - 1
if numberofcars >= 99 then
numberofcars = 99
endif
GoSub DrawBorder
OldTick = TickCount () 'find out what the time is before we start
while not levelend
key$ = InScankey ()
Gosub GetTimeElapsed
if key$ = VK_UP then
mx=0:my=-1
makemove = true
endif
if key$ = VK_DOWN then
mx=0:my=1
makemove = true
endif
if key$ = VK_LEFT then
mx=-1:my=0
makemove = true
endif
if key$ = VK_RIGHT then
mx=1:my=0
makemove = true
endif
if CharAt$(x+mx,y+my) <> "#" and makemove then
' Record position of Toad before moving for cleanup purposes
ox=x:oy=y
if CharAt$(x+mx,y+my) = "<" or CharAt$(x+mx,y+my) = ">" then
x=x+mx
y=y+my
locate x,y:print "X"
levelend = true
crashed = true
else
x=x+mx
y=y+my
mx=0:my=0
' Display toad with correct direction
locate x,y
print "*"
' Did the toad reach safety??
if y = 1 then
levelend = true
endif
endif
color (0,0,0):locate ox,oy:print " "
color (255,255,255)
makemove = false
endif
if not levelend and MoveText = 1 then
' Next, each active car moves
i=0
flag = true
while flag and i <= numberofcars
' Only deal with active cars
if caractive(i) = 1 then
if cardirection(i) = 1 then ' Right
if carx(i) = X_MAX then
carx(i) = X_MAX
caractive(i) = 0
GoSub EraseCar
else
carx(i) = carx(i) + cardirection(i)
GoSub DisplayCar
GoSub DidCarHitToad
endif
else ' Left
if carx(i) = X_MIN then
carx(i) = X_MIN
caractive(i) = 0
GoSub EraseCar
else
carx(i) = carx(i) + cardirection(i)
GoSub DisplayCar
' Check if car just hit the toad
GoSub DidCarHitToad
endif
endif
endif
i=i+1
wend
i=0
flag = true
while flag and i <= numberofcars
if caractive(i) = 0 then
caractive(i) = 1
flag = false
GoSub InitializeOneCar
endif
i=i+1
wend
endif
DrawText()
wend
' Level Over
Sleep(1100)
' Determine why level ended
if crashed then
lives = lives - 1
else
' We won!
score = score + (level * 10)
level = level + 1
endif
GoSub ResetAllCars
ClearKeys()
wend
cls
locate 3,2:print "*" + " TOADDER " + "*"
locate 4,4:print "SCORE:" + score
locate 4,6:print "GAME OVER"
locate 3,8:print "PLAY AGAIN?"
locate 6,9:print "(Y/N)"
DrawText()
flag = true
while flag
key$ = Inkey$ ()
if key$ = "N" or key$ = "n" then
continuegame = false
flag = false
endif
if key$ = "Y" or key$ = "y" then
flag = false
endif
wend
wend
end
' *****************************
' SUBROUTINES
' *****************************
SetRandomCarY:
j=0
while j = 0 or j = 4 or j = 7 ' Rows a car cannot drive on
j = (rnd() % 8) + 2
wend
cary(i) = j
Return
InitializeOneCar:
GoSub SetRandomCarY
if cary(i) = 2 or cary(i) = 5 or cary(i) = 8 then
cardirection(i) = -1
carx(i) = 14
else ' Rows 3 or 7
cardirection(i) = 1
carx(i) = 2
endif
if CharAt$(carx(i),cary(i)) = "<" or CharAt$(carx(i),cary(i)) = ">" or CharAt$(carx(i)+1,cary(i)) = "<" or CharAt$(carx(i)+1,cary(i)) = ">" then
carx(i)=0
cary(i)=0
cardirection(i)=0
caractive(i)=0
else
if cardirection(i) = 1 then ' Right
if CharAt$(carx(i),cary(i)) <> " " or CharAt$(carx(i)-1,cary(i)) <> " " then
GoSub DisplayCar
levelend = true
crashed = true
else
GoSub DisplayCar
endif
else 'Left
if CharAt$(carx(i),cary(i)) <> " " or CharAt$(carx(i)+1,cary(i)) <> " " then
GoSub DisplayCar
levelend = true
crashed = true
else
GoSub DisplayCar
endif
endif
endif
Return
DidCarHitToad:
if carx(i) = x and cary(i) = y then
flag = false
locate carx(i),cary(i):print "X"
levelend = true
crashed = true
endif
Return
ResetAllCars:
for i=0 to numberofcars
carx(i)=0
cary(i)=0
cardirection(i)=0
caractive(i)=0
next
Return
DisplayCar:
if cardirection(i) = 1 then
locate carx(i)-1,cary(i):print "<"
locate carx(i),cary(i):print ">"
if carx(i) > 2 then
locate carx(i)-2,cary(i):print " "
endif
else
locate carx(i),cary(i):print "<"
locate carx(i)+1,cary(i):print ">"
if carx(i) < 14 then
locate carx(i)+2,cary(i):print " "
endif
endif
Return
EraseCar:
if cardirection(i) = 1 then
locate carx(i)-1,cary(i):print " "
locate carx(i),cary(i):print " "
else
locate carx(i),cary(i):print " "
locate carx(i)+1,cary(i):print " "
endif
carx(i)=0
cary(i)=0
cardirection(i)=0
caractive(i)=0
Return
GetTimeElapsed:
CurrentTick = TickCount()
TimeTick = CurrentTick - OldTick
if TimeTick >= TextSpeed then
MoveText = 1
OldTick = CurrentTick
else
MoveText = 0
endif
Return
DrawBorder:
cls
locate x,y:print "*"
for i = 0 to 16
color(255,255,255)
locate i,0 : print "#"
locate i,11 : print "#"
next
for i = 0 to 11
color(255,255,255)
locate 0,i : print "#"
locate 16,i : print "#"
next
locate 1,1 :print "###"
locate 7,1 :print "###"
locate 13,1:print "###"
' Little Blocker Walls
locate 4,4 :print "###"
locate 7,7 :print "###"
locate 10,4:print "###"
' Additional Walls for higher levels
if level > 2 then
locate 6,7 :print "#"
locate 10,7:print "#"
endif
if level > 4 then
locate 5,7 :print "#"
locate 11,7:print "#"
endif
if level > 8 then
locate 7,4 :print "#"
locate 9,4 :print "#"
endif
if level > 10 then
locate 4,7 :print "#"
locate 12,7:print "#"
endif
color(116,164,109):locate 5,0: print "TOADDER"
color(255,255,255):locate 14,0:print "*":print lives
locate 0,0: print score
Return
Enjoy!
Trooper