Python Forum
procedural in class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
procedural in class
#1
Hello!

Made a game in the genre of rts, space. Ready for 80%. Wrote over 12,000 lines of code. Unfortunately, I did everything with a procedural language, without using classes. Used pure python + PIL.
   
Now I'm starting to get confused in the code, but I still have to do it further))). I want to translate all the graphics into classes, but I don’t know how to distribute them. All the graphics I have done on canvas. In total I used (in the galaxy menu) 4 canvas, the rest of the graphics are made on 2 canvas. I don’t know - how to divide all the graphics into classes? Each button in the game is a new picture and everything in this menu is located. Do classes on the menu buttons? I also have a pixel animation (one ship is one pixel). Ships, fleet - do classes also have to be done? Which parent, which child class?
Questions, questions, questions ...

In the procedural style, everything works great, but I never wrote classes, so I don’t understand how to divide everything into classes. I would be grateful for any help!

p.s. The game is made in Russian, but to make it clear to you - all the menu buttons have been translated into English. Now I am writing a small review on my game - this is a link, but in Russian.
I am almost 50 years old, I am engaged in farming, writing a game in my free time - a hobby. Maybe that's why I take classes in programming hard ...
Reply
#2
berkut72 Wrote:I don’t understand how to divide everything into classes.

I see two strategies to introduce classes in your code. The first strategy is to use the words of the domain for your game. I don't understand russian, but by pasting your text into Google translate, I get paragraphs such as this one

Quote:By sending spy probes to the galaxy you can find such a stellar system inhabited by robots. Feel free to join the battle - this will be a wonderful training ground for you, as the commander of the fleet and your ships, but remember - they can also "touch" your planet. You can expect a very nice bonus - besides the fact that you can capture this already developed star system and incorporate it into your empire, you can also very likely find artifacts of the ancient race on such a planet that will greatly enhance your capabilities

In this text, one sees conceptual objets appear such as SpyProbe or Ship or Fleet or Planet, StarSystem, Artifact etc. Any of these concepts could be the starting point of a class. A choice must be made among them to see which classes would be the more relevant in your program, but for example if many functions manipulate the data of a star system, then it is a good candidate as a class.

The second strategy is more down-to-earth but it could be fruitful as well: try to identify a certain group of functions in the code that manipulate the same set of data, say the three variables spam, eggs and ham. This set of variables could be the starting point of a class with members spam, eggs and ham, and the functions would be candidates as methods for this class. Then try to find a good, illustrative name for the class that unites the data spam, eggs and ham.
Reply
#3
A player is a dictionary-type data structure, very convenient.

The problem in the graphical interface is what to make the main class here ... Or to do it without the main class, and in all classes to prescribe methods ... Then again it turns out the procedural code, and the classes can inherit something from the parent.

I had to use 4 canvas to implement the interface that I got (the fourth canvas is embedded in the third (size +), the third canvas is embedded in the second (size ++), the second canvas is embedded in the first (size +++))

Thank you for your attention! I’ll understand and study - not quite an old man yet, but I like to learn!

At first I thought that a ship could be made a class, but with a good thought, a fleet could contain a million ships. Is it right to make a million instances of one class? I decided that the class would be a fleet, which could include a lot of ships (any number). And depending on which ships the fleet will contain, register a method for each ship in the class. So, probably, it will be right?
Reply
#4
berkut72 Wrote:A player is a dictionary-type data structure, very convenient.
Every dictionary-type data structure that you have already created in the program is an excellent candidate to become a class.
berkut72 Wrote:Is it right to make a million instances of one class?
Handling a million instances in python would be certainly very slow. Don't do that unless you have a very specific program that requires it. Use classes with a few instances or a few dozens instances, sometimes a few hundreds (perhaps with the help of the namedtuple class and the __slots__ feature), but donc go beyond that without a very good reason, so yes in your case a Fleet class looks better than a Ship class.
Reply
#5
My biggest problem is how to convert a GUI to classes. Here I don’t even know how to start solving this problem?

Make parent class the very first canvas? Or is the class a control button? Or is the class the items inside every menu, and the control buttons are the parent class?

The night passed, some thoughts calmed down, took shape in something, and already this morning I thought that the set of several variables (spam, eggs, ham) that you proposed is a good idea! This is a very useful class definition kit.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020