#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
////小人位置
int position_x;
int position_y;
//星星位置
int position_star_x;
int position_star_y;
void gotoxy(int x,int y)//绫类似于清屏函数,光标移动到原点位置进行重画
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void Init()
{
position_x = 50;
position_y = 20;
position_star_x=50;
position_star_y = 0;
}
char person[5][20] =
{
" * ",
" * ",
"*****",
" * * ",
"* *",
};
void updateWithInput() //
{
char input;
if(kbhit()) // 判断是否有输入
{
input = getch(); //
if (input == 'a')
position_x--; // 位置左移
if (input == 'd')
position_x++; // 位置右移
}
}
int Scores = 0;
void UpdatePanel()
{
gotoxy(100, 0); //挪动鼠标到屏幕中央
printf("积分:%d",Scores);
int i;
for(i=0;i<5;i++)
{
gotoxy(position_x,position_y+i);
printf(person[i]);
}
//刷新星星位置
gotoxy(position_star_x,position_star_y);
printf("*");
position_star_y++;
//计算是否落到了小人身上
double dis = sqrt(pow((position_star_x - position_x),2)-pow((position_star_y - position_y),2));
if(dis<2)
{
Scores++;
position_star_y = 0;
position_star_x=rand()%100;
}
else
{
if(position_star_y==20)
{
position_star_y = 0;
position_star_x=rand()%100;
}
}
}
int main(int argc, char *argv[])
{
Init();
while(1)
{
system("cls");
UpdatePanel();
updateWithInput();
}
return 0;
}