#include<iostream>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
using namespace std;
#define GAMEWINDOWWIDTH 70
#define GAMEWINDOWHEIGHT 20
int Draw_enemy(int enemy_x, int enemy_y);
int MDraw(int d, int c, int count);
int Game_Rule(int d, int c, int *enemy_x, int *enemy_y);
int GameEnd(int count);
void gotoxy(int y,int x){
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int main(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
int a,b,c=30,d=19,g=60,h=2,A=1,B=1,C,count = 0;
int enemy_x, enemy_y;
cout<<"a后退,d前进,w上升,s下降" <<endl;
Sleep(5000);
srand(clock());
enemy_x = rand()%67+1;
srand(clock());
enemy_y = rand()%18+1;
MDraw(d,c,count);
Draw_enemy(enemy_x, enemy_y);
while(1==1)
{
if(_kbhit()){
char kb = _getch();
switch(kb){
case 27:
goto stop;
case 'w':
if(d==2)
break;
d-=1;
MDraw(d,c,count);
Draw_enemy(enemy_x, enemy_y);
break;
case 's':
if(d==19)
break;
d+=1;
MDraw(d,c,count);
Draw_enemy(enemy_x, enemy_y);
break;
case 'a':
if(c==2)
break;
c-=1;
MDraw(d,c,count);
Draw_enemy(enemy_x, enemy_y);
break;
case 'd':
if(c==GAMEWINDOWWIDTH-2)
break;
c+=1;
MDraw(d,c,count);
Draw_enemy(enemy_x, enemy_y);
break;
}
if(Game_Rule(d,c,&enemy_x,&enemy_y)){
count++;
MDraw(d,c,count);
Draw_enemy(enemy_x, enemy_y);
}
}
#if 0
cin>>b;
if(b==2)
c=c+1;
if(b==1)
c=c-1;
if(b==3)
d=d-1;
if(b==4)
d=d+1;
if(c>=60)
{
c=60;
}
if(c>=58)
{
break;
system("cls");
}
if(c<=0)
{
c=1;
}
if(d>=20)
{
d=20;
}
if(d<=2)
{
d=2;
}
#endif
}
stop:
GameEnd(count);
return 0;
}
int Draw_enemy(int enemy_x, int enemy_y)
{
gotoxy(enemy_y,enemy_x);
cout<<'#';
return 1;
}
int MDraw(int d, int c, int count)
{
system("cls");
gotoxy(1,1);
cout<<"----------------------------------------------------------------------";
for(int i=2;i<20;i++){
gotoxy(i, 1);
cout<<'|';
}
gotoxy(20,1);
cout<<"----------------------------------------------------------------------";
for(int i=2;i<20;i++){
gotoxy(i,GAMEWINDOWWIDTH);
cout<<'|';
}
gotoxy(5,80);
cout<<"count : "<<count;
gotoxy(d,c);
cout<<"☆";
gotoxy(0,0);
return 1;
}
int Game_Rule(int d,int c,int *enemy_x, int *enemy_y)
{
if(d== *enemy_y&&c== *enemy_x){
srand(clock());
*enemy_x = rand()%67+1;
srand(clock());
*enemy_y = rand()%18+1;
return 1;
}
return 0;
}
int GameEnd(int count)
{
system("cls");
cout<<"count : "<<count<<endl;
cout<<"game end"<<endl;
return 1;
}