Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 1675
Number of posts: 4764

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
When I try to get my program to do something with a keypress it quits Posted by Jeffrey Hope on 26 Oct 2003 at 1:22 PM
Here's the code I have that's been giving me the problem:

PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
5 DO
IF INKEY$ = CHR$(128) + CHR$(2) THEN CALL solo
IF INKEY$ = CHR$(128) + CHR$(3) OR INKEY$ = CHR$(128) + CHR$(4) THEN
PRINT "Sorry, cannot do that"
PLAY "01L2f"
GOTO 5
END IF
IF INKEY$ = CHR$(128) + CHR$(5) THEN CALL ending
LOOP WHILE INKEY$ = ""
END SUB

SUB solo
CLS
RANDOMIZE TIMER
a = INT((12 - 6 + 1) + RND * 6)
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."

I imcluded the first several lines so that you won't get an error message from it.
Report
Re: When I try to get my program to do something with a keypress it qu Posted by the walrus on 26 Oct 2003 at 9:36 PM
PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
DO
q$ = INKEY$
IF q$ = "1" THEN
 CALL solo
 EXIT DO
ELSEIF q$ = "2" OR q$ = "3" THEN
 PRINT "Sorry, cannot do that"
 'PLAY "01L2f"
ELSEIF q$ = "4" THEN
 'CALL ending
 EXIT DO
END IF
LOOP

SUB solo
CLS
RANDOMIZE TIMER
a = INT((12 - 6 + 1) + RND * 6)
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."

Report
Re: When I try to get my program to do something with a keypress it qu Posted by Jeffrey Hope on 27 Oct 2003 at 5:21 PM
:
: PRINT ""
: PRINT "1. Play solo"
: PRINT "2. Play with one friend (coming soon)"
: PRINT "3. Play with two friends (also coming soon)"
: PRINT "4. Get outta here"
: DO
: q$ = INKEY$
: IF q$ = "1" THEN
:  CALL solo
:  EXIT DO
: ELSEIF q$ = "2" OR q$ = "3" THEN
:  PRINT "Sorry, cannot do that"
:  'PLAY "01L2f"
: ELSEIF q$ = "4" THEN
:  'CALL ending
:  EXIT DO
: END IF
: LOOP
: 
: SUB solo
: CLS
: RANDOMIZE TIMER
: a = INT((12 - 6 + 1) + RND * 6)
: PRINT "Right now you hope to get the"
: PRINT "most money out of your given spins."
: 

:
Ummm... I am using QB 4.5


Jeffrey Hope

Jeffrey's Hermit Cave

Report
Re: When I try to get my program to do something with a keypress it qu Posted by the walrus on 27 Oct 2003 at 6:01 PM
: Ummm... I am using QB 4.5

yeah...so what? what about my example doesnt work with qb4.5? ive never really used qb 4.5 much, but i designed it in qbx, but i programmed for years in qb1...so it should definitely work in qb4.5... i didnt change anything that would cause problems with compiler differences...and as far as the code we are talking about is concerned there is NO difference in qbx and qb4.5

i did forget to remove the REM (') statements i had put there because i didnt have the necessary sub "ending" and the play statement also raised an error...i didnt even look to see why it didnt work, you might need to change it..

PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
DO
q$ = INKEY$
IF q$ = "1" THEN
 CALL solo
 EXIT DO
ELSEIF q$ = "2" OR q$ = "3" THEN
 PRINT "Sorry, cannot do that"
 PLAY "01L2f"
ELSEIF q$ = "4" THEN
 CALL ending
 EXIT DO
END IF
LOOP

SUB solo
CLS
RANDOMIZE TIMER
a = INT((12 - 6 + 1) + RND * 6)
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."

Report
Re: When I try to get my program to do something with a keypress it qu Posted by Jeffrey Hope on 29 Oct 2003 at 3:57 PM
: : Ummm... I am using QB 4.5
:
: yeah...so what? what about my example doesnt work with qb4.5? ive never really used qb 4.5 much, but i designed it in qbx, but i programmed for years in qb1...so it should definitely work in qb4.5... i didnt change anything that would cause problems with compiler differences...and as far as the code we are talking about is concerned there is NO difference in qbx and qb4.5
:
: i did forget to remove the REM (') statements i had put there because i didnt have the necessary sub "ending" and the play statement also raised an error...i didnt even look to see why it didnt work, you might need to change it..
:
:
: PRINT ""
: PRINT "1. Play solo"
: PRINT "2. Play with one friend (coming soon)"
: PRINT "3. Play with two friends (also coming soon)"
: PRINT "4. Get outta here"
: DO
: q$ = INKEY$
: IF q$ = "1" THEN
:  CALL solo
:  EXIT DO
: ELSEIF q$ = "2" OR q$ = "3" THEN
:  PRINT "Sorry, cannot do that"
:  PLAY "01L2f"
: ELSEIF q$ = "4" THEN
:  CALL ending
:  EXIT DO
: END IF
: LOOP
: 
: SUB solo
: CLS
: RANDOMIZE TIMER
: a = INT((12 - 6 + 1) + RND * 6)
: PRINT "Right now you hope to get the"
: PRINT "most money out of your given spins."
: 

:
Here's the ending sub:
SUB ending
FOR a = 1 TO 10
PRINT ""
NEXT a
PRINT "See the credits before you go (Y/N)?"
DO

Report
Re: When I try to get my program to do something with a keypress it qu Posted by KDivad Leahcim on 30 Oct 2003 at 9:38 PM
: Ummm... I am using QB 4.5

Which I just tried and it works in flawlessly...
Report
Re: When I try to get my program to do something with a keypress it qu Posted by davimedrade on 27 Oct 2003 at 3:58 AM
Before I say anything, I must know... which compiler are you using?


Davi Medrade
Master Programmer of Agnetron Software Ltda.
davimedrade@hotmail.com
So Paulo, Brazil

Report
Re: When I try to get my program to do something with a keypress it qu Posted by Jeffrey Hope on 27 Oct 2003 at 5:19 PM
: Before I say anything, I must know... which compiler are you using?
:
:
: Davi Medrade
: Master Programmer of Agnetron Software Ltda.
: davimedrade@hotmail.com
: So Paulo, Brazil
:
:
I am using QB 4.5
Report
Re: When I try to get my program to do something with a keypress it qu Posted by davimedrade on 28 Oct 2003 at 6:15 AM
I think that if you use labels instead of Line numbers, your progrram will be easier to understand.

Instead of:
1 PRINT "TEST"

Use:
IAmALabel:
PRINT "TEST"


Then, in the GOTO you can use the label instead of the line number:
GOTO IAmALabel



Davi Medrade
Master Programmer of Agnetron Software Ltda.
davimedrade@hotmail.com
So Paulo, Brazil

Report
Re: When I try to get my program to do something with a keypress it qu Posted by davimedrade on 28 Oct 2003 at 6:26 AM
PRINT "" 
PRINT "1. Play solo" 
PRINT "2. Play with one friend (coming soon)" 
PRINT "3. Play with two friends (also coming soon)" 
PRINT "4. Get outta here" 
KeyLoop:      'This label will make your code easier to read
DO 

In the following lines, use the string with the number, instead of the codes, and put the key into a variable, because INKEY$ is cleared each time you use it, then, the first IF will be true, if you pressed 2, but you used INKEY$ in this if, then it will be cleared and the other IFs will be always false:
     K$=INKEY$     'Saves the current key
     IF K$ = "2" THEN CALL solo 
     IF K$ = "3" OR K$ = "4" THEN 
          PRINT "Sorry, cannot do that" 
          PLAY "01L2f" 
          GOTO 5 
     END IF 
IF K$ = "5" THEN CALL ending 
LOOP 
END     'Why END SUB here?

SUB solo 
     CLS 
     RANDOMIZE TIMER 
     a = INT(7 + RND * 6) 'Use the result, insted of the operation with constants
     PRINT "Right now you hope to get the" 
     PRINT "most money out of your given spins." 
END SUB    'You must use END SUB only here!



I hope it will function.

And, remember: Indent your code. It will be easier to read and understand.

Mail Me!
Davi Medrade
Master Programmer of Agnetron Software Ltda.
davimedrade@hotmail.com
So Paulo, Brazil




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.