10-27-2024, 01:42 AM
Hi
I'm not sure if the post must be in this section (Games) or in Utilities.
It is a simple demonstration on how setup keys used for input in a game and how to use them into the game.
Here the game is just moving a character made by a full block in the limits of the screen (screen 0 -->25x80)
here the code
as you can see the code is chunked down and it uses Def for typing String variables.
Have a fun!
I'm not sure if the post must be in this section (Games) or in Utilities.
It is a simple demonstration on how setup keys used for input in a game and how to use them into the game.
Here the game is just moving a character made by a full block in the limits of the screen (screen 0 -->25x80)
here the code
Code: (Select All)
Rem selfdefinition of input from keyboard
DefStr S
Dim sKeyUP, sKeyDOWN, sKeyLeft, sKeyRight
_Title "Self Assignment Input Key Demo"
Print "inizialization"
KeyInit sKeyUP, sKeyDOWN, sKeyLeft, sKeyRight
Print "keys' setup done"
_Delay 2
Xc% = 2
Yc% = 1
Cls
Locate 1, 1
sInput = sKeyUP + sKeyDOWN + sKeyLeft + sKeyRight
Print "Move the character using these keys "; sInput
Print "°";
Do
sInp = InKey$
If InStr(sInput, sInp) Then
If sInp = sKeyUP Then
If Xc% > 1 Then
Printstring Xc%, Yc%, " "
Xc% = Xc% - 1
Printstring Xc%, Yc%, "°"
End If
ElseIf sInp = sKeyDOWN Then
If Xc% < 25 Then
Printstring Xc%, Yc%, " "
Xc% = Xc% + 1
Printstring Xc%, Yc%, "°"
End If
ElseIf sInp = sKeyLeft Then
If Yc% > 1 Then
Printstring Xc%, Yc%, " "
Yc% = Yc% - 1
Printstring Xc%, Yc%, "°"
End If
ElseIf sInp = sKeyRight Then
If Yc% < 80 Then
Printstring Xc%, Yc%, " "
Yc% = Yc% + 1
Printstring Xc%, Yc%, "°"
End If
End If
End If
Loop
End
Sub Printstring (X%, Y%, sS)
Locate X%, Y%
Print sS;
End Sub
Sub KeyInit (sUp, sDown, sLeft, sRight)
Print "Press the key for going UP"
Do
sUp = ""
Do
sUp = InKey$
Loop While sUp = ""
Print " you have choosen "; sUp
Loop Until Confirm$ = "y"
Print "Press the key for going DOWN"
Do
sDown = ""
Do
sDown = InKey$
Loop While sDown = ""
Print " you have choosen "; sDown
Loop Until Confirm$ = "y"
Print "Press the key for going Left"
Do
sLeft = ""
Do
sLeft = InKey$
Loop While sLeft = ""
Print " you have choosen "; sLeft
Loop Until Confirm$ = "y"
Print "Press the key for going Right"
Do
sRight = ""
Do
sRight = InKey$
Loop While sRight = ""
Print " you have choosen "; sRight
Loop Until Confirm$ = "y"
End Sub
Function Confirm$
Print "Do you confirm your choice? Y/N"
sInp = ""
While sInp = ""
sInp = LCase$(InKey$)
If sInp <> "" And InStr("yn", sInp) = 0 Then sInp = ""
Wend
Confirm$ = sInp
End Function
as you can see the code is chunked down and it uses Def for typing String variables.
Have a fun!