Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Roark

macrumors newbie
Original poster
Dec 21, 2004
13
0
Fountainhead
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

macrumors 6502
Apr 16, 2003
411
0
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.

MP
 

Mechcozmo

macrumors 603
Jul 17, 2004
5,215
2
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

macrumors 68040
Apr 17, 2004
3,543
305
Nowheresville
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:
PHP:
#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!

PHP:
... //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

macrumors newbie
Original poster
Dec 21, 2004
13
0
Fountainhead
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

macrumors 65816
Jul 23, 2004
1,065
0
Dallas
Roark said:
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:
 

locrelite

macrumors newbie
Feb 10, 2006
1
0
Absurd

hcuar said:
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
locrelite said:
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

macrumors 603
Oct 16, 2003
6,301
1
locrelite said:
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

macrumors 603
Oct 16, 2003
6,301
1
The source code for the relatively simple 2D scroller SuperTux.
game1-011.jpg

The source:
tuxSource.png

Get it here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.