#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;
#define FIND 50
char map[25][2005];
bool sign=false;
int kill=0;
int stmap_r,enmap_r,stmap_a,enmap_a;
int pl_mosthp=100,pl_row=10,pl_arr=45,pl_hp=100;
int hori=0,vert=0,dire;//1,2,3,4 左,右,下,上;
int isjumping=0;
int mrow[205],marr[205],nowmons=0,monjump[205]={};
int brow[100],barr[100],nowbul=0,buldire[100];
long long times=0;
char key;
void start(){
cout<<" THE GAME(还没想好)"<<endl;
cout<<"提示 wasd控制移动 "<<endl;
cout<<" e键发射子弹 q键退出 "<<endl;
cout<<" 空格暂停 "<<endl;
Sleep(5000);
MessageBox(NULL,TEXT("本游戏会出现一些现象,请注意!"),TEXT("公告"),MB_OK);
MessageBox(NULL,TEXT("一、小怪会在重复时只显示一个'o'!"),TEXT("公告"),MB_OK);
MessageBox(NULL,TEXT("二、小怪被击杀后,会留下一个“尸体“静止不动,会被子弹等物品覆盖!"),TEXT("公告"),MB_OK);
}
void mapjudge(int x,int y) {
stmap_r=x-10;
if(stmap_r<=1) {
stmap_r=1;
}
enmap_r=stmap_r+15;
if(enmap_r>=19) {
enmap_r=19;stmap_r=4;
}
stmap_a=y-70;
if(stmap_a<=0) {
stmap_a=0;
}
enmap_a=stmap_a+140;
}
void color(int a){
if(a==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(a==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(a==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);
if(a==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE);
if(a==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
if(a==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);
if(a==7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
}//白蓝绿紫红靛黄
void gotoxy(int x, int y) {
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
return ;
}
void jump(){
if(isjumping==0){
vert-=4;
isjumping=1;
}
}
void deletebul(int i){
for(int x=i;x<nowbul;x++){
brow[x]=brow[x+1];
barr[x]=barr[x+1];
buldire[x]=buldire[x+1];
}
nowbul--;
}
void shoot(){
nowbul++;
int x=nowbul-1;
buldire[x]=dire;
brow[x]=pl_row;
barr[x]=pl_arr;
}
void bulgo(){
for(int i=0;i<nowbul;i++){
if(buldire[i]==1){
barr[i]--;
map[brow[i]-1][barr[i]]=' ';
}
else if(buldire[i]==2){
barr[i]++;
map[brow[i]-1][barr[i]-2]=' ';
}
else if(buldire[i]==3){
brow[i]++;
map[brow[i]-2][barr[i]-1]=' ';
}
else if(buldire[i]==4){
brow[i]--;
map[brow[i]][barr[i]-1]=' ';
}
if(barr[i]<= 2||barr[i]>=1998||brow[i]<=2||brow[i]>=19||map[brow[i]-1][barr[i]-1]=='#') {
deletebul(i);
}
}
}
void keyinput(){
if(kbhit()) {
key=getch();
if(key=='a') {
hori--;
}
else if(key=='d') {
hori++;
}
else if(key=='s') {
vert++;
}
else if(key=='w') {
jump();
}
else if(key=='e'&&nowbul<=10) {
shoot();
}
else if(key==' ') {
system("pause");
system("cls");
}
else if(key=='q') {
system("cls");
char exit;
cout<<"你确定要退出?(退出 按0,其它 不退出)\n";
exit=getch();
if(exit=='0'){
cout<<"ok 欢迎下次再玩";
Sleep(1000);
sign=true;
}
}
}
}
void go(){
if(hori<0){//←
dire=1;
hori++;
if(pl_arr>=2&&map[pl_row-1][pl_arr-2]!='#'){
pl_arr-=1;
map[pl_row-1][pl_arr]=' ';
}
}
if(hori>0){//→
dire=2;
hori--;
if(pl_arr<=1998&&map[pl_row-1][pl_arr]!='#') {
pl_arr+=1;
map[pl_row-1][pl_arr-2]=' ';
}
}
if(vert<0){//↓
dire=4;
vert++;
if(pl_row>=2&&map[pl_row-2][pl_arr-1]!='#') {
pl_row-=1;
map[pl_row][pl_arr-1]=' ';
}
}
if(vert>0){//↑
dire=3;
vert--;
if(pl_row<=18&&map[pl_row][pl_arr-1]!='#') {
pl_row+=1;
map[pl_row-2][pl_arr-1]=' ';
}
}
}
void gra(){
if(pl_row<=18&&map[pl_row][pl_arr-1]!='#') vert++;
for(int i=0;i<nowmons;i++) {
if(mrow[i]<=18&&map[mrow[i]][marr[i]-1]!='#') {
mrow[i]++; map[mrow[i]-2][marr[i]-1]=' ';
}
}
for(int i=0;i<nowbul;i++){
if(brow[i]<=18&&map[brow[i]][barr[i]-1]!='#'&×%10==0) {
brow[i]++; map[brow[i]-2][barr[i]-1]=' ';
}
}
}
int nearyou(int x, int y) {
float d1,d2,d3,d4;
d1=sqrt(pow(abs(x - pl_row), 2) + pow(abs(y - 1 - pl_arr), 2));
d2=sqrt(pow(abs(x - pl_row), 2) + pow(abs(y + 1 - pl_arr), 2));
d3=sqrt(pow(abs(x - 3 - pl_row), 2) + pow(abs(y - pl_arr), 2));
d4 = sqrt(pow(abs(x + 1 - pl_row), 2) + pow(abs(y - pl_arr), 2));
if (d1 == min(d1, min(d2, min(d3, d4))) && d1<=FIND) return 1;
if (d2 == min(d1, min(d2, min(d3, d4)))&& d2<=FIND) return 2;
if (d3 == min(d1, min(d2, min(d3, d4)))&& d3<=FIND) return 3;
if (d4 == min(d1, min(d2, min(d3, d4)))&& d4<=FIND) return 4;
else return 0;
}
void monsgo() {
for (int i = 0; i < nowmons; i++) {
int dis = nearyou(mrow[i] - 1, marr[i] - 1);
if(times%2==0){
if (dis == 1) {
marr[i]--; map[mrow[i]-1][marr[i]]=' ';
}
else if (dis == 2) {
marr[i]++; map[mrow[i]-1][marr[i]-2]=' ';
}
else if (dis == 3 && monjump[i] == 0) {
mrow[i]-=3; map[mrow[i]+2][marr[i]-1]=' '; monjump[i]=1;
}
else if (dis == 4&&map[mrow[i]][marr[i]-1]!='#') {
mrow[i]++; map[mrow[i]-2][marr[i]-1]=' ';
}
}
}
}
void deletemons(int i){
for(int x=i;x<nowmons;x++){
mrow[x]=mrow[x+1];
marr[x]=marr[x+1];
}
nowmons--;
}
void getbul(){
for(int i=0;i<nowmons;i++){
for(int j=0;j<nowbul;j++){
if(brow[j]==mrow[i]&&barr[j]==marr[i]){
deletebul(j);
deletemons(i);
kill++;
}
}
}
}
void getmons(){
for(int i=0;i<nowmons;i++){
if(abs(mrow[i]-pl_row)<=1&&abs(marr[i]-pl_arr)<=1){
deletemons(i);
pl_hp-=20;
}
}
}
int main(){
for(int i=0; i<20; i++) {
for(int j=0; j<2000; j++) {
map[i][j]=' ';
}
}
for(int i=0;i<2000;i++){
map[18][i]='#';
}
map[17][1995]='0';
system("mode con cols=150 lines=30");
srand(unsigned(time(NULL)));
start();
CONSOLE_CURSOR_INFO cursor_info= {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(int i=0;i<200;i++){
nowmons++;
mrow[i]=rand()%15+3;
marr[i]=rand()%1990+3;
}
while(true){
if(pl_row==18) isjumping=0;
for(int i=0;i<nowmons;i++) if(mrow[i]==18) monjump[i]=0;
keyinput();
gra();
go();
monsgo();
bulgo();
getbul();
getmons();
map[pl_row-1][pl_arr-1]='I';
for(int i=0;i<nowmons;i++) map[mrow[i]-1][marr[i]-1]='o';
for(int i=0;i<nowbul;i++) map[brow[i]-1][barr[i]-1]='*';
for(int i=1; i<=142; i++) printf("-");
printf("\n");
for(int i=stmap_r; i<enmap_r; i++) {
printf("|");
for(int j=stmap_a; j<enmap_a; j++) {
putchar(map[i][j]);
}
printf("|");
printf("\n");
}
for(int i=1; i<=142; i++) printf("-");
printf("\n");
mapjudge(pl_row,pl_arr);
printf("血量 :%d\n",pl_hp);
printf("横坐标 :%d\n",pl_row);
printf("纵坐标 :%d\n",pl_arr);
printf("击败 : %d\n",kill);
//帧率测试
printf("时间 :%lld",times);
times++;
gotoxy(0,0);
if(sign==true) return 0;
if(pl_hp<=0){
system("cls");
cout<<"YOU LOSE!";
return 0;
}
if(map[pl_row-1][pl_arr-1]=='0'){
system("cls");
cout<<"welcome通关!!!\n";
Sleep(1000);
cout<<"放个彩蛋,要看吗?(输入0,要看),其他退出\n";
string x;
cin>>x;
if(x=="0") system("curl ascii.live/rick");
return 0;
}
if(times%10==0&&pl_hp<100) pl_hp++;
}
return 0;
}//彩蛋代码 system("curl ascii.live/rick")
1 个赞
if(time%10==0&&pl_hp<100) pl_hp++;
这里出了错误
time改成times就行
为啥我看不到地图啊
数组越界了
啊?我没有报错啊
改了
针不戳
3Q