Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Self Assignment input keys Demo
#1
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
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!
Reply
#2
What if my friend, whose name is Siam Tupid, chooses the same key each time? Well, I guess he's only going in one direction, anyway. Straight down! Other than that, I'll give it a +1 for neat concept because goof proofing aside, it works as advertised.

Pete Smile
Shoot first and shoot people who ask questions, later.
Reply
#3
Hi Pete
thank you for your feedback.
I find it very useful and it pulls over the development of this demo.
Here the adding for proof of user Siam Tupid.

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)

    Do
        Print "Press the key for going UP"
        sUp = ""
        ' for dummy user or accidental wrong choice
        sUsed = ""
        Do
            sUp = InKey$
            If sUp <> "" And AlreadyUsed(sUsed, sUp) Then sUp = ""
        Loop While sUp = ""
        Print " you have choosen "; sUp
    Loop Until Confirm$ = "y"
    ' for dummy user or accidental wrong choice
    sUsed = sUp


    Do
        Print "Press the key for going DOWN"
        sDown = ""
        Do
            sDown = InKey$
            If sDown <> "" And AlreadyUsed(sUsed, sDown) Then sDown = ""
        Loop While sDown = ""
        Print " you have choosen "; sDown
    Loop Until Confirm$ = "y"
    ' for dummy user or accidental wrong choice
    sUsed = sUp + sDown


    Do
        Print "Press the key for going Left"
        sLeft = ""
        Do
            sLeft = InKey$
            If sLeft <> "" And AlreadyUsed(sUsed, sLeft) Then sLeft = ""
        Loop While sLeft = ""
        Print " you have choosen "; sLeft
    Loop Until Confirm$ = "y"
    ' for dummy user or accidental wrong choice
    sUsed = sUp + sDown + sLeft


    Do
        Print "Press the key for going Right"
        sRight = ""
        Do
            sRight = InKey$
            If sRight <> "" And AlreadyUsed(sUsed, sRight) Then sRight = ""
        Loop While sRight = ""
        Print " you have choosen "; sRight
    Loop Until Confirm$ = "y"
End Sub

Function AlreadyUsed (sUsed, sNew)
    AlreadyUsed = InStr(sUsed, sNew)
End Function

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

thanks to let me go on with this demo
Reply
#4
LOL @ "for dummy user."

+1, because I like the checking performed by a function.

Pete
Shoot first and shoot people who ask questions, later.
Reply




Users browsing this thread: 1 Guest(s)