## Crosshairs Question

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

davinci28
Newbie
Posts: 3
Joined: Tue Sep 18, 2007 9:28 pm

### Crosshairs Question

Hey guys, novice programer here. I'm trying to make a set of crosshairs that will be able to moved around the screen. You know, like for targeting. I wrote the code, and it works, but it is jumpy. Please help me streamline it in whatever way possible.

Code: Select all

``````SCREEN 13
CLS
x = 40
y = 40
LINE (0, y)-(320, y), 2
LINE (x, 0)-(x, 200), 2
1
DO
IF INKEY\$ = "s" THEN
10
LINE (0, y)-(320, y), 0
LINE (x, y)-(x, y + 1), 2
y = y + 1
LINE (0, y)-(320, y), 2
GOTO 1
END IF

IF INKEY\$ = "w" THEN
20
LINE (0, y)-(320, y), 0
LINE (x, y)-(x, y - 1), 2
y = y - 1
LINE (0, y)-(320, y), 2
GOTO 1
END IF

IF INKEY\$ = "d" THEN
30
LINE (x, 0)-(x, 200), 0
LINE (x, y)-(x + 1, y), 2
x = x + 1
LINE (x, 0)-(x, 200), 2
GOTO 1
END IF

4
IF INKEY\$ = "a" THEN
LINE (x, 0)-(x, 200), 0
LINE (x, y)-(x - 1, y), 2
x = x - 1
LINE (x, 0)-(x, 200), 2
GOTO 1
END IF
q
LOOP UNTIL INKEY\$ = "q"
END

``````

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

### Re: Crosshairs Question

davinci28 wrote:it is jumpy
Right. Try this

DO
IF INKEY\$ = "s" THEN

Change it to

DO
K\$ = INKEY\$
IF K\$ = "s" THEN

And substitute K\$ for all the INKEY\$ tests below.

The reason: Say you press an "a" and your loop happens to be on the first instruction. The command IF INKEY\$ = "s" will find that you did not press an "s". BUT NOW THE BUFFER IS EMPTY. The next instruction which tests for IF INKEY\$ ="a" will fail because now INKEY="" (null). Well if you hold down "a" long enough, but luck eventually the test will succeed. That's why it is choppy.

Mac

Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:
EDITED TO ADD: Something keeps screwing my code up, I'll try again... It seems to be something with the greater-than/ less-than signs, I'll use equals instead.

Here is how I would do what you are doing:

Code: Select all

``````SCREEN 13
CLS
newx = 160
newy = 100
DO
x = newx
y = newy
LINE (0, y)-(320, y), 2
LINE (x, 0)-(x, 200), 2
DO
k\$=INKEY\$
LOOP WHILE k\$ = ""
IF k\$ = "s" THEN
newy = newy + 1
IF newy = 200 THEN newy =  0
END IF
IF k\$ = "w" THEN
newy = newy - 1
IF newy = -1 THEN newy =  199
END IF
IF k\$ = "d" THEN
newx = newx + 1
IF newx = 320 THEN newx =  0
END IF
IF k\$ = "a" THEN
newx = newx - 1
IF newx = -1 THEN newx = 319
END IF
LINE (0, y)-(320, y), 0
LINE (x, 0)-(x, 200), 0
LOOP UNTIL k\$ = "q"
END
``````
Properly structured code shouldn't need line numbers/labels or GOTOs. It should also only list something once, i.e. if you draw the crosshairs in onle place in the code, you shouldn't need to use those same statements in another spot.

Study the code above. The best way to learn programming is to study other people's code and figure out why they did what they did. I hope this helps!
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm
Theophage wrote:Something keeps screwing my code up,
Yeah, I just tried to post

Code: Select all

``````if a < b then
if b > a then
test``````
As a result, I got

Code: Select all

``````if a a then
test``````
And everything except "if a" was in bold.

Since I had HTML enabled (I wish the default was "no") the system took "< b" to mean "make everything bold"

So the trick is to always check the box to disable HTML. (Check at left when posting to see if it is enabled or not)

Anyway, good advice to the OP re: goto, etc. It's best to learn how to code better.

I took it a step further here

Code: Select all

``````CLS
x = 160: y = 100 ' Initial coordinates
DO
LINE (0, y)-(320, y), 2
LINE (x, 0)-(x, 200), 2
DO: k\$ = LCASE\$(INKEY\$): LOOP WHILE k\$ = ""
newx = x: newy = y ' Save current coordinates
SELECT CASE k\$
CASE "s": IF y = 199 THEN newy = 0 ELSE newy = y + 1
CASE "w": IF y = 0 THEN newy = 199 ELSE newy = y - 1
CASE "d": IF x = 319 THEN newx = 0 ELSE newx = x + 1
CASE "a": IF x = 0 THEN newx = 319 ELSE newx = x - 1
CASE "q": EXIT DO
END SELECT
LINE (0, y)-(320, y), 0
LINE (x, 0)-(x, 200), 0
x = newx: y = newy
LOOP
CLS
SYSTEM``````
and as I prefer arrow keys to move stuff and ESC to get out of programs rather than "Q",

Code: Select all

``````SCREEN 13
CLS
x = 160: y = 100 ' Initial coordinates
DO
LINE (0, y)-(320, y), 2
LINE (x, 0)-(x, 200), 2
DO: k\$ = INKEY\$: LOOP WHILE k\$ = ""
IF k\$ = CHR\$(27) THEN EXIT DO
IF LEN(k\$) = 2 THEN
newx = x: newy = y ' Save current coordinates
SELECT CASE RIGHT\$(k\$, 1)
CASE "P": IF y = 199 THEN newy = 0 ELSE newy = y + 1
CASE "H": IF y = 0 THEN newy = 199 ELSE newy = y - 1
CASE "M": IF x = 319 THEN newx = 0 ELSE newx = x + 1
CASE "K": IF x = 0 THEN newx = 319 ELSE newx = x - 1
END SELECT
END IF
LINE (0, y)-(320, y), 0
LINE (x, 0)-(x, 200), 0
x = newx: y = newy
LOOP
CLS
SYSTEM``````
Mac

Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:
You could use a flag to see if coords have changed and redraw the crosshairs only when they move...

davinci28
Newbie
Posts: 3
Joined: Tue Sep 18, 2007 9:28 pm
Thanks a ton guys!
Theophage, yours works so well! Plus, I understand it.
Thanks again!