Author Topic: Trying to clean up some code  (Read 155 times)

Trying to clean up some code
« on: June 13, 2018, 10:16:48 PM »
Hey all. Been a long time since I’ve posted on the .net site. In my spare time, I’ve been programming a simple Final Fantasy type rpg. I’d like to know if there is a way I’m overlooking to consolidate some code. This particular snip of code is for drawing a player character on the screen at a specific location (tile). The screen consists of 84 tiles. The location of the tile that the character will be occupying is stored as a variable named “location” . The graphic of the character is stored as a variable named “player”. The graphic can change depending on which character you want to see on the map. It can be changed at will depending on who you want to see on the map, (wizard, warrior, thief, etc). My current code works perfectly but I am trying to slim down.

Select case location  ‘selects tile to draw a graphic in
          Case 1  ‘tile 1
          Select case player
                    Case 1
                    _putimage (2,2), warrior&  ‘draws warrior graphic in tile 1
                    Case 2
                    _putimage (2,2), wizard& ‘draws wizard graphic in tile 1
                   Case 3
                    _putimage (2,2), thief& ‘draws thief graphic in tile 2
          End select
          Case 2 ‘tile 2
          Select case player
                    Case 1
                    _putimage (80,2), warrior&  ‘draws warrior graphic in tile 2
                    Case 2
                    _putimage (80,2), wizard& ‘draws wizard graphic in tile 2
                    Case 3
                    _putimage (80,2), thief& ‘draws thief graphic in tile 2
          End select
End select


This repeats 82 more times until all tiles are included. I plan on expanding the graphic set for the player to utilize, but as you can see, each new graphic will add 84 lines of code for telling the program where to draw it. Is there a way to clean this up? Can Qb64 rename files? For example, if I want to display the wizard graphic, can it rename the wizard graphic file to a generic and use _putimage generic&? Then If I want the warrior graphic later, it will rename the wizard graphic file back to wizard and then label the warrior graphic file generic? I’m out of ideas
Thank you kindly 😃

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Trying to clean up some code
« Reply #1 on: June 14, 2018, 12:57:19 AM »
You need two things to reduce this drastically: arrays and loops. First for your characters:

Code: [Select]
DIM character(1 to 3) AS LONG
character(1) = _loadimage("warriorPic.png", 32)
character(2) = _loadimage("wizardPic.png", 32)
character(3) = _loadimage("thiefPic.png", 32)

Then, when you need, you just go _PUTIMAGE (xCoordinate, yCoordinate), character(player) - much less work already.

Now for your tile coordinates, you'd be good with something like:

Code: [Select]
TYPE TILE
    x as integer
    y as integer
END TYPE

DIM tile(1 to 84) AS TILE
tile(1).x = 2: tile(1).y = 2
tile(2).x = 80: tile(2).y = 2
'etc...

With the data structured like this you actually don't even need a loop. All you'd need is one line:
Code: [Select]
_PUTIMAGE (tile(location).x, tile(location).y), character(player)
Bam. Hope it's what you were after.

Welcome to the forum!

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Trying to clean up some code
« Reply #2 on: June 14, 2018, 01:13:00 AM »
To go even deeper down the structuring rabbit hole, you'd have the tile coordinates in a DATA set:

Code: [Select]
TYPE TILE
    x as integer
    y as integer
END TYPE

DIM tile(1 to 84) AS TILE
RESTORE tileData
FOR t = 1 TO 84
    READ tile(t).x, tile(t).y
NEXT

tileData:
DATA 2, 2, 80, 2
'etc...

Re: Trying to clean up some code
« Reply #3 on: June 14, 2018, 10:40:40 AM »
Hi. Fellippe,

 It's not long before I read in the forum that for every code that is often repeated is a good field. So instead of the SELECT CASE Player, type IF TILE (search) = PLAYER (i) THEN ... and put into that loop just that I = I + 1: IF I> 3 THEN I = 0. Is on this something wrong? Just add next index to TILE TYPE for player identity number.
Coding is relax (At least sometimes)

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Trying to clean up some code
« Reply #4 on: June 14, 2018, 10:45:27 AM »
Hi, Petr, I don't think I understand your point.

Re: Trying to clean up some code
« Reply #5 on: June 14, 2018, 11:08:57 AM »
Hello. It's the answer to your question yesterday. But under that, you've come up with some solutions.

When I wrote the game where my game area is divided into sectors (something I have written, but it's in diapers), I assigned a flag to each sector. Flag 1 = Wall, Flag 2 = Water, Flag 3 = Road. This is one element in TYPE. Another is .X and .Y, they indicate the location of this sector on the map, and I suggested for your question in the first post to add a character flag to this TYPE. Then once through the loop you will pass through all the sectors and see what character is in which sector. Here is a pseudo code:

TYPE AREA
X as an integer  -  place on X
Y as an integer   - place on y
flag as _byte      - this is for the sector type (water, wall, forest ...)
character as _byte - this is for the character number (1 = hero, 2 = warrior...
END TYPE

then one loop you will find everything you need. For moving then use X and Y coordinates (if hero(index).X => AREA(index).X and hero(index).X <= AREA(index).X + AREA WIDTH and the same conditions for Y then...



And I hope I understand your post correctly.
Coding is relax (At least sometimes)

Re: Trying to clean up some code
« Reply #6 on: June 14, 2018, 11:09:08 AM »
Thanks for your input. I’m self taught so there is a tremendous amount I don’t yet know. I won’t have time to play with the code until the weekend, but I do have another question regarding the images. I have read that whenever you use _loadimage, that you should _putimage and then _freeimage to free it from memory. Before the select case loops, I _loadimage all the graphics I will need, then after the loops, I free them all. Using your example, how and when do I use _freeimage? Or use it at all? If I freeimage in an array, does that mess it up? Thanks again!

Re: Trying to clean up some code
« Reply #7 on: June 14, 2018, 11:22:30 AM »
Hi Feljar.

I'll just tell you how I did it. And I'm not saying it can not be done better. I have a picture of water for water. Each tile with the water flag then displays this image using the putimage (and it still holds the same memory space as one picture for all the tiles, the putimage only inserts the image of the memory into the current screen). If you use small pictures to insert during the game, you can keep them in memory. Another option that Fellippe has shown me is that before you can see the scene by the flags in the box, check the pictures you need and put them in memory. After you leave this area (you control it with X and Y in TYPE), you release it from memory. But it is a small problem, if you have a slow hard disk, it causes a small jerk.
Coding is relax (At least sometimes)

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Trying to clean up some code
« Reply #8 on: June 14, 2018, 11:26:10 AM »
If you're going to use the same images during the whole game, you don't need to free them. Load them once, use them throughout the program as many times as desired. Just be careful not to write the _LOADIMAGE line in a loop.

Re: Trying to clean up some code
« Reply #9 on: June 14, 2018, 11:37:42 AM »
I found my source. It is not playable, but moving show there brown quad (it will be the plane, in time).  Use mouse, look at game area size and memory consumption.

Code: [Select]

TYPE Oblast 'AREA
    X AS _UNSIGNED INTEGER '    pozice X v herni oblasti
    Y AS _UNSIGNED INTEGER '    pozice Y v herni oblasti
    Zivot AS _UNSIGNED _BYTE '  zivot. Nektere subjekty pujdou znicit, zalezi to na kontrolnim podprogramu, pro ktery typ to dovoli
    Typ AS _UNSIGNED _BYTE '    typ. Jestli se jedna o kostku s domem, muze byt pevne stanovena velikost
END TYPE

Prvku = 1000
DIM O(Prvku) AS Oblast
DIM SHARED posX, posY, imX, imY, Pohyb2 ', WaterIMG&
DIM SHARED TreeType(10) AS LONG

WaterIMG& = _NEWIMAGE(151, 151, 32)
tankImg0& = _NEWIMAGE(320, 240, 256)
tankImg1& = _NEWIMAGE(320, 240, 256)
TreeImg0& = _NEWIMAGE(150, 320, 256)

_CLEARCOLOR 0, tankImg0&
_CLEARCOLOR 0, tankImg1&



imX = 1: imY = 1



'aby se vykreslovalo plynule vse, musim stanovit maximalni velikost nejvetsiho objektu v pixelech:
CONST MaxObject = 150
CONST Zelena& = _RGB32(0, 255, 0)
posX = 150
posY = 150
water WaterIMG& 'aqua
tank tankImg0&
tank1 tankImg1&


'umele naplnim pole objektu nahodnymi prvky:

FOR R = 1 TO 1000
    X = X + 150: IF X > 4500 THEN y = y + 150: X = 0
    T = RND * 50
    SELECT CASE T
        CASE 0 TO 10: tp = 0
        CASE 11 TO 20: tp = 1
        CASE 21 TO 25: tp = 2
        CASE 25 TO 30: tp = 3
        CASE 31 TO 40: tp = 3 + (T - 30)
    END SELECT


    O(R).X = X 'INT(RND + 1 * 10000)
    O(R).Zivot = 100
    O(R).Typ = tp 'INT(RND * 5)
    O(R).Y = y 'INT(RND + 1 * 10000)
NEXT R


'necham vygenerovat 10 typu stromu
FOR TreeGen = 1 TO 10
    Tree TreeImg0&
    TreeType&(TreeGen) = _COPYIMAGE(TreeImg0&)
NEXT TreeGen




', rndpoleY

'O(1).X = 1800: O(1).Y = 1800: O(1).Typ = 2

'kurzory pro DRAW
R$ = STR$(_RGB32(255, 255, 255))
Ri$ = STR$(_RGB32(255, 0, 55))
UP$ = "C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
LFT$ = "TA90 C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
DN$ = "TA180 C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
RGHT$ = "TA270 C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
Normal$ = "TA22 C" + Ri$ + " R4 U12 R6 H8 G8 R6 D12  BR1 BU1 P" + R$ + "," + Ri$



_MOUSEHIDE
SCREEN _NEWIMAGE(800, 600, 32): _FULLSCREEN , _SMOOTH
'vlastni vykresleni:
DO WHILE i$ <> CHR$(27)
    i$ = INKEY$
    CLS
    water WaterIMG&
    '    tank tankImg0&
    '   tank1 tankImg1&



    mi& = _MOUSEINPUT
    Rb = _MOUSEBUTTON(2)
    Lb = _MOUSEBUTTON(1)
    mX = _MOUSEX
    mY = _MOUSEY

    '-------------- jeden typ posunu najetim na okraj --------------------
    '   IF TIMER > prodleva AND TIMER > 0 THEN


    ' ----------------- posun stiskem praveho tlacitka ------------------------
    IF Rb THEN
        IF SGN(_MOUSEMOVEMENTX) = 1 THEN posX = posX + 1: prodleva = TIMER + .005
        IF SGN(_MOUSEMOVEMENTY) = 1 THEN posY = posY + 1: prodleva = TIMER + .005
        IF SGN(_MOUSEMOVEMENTX) = -1 THEN posX = posX - 1: prodleva = TIMER + .005
        IF SGN(_MOUSEMOVEMENTY) = -1 THEN posY = posY - 1: prodleva = TIMER + .005
    END IF
    '---------------------------------------------------------------------------

    SELECT CASE mX
        CASE IS < 20: posX = posX - 1: prodleva = TIMER + .01: DRAW LFT$
        CASE IS > _WIDTH - 20: posX = posX + 1: prodleva = TIMER + .01: DRAW RGHT$
    END SELECT
    SELECT CASE mY
        CASE IS < 20: posY = posY - 1: prodleva = TIMER + .01: DRAW UP$
        CASE IS > _HEIGHT - 20: posY = posY + 1: prodleva = TIMER + .01: DRAW DN$
    END SELECT
    '   END IF
    IF posX < 150 THEN posX = 150
    IF posY < 1 THEN posY = 1
    '  END IF



    FOR vykresli = LBOUND(o) TO UBOUND(o)
        IF O(vykresli).X > posX - 800 AND O(vykresli).X < posX + 800 AND O(vykresli).Y > posY - 600 AND O(vykresli).Y < posY + 600 THEN
            SELECT CASE O(vykresli).Typ
                CASE 0: Trava O(vykresli).X - posX, O(vykresli).Y - posY
                CASE 1: _PUTIMAGE (O(vykresli).X - posX, O(vykresli).Y - posY), WaterIMG&, 0, (0, 0)-(150, 150)
                CASE 2: Trava O(vykresli).X - posX, O(vykresli).Y - posY
                    _PUTIMAGE (O(vykresli).X - posX, O(vykresli).Y - posY)-(O(vykresli).X - posX + 80, O(vykresli).Y - posY + 50), tankImg0&, 0, (0, 0)-(319, 199)
                CASE 3: Trava O(vykresli).X - posX, O(vykresli).Y - posY
                    _PUTIMAGE (O(vykresli).X - posX, O(vykresli).Y - posY)-(O(vykresli).X - posX + 80, O(vykresli).Y - posY + 50), tankImg1&, 0, (0, 0)-(319, 199)
                CASE 4 TO 13: Trava O(vykresli).X - posX, O(vykresli).Y - posY
                    _PUTIMAGE (O(vykresli).X - posX, O(vykresli).Y - posY)-(O(vykresli).X - posX + 80, O(vykresli).Y - posY + 50), TreeType&(O(vykresli).Typ - 3), 0, (0, 0)-(240, 320)

            END SELECT

            IF Lb AND posX + mX > O(vykresli).X AND posX + mX < O(vykresli).X + 24 AND posY + mY > O(vykresli).Y AND posY + mY < O(vykresli).Y + 30 THEN
                LOCATE 3, 1
                PRINT O(vykresli).X; O(vykresli).Y; O(vykresli).Zivot; O(vykresli).Typ
                _DISPLAY
                SLEEP 1
                Lb = 0
            END IF

        END IF
    NEXT
    LOCATE 1, 1: PRINT posX, posY, INT(pohybX), INT(pohybY), posX + mX, posY + mY



    pohyb


    LINE (0, 0)-(800, 60), _RGB32(0, 0, 0), BF

    DRAW "BM" + STR$(mX) + "," + STR$(mY)
    IF mX > 20 AND mX < 780 AND mY > 20 AND mY < 570 THEN DRAW Normal$

    SELECT CASE mX
        CASE IS < 20: DRAW LFT$
        CASE IS > _WIDTH - 20: DRAW RGHT$
    END SELECT
    SELECT CASE mY
        CASE IS < 20: DRAW UP$
        CASE IS > _HEIGHT - 30: DRAW DN$
    END SELECT




    _DISPLAY
    _LIMIT 500
LOOP







SUB Trava (x, y) 'zeleny obdelnik
    LINE (x, y)-(x + 150, y + 150), Zelena&, BF

END SUB

SUB water (WaterImg&)
    SHARED sinus, pruchod
    pruchod = pruchod + 1
    IF sinus > 4 * _PI THEN sinus = 0
    IF pruchod MOD 50 = 0 THEN
        pruchod = 0
        _DEST WaterImg&
        CLS , _RGB32(0, 0, 155)
        FOR x = 0 TO 150
            FOR y = -99 TO 199 STEP 8
                sinus = sinus + .011 ' + RND / 25
                PSET (x, y + (SIN(sinus) * 25)), _RGB32(0, 255 * RND, 255)
        NEXT y, x
        IF sinus > 8 * _PI THEN sinus = -sinus
        _DEST 0
    END IF
END SUB

SUB pohyb 'simulace pohybu jednotek - v tomto pripade to bude hnedy obdelnik, simuluje letoun
    '  IF TIMER MOD 2 = 0 THEN
    SHARED pohybX, pohybY




    IF pohybX > 10000 OR pohybX < 0 THEN imX = imX * -1
    pohybMx = RND * imX
    pohybX = pohybX + pohybMx / 10

    IF pohybY > 10000 OR pohybY < 0 THEN imY = imY * -1
    pohybMy = RND * imY
    pohybY = pohybY + pohybMy / 10


    'doplnit podminku, aby vykresleni probehlo pouze pri viditelnosti - zavislost na PosX a PosY OK
    IF posX - 150 < pohybX AND posX + 800 > pohybX AND posY - 150 < pohybY AND posY + 600 > pohybY THEN
        LINE (pohybX - posX, pohybY - posY)-(pohybX - posX + 25, pohybY - posY + 30), _RGB32(116, 61, 0), BF
    END IF
    ' END IF
END SUB

SUB Tree (TreeImg0&) 'generator TREE
    DO WHILE prumer < 22
        prumer = CINT(RND * 40)
    LOOP
    _DEST TreeImg0&
    '    CLS
    LINE (72, 160 - prumer)-(78, 200 + prumer), 187, BF

    FOR koruna = 1 TO 7.28 STEP 0.01
        X = 75 + SIN(koruna) * prumer

        DO WHILE koef < .36 OR koef > .8
            kf = INT(RND * 10)
            koef = kf / 10
        LOOP

        Y = 120 + COS(koruna) * prumer / koef '.5
        IF oldX = 0 THEN oldX = X
        IF oldY = 0 THEN oldY = Y
        LINE (oldX, oldY)-(X, Y), 2
    NEXT koruna

    FOR Yplod = 0 TO 319
        FOR Xplod = 0 TO 149
            _SOURCE TreeImg0&
            IF POINT(Xplod, Yplod) = 2 AND POINT(Xplod + 1, Yplod + 1) = 2 AND POINT(Xplod - 1, Yplod - 1) = 2 AND (RND * 10) > 8 THEN PSET (Xplod, Yplod), RND * G
        NEXT Xplod
        G = G + 20
    NEXT Yplod
    _CLEARCOLOR 0, TreeImg0&
    _DEST 0
END SUB

SUB tank1 (tankImg1&)
    _DEST tankImg1&
    podvozek NOT Pohyb2
    PAINT (60, 70), 19, 15 'vybarveni podvozku
    PAINT (64, 154), 19, 15 'vybarveni podvozku

    OVAL 30, 100, 100, 145, 15
    PAINT (32, 102), 162, 15
    PAINT (44, 102), 164, 15
    PAINT (50, 102), 166, 15
    LINE (40, 96)-(40, 148), 166 'leva cara na tankove vezi
    LINE (45, 96)-(45, 148), 160 ' prava cara na tankove vezi
    PAINT (87, 98), 164, 15
    PAINT (92, 98), 162, 15
    LINE (85, 96)-(85, 148), 160 'leva cara na tankove vezi
    LINE (90, 96)-(90, 148), 166 ' prava cara na tankove vezi
    LINE (40, 95)-(55, 30), 22, BF 'delo
    LINE (90, 95)-(75, 30), 22, BF 'delo 2

    LINE (47, 95)-(47, 30), 15
    LINE (83, 95)-(83, 30), 15

    _DEST 0
END SUB



SUB tank (tankImg0&)
    _DEST tankImg0&
    podvozek Pohyb2
    '------------- NAMALOVANY PODVOZEK S PASAMA ---------------------------

    OVAL 50, 120, 80, 145, 15 'TANKOVA VEZ
    PAINT (52, 122), 166, 15
    CIRCLE (65, 132), 10, 15 'Vrchni poklop
    PAINT (65, 132), 24, 15

    LINE (60, 115)-(70, 40), 22, BF 'delo
    LINE (65, 115)-(65, 40), 20 '"odlesk" v delu / horni cara
    _DEST 0
END SUB

SUB podvozek (pohyb2)

    FOR PASY = 50 + pohyb2 TO 160 STEP 15
        IF PASY MOD 2 = 0 THEN CL = 199 ELSE CL = 240
        LINE (35, PASY + 15)-(45, PASY), CL, BF 'LEVY PAS
        LINE (85, PASY + 15)-(95, PASY), CL, BF 'PRAVY PAS
    NEXT PASY
    IF pohyb2 THEN pohyb2 = 0 ELSE pohyb2 = 1
    OVAL 40, 60, 90, 160, 15
    PAINT (42, 62), 30, 15
    OVAL 45, 65, 85, 155, 15
    PAINT (43, 63), 28, 15

END SUB

SUB OVAL (x1, y1, x2, y2, BARVA)
    IF x1 > x2 THEN SWAP x1, x2
    IF y1 > y2 THEN SWAP y1, y2

    CIRCLE (x1 + 10, y1 + 5), 10, BARVA, 1.5, 3.1 ' levy horni oblouk
    CIRCLE (x2 - 10, y1 + 5), 10, BARVA, 6.2, 1.5 ' pravy horni oblouk
    CIRCLE (x1 + 10, y2 - 5), 10, BARVA, 3, 4.8 ' levy spodni oblouk
    CIRCLE (x2 - 10, y2 - 5), 10, BARVA, 4.7, .2 'pravy spodni roh

    LINE (x1 + 7, y1 - 5)-(x2 - 7, y1 - 5), BARVA 'horni cara
    LINE (x1, y1 + 5)-(x1, y2 - 5), BARVA 'leva strana
    LINE (x1 + 7, y2 + 5)-(x2 - 7, y2 + 5), BARVA 'spodni cara
    LINE (x2, y2 - 5)-(x2, y1 + 5), BARVA 'prava strana

END SUB



Allow me an explanation of why I was here writing to Fellippe instead of Feljar. I just saw that it was the last post from Fellippe - and somehow I mistaken it, I had him as the author of the thread ...
« Last Edit: June 14, 2018, 12:00:22 PM by Petr »
Coding is relax (At least sometimes)

Re: Trying to clean up some code
« Reply #10 on: June 14, 2018, 10:29:42 PM »
I had some time tonight to play around. I need some guidance. I only played with character graphics, no tiles at the moment.

DIM character (1 to 3) AS LONG
character(1) = _LOADIMAGE(“sprites\warrior.png”, 32)
character(2) = _LOADIMAGE(“sprites\wizard.png”, 32)
character(3) = _LOADIMAGE(“sprites\thief.png”, 32)
SCREEN _NEWIMAGE(1350,750,32)

_PUTIMAGE (50,50), character(1)

It worked. It put the warrior graphic on screen. If I put (2) in the bracket, the wizard graphic appears. But this doesn’t really help me unless I’m doing it wrong. I will still need the inner set of Select Case to choose which graphic to display (1), (2), or (3). I was under the impression I could type something like “character =1” or “character =2” and when using _PUTIMAGE (50,50), character, it would put whatever graphic that character variable was currently set at.

Using “_PUTIMAGE (50,50) , character”  yields an illegal function call
Using “_PUTIMAGE (50,50),  character&” yields an illegal function call
 
From experimenting, _PUTIMAGE (50,50), character(1) is the only thing that I can get to work and it only displays what I tell it to display, (1), (2), or (3). What am I doing wrong? Your example seemed simple enough, I have to be missing something 😓

Re: Trying to clean up some code
« Reply #11 on: June 14, 2018, 10:32:28 PM »
Never mind! I found what I was doing wrong. It’s working perfectly now. 👍

I will tackle the tiles another night. Thanks to all for the feedback

Feljar

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Trying to clean up some code
« Reply #12 on: June 14, 2018, 10:52:11 PM »
You had told us you had a variable named “player” that contained 1, 2 or 3 for the character. All you need is to pass character(player) to the _PUTIMAGE statement.

Re: Trying to clean up some code
« Reply #13 on: June 14, 2018, 11:02:31 PM »
Yup totally missed it. I was putting numbers in the brackets. Didn’t occur to me until five seconds after I hit post. 😂

Re: Trying to clean up some code
« Reply #14 on: June 16, 2018, 06:53:34 PM »
Hello again, last question for a while, I promise! I’ve spent a few hours off and on the last two days to get the tile placement correct. Using your example above, I’ve successfully been able to place tiles in the correct coordinates. What I can’t fiqure out, is how to determine what type of tile to place in the coordinates. I have a Read/Data for the tile type that should be placed,  it I can not seem to place it properly in the code so that it works.
 
DIM tiletype(1 to 3) AS LONG ‘tile graphics to draw
tiletype(1) = _LOADIMAGE(“sprites\grass.png”,32)
tiletype(2) = _LOADIMAGE(“sprites\snow.png”,32)
tiletype(3) = _LOADIMAGE(“sprites\mountains.png”,32)

RESTORE mapdata ‘reads 1,2 or 3 to determine what type of tile to place
FOR k = 1 to 84
READ mapdata
Next k


‘This sections decides the location in order to place tiles to create a map
TYPE tiles
x AS INTEGER
y AS INTEGER
END TYPE

DIM tiles(1 to 84) AS tiles ‘reads tile location for placement
RESTORE tiletypedata
FOR t=1 to 84
READ tiles(t).x, tiles(t).y
_PUTIMAGE(tiles(t).x, tiles(t).y), tiletype(mapdata) ‘ mapdata = 1, 2 or 3 as read from the mapdata
NEXT t

mapdata:
DATA 1,1,1,1,2,2,1,2,1,2,1,3,1,1,2 etc... until 84 entries
tiletypedata:
DATA 1,1,81,1,161,1 etc... until 84 entries

I believe I need to nest the coordinate getting inside the first set, so the logic order will be:

Read the map data to find type of tile (1 to 3)
Read the coordinates to place tile
_PUTIMAGE
Repeat 83 more times via For\Next

The results of my efforts vary depending on where I place things, but I haven’t been able to nail it down so it works properly. I’m guessing my fault is in the first data retrieval, for if I substitute a 1,2 or 3 in the _PUTIMAGE line, it draws a screen, but only of the 1,2, or 3 type of tiles. OR, when reading the tile type data, it reads the whole line and not bit by bit, so the Putimage gets goofed up. I tried putting “next k” at the end of the program instead of where it currently is, so every loop the it reads one tile type, them does the coordinates stuff, then loops back to the tile type, but it didn’t work.

Thank you for any insight, I greatly appreciate it. Utilizing the help you gave me above, will save me tremendous amounts of typing time 😃



« Last Edit: June 16, 2018, 07:49:07 PM by Feljar »