C++ Help

Discussion in 'Mac Programming' started by Roark, Jan 16, 2005.

  1. Roark macrumors newbie


    Dec 21, 2004

    So far I have 2 problems in C++

    1) I'm pretty much new to this soo bear with me. I get the whole variables thing and simple algebraic expressions. I even get the if else statements. Where i get stuck is the functions :mad: . could someone please give me a VERY simple explanation of what they do and are for. Also that thing at the top -int main()-...I don't get that either. :rolleyes:

    2) If somebody could give me an extremely simple game example or a website with one to check ou that would be great. It needs to be thouroughly explained though so I learn something.

  2. mwpeters8182 macrumors 6502

    Apr 16, 2003
    Boston, MA
    I'd check out a book on basic C/C++ programming. Anything like a game example would be far too complicated for someone who's just started programming (which it sounds like you are). I'd suggest the SAMS books, or something along those lines.

    Have you studied any of the counting structures yet? I would do that before functions. But the best way to learn is by getting a book, and programming any of the examples.

  3. Mechcozmo macrumors 603


    Jul 17, 2004
    A function is a piece of code that you write to compute, say, sales tax. Then, wherever you are in your code, you type the name of the function and how much the item costs, and set the function equal to a variable. The function goes, calculates the sales tax, and then returns the value (initial value+sales tax) which you can then use. Better than re writing a million lines of code to get the sales tax every time you need it.
  4. slooksterPSV macrumors 68040


    Apr 17, 2004
    int main() ( or even if you wanted to use void main() ) is the programs starting execution. It looks there to begin the program. The reason people use int is because they can send return codes at the end example:
    #include <iostream>

    using namespace std

    int main()
    //some code
    return program_code//0 if everything functioned correctly
      //x if it didn't which it'll let you know
      // but most programmers just put return 0;
    Now with int main you can do some pretty cool things, you can have it to where the user sends arguments that do things when you load the program. NOTE: THIS NEXT EXAMPLE ISN'T 100% CORRECT! LOOK IT UP!


    ... //started all the functions, we're skipping writing code

    bool start(char[] v)
    bool execution_code;
    //more code
    return execution_code;

    int main (int argvchar[] argcchar *args)
    bool worked 0;
    argv == 1) && (argc == '-v')) //lets say -v is for verbose mode
    worked start(argc);

    3 dots (...) signifies, code would go there, but they didn't type it or that. There's a quick example. Pretty soon you can make cool looking functions that just pass functions like this
    (*)(apple)(x, y) //in sense that would look like this for calling that function
    apple(x, y);

    There's my 2 cents
  5. Roark thread starter macrumors newbie


    Dec 21, 2004

    Thanks for the help but I don't think I asked the right way. I wanted to see a very simple game and its code EXPLAINED (even if I don't understand it). You know. Something 2d and simple so I could know where I should work up to.

    Also... I need an editor (I have Taco if that works) and a free easy to download and install compiler.

  6. hcuar macrumors 65816


    Jul 23, 2004
    I think you are a long, long way from understanding even a simple 2d game. I'd recommend going out and buying a book as mentioned before. I you aren't understanding simple function declarations, you're a long way from understanding object oriented programming. :cool:
  7. Toreador93 macrumors regular

    Sep 14, 2003

    A game, I think, would be very complicated. I've taken C++ I and part of C++ II, and I wouldn't know where to begin programming a game like Pong.
  8. locrelite macrumors newbie

    Feb 10, 2006

    What is this?

    I've been trying to get help from programmers with some conceptual stuff, and I get more of these "You won't understand, buy a book, step off".... Doesn't it piss you off? Why would anybody bother writing something like this? I you aren't understanding them.

    A function is mini program. It is a set of commands, given a name that can be called later, thereby executing the commands. I write actionscript and php, so my syntax would be relatively alien; but for something like breakout, you would have a couple of functions called, say, "impact".

    Now you would lay out a mathematical description of the ball's motion, and upon any change in the state of the ball, you compare its coordinates to the
    coordinates of the bricks and the paddle-thingy, whatever it is.

    if (the coordinates overlap) {
    call the impact function

    then send the angle and nature of the collision into the impact function. If it's a breakable brick, it's destroyed, if its the paddle, it may call another function determining the movement of a panel... regardless, the impact function will calculate the new trajectory of the ball, and determine if the thing it hit was destroyed. If it was destroyed, it would call a destroy function, which would remove the coordinates of the brick from the possible obstacle list (database, array, what-you-will).

    Now everytime the ball hits something, it calls the impact function, which determines essentially everything necessary for the game to run (sans graphics and GUI).
  9. zimv20 macrumors 601


    Jul 18, 2002
    there are, of course, a million different ways to code a solution to a given problem.

    but if i were to write a version of breakout, i wouldn't do it like this. not if i had an OO language at my disposal.
  10. gekko513 macrumors 603


    Oct 16, 2003
    It still doesn't mean that someone new to programming can start learning by looking at something as complex as a game. Even a simple game.

    Maybe a nice straight forward animation would work as a suitable motivator because something actually happens on the screen while it's still simple enough to grasp.
  11. gekko513 macrumors 603


    Oct 16, 2003
    The source code for the relatively simple 2D scroller SuperTux.
    The source:
    Get it here.

Share This Page