Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Two player keyboard presses (or "Buzzers", if you will)

Author
Message
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 3rd Jul 2008 20:53
Here's my question: I'm making a two player game and I need a way to stop the program and jump to a specific part in the program based on a letter press, e.g. Player 1's letter is "A" and Player 2's letter is "L". Also, this event will be timed, so if no player presses their letter, I would like a "Time's Up" message. I'm using DBC version 1.13 but I can't figure out an efficient way to do this.

Thanks, Donald
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 3rd Jul 2008 22:05 Edited at: 3rd Jul 2008 22:06
ok, so let me see if I can get this straight. Your questions are:
1. How to jump to a different part in the code.
2. How to time key presses.

Here is a rundown. The checking part will be in a loop. Do loops work fine, but you can use another one if you prefer to. Anyway,you will then use an if-then coupled with inkey$() and lower$() (this is to make sure that the input isn't case sensitive) to check when a key is pressed (2 if-thens for 2 buttons). Inside, you will use the gosub command to go to a different part of the code. At the end of the loop, you will increment a variable, then check to see if the variable equals your max time. If it is, then you can gosub to a times up subroutine.

For more info on these commands, type them in and press f1.

So here is the code:


Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 3rd Jul 2008 23:30 Edited at: 4th Jul 2008 02:38
When I run this, the "Time up !!!" message appears, but if I press "a" or "l", I never see the appropriate buzzed in message. Conversely, if I add a "Print timer, "";" statement after the "if timer=200..." line, it works. Is my coding in error?

Thanks, Donald

[code snippet]
do
if lower$(inkey$())="a" then gosub player1
if lower$(inkey$())="l" then gosub player2
if timer=200 then gosub timeup
inc timer,1
loop

timeup:
Print "Time's up !!!"
do:loop

player1:
Print "Player 1 buzzed in first"
do:loop

player2:
Print "Player 2 buzzed in first"
do:loop
[code snippet]
sinisterstuf
18
Years of Service
User Offline
Joined: 28th Mar 2007
Location: Namibia, Southern Africa
Posted: 4th Jul 2008 01:20
Ok, I'm not realy sure what you're trying to do in some parts. Your labels should end with the RETURN command, like:



not really sure what the do:loop things in your code are supposed to do...
Also, I don't know how fast the progam loops and therefore how fast the timer increments but maybe if the program jumps to the timeup label too fast then you should increase the value to give you more time to press the buttons...

was this helpful?

When the going get stuf,
the stuf get's sinister
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 4th Jul 2008 02:03
I put in the "do:loop" commands just to pause the program, so I can see what's going on
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 4th Jul 2008 02:52
Here's some sample code:


do
REM ink rgb(255,255,255),1:set cursor 605,460:print timer
if lower$(inkey$())="a" then P=1 :gosub player
if lower$(inkey$())="l" then P=2 :gosub player
if timer=2000 then gosub timeup
inc timer,1
REM ink rgb(0,0,0),1: set cursor 605,460:print timer
loop

timeup:
ink rgb(255,255,255),1
set cursor 0,0
Print "Time's up !!!"
goto end

player:
ink rgb(255,255,255),1
set cursor 0,0
Print "Player ";P;" buzzed in first"
print "What's your answer, Player ";P;"?"

end:


If I unRemark the rem lines, the program works, but the lines Remarked, the "Time's Up" message appears. And also, it doesn't seem to matter how high the value of timer is. As long as I give the program something to do, printing the value the timer, e.g., it works.

Thanks for your replies and your patience, Donald
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 4th Jul 2008 03:07
Are 2 players playing simultaneously? Do you want to register only 1 key press (i.e. if player 2 decides to just keep their key held down you don't want the key to repeat and you don't want other keys not being able to be read)

Just to expand on BN2's idea a bit... I would avoid using inkey$() because that has a tendency to lockout other keys and it can be slow. It's good for multiple choice situations but it seems your setting up a "first one to answer the question wins" situation. Using KEYSTATE() has a much better response and can check multiple keypresses without interfering with one another. Anyway, here is an example. Basically, it runs the timer down using the internal TIMER() command. If 5000ms pass (5 secs) it jumps to the buzzer. If player 1 or 2 press their key before the timer runs out, it jumps to their routine. The remarks explain what's going on. Ask if it doesn't make sense:



Enjoy your day.
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 8th Jul 2008 04:24
Thanks, Latch. Your code is exactly what I needed.

Thanks to everyone for their help. Donald

Attachments

Login to view attachments

Login to post a reply

Server time is: 2025-06-06 00:15:45
Your offset time is: 2025-06-06 00:15:45