Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Keyboard buffer manipulation

Dr. COM WIZDr. COM WIZ Member Posts: 74
Hello, World!
This is Dr. COM WIZ. I'm in the middle of game development at this time. I have this one well planned (and am still planning), so hopefully I'll finish it this time.
I ran into a problem with control, though. I tested my scrolling engine to see how it works under with delaying. Guess what? It failed. I pasted a delay in the middle of the engine and was digusted with the way it looped even when you stop pressing the button. That was the only problem: It looped even when you stop pressing the deirectional button. That happens if the button is held for a second or two.
So, I visited my library of QBasic tutorials (in my computer, which containe MBs upon MBs of tutorials, 22.1 to be more exact) and went to searching. I wanted to know how to clear the keyboard buffer so that it doesn't repaet without will. Sure enough, there it was, Buff's QBasic Tips that I downloaded months ago. On it "How to clear the Keyboard buffer," which was EXACTLY what I was looking for.
It gave me a line of code to use and I stuck it in the middle of my engine. The only problem is that I have to put a delay right after it so that the engine will allow the user to regain control over the keyboard again. Just a short delay, not too long.
However, looking at games today (and yesterday), the user has better control over the keyboard or control pad in the way that they don't have to wait a whole half second for the button to repeat in order for the game character to go forward. Like in the original Super Mario Bros.(tm), once the forward button is presses, Maro immediately goes and keeps going until the user releases the button. I rarely see a QBasic game that did that without having to wat until the computer processes a repeat.

On the contrary, there are some exceptional games that have this Mario control trait. They are rare, but they are there (hey that ryhmed). So, if any of you programmers out there know how to delete the delay before key repetition, please let us know. Let the world know so that we all can create better games with better control!

Also, if anyone knows a faster way to clear the keyboard buffer than the way below, please let us know that, too.

[code]
DEF SEG = 0: POKE &H41A, PEEK(&H41C)
DEF SEG
FOR delay! = 1 TO 4000 'The delay it takes to
NEXT 'regain control over the
'keyboard.

'Change the delay according to your computer's charecteristics.
[/code]

If any of you want to see the unfinished engine that I was talking about, find it here:

http://www.programmersheaven.com/msgboard/read.asp?Board=15&MsgID=100456&Setting=A9999F0001

It would be best to read the messages around and in the coding. It will help you to understand why the engine is the way it is, what is going to become of it, and what you can do about it.

To get that page of very useful QBasic tips I was talking about, go here, which contains over 50 tips in four levels from beginner to advanced.
www.telepath.comoasys ips.htm
(Buff's Tip Page)
As surprised as I was, there was a tip in the Beginner's level that I didn't even know! There was, as well, a few Advanced tips (just a few, like, 3 or so) didn't even understand. Check it out and tell you friends.
This has been,
Dr. COM WIZ
PS Thank you and goodnight!

Comments

  • Dr. COM WIZDr. COM WIZ Member Posts: 74
    Hello, World!
    This is Dr. COM WIZ. I recently had problems with keyboard control in my game in current development. My controls were acting much to slowly and I asked for help on this messageboard. While no one was able to help, I found my own solution that would prove VERY beneficial to all you other programmers!

    I found out two pieces of code that would supercharge and enhance your game control.

    This piece of code will clear your keyboard buffer so that you won't hear those annoying beeps when holding the key for too long. It will also make it so that the key would stop repeating when you relase it.
    [code]
    'put this kode right before you get the user's key input
    'in your game loop
    DEF SEG = 0: POKE &H41A, PEEK(&H41C)
    DEF SEG
    FOR delay! = 1 TO 4000 'The delay it takes to
    NEXT 'regain control over the
    'keyboard.

    'Change the delay according to your computer's charecteristics.
    'Note: The delay probably won't be needed if you use the code
    'below this one
    [/code]

    This next piece of code is so simple, but it is extremely faster than INKEY$. It used a little bit differently, but it's a lot-o-bit faster and beneficial.

    [code]
    DO
    Keyp = INP(&H60) 'Don't forget that this returns a numeric value,
    'not a string value.
    IF keyp = 27 THEN PRINT "You pressed ESC"
    IF keyp = 155 THEN PRINT "You released ESC"

    LOOP
    [/code]

    As you can see, when a key is pressed, the INP(&H60) returns its keycode. However, when it is released, it returns it key code plus 128. Get familiar with this code, it is said to be 90% faster (but seems much faster than that). Unlike INKEY$, you do not have to wait for the comptuer to process a repeat. Once you hold it, it will repeat for as long as you hold it without stopping. And when occampanied with the first piece of code I gave, it will stop reapeating once you let go.

    Both of these tips were takend from Buff's Tip page
    (www.telepath.com/oasys/tips.htm)

    This has been,
    Dr. COM WIZ
    PS Thank you and goodnight!

  • PuzzlerPuzzler Member Posts: 208
    Hello! I haven't heard form you in awhile... It's good to see old faces hanging around. Hows that game going?

    --------------------------------------------
    I will bend your mind with my spoon...

  • Dr. COM WIZDr. COM WIZ Member Posts: 74
    : Hello! I haven't heard form you in awhile... It's good to see old faces hanging around. Hows that game going?
    :
    : --------------------------------------------
    : I will bend your mind with my spoon...
    :
    :

    [font=3]Shout out to BASIC Friend: "This is for you, too."[/font]
    Hello, Puzzler!
    This is Dr. COM WIZ. If you read my first recent message, you'll know why it's been such a while that I haven't posted messages on Programmer's Heaven. Well, I'm back and glad to be. Thank you for caring.
    How is the game coming? It's great! I have my main game engine well built and virtually bugless. The control is fast and so is the engine. It even has a univeral delay routine--it's the same speed on virtually all computers with a clock (all computers, excpet maybe the one that Steve Russel used to make the very frist computer game in the 60's)! Would you like to see the engine? Here it is!:

    [code]
    REM * * ) )Entry: This is the 3rd edited scrolling for my game project
    'Date Started: 2/5/02
    'For the QBasic Interperter or compiler
    DECLARE SUB moveup ()
    DECLARE SUB movedown ()
    DECLARE SUB moveleft ()
    DECLARE SUB moveright ()

    ON TIMER(1) GOSUB frames
    TIMER ON
    SCREEN 13
    CLS
    DIM SHARED maze$(490)
    DIM SHARED map$(75, 75)
    DIM SHARED x, y
    DIM SHARED ysv%, xsv%
    DIM SHARED xt%, yt%
    DIM SHARED xpos%, ypos%
    DIM SHARED hidtile$

    ysv% = 1: xsv% = 1 'map viewing starting points
    xt% = 0: yt% = 0 'hieght and width of each tile
    vismapx% = 10: vismapy% = 10 'map viewing endpoint
    xpos% = 5: ypos% = 5 'the character's tile position

    maze$(1) = " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    maze$(2) = " X ####### XXXXXX XXXX X"
    maze$(3) = " X#######XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    maze$(4) = " XXXXXXXXXX X"
    maze$(5) = " X + XXXXXXX XXXX###X XXXX X"
    maze$(6) = " X X"
    maze$(7) = " X XXXXX####XXXXXXXXXXXXXXXXXXXX X"
    maze$(8) = " X X XX##XX XXX X"
    maze$(9) = " X # # ## X X"
    maze$(10) = " X XX X X"
    maze$(11) = " X X X"
    maze$(12) = " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

    REM * * ) )this LOOP block is where the map is loaded( ( * *

    DO
    x = x + 1
    FOR y = 1 TO LEN(maze$(x))
    map$(x, y) = MID$(maze$(x), y, 1)
    NEXT y
    IF LEN(maze$(x)) = 0 THEN EXIT DO
    LOOP
    'end of map loading

    hidtile$ = map$(xpos%, ypos%) 'this is the tile behind the character
    map$(xpos%, ypos%) = "C" 'the representation of the character on map


    REM * * ) )This is the scrolling engine it self( ( * *
    fp = TIMER
    DO
    start! = TIMER

    keypress = INP(&H60) 'this is an extremely fast alternative to INKEY$
    'This is the key configuration
    IF keypress = 72 THEN 'up
    CALL moveup
    ELSEIF keypress = 80 THEN 'down
    CALL movedown
    ELSEIF keypress = 77 THEN 'right
    CALL moveright
    ELSEIF keypress = 75 THEN 'left
    CALL moveleft
    END IF
    map$(xpos%, ypos%) = "C" 'the representation of the character on map

    FOR y = ysv% TO ysv% + vismapy% - 1 '_
    FOR x = xsv% TO xsv% + vismapx% - 1 ' |
    LOCATE x - xsv% + 1, y - ysv% + 1 ' |
    PRINT map$(x, y) ' |-------Scrolling
    NEXT ' |
    NEXT '_|

    DEF SEG = 0: POKE &H41A, PEEK(&H41C): DEF SEG


    DO WHILE TIMER - start! < .04
    LOOP

    fps = fps + 1



    LOOP
    'I thought of this myself!
    frames:
    PRINT fps; "frames per second"
    fps = 0
    RETURN

    SUB movedown
    IF map$(xpos% + 1, ypos%) = "X" THEN EXIT SUB
    map$(xpos%, ypos%) = hidtile$
    xsv% = xsv% + 1
    xpos% = xpos% + 1
    hidtile$ = map$(xpos%, ypos%)
    END SUB

    SUB moveleft
    IF map$(xpos%, ypos% - 1) = "X" THEN EXIT SUB
    map$(xpos%, ypos%) = hidtile$
    ysv% = ysv% - 1
    ypos% = ypos% - 1
    hidtile$ = map$(xpos%, ypos%)
    END SUB

    SUB moveright
    IF map$(xpos%, ypos% + 1) = "X" THEN EXIT SUB
    map$(xpos%, ypos%) = hidtile$
    ysv% = ysv% + 1
    ypos% = ypos% + 1
    hidtile$ = map$(xpos%, ypos%)
    END SUB

    SUB moveup
    IF map$(xpos% - 1, ypos%) = "X" THEN EXIT SUB
    map$(xpos%, ypos%) = hidtile$
    xsv% = xsv% - 1
    xpos% = xpos% - 1
    hidtile$ = map$(xpos%, ypos%)
    END SUB

    SUB postchar
    10
    END SUB
    [/code]

    This is pretty good, right? Now, all it needs is graphics!

    This engine is not yet equipped to handle recieving items. I'll had that plus a routine to be able to handle color background and sprite animation (including a walking character). However, I could sure use any help given. Please inform me of any faster techniques than this, so that it will also run well on extremely slow computers. I am also looking for artcals that discuss tile art. I am a 3D artist, but *good* tile art is pretty darn difficult. Please inform me if you run into one. Oh, and, plesae report any glitches or bugs in the engine.

    By the way, I'm defintely doing tile on this one. I understand Pixel-by pixel and how it works, but this project will only scroll tiles as the next one *might* scroll pixels.


    This has been,
    Dr. COM WIZ
    PS Thank you and goodnight!

  • PuzzlerPuzzler Member Posts: 208
    A couple of suggetsions:

    1. Break it into routines for a more structured approach (like have a routine to load the map, one to display it, one to scroll it, etc.)

    2. Make a routine to load maps from binary or text files.

    3. I am a tile artist nd I do 24-bit BMP's. You should get future library to load them (I have a big library of them done already). It can be found at http://qbasic.qb45.com/

    --------------------------------------------
    I will bend your mind with my spoon...

  • Dr. COM WIZDr. COM WIZ Member Posts: 74
    [b][red]This message was edited by the Dr. COM WIZ at 2002-2-10 22:52:48[/red][/b][hr]
    Hello, Puzzler!
    This is Dr. COM WIZ. I'm writing concerning the development of my new game.

    I had a question to ask, but I just answered it myslef as I was typing.
    Also, I already planned on using modules to break down my coding. What you saw was just a very rough draft and is, even the engine still, work in progress.

    However, I'm not exactly sure where to go next. Should I start making my graphics already? Guests (at home) won't be amazed by my work unless they see some graphics, but am I ready for that now or should my engine be 100% established before I move on to graphics? I'm pretty eager to start graphics, but I'm not sure if I should now. This is my first planned game and I would appreciate proffesional advice from people like you.

    I'm also not sure on whether I should have my graphics stored in external binary files or created with QBasic's DATA statments. What are the pros and cons of that?

    What about multiple framesets? How can I do that versus having one frame in each file (which can be a nice big mess of files)?


    This has been,
    Dr. COM WIZ
    PS Thank you and goodnight!



  • PuzzlerPuzzler Member Posts: 208
    OK. You should have your maps stored in exterior text files and your graphics in binary files. You are doing this in screen 13 right (no SVGA)? If so, you also need to have a fast pallete function and pallete files (a pallete file and loader are musts for good screen 13 game).

    Now everything I said above is based on the following:

    1. You might want to add on to your game in the future.
    2. You are not worried about people making mods for your game.

    If eithter of these is false, then do everything internally. This will make your game virtually unmodifiable, unless the people have the source code.

    As for the pallete, I can't exaclty remember how to do a struct in BASIC but I can give you on in C++ which you get the general idea from. Or you can just get ABC and look under graphics rotuines. If you download ABC, also look for a BMP loaader for screen 13 (as I will give you some art and they are easier to edit than binary files).

    Another thing you may want is music. For this, find a wav player in ABC.

    Good luck on your game!

    --------------------------------------------
    I will bend your mind with my spoon...

  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    _______ / http://forcoder.org / free video tutorials and ebooks about // Go Visual Basic .NET C# Python PHP C++ Objective-C PL/SQL Swift R C Visual Basic JavaScript Delphi Perl Assembly Java MATLAB Scratch Ruby Lisp LabVIEW Logo VBScript Awk Ada Rust Apex ABAP D Erlang FoxPro Prolog ML Dart Julia Hack Bash Crystal Fortran Scala Scheme Transact-SQL Clojure Kotlin Alice SAS F# COBOL Lua // _______

Sign In or Register to comment.