E
E
Evgeny Petryaev2020-09-27 04:16:17
Game development
Evgeny Petryaev, 2020-09-27 04:16:17

How to add images/objects without inflating the class code?

I studied on the examples of the old graphic library, by the way, the first program was created by me in 2012, but there is not a single release, all because it is difficult to navigate in a program in which a class starts to take 1000+ lines of code, I will explain here there is a method for example loadimage ():

LoadImages()
{
...
   loadfile("1.png");
   loadfile("2.png);
...
   loadfile("100500.png");
...
}

And the same with coordinates
setverteces()
{
...
   object1.x=0.1;
   object1.y=0.2;
   object2.x=0.1;
   object2.y=0.3;
...
   object100500.x=0.4;
   object100500.y=-0.8;
...
}
,
the class swells to epic proportions. One scrolling to the desired method takes a lot of time and effort, which even discourages interest in this. How can you write, say, an average of 10-15 methods in a class, each of which will fit on the screen. That is, to speed up the development, otherwise I just won’t have time to do anything? How is it implemented in the same units and other things? Is it possible to implement so as not to touch the code, but only resources with the addition of features and mechanics, levels and objects.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2020-09-27
@Gremlin92

It can be turned..

object1.x=0.1;
object1.y=0.2;
object2.x=0.1;
object2.y=0.3;

in it
object1.SetCoord(0.1, 0.2);
object2.SetCoord(0.1, 0.2);

To load images, a file is used (eg json, xml, or at least just ini) or a dictionary in the code in a separate file.
If the texture is loaded to this object, then it's worth writing a normal constructor
object1 = new object(0.1, 0.2, "100500.png");
. And it's better to create objects through serialization / deserialization. Prepare a json file with an array of objects. With coordinates, texture paths and other properties. And load the whole thing in one cycle with a couple of lines.
Well, or for each object its own json, for example:
{
  "x": 0.2,
  "y": 0.1,
  "texture": "car.png"
}

And create an object by specifying this file:
object = new Object("/object1.json");

M
maaGames, 2020-09-27
@maaGames

You need to transfer the list of images to a text file and load them in a loop - the class code will not be inflated. Load object coordinates from a file and process in a loop - the class code will not be inflated.
xml, json, just in its text format - it doesn't matter. It is more convenient to load data from a file, then you can modify it simply by changing text files, without recompilation. Maybe even without restarting the game.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question