创世沈
C++#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
int x,y;
int hp=5;
int score=0;
int rx,ry;
int mx,my;
int mhp=6;
bool monLive=1;
char maze[30][60];
void gotoxy(int x,int y)
{
COORD p={x,y};
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h,p);
}
void buildmap()
{
for(int i=0;i<25;i++)
{
for(int j=0;j<50;j++)
{
if(i==0||i==24||j==0||j==49)maze[i][j]='#';
else maze[i][j]=' ';
}
}
for(int i=0;i<90;i++)
{
int a=1+rand()%23;
int b=1+rand()%48;
maze[a][b]='#';
}
}
void drawmap()
{
for(int i=0;i<25;i++)
{
for(int j=0;j<50;j++)
{
gotoxy(j,i);
cout<<maze[i][j];
}
}
}
void newstar()
{
while(1)
{
int tx=2+rand()%40;
int ty=2+rand()%20;
if(maze[ty][tx]==' ')
{
rx=tx;ry=ty;
break;
}
}
}
void newmon()
{
while(1)
{
int tx=6+rand()%34;
int ty=6+rand()%14;
if(maze[ty][tx]==' '&&abs(tx-x)>8&&abs(ty-y)>8)
{
mx=tx;my=ty;
break;
}
}
}
void monmove()
{
if(!monLive)return;
int nx=mx,ny=my;
int dx=x-mx;
int dy=y-my;
int rd=rand()%5;
if(rd==0&&dx>0&&maze[my][mx+1]==' ')nx++;
else if(rd==1&&dx<0&&maze[my][mx-1]==' ')nx--;
else if(rd==2&&dy>0&&maze[my+1][mx]==' ')ny++;
else if(rd==3&&dy<0&&maze[my-1][mx]==' ')ny--;
gotoxy(mx,my);
cout<<" ";
mx=nx;my=ny;
}
int main()
{
HANDLE hc=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ci;
GetConsoleCursorInfo(hc,&ci);
ci.bVisible=0;
SetConsoleCursorInfo(hc,&ci);
srand((unsigned int)time(0));
buildmap();
drawmap();
x=10;y=10;
newstar();
newmon();
monLive=1;
mhp=6;
int movecnt=0;
bool fightcd=0;
int cdtime=0;
while(1)
{
gotoxy(0,0);
cout<<"分数:"<<score<<" 生命:"<<hp<<" 怪物血量:"<<mhp<<" ";
gotoxy(x,y);
cout<<" ";
if(_kbhit())
{
char k=_getch();
int nx=x,ny=y;
if(k=='w')ny--;
if(k=='s')ny++;
if(k=='a')nx--;
if(k=='d')nx++;
if(k=='j'&&!fightcd&&monLive)
{
if(abs(x-mx)<=2&&abs(y-my)<=2)
{
mhp--;
fightcd=1;
cdtime=15;
}
}
if(maze[ny][nx]==' ')
{
x=nx;y=ny;
}
}
if(fightcd)
{
cdtime--;
if(cdtime<=0)fightcd=0;
}
movecnt++;
if(movecnt>=6)
{
monmove();
movecnt=0;
}
if(monLive)
{
gotoxy(mx,my);
cout<<"●";
}
gotoxy(x,y);
cout<<"○";
gotoxy(rx,ry);
cout<<"★";
if(rx==x&&ry==y)
{
score++;
gotoxy(rx,ry);
cout<<" ";
newstar();
}
if(monLive&&mx==x&&my==y)
{
hp--;
gotoxy(x,y);
cout<<" ";
while(1)
{
int tx=8+rand()%25;
int ty=8+rand()%12;
if(maze[ty][tx]==' ')
{
x=tx;y=ty;
break;
}
}
}
if(mhp<=0&&monLive)
{
monLive=0;
score+=80;
gotoxy(18,10);
cout<<"击杀成功+80分";
Sleep(1200);
gotoxy(18,10);
cout<<" ";
gotoxy(mx,my);
cout<<" ";
newmon();
mhp=6;
monLive=1;
}
if(hp<=0)
{
gotoxy(18,10);
cout<<"游戏结束 总分:"<<score;
Sleep(2500);
exit(0);
}
Sleep(70);
}
return 0;
}
//来源:创世沈
//awa