跪求在VC/C++环境下运行的小游戏完整源代码,如:贪吃蛇、推箱子啊都可以。邮箱609083568@qq.com
2个回答
展开全部
#include <windows.h>
#include <conio.h>
#include <iostream>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27
#define EMPTY 0
#define WALL 1
#define BOX 2
#define GOAL 3
#define MAN 4
#define MAXMISSION 4
using namespace std;
typedef struct point
{
int x;
int y;
}point,Man,Goal;
class frame
{
public:
frame();
void go(int y, int x);
void color(int n_color);
void drawScrop();
void changeScrop();
void findMan();
void begin();
void setGoal();
bool manOnGoal();
int findGoal();
private:
int lev;
int step;
int scrop[10][19];
int GoalNum;
Goal CopyGoal[10];
Man man;
};
frame::frame()
{
GoalNum=0;
lev=1;
man.x=0;
man.y=0;
step=0;
int i,j;
system("title 推箱子ver1.0 by Zane");
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
scrop[i][j]=EMPTY;
color(0x07);
cout<<"┏━━━━━━━━━┓";
go(0,10);
for (i=1; i<10; i++)
{
go(i,0);
cout<<"┃ ┃";
}
go(10,0);
cout<<"┗━━━━━━━━━┛";
go(1,25);
cout<<"关数:";
go(2,24);
cout<<"┏━━━━┓";
go(3,24);
cout<<"┃ ┃";
go(4,24);
cout<<"┗━━━━┛";
go(6,25);
cout<<"步数:";
go(7,24);
cout<<"┏━━━━┓";
go(8,24);
cout<<"┃ ┃";
go(9,24);
cout<<"┗━━━━┛";
go(1,38);
cout<<"提示:";
go(3,38);
cout<<"可以通过输入数字来选择关卡";
}
void frame::go(int y, int x)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void frame::color(int n_color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n_color);
}
void frame::changeScrop()
{
int i,j;
step=0;
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
scrop[i][j]=EMPTY;
switch(lev)
{
case 1:
GoalNum=1;
for(i=2;i<19;i+=2)
{
scrop[1][i]=WALL;
scrop[9][i]=WALL;
}
for(i=1;i<10;i++)
{
scrop[i][2]=WALL;
scrop[i][18]=WALL;
}
scrop[4][6]=MAN;
scrop[4][10]=BOX;
scrop[7][12]=GOAL;
CopyGoal[0].x=7;
CopyGoal[0].y=12;
break;
case 2:
GoalNum=3;
for(i=2;i<11;i+=2)
scrop[1][i]=WALL;
scrop[2][2]=WALL;scrop[2][10]=WALL;
for(i=3;i<6;i++)
{
scrop[i][2]=WALL;
scrop[i][10]=WALL;
scrop[i][14]=WALL;
scrop[i][18]=WALL;
}
for(i=5;i<10;i++)
{
scrop[i][4]=WALL;
scrop[i][6]=WALL;
scrop[i][12]=WALL;
scrop[i][18]=WALL;
}
scrop[5][12]=WALL;scrop[3][16]=WALL;
scrop[7][6]=EMPTY;scrop[8][6]=EMPTY;scrop[6][12]=EMPTY;scrop[9][18]=EMPTY;
scrop[8][14]=WALL;scrop[8][16]=WALL;
scrop[9][6]=WALL;scrop[9][8]=WALL;scrop[9][10]=WALL;
scrop[4][4]=MAN;
scrop[3][6]=BOX;scrop[3][8]=BOX;scrop[4][6]=BOX;
for(i=0,j=4;j<7;j++,i++)
{
CopyGoal[i].x=j;
CopyGoal[i].y=16;
scrop[CopyGoal[i].x][CopyGoal[i].y]=GOAL;
}
break;
case 3:
GoalNum=2;
for(i=2;i<9;i+=2)
scrop[1][i]=WALL;
for(i=2;i<9;i++)
{
scrop[i][2]=WALL;
scrop[i][8]=WALL;
}
scrop[8][4]=WALL;scrop[8][6]=WALL;scrop[4][10]=WALL;scrop[7][10]=WALL;
for(i=4;i<8;i++)
scrop[i][12]=WALL;
scrop[5][6]=BOX;scrop[5][8]=BOX;scrop[5][10]=MAN;
CopyGoal[0].x=5;CopyGoal[0].y=4;
scrop[CopyGoal[0].x][CopyGoal[0].y]=GOAL;
CopyGoal[1].x=6;CopyGoal[1].y=8;
scrop[CopyGoal[1].x][CopyGoal[1].y]=GOAL;
break;
case 4:
GoalNum=3;
for(i=2;i<13;i+=2)
{
scrop[1][i]=WALL;
scrop[6][i]=WALL;
}
for(i=2;i<6;i++)
{
scrop[i][2]=WALL;
scrop[i][12]=WALL;
}
scrop[2][6]=WALL;scrop[5][4]=WALL;scrop[6][2]=EMPTY;
scrop[3][6]=BOX;scrop[3][8]=BOX;scrop[4][8]=BOX;
scrop[3][4]=MAN;
CopyGoal[0].x=2;CopyGoal[0].y=4;
scrop[CopyGoal[0].x][CopyGoal[0].y]=GOAL;
CopyGoal[1].x=4;CopyGoal[1].y=4;
scrop[CopyGoal[1].x][CopyGoal[1].y]=GOAL;
CopyGoal[2].x=4;CopyGoal[2].y=6;
scrop[CopyGoal[2].x][CopyGoal[2].y]=GOAL;
break;
}
}
void frame::drawScrop()
{
int i,j;
go(1,2);
color(0x07);
for(i=1;i<10;i++)
{
for(j=2;j<19;j+=2)
{
switch(scrop[i][j])
{
case EMPTY:
cout<<" ";
break;
case WALL:
cout<<"■";
break;
case BOX:
cout<<"□";
break;
case GOAL:
cout<<"×";
break;
case MAN:
cout<<"☆";
break;
}
}
go(i+1,2);
}
go(3,26);
cout<<lev;
go(8,26);
cout<<" ";
go(8,26);
cout<<step;
go(11,2);
}
void frame::findMan()
{
int i,j;
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
{
if(scrop[i][j]==MAN)
{
man.x=i;
man.y=j;
return;
}
}
}
int frame::findGoal()
{
int i,j,k=0;
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
if(scrop[i][j]==GOAL)
k++;
return k;
}
void frame::setGoal()
{
int i;
for(i=0;i<GoalNum;i++)
if(scrop[CopyGoal[i].x][CopyGoal[i].y]==EMPTY)
scrop[CopyGoal[i].x][CopyGoal[i].y]=GOAL;
}
bool frame::manOnGoal()
{
int i;
findMan();
for(i=0;i<GoalNum;i++)
if(CopyGoal[i].x==man.x&&CopyGoal[i].y==man.y)
return true;
return false;
}
void frame::begin()
{
bool ManOnGoal=0,k2=0;
changeScrop();
drawScrop();
while(1)
{
findMan();
if(_kbhit())
{
int key=_getch();
switch (key)
{
case UP:
if(scrop[man.x-1][man.y]==EMPTY||scrop[man.x-1][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x-1][man.y]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x-1][man.y]==BOX)
{
if(scrop[man.x-2][man.y]==EMPTY||scrop[man.x-2][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x-1][man.y]=MAN;
scrop[man.x-2][man.y]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
if(lev<MAXMISSION)
lev++;
changeScrop();
}
}
}
drawScrop();
step++;
break;
case DOWN:
if(scrop[man.x+1][man.y]==EMPTY||scrop[man.x+1][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x+1][man.y]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x+1][man.y]==BOX)
{
if(scrop[man.x+2][man.y]==EMPTY||scrop[man.x+2][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x+1][man.y]=MAN;
scrop[man.x+2][man.y]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
lev++;
changeScrop();
}
}
}
drawScrop();
step++;
break;
case LEFT:
if(scrop[man.x][man.y-2]==EMPTY||scrop[man.x][man.y-2]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y-2]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x][man.y-2]==BOX)
{
if(scrop[man.x][man.y-4]==EMPTY||scrop[man.x][man.y-4]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y-2]=MAN;
scrop[man.x][man.y-4]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
lev++;
changeScrop();
}
}
}
setGoal();
drawScrop();
break;
case RIGHT:
if(scrop[man.x][man.y+2]==EMPTY||scrop[man.x][man.y+2]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y+2]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x][man.y+2]==BOX)
{
if(scrop[man.x][man.y+4]==EMPTY||scrop[man.x][man.y+4]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y+2]=MAN;
scrop[man.x][man.y+4]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
lev++;
changeScrop();
}
}
}
setGoal();
drawScrop();
break;
case '1':
lev=1;
changeScrop();
setGoal();
drawScrop();
break;
case '2':
lev=2;
changeScrop();
setGoal();
drawScrop();
break;
case '3':
lev=3;
changeScrop();
setGoal();
drawScrop();
break;
case '4':
lev=4;
changeScrop();
setGoal();
drawScrop();
break;
case ESC:
exit(-1);
break;
}
}
}
}
int main()
{
frame f;
f.begin();
return 0;
}
#include <conio.h>
#include <iostream>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27
#define EMPTY 0
#define WALL 1
#define BOX 2
#define GOAL 3
#define MAN 4
#define MAXMISSION 4
using namespace std;
typedef struct point
{
int x;
int y;
}point,Man,Goal;
class frame
{
public:
frame();
void go(int y, int x);
void color(int n_color);
void drawScrop();
void changeScrop();
void findMan();
void begin();
void setGoal();
bool manOnGoal();
int findGoal();
private:
int lev;
int step;
int scrop[10][19];
int GoalNum;
Goal CopyGoal[10];
Man man;
};
frame::frame()
{
GoalNum=0;
lev=1;
man.x=0;
man.y=0;
step=0;
int i,j;
system("title 推箱子ver1.0 by Zane");
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
scrop[i][j]=EMPTY;
color(0x07);
cout<<"┏━━━━━━━━━┓";
go(0,10);
for (i=1; i<10; i++)
{
go(i,0);
cout<<"┃ ┃";
}
go(10,0);
cout<<"┗━━━━━━━━━┛";
go(1,25);
cout<<"关数:";
go(2,24);
cout<<"┏━━━━┓";
go(3,24);
cout<<"┃ ┃";
go(4,24);
cout<<"┗━━━━┛";
go(6,25);
cout<<"步数:";
go(7,24);
cout<<"┏━━━━┓";
go(8,24);
cout<<"┃ ┃";
go(9,24);
cout<<"┗━━━━┛";
go(1,38);
cout<<"提示:";
go(3,38);
cout<<"可以通过输入数字来选择关卡";
}
void frame::go(int y, int x)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void frame::color(int n_color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n_color);
}
void frame::changeScrop()
{
int i,j;
step=0;
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
scrop[i][j]=EMPTY;
switch(lev)
{
case 1:
GoalNum=1;
for(i=2;i<19;i+=2)
{
scrop[1][i]=WALL;
scrop[9][i]=WALL;
}
for(i=1;i<10;i++)
{
scrop[i][2]=WALL;
scrop[i][18]=WALL;
}
scrop[4][6]=MAN;
scrop[4][10]=BOX;
scrop[7][12]=GOAL;
CopyGoal[0].x=7;
CopyGoal[0].y=12;
break;
case 2:
GoalNum=3;
for(i=2;i<11;i+=2)
scrop[1][i]=WALL;
scrop[2][2]=WALL;scrop[2][10]=WALL;
for(i=3;i<6;i++)
{
scrop[i][2]=WALL;
scrop[i][10]=WALL;
scrop[i][14]=WALL;
scrop[i][18]=WALL;
}
for(i=5;i<10;i++)
{
scrop[i][4]=WALL;
scrop[i][6]=WALL;
scrop[i][12]=WALL;
scrop[i][18]=WALL;
}
scrop[5][12]=WALL;scrop[3][16]=WALL;
scrop[7][6]=EMPTY;scrop[8][6]=EMPTY;scrop[6][12]=EMPTY;scrop[9][18]=EMPTY;
scrop[8][14]=WALL;scrop[8][16]=WALL;
scrop[9][6]=WALL;scrop[9][8]=WALL;scrop[9][10]=WALL;
scrop[4][4]=MAN;
scrop[3][6]=BOX;scrop[3][8]=BOX;scrop[4][6]=BOX;
for(i=0,j=4;j<7;j++,i++)
{
CopyGoal[i].x=j;
CopyGoal[i].y=16;
scrop[CopyGoal[i].x][CopyGoal[i].y]=GOAL;
}
break;
case 3:
GoalNum=2;
for(i=2;i<9;i+=2)
scrop[1][i]=WALL;
for(i=2;i<9;i++)
{
scrop[i][2]=WALL;
scrop[i][8]=WALL;
}
scrop[8][4]=WALL;scrop[8][6]=WALL;scrop[4][10]=WALL;scrop[7][10]=WALL;
for(i=4;i<8;i++)
scrop[i][12]=WALL;
scrop[5][6]=BOX;scrop[5][8]=BOX;scrop[5][10]=MAN;
CopyGoal[0].x=5;CopyGoal[0].y=4;
scrop[CopyGoal[0].x][CopyGoal[0].y]=GOAL;
CopyGoal[1].x=6;CopyGoal[1].y=8;
scrop[CopyGoal[1].x][CopyGoal[1].y]=GOAL;
break;
case 4:
GoalNum=3;
for(i=2;i<13;i+=2)
{
scrop[1][i]=WALL;
scrop[6][i]=WALL;
}
for(i=2;i<6;i++)
{
scrop[i][2]=WALL;
scrop[i][12]=WALL;
}
scrop[2][6]=WALL;scrop[5][4]=WALL;scrop[6][2]=EMPTY;
scrop[3][6]=BOX;scrop[3][8]=BOX;scrop[4][8]=BOX;
scrop[3][4]=MAN;
CopyGoal[0].x=2;CopyGoal[0].y=4;
scrop[CopyGoal[0].x][CopyGoal[0].y]=GOAL;
CopyGoal[1].x=4;CopyGoal[1].y=4;
scrop[CopyGoal[1].x][CopyGoal[1].y]=GOAL;
CopyGoal[2].x=4;CopyGoal[2].y=6;
scrop[CopyGoal[2].x][CopyGoal[2].y]=GOAL;
break;
}
}
void frame::drawScrop()
{
int i,j;
go(1,2);
color(0x07);
for(i=1;i<10;i++)
{
for(j=2;j<19;j+=2)
{
switch(scrop[i][j])
{
case EMPTY:
cout<<" ";
break;
case WALL:
cout<<"■";
break;
case BOX:
cout<<"□";
break;
case GOAL:
cout<<"×";
break;
case MAN:
cout<<"☆";
break;
}
}
go(i+1,2);
}
go(3,26);
cout<<lev;
go(8,26);
cout<<" ";
go(8,26);
cout<<step;
go(11,2);
}
void frame::findMan()
{
int i,j;
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
{
if(scrop[i][j]==MAN)
{
man.x=i;
man.y=j;
return;
}
}
}
int frame::findGoal()
{
int i,j,k=0;
for(i=1;i<10;i++)
for(j=2;j<19;j+=2)
if(scrop[i][j]==GOAL)
k++;
return k;
}
void frame::setGoal()
{
int i;
for(i=0;i<GoalNum;i++)
if(scrop[CopyGoal[i].x][CopyGoal[i].y]==EMPTY)
scrop[CopyGoal[i].x][CopyGoal[i].y]=GOAL;
}
bool frame::manOnGoal()
{
int i;
findMan();
for(i=0;i<GoalNum;i++)
if(CopyGoal[i].x==man.x&&CopyGoal[i].y==man.y)
return true;
return false;
}
void frame::begin()
{
bool ManOnGoal=0,k2=0;
changeScrop();
drawScrop();
while(1)
{
findMan();
if(_kbhit())
{
int key=_getch();
switch (key)
{
case UP:
if(scrop[man.x-1][man.y]==EMPTY||scrop[man.x-1][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x-1][man.y]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x-1][man.y]==BOX)
{
if(scrop[man.x-2][man.y]==EMPTY||scrop[man.x-2][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x-1][man.y]=MAN;
scrop[man.x-2][man.y]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
if(lev<MAXMISSION)
lev++;
changeScrop();
}
}
}
drawScrop();
step++;
break;
case DOWN:
if(scrop[man.x+1][man.y]==EMPTY||scrop[man.x+1][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x+1][man.y]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x+1][man.y]==BOX)
{
if(scrop[man.x+2][man.y]==EMPTY||scrop[man.x+2][man.y]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x+1][man.y]=MAN;
scrop[man.x+2][man.y]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
lev++;
changeScrop();
}
}
}
drawScrop();
step++;
break;
case LEFT:
if(scrop[man.x][man.y-2]==EMPTY||scrop[man.x][man.y-2]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y-2]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x][man.y-2]==BOX)
{
if(scrop[man.x][man.y-4]==EMPTY||scrop[man.x][man.y-4]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y-2]=MAN;
scrop[man.x][man.y-4]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
lev++;
changeScrop();
}
}
}
setGoal();
drawScrop();
break;
case RIGHT:
if(scrop[man.x][man.y+2]==EMPTY||scrop[man.x][man.y+2]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y+2]=MAN;
setGoal();
drawScrop();
step++;
}
else
if(scrop[man.x][man.y+2]==BOX)
{
if(scrop[man.x][man.y+4]==EMPTY||scrop[man.x][man.y+4]==GOAL)
{
scrop[man.x][man.y]=EMPTY;
scrop[man.x][man.y+2]=MAN;
scrop[man.x][man.y+4]=BOX;
setGoal();
drawScrop();
step++;
if(findGoal()==0&&!manOnGoal())
{
lev++;
changeScrop();
}
}
}
setGoal();
drawScrop();
break;
case '1':
lev=1;
changeScrop();
setGoal();
drawScrop();
break;
case '2':
lev=2;
changeScrop();
setGoal();
drawScrop();
break;
case '3':
lev=3;
changeScrop();
setGoal();
drawScrop();
break;
case '4':
lev=4;
changeScrop();
setGoal();
drawScrop();
break;
case ESC:
exit(-1);
break;
}
}
}
}
int main()
{
frame f;
f.begin();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询