Skip to content

Instantly share code, notes, and snippets.

@Gi133
Created November 22, 2011 11:29
Show Gist options
  • Select an option

  • Save Gi133/1385461 to your computer and use it in GitHub Desktop.

Select an option

Save Gi133/1385461 to your computer and use it in GitHub Desktop.
Tamagotchi
// Tamagochi Game
/*
Student Name: Andreas Xirotyris
Student Number: 1005312
This pet application simulates a really hungry pet.
The game itself monitors a variable called FOOD and changes the pet's state accordingly.
There are 6 stages, ranging from Starving to Exploding (mainly because the player fed it way too much in a short amount of time).
You can Feed your pet by pressing F, or Kill it by pressing K or just leaving it without food for a bit.
Known Bugs:
Nill
Fixed Bugs:
Flickering -Fixed by writting a function that sets the pointer to a specific location of the CMD, and then just overwrite the text-
*/
// Globals
#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
#include <time.h>
using namespace std;
enum State {Danger, Stuffed, Peckish, Hungry, Starving, Explode}; //Declaring the pet's states.
//Function to set pointer to x and y coordinates.
void gotoxy ( short x, short y )
{
COORD coord = {x, y};
SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}
int main()
{
//Declaring variables. The initial food level and state of the pet
State S=Stuffed;
int food=100;
string action, petname;
char key;
//Asking the player to make his or her pet
cout<<"Pick a name for your pet: ";
cin>>petname;
//Go back to top after name is written to display the game's keys.
gotoxy(0,0);
cout<<"Commands: K to kill (set food to 1), F to feed your pet";
//Main game loop, aslong as food is not 0 the program will stay in the loop.
while (food>=0)
{
if (food>=400)
{
S=Explode;
}
else if (food>=300 && food<400)
{
S=Danger;
}
else if (food>=75 && food<300)
{
S=Stuffed;
}
else if (food>=50 && food<75)
{
S=Peckish;
}
else if (food>=25 && food<50)
{
S=Hungry;
}
else if (food>=1 && food<25)
{
S=Starving;
}
//Detect button presses
if (_kbhit())
{
key = toupper(_getch());
if (key=='F')
{
//Pressing F will add 10 to FOOD
food+=10;
}
if (key=='K')
{
//Pressing K will kill the pet by setting it's FOOD variable to 0.
food=0;
}
}
//Text output for each state
switch(S)
{
case Danger:
gotoxy(0,1);
cout<< petname<<" is about to have a heart attack, stop feeding it!";
cout<< "\n";
cout<< "Food: "<<food;
cout<< " ";
cout<< "\n";
break;
case Stuffed:
gotoxy(0,1);
cout<< petname<<" is stuffed.";
cout<< " ";
cout<< "\n";
cout<< "Food: "<<food;
cout<< " ";
cout<< "\n";
break;
case Peckish:
gotoxy(0,1);
cout<< petname<<" is slightly peckish.";
cout<< " ";
cout<< "\n";
cout<< "Food: "<<food;
cout<< " ";
cout<< "\n";
break;
case Hungry:
gotoxy(0,1);
cout<< petname<<" is hungry.";
cout<< " ";
cout<< "\n";
cout<< "Food :"<<food;
cout<< " ";
cout<< "\n";
break;
case Starving:
gotoxy(0,1);
cout<< petname<<" is Starving!";
cout<< " ";
cout<< "\n";
cout<< "Food: "<<food;
cout<< " ";
cout<< "\n";
break;
case Explode:
gotoxy(0,1);
food=0;
cout<< petname<<" has exploded?!";
cout<< " ";
cout<< "\n";
cout<< "Food: "<<food;
cout<< " ";
cout<< "\n";
Sleep(2500);
break;
}
Sleep(100);
//Pet eats food like there's no tomorrow.
food--;
}
//If food reaches 0, pet will die
if (food<=0)
{
gotoxy(0,1);
cout << "Your pet, called "<<petname<<", is DEAD!!";
cout << "\n";
cout << "The game will now exit...";
Sleep(3000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment