# how to design my programs and games structure?

This topic is 1321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi.

iworked on some programs that some about games and some were not. and i worket on engines like udk and ue4 but there were always aproblem.

something that you you may say structure would happen during the programing. i just imagined and object as a class and started coding for it but there were a lot of mistakes there and those programs was not as compicated as im working now.

its better to write an structure for it but i dont know what is the best way for it.

i have learnt about ssadm and object oriented analyze like class diagram and ...... but i dont know do them work here as in a game we have concepts like spawning or ai behavior and......

are there any good pattern or procedure that help me design my game from ground up or by engine or my program for game like an editor for my levels.

how should i imagine of my program look like.

should i think about it as an end project that what i want and after that start to document those designs and after that start to code? what about i wanted there to be some changes. how should  structure be to those changes be easy?

##### Share on other sites

I'm developing a 2D game with Pygame with a procedurally generated world from scratch and alone. What I can tell you is: It's really hard to have a solid way to start it. First, I t've tried to develop a whole engine for my game, and then I realized that it was way too general and did not fit in my original intentions (So, it requires a whole lot more planning if you want to develop a general-purpose engine).

First things first, start by creating the "functioning". Make some basic level/scenario and build the game from there (How the player interact with everything, for example). Then, build the Graphical User Interface later. If some key element of the user interface is required for interaction, like an inventory, than do it before the GUI as if it was just part of the basic "functioning".

The ground-structure is how the game works. If it has AI, then you have to create it in this test-level. After everything works fine in the test-environment, then, you could start developing a Map Editor and the GUI.

Since I work alone and have no time for Map Editors, I personally vote for procedurally generated levels.

I. Image the gameplay in your head.
II. Create a testing environment with a test-player and start developing that gameplay.
III. One you achieved what you imagined in a general form, start adding the details like GUI, specific players, enemies and levels.

Nevertheless, there's no FINAL solution on how to structure your game. Unfortunately, you'll have to discover it by yourself, and it's a vital process in your personal evolution as a programmer and game developer.

##### Share on other sites

I guess there are as many ways to learn to structure code as people in the world, but I'll share what works for me.

When I'm learning a new framework or language I choose a game that I can code in an afternoon (snake and space invaders are my favourites) and I code it without caring much about structure and such things, because if I barely know what code to write, I'm not even close to know how to properly structure it. I choose a project I can code in a couple of hours because I try to finish it in one sitting, but I guess that's up to personal preferences.

One or two days after I rewrite the same game from scratch. Here I already know what code I need to write, so I can think how to structure it and I've had time to think about it. Sometimes, I will rewrite the game from scratch once again after a week or so, just for the sake of it.

Edit: yeah, and I forgot the point of the question. The Only True and Perfect WayTM to structure a program doesn't exist. Structuring the code is a skill and it needs lots of practise, as everything else. Choose projects of a manageable size for your skill and practise. After a few projects completed, you'll see how you have a clearer idea of how to structure the code every time you start a new project.

Footnote: embrace the refactor. Not for few times I've had to refactor comlpetely a project because the scope had grown beyond expected or simply due to bad decisions at the beginning. In my humble experience, refactor is an unavoidable fact of programmer's life.

Edited by Avalander

##### Share on other sites

Ask for a professional freelance designer in the forum ;) You will get great designs!

##### Share on other sites

Here is a readable way to structure things in Pong.


// Ball uses inheritance
public class Ball extends Sprite
{

public class Ball(double x , double y)
{
super(x,y);
}
}
// Sprite uses composition
public class Sprite implements GameComponent
{
private Vector2D position;
private Image image;

public Sprite(double x, double y)
{
position = new Vector2D(x,y);
}
}

public interface GameComponent
{
public void update();
public void draw(Graphics2D g2d);
}


One way to learn is to struggle with the design. Write the code design on paper. Contemplate on it. Why does this work and why did this break and the potential damage that can be done further down the line? It comes with experience and practice. You will improve through struggling and pushing on. Another way is to glance at other people's code to get an idea about how to approach the design instead of copying it.

As for structure the entire game, that can be quite complicated even for small games. Every time I went to outdo my last game project in terms of scale, I would use code from my previous games and then assemble together. Sometimes my design implementation is so bad for a specific feature from the last game, but I can change the design to make it more readable. You can fix a bad design through having staring at it for 4 months.

I guess the best way is to write clean code. Try to write clean code like the above. You can only write clean code if you have a holistic view of it which can be done using pencil and paper. I draw diagrams and pseudo-code. Use whatever techniques fit your needs.

Edited by warnexus

##### Share on other sites
Structure is probably the entire challenge of programming IMO. When you know what you want to do its usually trivial to just write out the directions to do it, designing how everything will come together is really the heart of the puzzle.

##### Share on other sites

Designing your code is the hardest part for me too. I usually already know how to implement the logic of the game I wanted but I couldn't start because I can't decide on how to design the bigger picture.

One design pattern that I started embracing is the Entity-Component (System) pattern. I find it easier to start coding without thinking too much about the design with this pattern. I can start writing independent components and then piece them together later. Sometimes, you just have to code all the necessary components first (make dependencies minimal) before you can have the general idea for your overall code. Idk if I'm making sense, I suck at explanations.

##### Share on other sites

i think entity-component-system is something like first you design the footballer as entity and after that you work on his functions like walking or running and varibles like height as component and work on its relation with other class object like ball as system. basically most of us i think do that. thank you

##### Share on other sites

Separation of Concerns

Separate different parts of your code into logical units. For example, file system access could be in its own module so it can be replaced more easily when porting to another platform. Also, don't mix low-level UI code and game logic so it's easier to replace UI libs if needed.

DRY (Don't Repeat Yourself)

If you find using a chunk of code in several places, extract a method and replace usages with a call to that method.

If you find some object getting various data from another object and performing logic on it, consider moving the logic into the target object's method instead.

Avoid magic numbers

Unnamed numbers are confusing and if a certain value is used in many places, updating it will easily lead to mistakes when you forget to update all occurrences. Use well-named variables instead.

Avoid excessive branching

Control flow is hard to follow if there are many nested branches. Some branches can be converted into methods.

Declare variables close to their usage

The smaller the scope, the easier is your mental load.

Express intent

If you find yourself doing a for-loop to find a value from a collection, use a specific find-method instead. It makes it clear to the reader what you're doing.

Comment "why" instead of "what"

Don't write in comments things that can be seen from the code.