Help with masking and code layout. Please and thanks.

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

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Tue Jul 22, 2008 11:39 am

Keep READ. Restore just goes back (or forward if you want).
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Super Mario
Coder
Posts: 21
Joined: Mon Jul 21, 2008 8:34 am
Location: My computer

Post by Super Mario » Tue Jul 22, 2008 11:42 am

Wait...what? Sorry, lost ya there.

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Tue Jul 22, 2008 3:23 pm

Alright, so you have:

Code: Select all

'do stuff with an x
FOR i% = 1 TO 25
    READ p%
    'blah blah blah
NEXT i%

'do stuff with an O
FOR i% = 1 TO 25
    READ p%
    'blah blah blah
NEXT i%

'an x
DATA 1, 0, 0, 0, 1
DATA 0, 1, 0, 1, 0
DATA 0, 0, 1, 0, 0
DATA 0, 1, 0, 1, 0
DATA 1, 0, 0, 0, 1

'an o
DATA 0, 1, 1, 1, 0
DATA 1, 0, 0, 0, 1
DATA 1, 0, 0, 0, 1
DATA 1, 0, 0, 0, 1
DATA 0, 1, 1, 1, 0
Well, lets say you may want to reread x in the future.

Code: Select all

'do stuff with an x
FOR i% = 1 TO 25
    READ p%
    'blah blah blah
NEXT i%

'do stuff with an O
FOR i% = 1 TO 25
    READ p%
    'blah blah blah
next i%

'tell the computer to go back to labelX
RESTORE labelX

'do stuff with an x
FOR i% = 1 TO 25
    READ p%
    'blah blah blah
NEXT i%

'an x
labelX:
DATA 1, 0, 0, 0, 1
DATA 0, 1, 0, 1, 0
DATA 0, 0, 1, 0, 0
DATA 0, 1, 0, 1, 0
DATA 1, 0, 0, 0, 1

'an o
DATA 0, 1, 1, 1, 0
DATA 1, 0, 0, 0, 1
DATA 1, 0, 0, 0, 1
DATA 1, 0, 0, 0, 1
DATA 0, 1, 1, 1, 0
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Super Mario
Coder
Posts: 21
Joined: Mon Jul 21, 2008 8:34 am
Location: My computer

Post by Super Mario » Tue Jul 22, 2008 4:44 pm

Okay. Here is the program in its entirety. Two problems I have.

1. Why does all of the land change to water when I go to move. I can't seem to find the problem.

2. Is there a way to eliminate the flicker when I move?

Code: Select all

DIM grass(10, 10), path(10, 10), water(10, 10), tent(10, 10), scout(10, 10), scoutmask(10, 10)

SCREEN 13

scoutx = 5
scouty = 1

FOR y = 1 TO 10
FOR x = 1 TO 10
READ clr
IF clr = 1 THEN PSET (x, y), 10
IF clr = 0 THEN PSET (x, y), 2
NEXT
NEXT

GET (1, 1)-(10, 10), grass

FOR y = 1 TO 10
FOR x = 1 TO 10
READ clr
IF clr = 0 THEN PSET (x, y), 6
IF clr = 1 THEN PSET (x, y), 14
NEXT
NEXT

GET (1, 1)-(10, 10), path

FOR y = 1 TO 10
FOR x = 1 TO 10
READ clr
IF clr = 0 THEN PSET (x, y), 3
IF clr = 1 THEN PSET (x, y), 11
NEXT
NEXT

GET (1, 1)-(10, 10), water

FOR y = 1 TO 10
FOR x = 1 TO 10
READ clr
IF clr = 0 THEN PSET (x, y), 6
IF clr = 1 THEN PSET (x, y), 8
IF clr = 2 THEN PSET (x, y), 0
NEXT
NEXT

GET (1, 1)-(10, 10), tent

FOR y = 1 TO 10
FOR x = 1 TO 10
READ clr
IF clr = 0 THEN PSET (x, y), 0
IF clr = 1 THEN PSET (x, y), 2
IF clr = 2 THEN PSET (x, y), 8
IF clr = 3 THEN PSET (x, y), 14
IF clr = 4 THEN PSET (x, y), 4
NEXT
NEXT

GET (1, 1)-(10, 10), scout

FOR y = 1 TO 10
FOR x = 1 TO 10
READ clr
IF clr = 0 THEN PSET (x, y), 255
IF clr = 1 THEN PSET (x, y), 2
IF clr = 2 THEN PSET (x, y), 8
IF clr = 3 THEN PSET (x, y), 14
IF clr = 4 THEN PSET (x, y), 4
NEXT
NEXT

GET (1, 1)-(10, 10), scoutmask

CLS
FOR y = 1 TO 15
FOR x = 1 TO 30
READ tilenumber

IF tilenumber = 1 THEN PUT (x * 10, y * 10), grass, PSET
IF tilenumber = 2 THEN PUT (x * 10, y * 10), path, PSET
IF tilenumber = 3 THEN PUT (x * 10, y * 10), water, PSET
IF tilenumber = 4 THEN PUT (x * 10, y * 10), tent, PSET
                                           
NEXT
NEXT

DO
p$ = INKEY$
oldscoutx = scoutx
oldscouty = scouty

PUT (oldscoutx * 10, oldscouty * 10), scoutmask, AND
PUT (oldscoutx * 10, oldscouty * 10), scout, OR

IF p$ <> "" THEN

WAIT &H3DA, 8

IF p$ = "w" AND oldscouty > 1 THEN scouty = oldscouty - 1
IF p$ = "s" AND oldscouty < 15 THEN scouty = oldscouty + 1
IF p$ = "a" AND oldscoutx > 1 THEN scoutx = oldscoutx - 1
IF p$ = "d" AND oldscoutx < 30 THEN scoutx = oldscoutx + 1

CLS

RESTORE tilenumber
FOR y = 1 TO 15
FOR x = 1 TO 30


IF tilenumber = 1 THEN PUT (x * 10, y * 10), grass, PSET
IF tilenumber = 2 THEN PUT (x * 10, y * 10), path, PSET
IF tilenumber = 3 THEN PUT (x * 10, y * 10), water, PSET
IF tilenumber = 4 THEN PUT (x * 10, y * 10), tent, PSET
                                          
NEXT
NEXT

END IF

LOOP UNTIL p$ = CHR$(27)

grass:
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,1,0,0,0,0,0,0,0
DATA 0,0,0,0,0,1,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,1,0,0,0
DATA 0,1,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,1,0,0
DATA 0,0,0,0,0,0,0,0,0,0

path:
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,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,1,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3

User avatar
burger2227
Veteran
Posts: 2345
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Tue Jul 22, 2008 6:10 pm

Have fun!

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

User avatar
Super Mario
Coder
Posts: 21
Joined: Mon Jul 21, 2008 8:34 am
Location: My computer

Post by Super Mario » Tue Jul 22, 2008 6:30 pm

...What?

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Tue Jul 22, 2008 6:31 pm

burger2227 wrote:Have fun!

Ted
I think I'm going through pointer withdraw. I'll take any ptr.

Still working on the code. I don't know what I did, but it stopped putting water over everything.
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Tue Jul 22, 2008 7:51 pm

Got it to work. I had to cheat a little, since -lang QB is more FBish with it's graphics and was giving me trouble with page flipping. So I used screenLock and screenUnlock.

A couple things to keep in mind:
-Draw sprites after tiles, otherwise the tile will be over the sprite
-Indentations help make code clearer
-Load the map data into an array, instead of restoring it. Why? Because you can use it to make the character interact with the map. Such as not going over water.
-Green pants are hard to see on grass 8)

Code: Select all

SCREEN 13

'graphics
DIM grass(10, 10), path(10, 10), water(10, 10) 
dim tent(10, 10), scout(10, 10), scoutmask(10, 10)
dim map(15, 30)

'initialize variables
dim scoutX
dim scouty
dim clr
dim x
dim y
dim key
dim tile

'player coordinates
scoutX = 5
scoutY = 1

'load grass tile
FOR y = 1 TO 10
    FOR x = 1 TO 10
        READ clr
        IF clr = 1 THEN PSET (x, y), 10
        IF clr = 0 THEN PSET (x, y), 2
    NEXT
NEXT
GET (1, 1)-(10, 10), grass

'load path tile
FOR y = 1 TO 10
    FOR x = 1 TO 10
        READ clr
        IF clr = 0 THEN PSET (x, y), 6
        IF clr = 1 THEN PSET (x, y), 14
    NEXT x
NEXT y
GET (1, 1)-(10, 10), path

'load water tile
FOR y = 1 TO 10
    FOR x = 1 TO 10
        READ clr
        IF clr = 0 THEN PSET (x, y), 3
        IF clr = 1 THEN PSET (x, y), 11
    NEXT x
NEXT y
GET (1, 1)-(10, 10), water

'load tent tile
FOR y = 1 TO 10
    FOR x = 1 TO 10
        READ clr
        IF clr = 0 THEN PSET (x, y), 6
        IF clr = 1 THEN PSET (x, y), 8
        IF clr = 2 THEN PSET (x, y), 0
    NEXT x
NEXT y
GET (1, 1)-(10, 10), tent

'sprite
FOR y = 1 TO 10
    FOR x = 1 TO 10
        READ clr
        IF clr = 0 THEN PSET (x, y), 0
        IF clr = 1 THEN PSET (x, y), 2
        IF clr = 2 THEN PSET (x, y), 8
        IF clr = 3 THEN PSET (x, y), 14
        IF clr = 4 THEN PSET (x, y), 4
    NEXT x
NEXT y
GET (1, 1)-(10, 10), scout

'load mask
FOR y = 1 TO 10
    FOR x = 1 TO 10
        READ clr
        IF clr = 0 THEN PSET (x, y), 255
        IF clr = 1 THEN PSET (x, y), 2
        IF clr = 2 THEN PSET (x, y), 8
        IF clr = 3 THEN PSET (x, y), 14
        IF clr = 4 THEN PSET (x, y), 4
    NEXT x
NEXT y
GET (1, 1)-(10, 10), scoutmask

'load map
FOR y = 1 TO 15
    FOR x = 1 TO 30

    read tile
    map(y, x) = tile 
    
    NEXT x
NEXT y
    
DO
    'wait &h3DA, &h8
    screenunlock
    screenlock
    cls
    
    'keyboard input
    key$ = INKEY$

    IF key$ = chr(255) + chr(72) THEN scouty = scouty - 1
    IF key$ = chr(255) + chr(80) THEN scouty = scouty + 1
    IF key$ = chr(255) + chr(75) THEN scoutx = scoutx - 1
    IF key$ = chr(255) + chr(77) THEN scoutx = scoutx + 1
    
    FOR y = 1 TO 15
        FOR x = 1 TO 30
            
        IF map(y, x) = 1 THEN PUT (x * 10, y * 10), grass, PSET
        IF map(y, x) = 2 THEN PUT (x * 10, y * 10), path, PSET
        IF map(y, x) = 3 THEN PUT (x * 10, y * 10), water, PSET
        IF map(y, x) = 4 THEN PUT (x * 10, y * 10), tent, PSET
                                         
        NEXT x
    NEXT y
    
    PUT (scoutX * 10, scoutY * 10), scoutmask, AND
    PUT (scoutX * 10, scoutY * 10), scout, OR
LOOP UNTIL key$ = CHR$(27)

grass:
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,1,0,0,0,0,0,0,0
DATA 0,0,0,0,0,1,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,1,0,0,0
DATA 0,1,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,1,0,0
DATA 0,0,0,0,0,0,0,0,0,0

path:
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0

water:
DATA 0,0,0,0,0,0,0,0,0,0
DATA 1,1,1,1,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,1,1,1,1,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 1,1,1,1,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,1,1,1,1,0
DATA 0,0,0,0,0,0,0,0,0,0
DATA 1,1,1,1,0,0,0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,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,2,4,1,1,1,1,1,1,1,1,3,3,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,3,3,3,3
DATA 1,1,1,1,2,2,1,1,1,4,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,3,3,3,3
DATA 1,1,1,1,2,2,1,1,1,2,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,3,3,3,3
DATA 1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,2,1,1,1,1,3,3,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,4,1,1,1,1,1,1,1,1,1,4,1,1,1,1,3,3,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3
DATA 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,3,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3
DATA 1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3 
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Super Mario
Coder
Posts: 21
Joined: Mon Jul 21, 2008 8:34 am
Location: My computer

Post by Super Mario » Wed Jul 23, 2008 8:51 am

Okay. I think I can take it from here. Thanks for all your help. I really appreciate it. :D

Post Reply