Forum Thread
  Posts  
Game program (Forums : Coding & Scripting : Game program) Locked
Thread Options
Mar 10 2014 Anchor

Help Please,,,
This is a project we are doing in our CS programming class, we are planning to do a game program using Dev C++, the game objective is to catch the falling numbers with a “pot” and reach the designated limit before time runs out. But we have a hard time on how to program this,,, this is what we have done so far,,
ThanK you!
‪#‎include‬
#include
#include
#include
#include
enum Direction //declare the direction of the moving character.
{
Left,
Right,
};

struct Apple //declare the variables that will be used for the object apple. //A structure type is a user-defined composite type. It is composed of fields or members that can have different types.
{
int Xpos; //horizontal position of the apple.
int Ypos; //vertical position of the apple.
BOOL Falling; // true or false, test if the apple is falling.
BOOL swtch;

}Apple[500]; // an array composing of 500 rows

struct Hero //declare the variables that will be used for the object hero.
{
int Xpos; //horizontal position of the hero.
int Ypos; //vertical position of the hero.
Direction direction; // direction either left/right of the hero.
int Frame; //use for the display of each action.
}Hero;

CHAR _Apple[9][11] = //_Apple is a 2D array composed of characters with 9 rows and 11 columns
{
" # ",
" ## ",
" # ",
" # ",
" ### ",

};
CHAR _AppleA[9][11] = //_Apple is a 2D array composed of characters with 9 rows and 11 columns
{
" #### ",
" # #",
" # #",
" # #",
" #### ",

};
CHAR _AppleB[9][11] = //_Apple is a 2D array composed of characters with 9 rows and 11 columns
{
" #### ",
" # #",
" # ",
" # ",
" ######",

};
//Hero_Right is a 2D array composed of characters with 26 rows and 20 columns
CHAR Hero_RightA[26][20] =
{

" ###### ",
" # # ",
" # # ",
" ####### ",
};

CHAR Hero_RightB[26][20] =
{
" ###### ",
" # # ",
" # # ",
" ####### ",
};

CHAR Hero_LeftA[26][20] =
{

" ###### ",
" # # ",
" # # ",
" ####### ",
};

CHAR Hero_LeftB[26][20] =
{
" ###### ",
" # # ",
" # # ",
" ####### ",
};

int Score;
int pickRandom(int min, int max)
{
int num;

srand(time(NULL));
num = rand() % (max - min + 1) + min;

return num;

}
void Draw_Hero() //function for drawing the hero
{
for(int Y = 0; Y < 26; Y ++) // loop the rows of the array _hero
{
for(int X = 0; X < 20; X ++) //loop the columns of the array hero
{
COORD Pos; // the coordinates/position of each value of the array hero

Pos.X = X + Hero.Xpos;
Pos.Y = Y + Hero.Ypos;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

switch(Hero.direction)
{
case Left:
if(Hero.Frame == 0)
printf("%c", Hero_LeftA[Y][X]);
else
printf("%c", Hero_LeftB[Y][X]);
break;
case Right:
if(Hero.Frame == 0)
printf("%c", Hero_RightA[Y][X]);
else
printf("%c", Hero_RightB[Y][X]);
break;
}
}
}
}

void Erase_Hero()
{
for(int Y = 0; Y < 26; Y ++)
{
for(int X = 0; X < 20; X ++)
{
COORD Pos;

Pos.X = X + Hero.Xpos;
Pos.Y = Y + Hero.Ypos;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

printf(" ");
}
}
}

void Update_Hero()
{
if(kbhit()) // keyboard hit
{
CHAR Ch = getch();

switch(Ch)
{
case 77:
Erase_Hero();

Hero.direction = Right;

if(Hero.Xpos < 298)
Hero.Xpos += 4;

if(Hero.Frame == 0)
Hero.Frame = 1;
else
Hero.Frame = 0;

Draw_Hero();
break;
case 75:
Erase_Hero();

Hero.direction = Left;

if(Hero.Xpos > 0)
Hero.Xpos -= 4;

if(Hero.Frame == 0)
Hero.Frame = 1;
else
Hero.Frame = 0;

Draw_Hero();
break;
case 27:
exit(0);
break;
}
}
}

void Draw_Apple(int Xpos, int Ypos)
{
for(int Y = 0; Y < 9; Y ++)
{
for(int X = 0; X < 11; X ++)
{
COORD Pos;

Pos.X = X + Xpos;
Pos.Y = Y + Ypos;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

printf("%c", _Apple[Y][X]);
// printf("%c", _Apple[Y][X]);
}
}
}

void Draw_AppleA(int Xpos, int Ypos)
{
for(int Y = 0; Y < 9; Y ++)
{
for(int X = 0; X < 11; X ++)
{
COORD Pos;

Pos.X = X + Xpos;
Pos.Y = Y + Ypos;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

printf("%c", _AppleA[Y][X]);
// printf("%c", _Apple[Y][X]);
}
}
}
void Draw_AppleB(int Xpos, int Ypos)
{
for(int Y = 0; Y < 9; Y ++)
{
for(int X = 0; X < 11; X ++)
{
COORD Pos;

Pos.X = X + Xpos;
Pos.Y = Y + Ypos;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

printf("%c", _AppleB[Y][X]);
// printf("%c", _Apple[Y][X]);
}
}
}

void Erase_Apple(int Xpos, int Ypos)
{
for(int Y = 0; Y < 9; Y ++)
{
for(int X = 0; X < 11; X ++)
{
COORD Pos;

Pos.X = X + Xpos;
Pos.Y = Y + Ypos;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

printf(" ");
}
}
}

void Update_Apples()
{
for(int i = 0; i < 500; i ++)
{
if(Apple[i].Falling)
{
if(Apple[i].Ypos < 72)
{
Erase_Apple(Apple[i].Xpos, Apple[i].Ypos);
Apple[i].Ypos ++;
int x,x2,y,nCounter;
x = pickRandom(1,100); /* picks a random x coordinate from 1 to 79 */
int m = x%2;
if (m == 0){
Draw_AppleA(Apple[i].Xpos, Apple[i].Ypos);
}
else{
Draw_AppleB(Apple[i].Xpos, Apple[i].Ypos);
}
if(Apple[i].Ypos >= Hero.Ypos)
{
if(Apple[i].Xpos >= Hero.Xpos &&
Apple[i].Xpos + 11 <= Hero.Xpos + 20)
{
Score ++;

if(Score >= 10)
{
MessageBox(NULL, "*** [ Congratulations!!! ] *** You collected 10 apples! You win! (:", NULL,
MB_OK);

exit(0);
}
}
}
}
else
{
Erase_Apple(Apple[i].Xpos, Apple[i].Ypos);
Apple[i].Falling = FALSE;

Apple[i + 1].Falling = TRUE;
Apple[i + 1].Xpos = rand() % 289;
Apple[i + 1].Ypos = 0;
}
}
}

}

int main()
{
srand(GetTickCount());

CONSOLE_CURSOR_INFO CursorInfo;

CursorInfo.bVisible = FALSE;
CursorInfo.dwSize = 1;

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CursorInfo);

COORD Size;

Size.X = 320;
Size.Y = 100;

SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), Size);

Hero.Xpos = 0;
Hero.Ypos = 72;
Hero.direction = Right;
Hero.Frame = 0;

Draw_Hero();

Apple[0].Falling = TRUE;
Apple[0].Xpos = rand() % 289;
Apple[0].Ypos = 0;
Draw_Apple(Apple[0].Xpos, Apple[0].Ypos);
while(TRUE)
{
Update_Hero();

static DWORD TickCount = GetTickCount();

if(GetTickCount() - TickCount > 50)
{
Update_Apples();
TickCount = GetTickCount();
}
}
return 0;
}

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.