Quote: "1. How do I display string and numeric variables in specific locations on the screen?"
A couple of ways typically; you can set the cursor position by using SET CURSOR Xpos,Ypos or you can simply use the TEXT command.
sync on : sync rate 60
repeat
cls
set cursor 200,150
ink rgb(255,0,0),0 : // red
print "printing at 200,150."
ink rgb(255,0,255),0 : // purple
text 100,400,"Press any key to exit."
sync
until scancode() > 0 : // wait for a keypress
end
Quote: "2. How do you create and use accumulators?"
The only accumulators I can think of are from my Assembly language days from years ago. Not sure what you mean here.
Quote: "3. What is the difference between a function and a sub routine. Can you give an example as well?"
Functions can return a value to the call and values can be passed to a function. Variables inside a function are considered local (i.e. the values are lost once you leave the function) and must be declared as global if you intend to use the changed value in the rest of the code. Example:
sync on : sync rate 60
colr = rgb(rnd(100) + 155,rnd(100) + 155,rnd(100) + 155) : // pick a random color
x = rnd(200) + 200 : y = rnd(200) + 200 : width = rnd(200) + 10 : height = rnd(200) + 10
KeyDelay = timer() : Svalue = 0
repeat
cls
nt = MyFunction(x,y,x + width, y + height,colr)
if Svalue = 0 then sValue = nt
ink rgb(255,255,0),0 : //yellow
text 50,100,"Svalue = " + str$(Svalue)
text 50,120,"WillNotShowUp = " + str$(WillNotShowUp)
text 50,200,"Press R to call MyFunction again."
text 50,220,"Click any mouse button to exit."
if timer() > KeyDelay
if keystate(19) = 1 : // user is pressing the R key
colr = rgb(rnd(100) + 155,rnd(100) + 155,rnd(100) + 155) : // pick a random color
x = rnd(200) + 200 : y = rnd(200) + 200 : width = rnd(200) + 10 : height = rnd(200) + 10
Svalue = 0
KeyDelay = timer() + 500 : // wait half a second before the user can change it again
endif
endif
sync
until mouseclick() > 0
end
function MyFunction(x,y,width,height,colr)
ink colr
box x,y,x + width,y + height
RandomValue = rnd(10) : // will return a random number from 0 - 10
WillNotShowUp = -25
endfunction RandomValue
If you change the variable, WillNotShowUp, to global one, it will indeed show up. See here:
sync on : sync rate 60
global WillNotShowUp
colr = rgb(rnd(100) + 155,rnd(100) + 155,rnd(100) + 155) : // pick a random color
x = rnd(200) + 200 : y = rnd(200) + 200 : width = rnd(200) + 10 : height = rnd(200) + 10
KeyDelay = timer() : Svalue = 0
repeat
cls
nt = MyFunction(x,y,x + width, y + height,colr)
if Svalue = 0 then sValue = nt
ink rgb(255,255,0),0 : //yellow
text 50,100,"Svalue = " + str$(Svalue)
text 50,120,"WillNotShowUp = " + str$(WillNotShowUp)
text 50,200,"Press R to call MyFunction again."
text 50,220,"Click any mouse button to exit."
if timer() > KeyDelay
if keystate(19) = 1 : // user is pressing the R key
colr = rgb(rnd(100) + 155,rnd(100) + 155,rnd(100) + 155) : // pick a random color
x = rnd(200) + 200 : y = rnd(200) + 200 : width = rnd(200) + 10 : height = rnd(200) + 10
Svalue = 0
KeyDelay = timer() + 500 : // wait half a second before the user can change it again
endif
endif
sync
until mouseclick() > 0
end
function MyFunction(x,y,width,height,colr)
ink colr
box x,y,x + width,y + height
RandomValue = rnd(10) : // will return a random number from 0 - 10
WillNotShowUp = -25
endfunction RandomValue
Subroutines will execute the code and return to where they were called. They do not return a value, but variables that are changed within a subroutine stay that way within the rest of the code. Subroutines need to have a label inserted in the code at their beginning (followed by a colon) and must have a RETURN statement at the end. Example:
sync on : sync rate 60
ScrWid = screen width() : ScrHgt = screen height()
a$ = "X and Y values have changed"
MoveDelay = timer()
repeat
if timer() > MoveDelay
cls
gosub MySub
ink colr
text x,y,a$
endif
sync
until mouseclick() > 0
end
MySub:
width = text width(a$)
height = text height(a$)
x = rnd(ScrWid - width)
y = rnd(ScrHgt - height)
colr = rgb(rnd(100) + 155,rnd(100) + 155,rnd(100) + 155)
MoveDelay = timer() + 1000 : // wait a second before changing again
return
Quote: "4. How do you write a For/Next loop and can it be displayed differently under different conditions?"
Examples:
sync on : sync rate 60
ScrWid = screen width() - 20 : ScrHgt = screen height() - 20
while returnkey() = 0
cls
low = rnd(30) + 20 : high = rnd(150) + 60
ink rgb(255,128,64),0
for jj = low to high
text rnd(ScrWid),rnd(ScrHgt),"A"
next jj
ink rgb(0,128,255),0
for n = 0 to -20 step -1 : text n * -30,n * -25,"N" : next n
sync
time = timer() + 1000
repeat
if returnkey() = 1 then exit
until timer() > time
endwhile
end
Quote: "5. How do you setup and use While/Endwhile, Repeat/Until, and Do/Loop repeat structures? "
I have demonstrated all but the Do - Loop. I tend not to use it anymore, but here you go:
sync on : sync rate 60
ScrWid = screen width() : ScrHgt = screen height()
Delay = timer()
do
if timer() > Delay then cls rgb(rnd(255),rnd(255),rnd(255)) : Delay = timer() + 750 : // clears the screen using a colr
ink rgb(255,255,255),0
center text ScrWid / 2,ScrHgt / 2,"Press the SPACEBAR to exit."
if spacekey() = 1 then exit
sync
loop
end
EDIT: Man, I hate it when I take the time to help a newbie, but then the newbie never shows up again or does not say whether or not the info helped them.
So many games to code.......so little time.