HomeRobber

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

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

HomeRobber

Post by izidor »

I am creating game engine. And I'm kinda stuck.
How to make character move on DATA based map.

Code: Select all

DIM wall(10, 10), floor(10, 10)
DIM map(20, 19)

screen 13

for y = 1 to 10
for x = 1 to 10
read clr
pset (x, y), clr
NEXT: NEXT
GET (1, 1)-(10, 10),wall

for y = 1 to 10
for x = 1 to 10
read clr
pset (x, y), clr
NEXT: NEXT
GET (1, 1)-(10, 10),floor


CLS
for y = 1 to 20
for x = 1 to 20
read map(x, y)
NEXT:NEXT

FOR y = 1 TO 19
FOR x = 1 TO 20
IF map(x, y) = 1 THEN PUT (x * 10 - 10, y * 10 - 10), wall
IF map(x, y) = 2 THEN PUT (x * 10 - 10, y * 10 - 10), floor
NEXT
NEXT

'wall
0,8,8,8,8,8,8,8,8,0
8,7,7,8,7,7,7,7,7,8
8,8,8,8,8,8,8,8,8,8
8,7,7,7,7,7,8,7,7,8
8,8,8,8,8,8,8,8,8,8
8,7,7,8,7,7,7,7,7,8
8,8,8,8,8,8,8,8,8,8
8,7,7,8,7,7,8,7,7,8
8,7,7,8,7,7,8,7,7,8
0,8,8,8,8,8,8,8,8,0

'floor
6,6,6,6,6,6,6,6,6,6
0,0,0,0,0,0,0,0,0,0
6,6,6,6,6,6,6,6,6,6
0,0,0,0,0,0,0,0,0,0
6,6,6,6,6,6,6,6,6,6
0,0,0,0,0,0,0,0,0,0
6,6,6,6,6,6,6,6,6,6
0,0,0,0,0,0,0,0,0,0
6,6,6,6,6,6,6,6,6,6
0,0,0,0,0,0,0,0,0,0

'map
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,1,2,1,2,2,1,2,2,2,2,2,2,2,2,1
1,2,2,2,1,2,2,1,2,2,1,2,2,2,2,2,2,2,2,1
1,2,2,1,2,2,1,1,2,2,1,2,2,2,2,2,2,2,2,1
1,2,1,2,2,2,1,2,2,2,1,2,2,2,2,2,2,2,2,1
1,1,2,2,2,1,1,2,1,2,1,2,2,2,2,2,2,2,2,1
1,2,2,1,1,1,2,2,1,2,1,1,1,1,1,1,2,1,1,1
1,2,2,1,2,2,1,1,1,2,1,2,2,2,2,2,2,2,2,1
1,2,2,1,1,1,1,2,2,2,1,2,2,2,2,2,2,2,2,1
1,2,2,1,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,1
1,2,2,1,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,1
1,2,2,1,2,1,1,1,1,1,1,1,2,1,1,1,2,1,1,1
1,2,2,2,2,1,2,2,2,2,2,1,2,1,2,2,2,1,1,1
1,2,2,2,2,1,2,2,2,2,2,1,1,1,2,2,2,1,1,1
1,1,1,1,1,1,2,1,1,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,1
1,3,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
ps
im sorry for noob questions, but im noob in programing
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

You can use the arrow keys in an INKEY$ loop. Is that what you want?

Do you need to move diagonally?
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Yes, I want to use INKEY$ but I dont know how to "tell" qbasic to move my character.
Harry Potter
Veteran
Posts: 111
Joined: Sat Feb 21, 2009 8:19 am
Location: New York, U.S.

Post by Harry Potter »

Try one of two things:

1. If you're using PUT with the XOR method to paste an image to the screen, write the image to the screen once. When you need to move it, write again in the original position, then write to the new position.

2. If you are using the PSET method or ANDing a mask to the screen then ORing the image to the screen, first read the background with GET. When you need to move, write back the background, then write the image to the new location.
Joseph Rose, a.k.a. Harry Potter
Creating magic in the computer community...or at least striving to! :(
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

The code below is for a text game, but can be adapted for graphical coordinate moves also. It cannot move diagonally easily. The Matrix array used is to determine if there is a wall obstacle taken from a DATA map of paths. Moves are made by changing rows = r% or columns = c%.

Code: Select all

r% = 2: c% = 2
COLOR 14: LOCATE r%, c%: PRINT CHR$(1); 'start position
DO
  DO: SLEEP: arrow$ = INKEY$: LOOP UNTIL arrow$ <> "" 'wait for a keypress
SELECT CASE arrow$
  CASE CHR$(0) + CHR$(72):  'UP
  IF Matrix(r% - 1, c%) = 0 THEN LOCATE r%, c%: PRINT SPACE$(1): r% = r% - 1
  CASE CHR$(0) + CHR$(75):  'LEFT
  IF Matrix(r%, c% - 1) = 0 THEN LOCATE r%, c%: PRINT SPACE$(1): c% = c% - 1
  CASE CHR$(0) + CHR$(77): 'RIGHT
  IF Matrix(r%, c% + 1) = 0 THEN LOCATE r%, c%: PRINT SPACE$(1): c% = c% + 1
  CASE CHR$(0) + CHR$(80): 'DOWN
  IF Matrix(r% + 1, c%) = 0 THEN LOCATE r%, c%: PRINT SPACE$(1): r% = r% + 1
END SELECT
COLOR 14: LOCATE r%, c%: PRINT CHR$(1);  'display smiley character
LOOP UNTIL arrow$ = CHR$(27) 'escape key exit
For diagonal moves, you need to use the keyboard Scan Codes using INP(&H60).

Ted
Last edited by burger2227 on Tue Apr 28, 2009 1:19 pm, edited 3 times in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Also XOR is the default PUT mode when you use no mode. If you PUT an image twice it will be erased the second time by XOR on a black background.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Sorry but that doesn't mean anything in the world to me. Sorry for being rude. Is it possible to make my character design in DATA statement like this:

Code: Select all

DATA 6,6,0,0,0,0,0,0,6,6
DATA 0,0,0,7,0,0,7,0,0,0
DATA 6,6,0,0,0,0,0,0,6,6
DATA 0,0,0,12,12,12,12,0,0,0
DATA 6,6,2,2,2,2,2,2,6,6
DATA 1,1,2,2,2,2,2,2,1,1
DATA 1,1,2,2,2,2,2,2,1,1
DATA 0,0,14,14,14,14,14,0,0,0
DATA 6,6,14,14,6,14,14,6,6,6
DATA 0,0,14,14,0,14,14,0,0,0
and make him move. The example you gave me isn't that clear to me.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Sure you can. I assume the DATA values are color numbers for PSET.

Draw it once and GET the square area. Then you can use the INKEY$ moves to PUT the image at another position. PUT only needs the top left corner coordinate position.

You MUST keep the sprite in the screen area! So set limits for the screen mode you are using. Screen 12 (640 X 480) limits are columns 0 to 630, rows 0 to 470 using a 10 X 10 sprite. Screen 13 (320 X 200) is 310 X 190. Set the coordinate limits in the INKEY$ arrow press loop I showed above.

In graphics you will want to move more than one coordinate also or it will move too slowly.
Last edited by burger2227 on Thu Apr 30, 2009 4:30 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Huh, again me

Code: Select all

screen 13

DIM wall(10, 10), floor(10, 10), door(10, 10), player(10, 10)
DIM map(20, 19)



for y = 1 to 10
for x = 1 to 10
read clr
pset (x, y), clr
NEXT: NEXT
GET (1, 1)-(10, 10),wall

for y = 1 to 10
for x = 1 to 10
read clr
pset (x, y), clr
NEXT: NEXT
GET (1, 1)-(10, 10),floor

for y = 1 to 10
for x = 1 to 10
read clr
pset (x, y), clr
NEXT: NEXT
GET (1, 1)-(10, 10),door

for y = 1 to 10
for x = 1 to 10
read clr
pset (x, y), clr
NEXT: NEXT
GET (1, 1)-(10, 10),player


CLS
for y = 1 to 19
for x = 1 to 20
read map(x, y)
NEXT:NEXT

FOR y = 1 TO 19
FOR x = 1 TO 20
IF map(x, y) = 1 THEN PUT (x * 10 - 10, y * 10 - 10), wall
IF map(x, y) = 2 THEN PUT (x * 10 - 10, y * 10 - 10), floor
IF map(x, y) = 3 THEN PUT (x * 10 - 10, y * 10 - 10), door
IF map(x, y) = 4 THEN PUT (x * 10 - 10, y * 10 - 10), player

NEXT
NEXT

'wall
DATA 0,8,8,8,8,8,8,8,8,0
DATA 8,7,7,8,7,7,7,7,7,8
DATA 8,8,8,8,8,8,8,8,8,8
DATA 8,7,7,7,7,7,8,7,7,8
DATA 8,8,8,8,8,8,8,8,8,8
DATA 8,7,7,8,7,7,7,7,7,8
DATA 8,8,8,8,8,8,8,8,8,8
DATA 8,7,7,8,7,7,8,7,7,8
DATA 8,7,7,8,7,7,8,7,7,8
DATA 0,8,8,8,8,8,8,8,8,0

'floor
DATA 6,6,6,6,6,6,6,6,6,6
DATA 0,0,0,0,0,0,0,0,0,0
DATA 6,6,6,6,6,6,6,6,6,6
DATA 0,0,0,0,0,0,0,0,0,0
DATA 6,6,6,6,6,6,6,6,6,6
DATA 0,0,0,0,0,0,0,0,0,0
DATA 6,6,6,6,6,6,6,6,6,6
DATA 0,0,0,0,0,0,0,0,0,0
DATA 6,6,6,6,6,6,6,6,6,6
DATA 0,0,0,0,0,0,0,0,0,0

'door
DATA 1,1,1,1,1,1,1,1,1,1
DATA 1,9,9,9,9,9,9,9,9,1
DATA 1,9,9,1,1,1,1,9,9,1
DATA 1,9,1,9,9,9,9,1,9,1
DATA 1,6,6,9,1,1,9,1,9,1
DATA 1,6,6,9,1,1,9,1,9,1
DATA 1,9,1,9,9,9,9,1,9,1
DATA 1,9,9,1,1,1,1,9,9,1
DATA 1,9,9,9,9,9,9,9,9,1
DATA 1,1,1,1,1,1,1,1,1,1

'player
DATA 6,6,0,0,0,0,0,0,6,6
DATA 0,0,0,7,0,0,7,0,0,0
DATA 6,6,0,0,0,0,0,0,6,6
DATA 0,0,0,12,12,12,12,0,0,0
DATA 6,6,2,2,2,2,2,2,6,6
DATA 1,1,2,2,2,2,2,2,1,1
DATA 1,1,2,2,2,2,2,2,1,1
DATA 0,0,14,14,14,14,14,0,0,0
DATA 6,6,14,14,6,14,14,6,6,6
DATA 0,0,14,14,0,14,14,0,0,0


'map
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,2,2,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,1
DATA 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1
DATA 1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
do
k$=INKEY$
x=2
y=18
if k$ = chr$(100) then
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor

x = x + 1
locate x,y
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor
end if

if k$ = chr$(97) then
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor

x = x - 1
locate x,y
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor
end if

if k$ = chr$(119) then
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor

y = y - 1
locate x,y
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor
end if

if k$ = chr$(115) then
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor

y = y + 1
locate x,y
PUT (x * 10 - 10, y * 10 - 10), player
PUT (x * 10 - 10, y * 10 - 10), floor
end if

loop until k$ = chr$(27)

If you compile you will see what is the problem. (use a,w,s,d)
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Save the previous x and y positions to replace the floor over the sprite.
px = x: py = y

Check if you can move to a new position first and only PUT the sprite if you can. Your map array can tell you that.

You should be able to figure out why the sprite is moving back and forth. Try PRINTing your variables somewhere out of the way.

Looks pretty good so far!
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
qbasicfreak
Veteran
Posts: 90
Joined: Wed Oct 22, 2008 3:27 pm
Location: canada

Post by qbasicfreak »

I'd love to see your game when you are done...
Not many people are designing engines now-a-days!
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

@burger2227

I will try it

@qbasicfreak

Then you will be my first tester :)

EDIT:

Huh, can you give me example ?
And i got idea:
If i make counter that will count how many times did i press (for now lets say) left arrow. We will call that counter "a%" and here is the formula:
x = x + a%
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

When you press a key, check your map floor values(2) from array.

Code: Select all

px = x: py = y  'save previous position
IF k$ = chr$(100) THEN
           IF map(x + 1, y) = 2 AND x * 10 < ? THEN 'floor to right maximum(310)
                 x = x +1
                 PUT (px * 10, py * 10), floor, PSET  'erase player
                 PUT (x * 10, y * 10), player, PSET 'place player             
           END IF
END IF
You do not need to use a 4 for the start position of the player. Just set the values of x and y to place it there at start. A multiple of 10 only! PUT cannot go offscreen!

There is no need to count presses as the INKEY$ loop responds to every press. However you cannot read 2 key presses at once.

Ted
Last edited by burger2227 on Thu Apr 30, 2009 2:06 pm, edited 5 times in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Bad news, code isn't working, i don't know why
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

The coordinate values for 10 X 10 PUT boxes can start at 0:

0 to 9, 10 to 19, 20 to 29, 30 to 39, etc. Each has 10 pixel positions each direction.

The right maximum PUT value can be 310 (to 319). x max = 31
The bottom maximum PUT value can be 190 (to 199). y max = 19

The x values can go from 0 to 31. y from 0 to 19. This should match your map data array from (0, 0) to (31, 19) if needed, to see what is placed there. Since your map is smaller than maximums, the wall array values will stop the sprite movements!
Last edited by burger2227 on Thu Apr 30, 2009 4:25 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

If you find some time, can you just type the code that will work with the game.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Remove the starting positions from the loop. That should be done BEFORE the LOOP:

Code: Select all

x=2 
y=18  
PUT (x * 10, y * 10), player  'FORGET THE -10 idea everywhere!
 'delete the floor PUT 
Also remove the first 2 PUTs after the IF K$ = ........THEN code lines. VERIFY a move by looking for value of 2 in the map array using the new value of x, y set by the keypress. If it is not 2 then don't PUT anything and don't change the x, y values!

ADD px = x: py = y at the start of loop and use them instead of x and y in the floor PUTs. The floor PUT erases the old player. USE PSET in your PUT statements because XOR will not work if the background is not black.
Last edited by burger2227 on Thu Apr 30, 2009 4:16 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Anonymous

Post by Anonymous »

Have a look at this code, it is not graphical but uses the same idea.

Code: Select all

'Declare Variables
Dim map(5,5) As Integer, PlayerX As Integer, PlayerY As Integer

Screen 13

'Load Map
For y = 1 To 5
	For x = 1 To 5
		Read map(x,y)
	Next
Next

'Setup Game values
PlayerX = 2
PlayerY = 2

'Game Loop
Do
	'Draw Map
	For x = 1 To 5
		For y = 1 To 5
			Locate y,x
			If map(x,y) = 0 Then Color 7
			If map(x,y) = 1 Then Color 2
			If map(x,y) = 2 Then Color 15
			Print Chr$(219)
		Next
	Next
	
	'Draw Player
	Locate PlayerY,PlayerX
	Color 15
	Print Chr$(2)
	
	'Wait for keyboard input
	Do
		P$ = InKey$
	Loop Until P$ <> ""
	
	'Check input and move player
	If P$ = "w" And map(PlayerX,PlayerY-1) < 2 Then
		PlayerY = PlayerY - 1
	ElseIf P$ = "s" And map(PlayerX,PlayerY+1) < 2 Then
		PlayerY = PlayerY + 1
	ElseIf P$ = "d" And map(PlayerX+1,PlayerY) < 2 Then
		PlayerX = PlayerX + 1
	ElseIf P$ = "a" And map(PlayerX-1,PlayerY) < 2 Then
		PlayerX = PlayerX - 1
	EndIf
	
Loop Until P$ = Chr$(27)

'Map Data
Data 2,2,2,2,2
Data 2,0,0,0,2
Data 2,0,0,1,2
Data 2,0,1,1,2
Data 2,2,2,2,2
also you could do it lke this

Code: Select all

'Declare Variables
Dim map(5,5) As Integer, PlayerX As Integer, PlayerY As Integer, PlayerBG As Integer

Screen 13

'Load Map
For y = 1 To 5
	For x = 1 To 5
		Read map(x,y)
		If map(x,y) = 10 Then
			PlayerX = x
			PlayerY = y
		EndIf
	Next
Next

'Setup Game values
PlayerBG = 0

'Game Loop
Do
	'Draw Map
	For x = 1 To 5
		For y = 1 To 5
			Locate y,x
			If map(x,y) = 0 Then Color 7
			If map(x,y) = 1 Then Color 2
			If map(x,y) = 2 Then Color 15
			Print Chr$(219)
		Next
	Next
	
	'Draw Player
	Locate PlayerY,PlayerX
	Color 15
	Print Chr$(2)
	
	'Wait for keyboard input
	Do
		P$ = InKey$
	Loop Until P$ <> ""
	
	'Change player pos back to orginal data value
	map(PlayerX,PlayerY) = PlayerBG
	
	'Check input and move player
	If P$ = "w" And map(PlayerX,PlayerY-1) < 2 Then
		PlayerY = PlayerY - 1
	ElseIf P$ = "s" And map(PlayerX,PlayerY+1) < 2 Then
		PlayerY = PlayerY + 1
	ElseIf P$ = "d" And map(PlayerX+1,PlayerY) < 2 Then
		PlayerX = PlayerX + 1
	ElseIf P$ = "a" And map(PlayerX-1,PlayerY) < 2 Then
		PlayerX = PlayerX - 1
	EndIf
	
	'Get the background data value
	PlayerBG = map(PlayerX,PlayerY)
	'Set player on map
	map(PlayerX,PlayerY) = 10
	
Loop Until P$ = Chr$(27)

'Map Data
Data 2,2,2,2,2
Data 2,10,0,0,2
Data 2,0,0,1,2
Data 2,0,1,1,2
Data 2,2,2,2,2
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

@burger2227

Yeah it work, but (why is there always but? :( ) it doesn't delete previous pic and it creates one bore on left or right side.

@Nixon

Thanks, maybe i will use it :D
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Try this AFTER a keypress move has been approved by the map array and x or y is changed. Don't forget to save the previous values of PX = X and PY = Y before you check the keypresses.

Code: Select all

IF X <> PX OR Y <> PY THEN  'look for a changed coordinate value ONLY
     PUT (PX * 10, PY * 10), floor, PSET  'replace player with floor first
     PUT (X * 10, Y * 10), player, PSET   'PUT player at new position
END IF
These 2 PUTs are ALL you need in the INKEY$ loop!

Remember that x and y values match the map array indices. Multiplying them by 10 matches the grahic screen PUT coordinates!

I gather that PX and PY eventually equaled X and Y if a move was not approved in your code.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply