大大维的游戏机计划1--贪吃蛇v1

时间:2022-10-11 14:50:56

本文为大大维原创,最早于博客园发表,转载请注明出处!!!

虽然本人一直是个免费的游戏测试员(/手动滑稽),但一直有着一个游戏架构师的梦想。正如马爸爸所说,梦想还是要有的,万一实现了呢?

这些天放寒假,有些空闲时间,就想着做一个简单的游戏机。能达到小时候十几块钱买的那种小霸王的功能就差不多了(有贪吃蛇,赛车小游戏,俄罗斯方块,雷霆战机,坦克大战,2048,拼图等),好吧,原谅我有些浮夸风(/手动斜眼)。。。

这不,下面是我这两天抠出来的一个最简单的贪吃蛇小游戏,姑且叫它“大大维的贪吃蛇v1”吧!!!

游戏功能比较简单,后续还需要完善。先将代码在这保存一份,日后看看,也是一件挺有意思的事情吧!!!

 /**--------------------------------------------------**/
/**名称:大大维的贪吃蛇v1 日期:2017/1/22**/
/**描述:实现了贪吃蛇的基本功能,可以调节游戏难度,**/
/**做了一个简单的用户界面 **/
/**存在的问题:由于使用了全局数组存贮,因此对Space **/
/**类的封装有些欠缺;由于使用了全局刷新,游戏运行中**/
/**会出现闪屏现象,游戏难度越高,闪动越严重;不能够**/
/**存储玩家的游戏记录,由于不是图形化界面,用户界面**/
/**较丑;不能改变游戏的地图大小 **/
/**笔者会在后续版本逐步完善这些问题!!! **/
/**--------------------------------------------------**/ #include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
#include<conio.h>
#include<windows.h>
using namespace std;
const int SpaceLENG=;
const int SpaceHIGH=;
const int HARDSPEED=;
const int NOMALSPEED=;
const int EASYSPEED=;
int speed=NOMALSPEED;
bool FLAG=true;
enum dir {UP,DOWN,LEFT,RIGHT};///使用小写会引发歧义
enum level {HARD,ESAY,NOMAL};
///----------------------------------------------------
///游戏空间定义
class Space
{
public:
void InitSpace();
void DisplaySpace();
public:
char SpaceData[SpaceLENG+][SpaceHIGH+];
};
void Space::InitSpace()
{
for(int i=-; i<SpaceLENG+; i++)
for(int j=-; j<SpaceHIGH+; j++)
{
if(i<||i==SpaceLENG||j<||j==SpaceHIGH)
SpaceData[i][j]='#';
else
SpaceData[i][j]=' ';
}
}
void Space::DisplaySpace()
{
for(int i=-; i<SpaceLENG+; i++)
{
for(int j=-; j<SpaceHIGH+; j++)
{
cout<<SpaceData[i][j];
}
cout<<endl;
}
cout<<endl<<endl;
}
Space *spa=new Space();
///----------------------------------------------------
///蛇节点定义
class SnakeNode
{
public:
void setXY(int m,int n)
{
x=m;
y=n;
}
void setNext(SnakeNode *temp)
{
next=temp;
}
void setPrior(SnakeNode *temp)
{
prior=temp;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
SnakeNode* getNext()
{
return next;
}
SnakeNode* getPrior()
{
return prior;
}
private:
int x,y;
SnakeNode *prior,*next;
};
///----------------------------------------------------
///食物定义
class Food
{
public:
void foodCreate()
{
bool flag=true;
while(flag)
{
srand((unsigned int)time(NULL));
fx=rand()%SpaceLENG;
fy=rand()%SpaceHIGH;
if(spa->SpaceData[fx][fy]!='*')
flag=false;
} spa->SpaceData[fx][fy]='$';
}
int getFx()
{
return fx;
}
int getFy()
{
return fy;
}
private:
int fx;
int fy;
};
///----------------------------------------------------
///蛇定义
class Snake:public SnakeNode,public Food
{
public:
Snake()
{
addHead(SpaceLENG/,SpaceHIGH/);
addHead(SpaceLENG/,SpaceHIGH/-);
addHead(SpaceLENG/,SpaceHIGH/-);
}
void addHead(int x,int y);
void delTail();
void moving();
void changePoint(char keydown);
SnakeNode* getHead()
{
return head;
}
SnakeNode* getTail()
{
return tail;
}
int getLEN()
{
return LEN;
}
private:
SnakeNode *head=NULL,*tail=NULL;
int LEN=;
dir point=LEFT;
};
void Snake::addHead(int x,int y)
{
SnakeNode *temp=new SnakeNode();
temp->setXY(x,y);
temp->setNext(head);
temp->setPrior(NULL);
if(head)
{
head->setPrior(temp);///如果头不为空
spa->SpaceData[head->getX()][head->getY()]='*';///旧头
}
head=temp;
if(!tail)tail=head;///如果尾为空
spa->SpaceData[head->getX()][head->getY()]='@';///新头
LEN++;
}
void Snake::delTail()
{
SnakeNode *temp=tail;
spa->SpaceData[tail->getX()][tail->getY()]=' ';
if(tail==head)
tail=head=NULL;
else
{
tail=tail->getPrior();
tail->setNext(NULL);
LEN--;
}
delete temp; }
void Snake::moving()
{
int a,b;
SnakeNode* temp=getHead();
a=temp->getX();
b=temp->getY();
switch(point)
{
case UP:
--a;
break;
case DOWN:
++a;
break;
case LEFT:
--b;
break;
case RIGHT:
++b;
break;
}
///撞墙
if(a==SpaceLENG||b==SpaceHIGH||a==-||b==-)
{
FLAG=false;
}
///撞自己身体
if(spa->SpaceData[a][b]=='*')
{
spa->SpaceData[a][b]=='@';///用头结点覆盖相撞节点
FLAG=false;
}
///吃食物
if(a==getFx()&&b==getFy())
{
addHead(a,b);
foodCreate();
}
///普通移动
else
{
addHead(a,b);
delTail();
}
}
void Snake::changePoint(char keydown)///方向控制
{
switch(keydown)
{
case 'w':
{
if(point!=DOWN)
{
point=UP;
break;
}
else break;
}
case 'W':
{
if(point!=DOWN)
{
point=UP;
break;
}
else break;
}
case 's':
{
if(point!=UP)
{
point=DOWN;
break;
}
else break;
}
case 'S':
{
if(point!=UP)
{
point=DOWN;
break;
}
else break;
}
case 'a':
{
if(point!=RIGHT)
{
point=LEFT;
break;
}
else break;
}
case 'A':
{
if(point!=RIGHT)
{
point=LEFT;
break;
}
else break;
}
case 'd':
{
if(point!=LEFT)
{
point=RIGHT;
break;
}
else break;
}
case 'D':
{
if(point!=LEFT)
{
point=RIGHT;
break;
}
else break;
}
}
}
///----------------------------------------------------
///用户交互界面创建
class UserView
{
public:
int menuCreate();
int gameSetting();
};
int UserView::menuCreate()
{
bool flag=true;
while(flag)
{
cout<<" MENU"<<endl<<endl;
cout<<"Instruction:"<<endl<<endl;
cout<<"The Snake's original length is 3."<<endl<<endl;
cout<<"Using 'w,s,a,d' or 'W,S,A,D' to control the direction."<<endl<<endl<<endl<<endl;
cout<<"Please Input 'Y'or'y' to start game;"<<endl;
cout<<"Please Input 'N'or'n' to end game;"<<endl;
cout<<"Please Input 'S'or's' to game setting;"<<endl;
cout<<"Please Input else to stay!"<<endl;
char startKey=getch();
if(startKey=='N'||startKey=='n')
{
system("cls");
cout<<"Good Bye!!!"<<endl;
exit();
}
else if(startKey=='Y'||startKey=='y')
{
system("cls");
cout<<" Game Start!!!"<<endl<<endl<<endl<<endl<<endl;
cout<<" GOOD LUCK!!!"<<endl;
getch();
flag=false;
}
else if(startKey=='S'||startKey=='s')
{
gameSetting();
}
system("cls");
}
system("cls");
return ;
}
int UserView::gameSetting()
{
system("cls");
cout<<" Setting:"<<endl<<endl<<endl<<endl;
cout<<"Please Chose Game Difficulty"<<endl;
cout<<"Input 'H'or'h'---Hard Level"<<endl;
cout<<"Input 'N'or'n'---Nomal Level"<<endl;
cout<<"Input 'E'or'e'---Easy Level"<<endl;
cout<<"Input else return the MENU"<<endl;
char hardKey=getch();
if(hardKey=='H'||hardKey=='h')
{
cout<<"The Game's Difficulty now is Hard Level!!!"<<endl;
speed=HARDSPEED;
return ;
}
else if(hardKey=='N'||hardKey=='n')
{
cout<<"The Game's Difficulty now is Nomal Level!!!"<<endl;
speed=NOMALSPEED;
return ;
}
else if(hardKey=='E'||hardKey=='e')
{
cout<<"The Game's Difficulty now is Easy Level!!!"<<endl;
speed=EASYSPEED;
return ;
}
system("cls");
return ;
}
///----------------------------------------------------
///程序入口
int main()
{
while(true)
{
FLAG=true;
UserView view;
view.menuCreate();
spa->InitSpace();
Snake sna;
sna.foodCreate();
spa->DisplaySpace();
while(FLAG)
{
char downKey=getch();
sna.changePoint(downKey);
///kbhit(),检查是否有键盘输入,若有,则返回一个非0值,否则返回0,在头文件<conio.h>中定义
///此处FLAG如果不加,会产生蛇吃墙现象
while(FLAG&&!kbhit())
{
system("cls");
sna.moving();
spa->DisplaySpace();
Sleep(speed);
}
}
cout<<"Game Over!!!"<<endl;
cout<<"The Snake's Length is :"<<sna.getLEN()<<endl<<endl<<endl<<endl;
cout<<"The Game Will Return Back MENU!"<<endl;
getch();
system("cls");
}
return ;
}

最后上几张游戏的运行的截图:

大大维的游戏机计划1--贪吃蛇v1

大大维的游戏机计划1--贪吃蛇v1

大大维的游戏机计划1--贪吃蛇v1

大大维的游戏机计划1--贪吃蛇v1

大大维的游戏机计划1--贪吃蛇v1

大大维的游戏机计划1--贪吃蛇v1

大大维的游戏机计划1--贪吃蛇v1