moving a number around on a bitmap

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

Post Reply
intellisquid
Newbie
Posts: 3
Joined: Fri Aug 18, 2006 11:59 am

moving a number around on a bitmap

Post by intellisquid »

Hi all. I'm trying to make a turn-based, puzzle style game that runs on a tile-based map. The player is a bit on a map, just like the walls and objects are. My code isn't working, though.

The 'up' and 'down' code follows. Up works, but down does not. They look exactly the same to me, so if anyone can tell me why down isn't working, I'd greatly appreciate it. I've run into the same problem when trying to use the 'right' key, too. I know I'm missing something really simple. :shock:

Map is a 20x12 array. I'll gladly post more code if needed. thanks in advance!

Code: Select all

IF k$ = "u" THEN
 FOR y = 0 TO 11
 FOR x = 0 TO 19
 IF map(x, y) = 2 THEN
  map(x, y) = 0
  map(x, y - 1) = 2
 END IF
 NEXT x
 NEXT y
END IF

IF k$ = "d" THEN
 FOR y = 0 TO 11
 FOR x = 0 TO 19
 IF map(x, y) = 2 THEN
  map(x, y) = 0
  map(x, y + 1) = 2
 END IF
 NEXT x
 NEXT y
END IF
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Re: moving a number around on a bitmap

Post by Stoves »

In the k$ = "d" code, change FOR y = 0 TO 11 to FOR y = 11 TO 0 STEP -1

In the k$ = "r" code, change FOR x = 0 TO 19 to FOR x = 19 TO 0 STEP -1

The reason for your problem on moving down and right is that you end up changing bits before you get a chance to scan them.

BTW, if you'd like to use arrow keys instead of letters, check out the following:

change "u" to CHR$(0) + "H"
change "d" to CHR$(0) + "P"
change "l" to CHR$(0) + "K"
change "r" to CHR$(0) + "M"

NOTE: To use the arrow keys, you must be using the INKEY$ function instead of the INPUT or INPUT$(1) command to check input.
(so precede checking the value of k$ with a line like "k$ = INKEY$")

Let me know if any of this doesn't make sense. I'm at work, so posting quickly.
intellisquid
Newbie
Posts: 3
Joined: Fri Aug 18, 2006 11:59 am

Post by intellisquid »

I think it makes perfect sense, finally. I had to play around with it for awhile, but eventually I realized changing a bit on x+1 or y+1 means the for/next loop is going to keep changing the bit until it pushes it out of the bounds of the array, because it has yet to scan x+1 or y+1...
Anyway, thank you so much. :D Have a good one.
intellisquid
Newbie
Posts: 3
Joined: Fri Aug 18, 2006 11:59 am

Post by intellisquid »

This is probably common knowledge, but I also figured out a shrewd little shortcut, of adding x+1 or y+1 by itself to 'trick' the for/next loop into skipping the bit the player has just moved into. :lol:
Post Reply