Qbasicnews.com

Full Version: Movement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI all, I have just started tinkering with qbasic and learning it from the help files. So go easy on me I'm a little green.

my problem is I am trying to make a $ move on the screen but it wont work what is wrong with this code?

let x=15
let y =15
screen 13
cls
do

if inkey$ = "8" then y = y - 1
if inkey$ = "2" then y = y + 1
if inkey$ = "4" then x = x - 1
if inkey$ = "6" then x = x + 1
cls

locate x,Y
print "$"

loop


I know I looks a little newbie but I can get "up" or 8 to work. When I push the button but the rest (6,4,2) I need to hold the keys down before the $ start to move. I would also like to know how to get the 4 stand alone arrow keys to work, I'm thinking it has to do with char$.

Thanks a lot for taking the time to help a confused newbie :???:
I advise you to do something like this instead:
[syntax="qbasic"]do
i$=inkey$
select case i$
case "8"
y=y-1
case "2"
y=y+1

' -- and so on --

end select
locate y,x '<-- Very important: on locate the y coordinate comes before the x
print "$"; '<-- the semicolon prohibits print from sending a cr+lf
loop[/syntax]

Also, if code only should be executed after a keypress (= turnbased
games and the like) this would increase the keytrapping even more,
since the program doesn't have to go through select case each loop:
change this:
[syntax="qbasic"] i$=inkey$[/syntax]
int this
[syntax="qbasic"] do
i$=inkey$
loop while i$=""[/syntax]

Also some general advice: let is not needed anymore...

Anonymous

Code:
uparrow$ = chr$(0) + "H"
rightarrow$ = chr$(0) + "M"
downarrow$ = chr$(0) + "P"
leftarrow$ = chr$(0) + "K"

x = 15
y = 15

screen 13

do

  locate y,x
  print "$"

  do
    keyin$ = inkey$

  loop until keyin$ <> ""

  cls

  select case keyin$

    case uparrow$
      y = y - 1

    case rightarrow$
      x = x + 1

    case downarrow$
      y = y + 1

    case leftarrow$
      x = x - 1

  end select

loop until keyin$ = chr$(27)


should be what you need
Thanks for the input guys. I think I got the select case idea. I still seem to be having a problem though, when the arrow keys are pushed the $ does not move but it does flicker telling me something is working. I copied the code straight from the program hopefully someone can point out what I'm not seeing.

uparrow$ = CHR$(0) + "h"
rightarrow$ = CHR$(0) + "m"
downarrow$ = CHR$(0) + "p"
leftarrow$ = CHR$(0) + "k"

x = 15
y = 15

SCREEN 13

DO

LOCATE y, x: PRINT "$";

DO
keyin$ = INKEY$

LOOP UNTIL keyin$ <> ""
CLS
SELECT CASE keyin$

CASE uparrow$
y = y - 1

CASE rightarrow$
x = x + 1

CASE downarrow$
y = y + 1

CASE leftarrow$
x = x - 1

END SELECT

LOOP UNTIL keyin$ = CHR$(27);


thanks in advance guys.

One more thing, I thought the point of changing inkey$ to something else was to save time typing? keyin$ is the same length as inkey$. I have only been doing this for a few days so would not know any better.
I forgot to mention that when reverting back to
uparrow$="8" ...ect

the $ does more around.

Anonymous

the letters need to be capital, "H" is ascii code 72 and thats the code you need for the up arrow. "h" will give you the wrong code.
Right on! Thanks guys, now I can start work on the game for the little one. :rotfl:

Anonymous

oh hey btw when you asked why you rename inkey...

its not to save typing, its actually because inkey$ is a function, and every time you do "if inkey$ = "d" " or "r$ = inkey$", youre calling the function and clearing the previous results. so if you read one thing in one inkey, and in the next inkey read something different, you would only have the second result at the end. this is why its best, if not real time, to do a "polling loop" to grab the value in inkey into a variable, and then check that variable against all possible cases before moving on.. hope that helped