C 언어로 간단한 게임 쓰기 - 베젤 연결 사각형
4507 단어 C 언어 게임
#include
#include
#include
// , "cls"
void gotoxy(int x , int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
//
//void HideCursor()
//{
// CONSOLE_CURSOR_INFO concur_info = {1, 0};
// SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),concur_info);
//}
//
int width,high;//
int pos_x,pos_y; //
int radius; //
int left,right; //
int ball_x,ball_y; //
int ball_vx,ball_vy; //
int target_x,target_y; //
void init() //
{
width = 20;
high = 25;
ball_x = 1;
ball_y = width/2;
ball_vx =1;
ball_vy =1;
pos_x = width/2;
pos_y = high;
radius = 5;
left = pos_x - radius;
right = pos_y + radius;
//HideCursor(); //
}
void show() //
{
gotoxy(0,0); // , “cls”
for(int i = 0;i <= high ; i++)
{
for (int j = 0; j <= width ; j++)
{
if((ball_x ==i) && (ball_y == j))
printf("*");
else if (j == width)
printf("||");
else if(i == high)
printf("=");
else
printf(" ");
}
printf("
");
}
}
void userInput() //
{
}
void userWithOut() //
{
// ,
ball_x = ball_x +ball_vx; //
ball_y = ball_y +ball_vy;
// ,
if((ball_x == 0) || (ball_x == high-1))
ball_vx = -ball_vx;
if((ball_y == 0) || (ball_y == width-1))
ball_vy = -ball_vy;
}
int main()
{
//
init();
while(1)
{
// ,
show();
//
userInput();
//
userWithOut();
}
}
2.리시브를 위해 베젤 기능을 실현하다.
#include
#include
#include
#include
// , "cls"
void gotoxy(int x , int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
//
void HideCursor()
{
CONSOLE_CURSOR_INFO concur_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&concur_info);
}
//
int width,high;//
int pos_x,pos_y; //
int radius; //
int left,right; //
int ball_x,ball_y; //
int ball_vx,ball_vy; //
int ball_num; //
int target_x,target_y; //
void init() //
{
width = 40;
high = 42;
ball_x = 1;
ball_y = width/2;
ball_vx =1;
ball_vy =1;
ball_num = 0;
pos_y = width/2;
pos_x = high;
radius = 2;
left = pos_y - radius;
right = pos_y + radius;
HideCursor(); //
}
void show() //
{
gotoxy(0,0); // , “cls”
int i,j;
for( i= 0;i <= high ; i++)
{
for (j = 0; j <= width ; j++)
{
if((ball_x ==i) && (ball_y == j))
printf("o");
else if (j == width)
printf("|");
else if(i == high)
printf("-");
else if ((i == high-1) && (j>=left) && (j<=right))
printf("*");
else
printf(" ");
}
printf("
");
//Sleep(20);
}
printf(" :%d
",ball_num);
}
void userInput() //
{
char input;
if(kbhit())
{
input =getch();
if(input =='a')
{
pos_y--;
left = pos_y - radius;
right = pos_y + radius;
}
if(input =='d')
{
pos_y++;
left = pos_y - radius;
right = pos_y + radius;
}
}
}
void userWithOut() //
{
if(ball_x == high-1)
{
if ((ball_y>= left) && (ball_y<=right))
{
ball_num++;
//ball_vy = -ball_vy;
ball_vx = -ball_vx;
}
else
{
printf("Game Over!!!");
exit(0);
}
}
// ,
ball_x = ball_x +ball_vx; // d
ball_y = ball_y +ball_vy;
//
if(pos_y < 0)
pos_y = 0;
else if(pos_y>width)
pos_y = width;
// ,
if((ball_x == 0) || (ball_x == high-1))
ball_vx = -ball_vx;
if((ball_y == 0) || (ball_y == width-1))
ball_vy = -ball_vy;
}
int main()
{
//
init();
while(1)
{
// ,
show();
//
userInput();
//
userWithOut();
}
}