Jump to content

  • Log In with Google      Sign In   
  • Create Account


Zael

Member Since 28 Jan 2008
Offline Last Active May 17 2012 09:36 AM
-----

Posts I've Made

In Topic: Android Image Loading

30 April 2012 - 09:50 AM

Is there any reason you are loading them from assets instead of resources? As I recall loading them from resources is slightly easier.

In Topic: Need some help and advice for creating my first game

29 April 2012 - 01:13 PM

This has been posted many times before. In short though:

C# and XNA are good for a beginner. You should probably be prepared to make your first several programs (games) text based stuff to get a feel for program flow and the art of programming. Visual Studio Express edition is free from Microsoft's website. I don't actually do much C# myself, so I can't really help as far as tutorials go.

Good luck!

In Topic: Help me please!

25 April 2012 - 03:16 PM

I will see if I can take a moment to look at it more in-depth over the next few evening, but right off the bat, you can make it a lot cleaner if you look at using functions. Functions help you abstract different procedures and not only easily re-use code, but also make the code itself more readable. In case you haven't encountered them before I have included a small example.

#include <iostream>

using namespace std;

int sumOfRange(int a, int b);

int main()
{
	 int a, b;
	 cout << "Provide first number: ";
	 cin >> a;
	 cout << "Provide second number: ";
	 cin >> b;
	 cout << "The sum of all numbers between " << a << " and " << b << " is: " << sumOfRange(a, b) << endl;
	 int c,d;
	 cout << "Provide first number: ";
	 cin >> c;
	 cout << "Provide second number: ";
	 cin >> d;
	 cout << "The sum of all numbers between " << c << " and " << d << " is: " << sumOfRange(c, d) << endl;
}

int sumOfRange(int a, int b)
{
	 int total = 0;
	 for(int i = a; i <= b; i++)
	 {
		  total+=i;
	 }
	 return total;
}


The first thing I do is declare the function on line 5. All functions must be declared before using. A function is comprised of really three parts. The first is the return type. In this case the function returns an int. The second is the name of the function. This is how the function is called. The last thing is a list of parameters. I believe there is an upper limit on the number of parameters a function can have, but I have never hit that limit (it is pretty high). Each parameter is essentially a variable that is declared just like you would inside a function except that it is a copy of the whatever value was passed to the function.

The next thing I do is define and declare my main function. Inside my main function I use the the function I declared on line 5. Finally after my main function I define my function. The function definition is essentially the code that is executed when the code is used.

A minor, but essential thing to understand is that because parameters are copies, if you change the value of a parameter in a function the variable used when calling the function is unchanged.

#include <iostream>

using namespace std;

void functionThatDoesNothing(int a)
{

	 a = 10;
}

int main()
{
	 int a = 5;
	 cout << "The variable 'a' is: "<< a << endl;
	 functionThatDoesNothing(a);
	 cout << "The variable 'a' is still "<< a << endl;
	 return 0;
}

You can see from this example that the function does not change a. I have also shown that you can define the function at the same time you declare it. It is usually best to define and declare the function separately if you have more than one file (declarations go in the .h or .hpp file and definitions go in the .cpp file).

If you want to have the parameter directly affect the variable that was used when calling the function you can use what is called "pass by reference". Whether a function parameter is "pass by value" or "pass by reference" depends on the way it is declared in the function declaration. To make a variable "pass by reference" you simply place an '&' character before the variable name. Below I will demonstrate the same code as above, except with "pass by reference".


#include <iostream>

using namespace std;

void functionThatDoesSomething(int & a)
{

	 a = 10;
}

int main()
{
	 int a = 5;
	 cout << "The variable 'a' is: "<< a << endl;
	 functionThatDoesSomething(a);
	 cout << "The variable 'a' is now "<< a << endl;
	 return 0;
}

"Pass by reference" can get a little bit dangerous (your code will crash) if you use it in certain ways (like for the return type of a function), but for the way I have demonstrated above it should be safe in most if not all situations.

P.S. Classes and Structs can also make code a lot cleaner and more readable, but I think learning to write your own functions first is a nice step in the right direction.

In Topic: Help me please!

23 April 2012 - 10:35 AM

Didn't mean to insult you. You would be surprised at some of the simple things people will ask how to do. In a cmd window I am not aware of any mouse functions (could be wrong), so you may still need to do a simple menu like that when the user makes choices. Of course maybe you are more creative than I. Ideally you would use a system library that reads each key as it is pressed (instead of waiting for the user to hit the enter key). Most text based games I have played use a method like that with certain keys toggling a menu.

In Topic: Help me please!

22 April 2012 - 11:17 AM

Typically in a text based game a menu will be simple output with different input option.


Example "Menu":
int choice = 0;
while(choice != 2)
{
	 std::cout << "What would you like to do?\n";
	 std::cout << "1) Start a new game\n";
	 std::cout << "2) Quit\n";
	 cin >> choice;
	 switch (choice)
	 {
		  case 1:
		  startGame();
		  break;
		  case 2:
		  return;
		  break:
     }
}

Does that make sense or are you asking something else?

PARTNERS