In game programming, it is for certain that your game requires user input so the game will be interactive. You need to know if a key is being pressed or not.
In KonsolScript, doing such won't get any complicated because 8
default keys are being handled for you to use.
Since KonsolScript is designed for making games, certain features are built-in to the language. The code below is demonstrating its use.
function main() { Screen:Show() while (B1 == false) { Screen:CLS() Screen:GoToXY(5, 5) Screen:PrintString("Press ESC to quit\n (press ALT | ENTER | SPACE | Arrow keys)") Screen:GoToXY(30, 50) if (BU == true) { Screen:PrintString("UP is pressed") } else if (BD == true) { Screen:PrintString("DOWN is pressed") } else if (BL == true) { Screen:PrintString("LEFT is pressed") } else if (BR == true) { Screen:PrintString("RIGHT is pressed") } else if (B2 == true) { Screen:PrintString("ALT is pressed") } else if (B3 == true) { Screen:PrintString("ENTER is pressed") } else if (B4 == true) { Screen:PrintString("SPACE is pressed") } Screen:Render() } }
When executed, the program should look like as shown by the screenshot.
Let's go thru the code.
Screen:Show()
As always, we need a viewing screen.
while (B1 == false) { /* scope... */ }
Then, we need to go on a loop which would only end if B1
is equal to true
.
…
Wait a minute. What is B1
? Did we declare any variable?
Oh! I forgot, B1
is, actually, one of the 8
default keys built-in to KonsolScript.
B1
is a Boolean variable that is hooked to the ESCAPE
key of the keyboard. Meaning, it will have a value of true
, if ESCAPE
key is being pressed, and a value of false
if not being pressed. Pretty simple, eh?
Screen:CLS()
Inside the scope of while
loop, we want to repeatedly clear the screen since we want to place or draw new stuff on the viewing screen.
Screen:GoToXY(5, 5) Screen:PrintString("Press ESC to quit\n (press ALT | ENTER | SPACE | Arrow keys)")
The code above is just some informational purposes. We want to tell the user what to do.
Screen:GoToXY(30, 50)
Next, we have another Screen:GoToXY
command. This will place the next word to print in the location of 30
pixels from left and 50
pixels from right.
if (BU == true) { Screen:PrintString("UP is pressed") } else if (BD == true) { Screen:PrintString("DOWN is pressed") } else if (BL == true) { Screen:PrintString("LEFT is pressed") } else if (BR == true) { Screen:PrintString("RIGHT is pressed") } else if (B2 == true) { Screen:PrintString("ALT is pressed") } else if (B3 == true) { Screen:PrintString("ENTER is pressed") } else if (B4 == true) { Screen:PrintString("SPACE is pressed") }
The code above introduces you to the other 7
other default keys. These are:
BU
- hooked with Arrow Up
keyBD
- hooked with Arrow Down
keyBL
- hooked with Arrow Left
keyBR
- hooked with Arrow Right
keyB2
- hooked with ALT
keyB3
- hooked with ENTER
keyB4
- hooked with SPACE
key
Aside from the 8
' default keys, KonsolScript also allows you to have your own Boolean variables which will be hooked to a key you desire.
I will change the code above to demonstrate the the Key class.
function main() { Var:Boolean btnA, btnS, btnD, btnX; Key:New("a", btnA) Key:New("s", btnS) Key:New("d", btnD) Key:New("x", btnX) Screen:Show() while (btnX == false) { Screen:CLS() Screen:GoToXY(5, 5) Screen:PrintString("Press X to quit\n (press A | S | D keys)") Screen:GoToXY(30, 50) if (btnA == true) { Screen:PrintString("A is pressed") } else if (btnS == true) { Screen:PrintString("S is pressed") } else if (btnD == true) { Screen:PrintString("D is pressed") } Screen:Render() } }
The only added codes are the ones written below.
Var:Boolean btnA, btnS, btnD, btnX; Key:New("a", btnA) Key:New("s", btnS) Key:New("d", btnD) Key:New("x", btnX)
Just like making buffer where we declare a Number
variable, in Key States
, we also declare a Boolean
variable to be our virtual key.
To make our own virtual key, we will use New function of Key class. We assign the key we want to be hooked on our Boolean
variable.
The changes made, though not very significant, are the ones written below.
while (btnX == false) { /* ... */ Screen:PrintString("Press X to quit\n (press A | S | D keys)") /* ... */ if (btnA == true) { Screen:PrintString("A is pressed") } else if (btnS == true) { Screen:PrintString("S is pressed") } else if (btnD == true) { Screen:PrintString("D is pressed")
All of which should be self-explanatory, I assume.
Before ending this tutorial, I'd like to demonstrate how to (in a way) ”properly” handle key inputs.
The code below is a modified source from the one used in 2-picture animation.
function main() { Var:Number imgShip, imgShip2; Var:Boolean next = false; Var:Number x = 150; Image:Load("myship.bmp", imgShip) Image:Load("myship2.bmp", imgShip2) Screen:Show() while (B1 == false) { Screen:CLS() if (next == false) { Image:TBlit(x, 200, imgShip, screen) next = true; } else { Image:TBlit(x, 200, imgShip2, screen) next = false; } if (BL == true) { x--; } else if (BR == true) { x++; } Screen:Render() } }
The only added codes were the ones written below.
Var:Number x = 150; /* ... */ if (BL == true) { x--; } else if (BR == true) { x++; }
But changes are made. See the code below.
Image:TBlit(x, 200, imgShip, screen) /* ... */ Image:TBlit(x, 200, imgShip, screen)
Learn from the code and start doing your own game. But be sure to have fun!