views:

91

answers:

2

Hi, I've developed a few arcade games so far and have the following structure in almost all of them: I have 1 file which contains a class called kernel with the following functions:

init()
welcome_screen()
menu_screen()
help_Screen()
play_game()
end_screen()

And another file, called Game, which basically calls all these functions and controls the flow of the game. Also, I have classes for different characters, whenever required.

Is this a good code structure, or should I try having different files(classes) for different functions like welcome screen, playing, help screen, etc? As in, instead of including all the code for these things in 1 file, should I be having different classes for each one of them? The only problem I think it might cause is that I would need certain variables like score, characters, etc which are common to all of them, that's why I have all these functions in a Kernel class so that all these functions can use these variables.

+1  A: 

It is unlikely there is a 'best' structure for this, but you can consider various patterns to help you separate your code into logical pieces.

For example, I'm writing a tetris/pentix clone for fun right now. It is roughly modeled in a MVC pattern. I have multiple static controllers which handle various parts of the game. Although the extra layer of calls might not be the most efficient, constant user input in my game precludes this from being an issue. Your game's specific requirements will have similar allowances/restrictions.

You can see the code here.

Alex
+1  A: 

Do what makes sense to you and continually be on watch for changes that you can make to make the whole thing less complex. Generally speaking, simple is better than complex, so if this structure is simple to you, it is good. When it becomes complex (which it will, if you work on it long enough), change it to make it simple again.

If the "design" doesn't beg for classes, don't add them. Only add things as they become necessary. In other words, "do simple things."

dash-tom-bang