Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3 4

Author Topic: I want to make a game  (Read 5246 times)

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I want to make a game
« Reply #15 on: January 03, 2011, 05:18:14 pm »

Hmm, depends on your language I guess. The point is that if you have a big chunk of code somewhere that's all working on drawing your character on screen, then it's usually best to split that off into a function called "drawCharacter()" or something. Even if you're only using it once and the code itself isn't that big, making functions like that makes it much easier to skim your function. Then you'll usually find some generic parts in the new function that you can reuse somewhere else with some small adjustments, or you still have big chunks that all together do one thing. In the end you're likely to end up with a bunch of low-level functions that can easily be checked for bugs, which are combined in higher-level functions which, if the functions they depend on work, are also easy to skim and debug. You can of course overdo it, and I guess 12 lines is a little low if you're using some lines just to assign some variables (make that 12 lines excluding variable initialization) or if you've got a particularly low-level language (but then again, in such a language, you can gain a lot by developing some base functions).


A good example is the following function, in C#:
Code: [Select]
public void DrawObject(MapObject obj)
        {
            Model model = obj.getModel();
            foreach (ModelMesh mesh in model.Meshes)
            {
                // This is where the mesh orientation is set, as well
                // as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = obj.getWorldMatrix();
                    effect.View = Cam.ViewMatrix;
                    effect.Projection = Cam.ProjectionMatrix;

                    effect.EnableDefaultLighting();
                    effect.DirectionalLight1.Enabled = true;
                    effect.DirectionalLight1.Direction = new Vector3(0.0f, 300.0f, 0.0f);
                    effect.DirectionalLight0.SpecularColor = new Vector3(0, 0, 0);
                    effect.EmissiveColor = new Vector3(0, 0, 0);
                }
                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
        }

Looks pretty concise, no? (that's exactly 12 lines excluding indentation, comments and putting accolades on separate lines) Now take the following function:
Code: [Select]
public void DrawWorld(GraphicsDevice Graph)
        {
            Graph.Clear(Color.Black);
            [s]DrawWorld()[/s] DrawBackground();
            foreach (MapObject MO in objectList)
            {
                DrawObject(MO);
            }
        }

If DrawBackground() is another function with 12 lines, you've got a large piece of code, which is hard to skim unless you're adding all kind of comment breaks. Instead, in this function you can just look at the function and say "Oh, that first draws the world, and then draws a list of objects on top of it", which you can't if you've got to parse some 30-odd lines of code.

(Note that I'd probably split the last part of the first function off into a low level function called AddLighting() because I'm using 5 lines to do 1 thing, which is setting up the lighting, but I'm not quite sure as to how to do that in C# since it does weird things with foreach variables)
« Last Edit: January 03, 2011, 05:49:54 pm by Virex »
Logged

Akura

  • Bay Watcher
    • View Profile
Re: I want to make a game
« Reply #16 on: January 03, 2011, 05:29:53 pm »

Does that DrawWorld function call itself? I haven't touched any kind of code for a while, but I'm pretty sure you're not supposed to do that. Or does C# consider a function with arguments and a function without them as two different functions?
Logged
Quote
They asked me how well I understood theoretical physics. I told them I had a theoretical degree in physics. They said welcome aboard.
... Yes, the hugs are for everyone.  No stabbing, though.  Just hugs.

G-Flex

  • Bay Watcher
    • View Profile
Re: I want to make a game
« Reply #17 on: January 03, 2011, 05:31:14 pm »

  • Function overloading is much older than C#, although I'm not sure how C# handles it.
  • Recursive functions aren't new either. One common way of implementing factorials is recursive.
Logged
There are 2 types of people in the world: Those who understand hexadecimal, and those who don't.
Visit the #Bay12Games IRC channel on NewNet
== Human Renovation: My Deus Ex mod/fan patch (v1.30, updated 5/31/2012) ==

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I want to make a game
« Reply #18 on: January 03, 2011, 05:44:39 pm »

Does that DrawWorld function call itself? I haven't touched any kind of code for a while, but I'm pretty sure you're not supposed to do that. Or does C# consider a function with arguments and a function without them as two different functions?
Oh whoops, Hadn't spotted that one. Since we don't really have a world yet, I had to add DrawWorld() to make a point, it's not in the original code (yet). Note that while due to function overloading this function wouldn't be recursive, overloading a function to have 2 conceptually different meanings (draw the background world vs. draw everything) is of course bad practice and a good way to get headaches.
I've replaced it with DrawBackground() for clarity.


Edit:
An example of code that could do with some functions is the following, in C, from the same project as the other one but this time to controll a little robot:
Code: [Select]
void motor(int vl, int vr) { // Set motor speed and direction: Input l/r,value (neg is LOW, pos=HIGH)
pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT);
if(vl >= 0) {
digitalWrite(8, HIGH);
analogWrite(6, abs(vl));
}
else {
digitalWrite(8, LOW);
analogWrite(6, abs(vl));
}

if(vr >= 0) // side == right
{
digitalWrite(7, HIGH);
analogWrite(5, abs(vr));
}
else {
digitalWrite(7, LOW);
analogWrite(5, abs(vr));
}
}

As you may've noticed, the code does essentially the same 4 times but worst of all, it's entirely unclear as to what it does (it actually sends a message to one of the robot's 2 engines to start turning forward or backward and it sets then the speed for that engine). In this case, making an appropriately named function that takes 4 arguments would make it a lot clearer to everyone what's going on, without needing comments (except perhaps to tell what's left and right, though that could be solved by replacing the values with well-named defines or constants)
« Last Edit: January 03, 2011, 05:59:55 pm by Virex »
Logged

Xkill

  • Bay Watcher
    • View Profile
Re: I want to make a game
« Reply #19 on: January 03, 2011, 06:12:00 pm »

Boy, this coding thing looks damn hard! C++ doens't attracted me, I think I'll stick to Python instead. I'm already reading a tutorial of it in RogueBasin and it looks really informative.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I want to make a game
« Reply #20 on: January 03, 2011, 06:16:04 pm »

Care to tell us what it is that doesn't attract you? Because most programming languages are quite similar, so you run the risk of running into the same problems over and over.
Logged

Xkill

  • Bay Watcher
    • View Profile
Re: I want to make a game
« Reply #21 on: January 03, 2011, 06:25:25 pm »

Really? I'd thought they where more different from each other. So in your opinion what's best for making a game, 'cause I'm pretty lost in here.
Logged

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: I want to make a game
« Reply #22 on: January 03, 2011, 06:26:07 pm »

C++ is industry standard, but I assume you're not making a AAA game.
Logged

Xkill

  • Bay Watcher
    • View Profile
Re: I want to make a game
« Reply #23 on: January 03, 2011, 06:30:01 pm »

Sorry for asking this, but what exactly is an AAA Game?
Logged

Simmura McCrea

  • Bay Watcher
    • View Profile
    • My Steam profile
Re: I want to make a game
« Reply #24 on: January 03, 2011, 06:31:49 pm »

No damn idea what it stands for, but it's a mainstream game, close as I can tell.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I want to make a game
« Reply #25 on: January 03, 2011, 06:32:18 pm »

It just means a high-profile game with a big budget, like for example Starcraft 2.


Anyhow, what are you looking for in a language?
Logged

GaelicVigil

  • Bay Watcher
    • View Profile
Re: I want to make a game
« Reply #26 on: January 03, 2011, 06:40:35 pm »

Hello everyone, i'm a new guy here, just registered in.

However, I want to make a game, something that I call a Roguelike Sandbox Life Simulation Game (Quite like Dwarf Fortress but in the Life Simulation Genre and the Modern Times and Space ones too in the later game.) I know that it looks quite strange, but I think its going to be cool.

Anyways, I would like to know a good Engine and Programing Language to make it, you guys know anyone?

Let me help you with that: Click Here
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: I want to make a game
« Reply #27 on: January 03, 2011, 06:59:57 pm »

In this case, I think asking around will be a big help. Since really, everyone tends to have these kinds of ideas from time to time, until they get shot down when they realize just how much work and effort it'd take. Plus, there is a problem with that search, not that much of it recommends a direct programming language with a library, most if it is for game engines that would be pretty clunky if used to make a roguelike.

Just to recap here:
Get a programing language and learn it.
   You'll want to pick up C++ or Python here, since I know they have the curses library somewhere.

Once you have Object oriented down, go ahead and learn how to make a roguelike. Generally, this can take a while to get here. Took 10 weeks for my university class to get me that far. Learning how to get a roguelike running from then on will take possibly longer.

In the meantime, write out your idea, the mechanics, and so on somewhere. Nothing is as annoying as getting that far just to find out that you have no idea how to start.
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: I want to make a game
« Reply #28 on: January 03, 2011, 10:15:54 pm »

In this case, I think asking around will be a big help. Since really, everyone tends to have these kinds of ideas from time to time, until they get shot down when they realize just how much work and effort it'd take. Plus, there is a problem with that search, not that much of it recommends a direct programming language with a library, most if it is for game engines that would be pretty clunky if used to make a roguelike.
Exactly. It might be more work than you think, but if you stick with it you can find tools for just about any type of game and language. If you really are planning on a roguelike, there's Dasleah's Roguelike Development Thread in this very sub-forum. Then again, you said you have no coding experience so I'd start here. That is, if you want to use C++. The three most viable options for roguelike development are the pdcurses library, libtcod, and using pseudo-ascii graphics by blitting ascii characters to the screen with SDL. Not that it matters now, you've got a while until you can even think about using those. Game programming is really not something you want to try your hand at until you're at least competent in one language.

Hope that helps. :)
« Last Edit: January 03, 2011, 10:18:14 pm by Gatleos »
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Googolplexed

  • Bay Watcher
  • My avatar is of whitespace, Not the firefox logo
    • View Profile
Re: I want to make a game
« Reply #29 on: January 03, 2011, 10:16:52 pm »

If you have NO coding experience, forget the project, at-least for a few months
Logged
Pages: 1 [2] 3 4