Contoh game yang dibuat dengan c++
Mungkin dapat kalian coba :)
Author : Brenton Andrew Saunders ( http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9005&lngWId=3 )
Description: The point of the game is to catch the apples as the fall from the sky. Catch 10 to win. The controls are simple. Left Arrow - Move Left, Right Arrow - Move Right, Escape Key - Quit.
"Berbagilah ilmu yang kita punya"
Mungkin dapat kalian coba :)
#include
#include
#include
#include
enum Direction
{
Left,
Right,
};
struct Apple
{
int Xpos;
int Ypos;
BOOL Falling;
}Apple[500];
struct Hero
{
int Xpos;
int Ypos;
Direction Direction;
int Frame;
}Hero;
CHAR _Apple[9][11] =
{
" ## ####",
" # # # #",
" ### ## ",
" ######## ",
"# # # ",
"# # ",
" # # ",
" # # # ",
" ## ## ",
};
CHAR Hero_RightA[26][20] =
{
" ##### ",
" # # ",
" # # ",
" # # # ",
" # # ## ",
" # # ",
" # # ",
" # # ## ",
" # ## # ",
" # # ",
" ####### ",
" # # ",
" # # # ",
" ## # # ",
" ## # # ",
" ## # # ",
" ####### ",
" # ## ",
" ### # ",
" # # ",
" # # ",
" ###### ",
" # # ",
" # # ",
" ####### ",
};
CHAR Hero_RightB[26][20] =
{
" ##### ",
" # # ",
" # # ",
" # # # ",
" # # ## ",
" # # ",
" # # ",
" # # ## ",
" # ## # ",
" # # ",
" ####### ",
" # # ",
" # ### ",
" ## ## ",
" # # # # ",
" # # ## # # ",
" ######### # ",
" # ## ",
" # # ### ",
" # # # # ",
" # ## ### ## # ",
" # ## # ## ",
" ## # # ## ",
" ## # ## ",
" ### ",
};
CHAR Hero_LeftA[26][20] =
{
" ##### ",
" # # ",
" # # ",
" # # # ",
" ## # # ",
" # # ",
" # # ",
" ## # # ",
" # ## # ",
" # # ",
" ####### ",
" # # ",
" # # # ",
" # # ## ",
" # # ## ",
" # # ## ",
" ####### ",
" ## # ",
" # ### ",
" # # ",
" # # ",
" ###### ",
" # # ",
" # # ",
" ####### ",
};
CHAR Hero_LeftB[26][20] =
{
" ##### ",
" # # ",
" # # ",
" # # # ",
" ## # # ",
" # # ",
" # # ",
" ## # # ",
" # ## # ",
" # # ",
" ####### ",
" # # ",
" ### # ",
" ## ## ",
" # # # # ",
" # # ## # # ",
" # ######### ",
" ## # ",
" ### # # ",
" # # # # ",
" # ## ### ## # ",
" ## # ## # ",
" ## # # ## ",
" ## # ## ",
" ### ",
};
int Score;
void Draw_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);
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())
{
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]);
}
}
}
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 ++;
Draw_Apple(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;
}
}
}
}
void 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();
}
}
}
Author : Brenton Andrew Saunders ( http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9005&lngWId=3 )
Description: The point of the game is to catch the apples as the fall from the sky. Catch 10 to win. The controls are simple. Left Arrow - Move Left, Right Arrow - Move Right, Escape Key - Quit.
"Berbagilah ilmu yang kita punya"
2 komentar
Write komentarasik nih buat di pelajari
Replylumaya buat nambah ilmu..
ReplyBerkomentarlah yang saling membangun dan sesuai topik yang diisajikan,
Dilarang menyematkan hal yang berbau link dan berkomentar kotor.
ConversionConversion EmoticonEmoticon