PDA

View Full Version : C++ Help




Roark
Jan 16, 2005, 11:17 PM
Okay

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.

Thanks,
Roark



mwpeters8182
Jan 16, 2005, 11:36 PM
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.

MP

Mechcozmo
Jan 16, 2005, 11:37 PM
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.

slooksterPSV
Jan 17, 2005, 12:00 AM
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 argv, char[] argc, char *args)
{
bool worked = 0;
if((argv == 1) && (argc == '-v')) //lets say -v is for verbose mode
{
worked = start(argc);
}
return worked;
}



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

Roark
Jan 17, 2005, 11:12 AM
Hey

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.

Thanks

hcuar
Jan 17, 2005, 11:18 AM
Hey

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.

Thanks

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:

Toreador93
Jan 17, 2005, 11:34 AM
http://www.cplusplus.com/

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.

locrelite
Feb 10, 2006, 03:50 PM
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:

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).

zimv20
Feb 10, 2006, 05:54 PM
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).
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.

gekko513
Feb 10, 2006, 06:19 PM
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.

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.

gekko513
Feb 10, 2006, 07:15 PM
The source code for the relatively simple 2D scroller SuperTux.
http://supertux.berlios.de/images/game1-011.jpg
The source:
http://lavacat.com/diverse/tuxSource.png
Get it here. (http://supertux.berlios.de/wiki/index.php/Download/Installation)