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

0 Members and 1 Guest are viewing this topic.

Offline Feljar

  • Newbie
  • Posts: 7
    • View Profile
Trying to clean up some code
« on: June 13, 2018, 09: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

FellippeHeitor

  • Guest
Re: Trying to clean up some code
« Reply #1 on: June 13, 2018, 11:57:19 pm »
You need two things to reduce this drastically: arrays and loops. First for your characters:

Code: QB64: [Select]
  1. DIM character(1 to 3) AS LONG
  2. character(1) = _loadimage("warriorPic.png", 32)
  3. character(2) = _loadimage("wizardPic.png", 32)
  4. 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: QB64: [Select]
  1. TYPE TILE
  2.     x as integer
  3.     y as integer
  4.  
  5. DIM tile(1 to 84) AS TILE
  6. tile(1).x = 2: tile(1).y = 2
  7. tile(2).x = 80: tile(2).y = 2
  8. 'etc...

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

Bam. Hope it's what you were after.

Welcome to the forum!

FellippeHeitor

  • Guest
Re: Trying to clean up some code
« Reply #2 on: June 14, 2018, 12:13:00 am »
To go even deeper down the structuring rabbit hole, you'd have the tile coordinates in a DATA set:

Code: QB64: [Select]
  1. TYPE TILE
  2.     x as integer
  3.     y as integer
  4.  
  5. DIM tile(1 to 84) AS TILE
  6. RESTORE tileData
  7. FOR t = 1 TO 84
  8.     READ tile(t).x, tile(t).y
  9.  
  10. tileData:
  11. DATA 2, 2, 80, 2
  12. 'etc...

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Trying to clean up some code
« Reply #3 on: June 14, 2018, 09: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.

FellippeHeitor

  • Guest
Re: Trying to clean up some code
« Reply #4 on: June 14, 2018, 09:45:27 am »
Hi, Petr, I don't think I understand your point.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Trying to clean up some code
« Reply #5 on: June 14, 2018, 10: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.

Offline Feljar

  • Newbie
  • Posts: 7
    • View Profile
Re: Trying to clean up some code
« Reply #6 on: June 14, 2018, 10: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!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Trying to clean up some code
« Reply #7 on: June 14, 2018, 10: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.

FellippeHeitor

  • Guest
Re: Trying to clean up some code
« Reply #8 on: June 14, 2018, 10: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.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Trying to clean up some code
« Reply #9 on: June 14, 2018, 10: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: QB64: [Select]
  1.  
  2. TYPE Oblast 'AREA
  3.     X AS _UNSIGNED INTEGER '    pozice X v herni oblasti
  4.     Y AS _UNSIGNED INTEGER '    pozice Y v herni oblasti
  5.     Zivot AS _UNSIGNED _BYTE '  zivot. Nektere subjekty pujdou znicit, zalezi to na kontrolnim podprogramu, pro ktery typ to dovoli
  6.     Typ AS _UNSIGNED _BYTE '    typ. Jestli se jedna o kostku s domem, muze byt pevne stanovena velikost
  7.  
  8. Prvku = 1000
  9. DIM O(Prvku) AS Oblast
  10. DIM SHARED posX, posY, imX, imY, Pohyb2 ', WaterIMG&
  11. DIM SHARED TreeType(10) AS LONG
  12.  
  13. WaterIMG& = _NEWIMAGE(151, 151, 32)
  14. tankImg0& = _NEWIMAGE(320, 240, 256)
  15. tankImg1& = _NEWIMAGE(320, 240, 256)
  16. TreeImg0& = _NEWIMAGE(150, 320, 256)
  17.  
  18. _CLEARCOLOR 0, tankImg0&
  19. _CLEARCOLOR 0, tankImg1&
  20.  
  21.  
  22.  
  23. imX = 1: imY = 1
  24.  
  25.  
  26.  
  27. 'aby se vykreslovalo plynule vse, musim stanovit maximalni velikost nejvetsiho objektu v pixelech:
  28. CONST MaxObject = 150
  29. CONST Zelena& = _RGB32(0, 255, 0)
  30. posX = 150
  31. posY = 150
  32. water WaterIMG& 'aqua
  33. tank tankImg0&
  34. tank1 tankImg1&
  35.  
  36.  
  37. 'umele naplnim pole objektu nahodnymi prvky:
  38.  
  39. FOR R = 1 TO 1000
  40.     X = X + 150: IF X > 4500 THEN y = y + 150: X = 0
  41.     T = RND * 50
  42.     SELECT CASE T
  43.         CASE 0 TO 10: tp = 0
  44.         CASE 11 TO 20: tp = 1
  45.         CASE 21 TO 25: tp = 2
  46.         CASE 25 TO 30: tp = 3
  47.         CASE 31 TO 40: tp = 3 + (T - 30)
  48.     END SELECT
  49.  
  50.  
  51.     O(R).X = X 'INT(RND + 1 * 10000)
  52.     O(R).Zivot = 100
  53.     O(R).Typ = tp 'INT(RND * 5)
  54.     O(R).Y = y 'INT(RND + 1 * 10000)
  55.  
  56.  
  57. 'necham vygenerovat 10 typu stromu
  58. FOR TreeGen = 1 TO 10
  59.     Tree TreeImg0&
  60.     TreeType&(TreeGen) = _COPYIMAGE(TreeImg0&)
  61. NEXT TreeGen
  62.  
  63.  
  64.  
  65.  
  66. ', rndpoleY
  67.  
  68. 'O(1).X = 1800: O(1).Y = 1800: O(1).Typ = 2
  69.  
  70. 'kurzory pro DRAW
  71. R$ = STR$(_RGB32(255, 255, 255))
  72. Ri$ = STR$(_RGB32(255, 0, 55))
  73. UP$ = "C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
  74. LFT$ = "TA90 C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
  75. DN$ = "TA180 C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
  76. RGHT$ = "TA270 C" + Ri$ + " R4 U6 R6 H8 G8 R6 D6  BR1 BU1 P" + R$ + "," + Ri$
  77. Normal$ = "TA22 C" + Ri$ + " R4 U12 R6 H8 G8 R6 D12  BR1 BU1 P" + R$ + "," + Ri$
  78.  
  79.  
  80.  
  81. 'vlastni vykresleni:
  82. DO WHILE i$ <> CHR$(27)
  83.     i$ = INKEY$
  84.     CLS
  85.     water WaterIMG&
  86.     '    tank tankImg0&
  87.     '   tank1 tankImg1&
  88.  
  89.  
  90.  
  91.     mi& = _MOUSEINPUT
  92.     Rb = _MOUSEBUTTON(2)
  93.     Lb = _MOUSEBUTTON(1)
  94.     mX = _MOUSEX
  95.     mY = _MOUSEY
  96.  
  97.     '-------------- jeden typ posunu najetim na okraj --------------------
  98.     '   IF TIMER > prodleva AND TIMER > 0 THEN
  99.  
  100.  
  101.     ' ----------------- posun stiskem praveho tlacitka ------------------------
  102.     IF Rb THEN
  103.         IF SGN(_MOUSEMOVEMENTX) = 1 THEN posX = posX + 1: prodleva = TIMER + .005
  104.         IF SGN(_MOUSEMOVEMENTY) = 1 THEN posY = posY + 1: prodleva = TIMER + .005
  105.         IF SGN(_MOUSEMOVEMENTX) = -1 THEN posX = posX - 1: prodleva = TIMER + .005
  106.         IF SGN(_MOUSEMOVEMENTY) = -1 THEN posY = posY - 1: prodleva = TIMER + .005
  107.     END IF
  108.     '---------------------------------------------------------------------------
  109.  
  110.     SELECT CASE mX
  111.         CASE IS < 20: posX = posX - 1: prodleva = TIMER + .01: DRAW LFT$
  112.         CASE IS > _WIDTH - 20: posX = posX + 1: prodleva = TIMER + .01: DRAW RGHT$
  113.     END SELECT
  114.     SELECT CASE mY
  115.         CASE IS < 20: posY = posY - 1: prodleva = TIMER + .01: DRAW UP$
  116.         CASE IS > _HEIGHT - 20: posY = posY + 1: prodleva = TIMER + .01: DRAW DN$
  117.     END SELECT
  118.     '   END IF
  119.     IF posX < 150 THEN posX = 150
  120.     IF posY < 1 THEN posY = 1
  121.     '  END IF
  122.  
  123.  
  124.  
  125.     FOR vykresli = LBOUND(o) TO UBOUND(o)
  126.         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
  127.             SELECT CASE O(vykresli).Typ
  128.                 CASE 0: Trava O(vykresli).X - posX, O(vykresli).Y - posY
  129.                 CASE 1: _PUTIMAGE (O(vykresli).X - posX, O(vykresli).Y - posY), WaterIMG&, 0, (0, 0)-(150, 150)
  130.                 CASE 2: Trava O(vykresli).X - posX, O(vykresli).Y - posY
  131.                     _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)
  132.                 CASE 3: Trava O(vykresli).X - posX, O(vykresli).Y - posY
  133.                     _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)
  134.                 CASE 4 TO 13: Trava O(vykresli).X - posX, O(vykresli).Y - posY
  135.                     _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)
  136.  
  137.             END SELECT
  138.  
  139.             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
  140.                 LOCATE 3, 1
  141.                 PRINT O(vykresli).X; O(vykresli).Y; O(vykresli).Zivot; O(vykresli).Typ
  142.                 _DISPLAY
  143.                 SLEEP 1
  144.                 Lb = 0
  145.             END IF
  146.  
  147.         END IF
  148.     NEXT
  149.     LOCATE 1, 1: PRINT posX, posY, INT(pohybX), INT(pohybY), posX + mX, posY + mY
  150.  
  151.  
  152.  
  153.     pohyb
  154.  
  155.  
  156.     LINE (0, 0)-(800, 60), _RGB32(0, 0, 0), BF
  157.  
  158.     DRAW "BM" + STR$(mX) + "," + STR$(mY)
  159.     IF mX > 20 AND mX < 780 AND mY > 20 AND mY < 570 THEN DRAW Normal$
  160.  
  161.     SELECT CASE mX
  162.         CASE IS < 20: DRAW LFT$
  163.         CASE IS > _WIDTH - 20: DRAW RGHT$
  164.     END SELECT
  165.     SELECT CASE mY
  166.         CASE IS < 20: DRAW UP$
  167.         CASE IS > _HEIGHT - 30: DRAW DN$
  168.     END SELECT
  169.  
  170.  
  171.  
  172.  
  173.     _DISPLAY
  174.     _LIMIT 500
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. SUB Trava (x, y) 'zeleny obdelnik
  183.     LINE (x, y)-(x + 150, y + 150), Zelena&, BF
  184.  
  185.  
  186. SUB water (WaterImg&)
  187.     SHARED sinus, pruchod
  188.     pruchod = pruchod + 1
  189.     IF sinus > 4 * _PI THEN sinus = 0
  190.     IF pruchod MOD 50 = 0 THEN
  191.         pruchod = 0
  192.         _DEST WaterImg&
  193.         CLS , _RGB32(0, 0, 155)
  194.         FOR x = 0 TO 150
  195.             FOR y = -99 TO 199 STEP 8
  196.                 sinus = sinus + .011 ' + RND / 25
  197.                 PSET (x, y + (SIN(sinus) * 25)), _RGB32(0, 255 * RND, 255)
  198.         NEXT y, x
  199.         IF sinus > 8 * _PI THEN sinus = -sinus
  200.         _DEST 0
  201.     END IF
  202.  
  203. SUB pohyb 'simulace pohybu jednotek - v tomto pripade to bude hnedy obdelnik, simuluje letoun
  204.     '  IF TIMER MOD 2 = 0 THEN
  205.     SHARED pohybX, pohybY
  206.  
  207.  
  208.  
  209.  
  210.     IF pohybX > 10000 OR pohybX < 0 THEN imX = imX * -1
  211.     pohybMx = RND * imX
  212.     pohybX = pohybX + pohybMx / 10
  213.  
  214.     IF pohybY > 10000 OR pohybY < 0 THEN imY = imY * -1
  215.     pohybMy = RND * imY
  216.     pohybY = pohybY + pohybMy / 10
  217.  
  218.  
  219.     'doplnit podminku, aby vykresleni probehlo pouze pri viditelnosti - zavislost na PosX a PosY OK
  220.     IF posX - 150 < pohybX AND posX + 800 > pohybX AND posY - 150 < pohybY AND posY + 600 > pohybY THEN
  221.         LINE (pohybX - posX, pohybY - posY)-(pohybX - posX + 25, pohybY - posY + 30), _RGB32(116, 61, 0), BF
  222.     END IF
  223.     ' END IF
  224.  
  225. SUB Tree (TreeImg0&) 'generator TREE
  226.     DO WHILE prumer < 22
  227.         prumer = CINT(RND * 40)
  228.     LOOP
  229.     _DEST TreeImg0&
  230.     '    CLS
  231.     LINE (72, 160 - prumer)-(78, 200 + prumer), 187, BF
  232.  
  233.     FOR koruna = 1 TO 7.28 STEP 0.01
  234.         X = 75 + SIN(koruna) * prumer
  235.  
  236.         DO WHILE koef < .36 OR koef > .8
  237.             kf = INT(RND * 10)
  238.             koef = kf / 10
  239.         LOOP
  240.  
  241.         Y = 120 + COS(koruna) * prumer / koef '.5
  242.         IF oldX = 0 THEN oldX = X
  243.         IF oldY = 0 THEN oldY = Y
  244.         LINE (oldX, oldY)-(X, Y), 2
  245.     NEXT koruna
  246.  
  247.     FOR Yplod = 0 TO 319
  248.         FOR Xplod = 0 TO 149
  249.             _SOURCE TreeImg0&
  250.             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
  251.         NEXT Xplod
  252.         G = G + 20
  253.     NEXT Yplod
  254.     _CLEARCOLOR 0, TreeImg0&
  255.     _DEST 0
  256.  
  257. SUB tank1 (tankImg1&)
  258.     _DEST tankImg1&
  259.     podvozek NOT Pohyb2
  260.     PAINT (60, 70), 19, 15 'vybarveni podvozku
  261.     PAINT (64, 154), 19, 15 'vybarveni podvozku
  262.  
  263.     OVAL 30, 100, 100, 145, 15
  264.     PAINT (32, 102), 162, 15
  265.     PAINT (44, 102), 164, 15
  266.     PAINT (50, 102), 166, 15
  267.     LINE (40, 96)-(40, 148), 166 'leva cara na tankove vezi
  268.     LINE (45, 96)-(45, 148), 160 ' prava cara na tankove vezi
  269.     PAINT (87, 98), 164, 15
  270.     PAINT (92, 98), 162, 15
  271.     LINE (85, 96)-(85, 148), 160 'leva cara na tankove vezi
  272.     LINE (90, 96)-(90, 148), 166 ' prava cara na tankove vezi
  273.     LINE (40, 95)-(55, 30), 22, BF 'delo
  274.     LINE (90, 95)-(75, 30), 22, BF 'delo 2
  275.  
  276.     LINE (47, 95)-(47, 30), 15
  277.     LINE (83, 95)-(83, 30), 15
  278.  
  279.     _DEST 0
  280.  
  281.  
  282.  
  283. SUB tank (tankImg0&)
  284.     _DEST tankImg0&
  285.     podvozek Pohyb2
  286.     '------------- NAMALOVANY PODVOZEK S PASAMA ---------------------------
  287.  
  288.     OVAL 50, 120, 80, 145, 15 'TANKOVA VEZ
  289.     PAINT (52, 122), 166, 15
  290.     CIRCLE (65, 132), 10, 15 'Vrchni poklop
  291.     PAINT (65, 132), 24, 15
  292.  
  293.     LINE (60, 115)-(70, 40), 22, BF 'delo
  294.     LINE (65, 115)-(65, 40), 20 '"odlesk" v delu / horni cara
  295.     _DEST 0
  296.  
  297. SUB podvozek (pohyb2)
  298.  
  299.     FOR PASY = 50 + pohyb2 TO 160 STEP 15
  300.         IF PASY MOD 2 = 0 THEN CL = 199 ELSE CL = 240
  301.         LINE (35, PASY + 15)-(45, PASY), CL, BF 'LEVY PAS
  302.         LINE (85, PASY + 15)-(95, PASY), CL, BF 'PRAVY PAS
  303.     NEXT PASY
  304.     IF pohyb2 THEN pohyb2 = 0 ELSE pohyb2 = 1
  305.     OVAL 40, 60, 90, 160, 15
  306.     PAINT (42, 62), 30, 15
  307.     OVAL 45, 65, 85, 155, 15
  308.     PAINT (43, 63), 28, 15
  309.  
  310.  
  311. SUB OVAL (x1, y1, x2, y2, BARVA)
  312.     IF x1 > x2 THEN SWAP x1, x2
  313.     IF y1 > y2 THEN SWAP y1, y2
  314.  
  315.     CIRCLE (x1 + 10, y1 + 5), 10, BARVA, 1.5, 3.1 ' levy horni oblouk
  316.     CIRCLE (x2 - 10, y1 + 5), 10, BARVA, 6.2, 1.5 ' pravy horni oblouk
  317.     CIRCLE (x1 + 10, y2 - 5), 10, BARVA, 3, 4.8 ' levy spodni oblouk
  318.     CIRCLE (x2 - 10, y2 - 5), 10, BARVA, 4.7, .2 'pravy spodni roh
  319.  
  320.     LINE (x1 + 7, y1 - 5)-(x2 - 7, y1 - 5), BARVA 'horni cara
  321.     LINE (x1, y1 + 5)-(x1, y2 - 5), BARVA 'leva strana
  322.     LINE (x1 + 7, y2 + 5)-(x2 - 7, y2 + 5), BARVA 'spodni cara
  323.     LINE (x2, y2 - 5)-(x2, y1 + 5), BARVA 'prava strana
  324.  
  325.  
  326.  
  327.  

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, 11:00:22 am by Petr »

Offline Feljar

  • Newbie
  • Posts: 7
    • View Profile
Re: Trying to clean up some code
« Reply #10 on: June 14, 2018, 09: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

Offline Feljar

  • Newbie
  • Posts: 7
    • View Profile
Re: Trying to clean up some code
« Reply #11 on: June 14, 2018, 09:32:28 pm »
Never mind! I found what I was doing wrong. It’s working perfectly now.

FellippeHeitor

  • Guest
Re: Trying to clean up some code
« Reply #12 on: June 14, 2018, 09: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.

Offline Feljar

  • Newbie
  • Posts: 7
    • View Profile
Re: Trying to clean up some code
« Reply #13 on: June 14, 2018, 10: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.

Offline Feljar

  • Newbie
  • Posts: 7
    • View Profile
Re: Trying to clean up some code
« Reply #14 on: June 16, 2018, 05: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, 06:49:07 pm by Feljar »