Qbasicnews.com

Full Version: Small key handler
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Nothing special, but it is just a small key multiple-key handler for the pure-QB people. It isn't commented, so if you are wondering, "Rls" means "release". For example, "jRlsCode" is the release code for j. I remember making one a long time ago, but I mde this one more refined. If it needs any more explaining, just ask.

Code:
'If you use it (yeah right), please give
'credit to PlayGGY

DEFINT A-Z

SCREEN 7, 0, 1, 0
CLS

CONST rlsCode = 128

CONST jKey = 0
CONST lKey = 1
CONST iKey = 2
CONST kKey = 3

CONST jKeyCode = 36
CONST lKeyCode = 38
CONST iKeyCode = 23
CONST kKeyCode = 37

CONST jRlsCode = jKeyCode + rlsCode
CONST lRlsCode = lKeyCode + rlsCode
CONST iRlsCode = iKeyCode + rlsCode
CONST kRlsCode = kKeyCode + rlsCode

CONST pressed = 1
CONST notPressed = 0

DIM gameKey(3) AS INTEGER

DIM x AS INTEGER
DIM y AS INTEGER

DO
    SELECT CASE INP(96)
        CASE jKeyCode
            gameKey(jKey) = pressed
        CASE lKeyCode
            gameKey(lKey) = pressed
        CASE iKeyCode
            gameKey(iKey) = pressed
        CASE kKeyCode
            gameKey(kKey) = pressed
        CASE jRlsCode
            gameKey(jKey) = notPressed
        CASE lRlsCode
            gameKey(lKey) = notPressed
        CASE iRlsCode
            gameKey(iKey) = notPressed
        CASE kRlsCode
            gameKey(kKey) = notPressed
    END SELECT

    IF gameKey(jKey) = pressed THEN
        x = x - 1
    END IF
    IF gameKey(lKey) = pressed THEN
        x = x + 1
    END IF
    IF gameKey(iKey) = pressed THEN
        y = y - 1
    END IF
    IF gameKey(kKey) = pressed THEN
        y = y + 1
    END IF

    PSET (x, y), 1
    PCOPY 1, 0
LOOP
*Grabs it for use in Operation Pong*
Useful, nice indentation, clean code...interesting, because I was just looking at some other (very messy, albeit) code to do this. Cool. Smile
Thanks! Smile

I am a C++ man now, and if you don't code well, your name goes on a hit list. Wink

It isn't flawless, because if you put in a game with a very low framerate, it may not "catch" the release code. But for most games it will work just fine. Glad to know you are using it!
Yeah...that's what I really hate about C and C++ (as SJ_Zero and I were talking about recently on IRC)...the lack of a friendly community. It's true - I'm pretty new at C, and I went to a forum...posted some encryption...they attacked me, saying that I'm a sucky cipher, and only they are good...
Back on topic...I don't need the release code. Smile Just need to detect keypresses. Tongue
Quote:Yeah...that's what I really hate about C and C++ (as SJ_Zero and I were talking about recently on IRC)...the lack of a friendly community. It's true - I'm pretty new at C, and I went to a forum...posted some encryption...they attacked me, saying that I'm a sucky cipher, and only they are good...
Back on topic...I don't need the release code. Smile Just need to detect keypresses. Tongue

Well, have you run that code? It detects if multiple keys are being pressed. Smile If you haven't run it, go ahead.

You need release codes for that.
* Zack is newbie at this kind of stuff.
Ohhh...in that case, it's even more useful...I can do cool stuff with the pong battle engine..hm....now all I need is PJ to wake himself up and complete the tiles. Tongue J/k. :wink:
There, just edited the original post to make it more clear.

About the C++ community, I go to GameDev.net (which is really the C/C++/Java/Python/Lua/C#/Visual Basic/assembly community), and it isn't that they are mean, it is that they won't help out anyone if they don't like your style. I am actually the same way. Tongue
Well, not a good policy. People nitpick about using only ANSI code...I agree with that, but don't refuse to help newbieson...I dunno, loops or whatever just because they include conio.h in their program.
[shameless plug]
You could upload that code to The Geekery....
[/shameless plug]
Hmm, good code but its a bit bloated(so i trimmed it). Also I tried to get it to work with the arrow keys, but seems to be not compatible!?

Trimmed version:
Code:
'If you use it (yeah right), please give
'credit to PlayGGY
'(Modded by <Dark>)

DEFINT A-Z
SCREEN 7, 0, 1, 0
CLS
DIM gameKey(3)
DO
    dmy$ = INKEY$
    SELECT CASE INP(96)
        CASE 36
            gameKey(0) = 1
        CASE 38
            gameKey(1) = 1
        CASE 23
            gameKey(2) = 1
        CASE 37
            gameKey(3) = 1

        CASE 164
            gameKey(0) = 0
        CASE 166
            gameKey(1) = 0
        CASE 151
            gameKey(2) = 0
        CASE 165
            gameKey(3) = 0
    END SELECT
    IF gameKey(0) = 1 THEN x = x - 1
    IF gameKey(1) = 1 THEN x = x + 1
    IF gameKey(2) = 1 THEN y = y - 1
    IF gameKey(3) = 1 THEN y = y + 1
    PSET (x, y), 1
    PCOPY 1, 0
LOOP
Thanks, dark_prevail, for the suggestions.

However, I don't really understand why you took out the constants and replaced them with numbers. That is a bad coding practice, and the shorter code that results is not worth the increased diffuculty in reading the code. And why did you add the statement with INKEY in there...?
Pages: 1 2 3