Help - Search - Members - Calendar
Full Version: Easy game to create using C++
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > Miscellaneous Tutorials
wazUPfoo
I created this very simple game using Dev-C++, which is a free program that is easy to use! Download here:http://www.bloodshed.net/dev/devcpp.html

So here's the code, it explains everything in there:
CODE
#include <iostream>//Includes common functions and such
using namespace std;//Use the std namespace
 
int main()//Program entry point
{
bool done=false;//loop quit variable
while(done!=true)//The main program loop.
{
 cout << "1 - Play the number game.\n 2 - Play the letter game.\n3 - Quit.\n";//This is the menu
  int choice=0; //Declare the variable choice and set its value to 0
  cin >> choice;//Get the user input and put it in choice
  switch(choice)
 {
 case 1: //This is the number game, where who ever is playing chooses a number between 1 and 10, and then puts it in!
            int theNum;
            cout << "Welcome to the Number Game!  Enter a number between 1 and 10!\n:";
            cin >> theNum; //This is were the person puts in the number, "theNum" refers to the number
            if (theNum >=1 && theNum <=10) //This means that if the number is greater then 1 and less then 10, it is true
            {
                cout << "Your number was " << theNum << "\n"; //If the number is greater then 1 and less then 10, this is the message you'll get
            }
            else
            {
                cout << " " << theNum << " is greater then 10 smartass!!\n"; //This is what happens if the number is greater then 10
            }
   break;
    case 2: //This is what you get is you press 2 in then menu, which is the letter game, basically the same thing as the number game!
           char theChar;
           cout << "This is the letter game! \n Enter one letter, only 1, or the game will go into a never ending loop!!!\n:";
           cin >> theChar;  //The person puts in the character here, "theChar" refers to the charater
           cout << "Your letter was " << theChar << "!\n"; //If the person puts in 1 letter, then you get thus screen.  If the person puts 2 or more letters the game will go into an infinate loop
  break;
  case 3:
  done=true;//quits the loop
        break;
 default: //This is what you get if you pressed a number you put in the menu wasn't choice 1, 2, or 3
  cout << "That wasn't one of the choices!\n";
    }
  cout << "\n\n\n";//You can put a few new lines in right here!!
} // End the while loop
}//end the program

There it is!
If you have any questions, ask them here, and I'll do my best to answer them since I'm also relatively new! victory.gif rolleyes.gif
Click here to try the game
raenef
Interesting 'game' compiled it, tried it. Not too bad. All it does really is repeat your choice back to ya though. perhaps a suggestion: Have the computer pick a random number (or letter) then the player try and guess it. happy.gif
wazUPfoo
alright, I'll try and figure that out tomarrow =)
raenef
Here, try experimenting around with this code: It should get ya started:

CODE
// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{
   srand(time(0)); // seed random number generator

   int theNumber = rand() % 100 + 1;  //random number between 1 and 100
   int tries = 0, guess;

   cout << "\tWelcome to Guess My Number\n\n";

//Guessing Loop
do
{

  cout << "Enter a guess:";
  cin >> guess;
  ++tries;

  if (guess > theNumber)
      cout << "Too high!\n\n";

  if (guess < theNumber)
      cout << "Too low!\n\n";
} while (guess != theNumber);
cout << "\nThat's it! You got it in" << tries << "guesses!\n";

return 0;
}


Source=
Beginning C++ Game Programming
Michael Dawson
Premier Press

Sidenote: Premier Press makes a ton of good game-programming books if you're intested in that genre.
wazUPfoo
Hey thanks I'll try that out!
I think I'll look up some of the books too =)
Smarty
could you post the game so we can see it?the people who don't have C++
wazUPfoo
Sure, but first I need to know a site were you can upload files like this or.zips
Smarty
try http://www.FreeFileHosting.net They might let you!
wazUPfoo
I found a file uploader =)
Click here!
Bryn
^^ It's now de-hosted.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.