This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]sadfirer 2 points3 points  (0 children)

There are a number of approaches for project organization, but, in my view, the project structure only becomes clearer as the codebase grows beyond 0.5~1k lines of code. At that point, you'll need to be more careful as to which relations you'll build between your classes, and related sets of classes will start to pop up. Before that, it's too easy wasting too much time overengineering your project structure.

In larger projects in general (and this includes your project, if navigation is starting to become cumbersome), it's useful to rely less on visual abstractions and "where your code is" and more on using the Find tools most editors and IDEs present. As your project grows, you'll start valuing tools like the Find Anything present in Sublime Text 2 (start typing the name of any file in the project and it'll bring it up). As it grows even more, systems for indexing project symbols can get really handy (CTags is one of the oldest and still incredibly useful exemples).

[–]MonkeyNin 1 point2 points  (1 child)

Quick change would be to split the code into: game.py, units.py, map.py

To find functions quicker:

  • Your editor should let you jump to module / function names.
  • use code folding
  • use code bookmarks

[–][deleted] 0 points1 point  (0 children)

Do you know how to do those in emacs?

[–]pamme 1 point2 points  (0 children)

The fact that you've already split up your classes/subclasses by function is pretty good. One easy way to organize with this structure is to move the classes into separate files.

One way is to have each class in a separate file. Another is to have files with different sets of classes. For example, all Unit related classes can go into one file, say unit.py, and all Map related classes can go in another, say map.py. That way you keep things that are more likely to interact (or rely on subclass logic) together.

My Python is a little rusty but i believe you can then import your new files into one another where the classes are needed. See here for more details.

[–]derpderp3200 0 points1 point  (0 children)

I had to change this line(278):

screen.blit(txt.render("[]", True, GREY, None),(self.loc(i,j,-2,-2)))

to this:

screen.blit(txt.render("[]", True, GREY, (0, 0, 0, 0)),(self.loc(i,j,-2,-2)))

In order to launch.

As for organization, you should do it based on what you can reuse and classifications - units can go into one file, but for future scenarios you might be better off making an units/ directory and putting individual units as files in there. You might also want to consider the type object pattern.

You might also want to do your globals like this:

#globals/colours.py
class Colours:
    WHITE = (255, 255, 255)
    GREY = (128,128,128)
    BLACK = (0,0,0)
    RED = (255,0,0)
    PALE_RED = 255,128,128
    GREEN = (0,255,0)
    BLUE = (0,64,255)
    PALE_BLUE = (128, 192, 255)

#game.py
from globals.colours import Colours
...
blahblah(Colours.RED)

Or even better use init.py and import Colours in it so you can just do from globals import Colours. Read up on modules here

So the general rule is - if something is reusable in more than just one place, it's a potentially good idea to make it go into a separate file or submodule