
#include <windowsh>
#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#ifndef SNAKE_H
#define SNAKE_H
class Cmp
{
friend class Csnake;
int rSign; //横坐标
int lSign; //竖坐标
public:
// friend bool isDead(const Cmp& cmp);
Cmp(int r,int l){setPoint(r,l);}
Cmp(){}
void setPoint(int r,int l){rSign=r;lSign=l;}
Cmp operator-(const Cmp &m)const
{
return Cmp(rSign-mrSign,lSign-mlSign);
}
Cmp operator+(const Cmp &m)const
{
return Cmp(rSign+mrSign,lSign+mlSign);
}
};
const int maxSize = 5; //初始蛇身长度
class Csnake
{
Cmp firstSign; //蛇头坐标
Cmp secondSign;//蛇颈坐标
Cmp lastSign; //蛇尾坐标
Cmp nextSign; //预备蛇头
int row; //列数
int line; //行数
int count; //蛇身长度
vector<vector<char> > snakeMap;//整个游戏界面
queue<Cmp> snakeBody; //蛇身
public:
int GetDirections()const;
char getSymbol(const Cmp& c)const
//获取指定坐标点上的字符
{
return snakeMap[clSign][crSign];
}
Csnake(int n)
//初始化游戏界面大小
{
if(n<20)line=20+2;
else if(n>30)line=30+2;
else line=n+2;
row=line3+2;
}
bool isDead(const Cmp& cmp)
{
return ( getSymbol(cmp)=='@' || cmprSign == row-1
|| cmprSign== 0 || cmplSign == line-1 ||
cmplSign == 0 );
}
void InitInstance(); //初始化游戏界面
bool UpdataGame(); //更新游戏界面
void ShowGame(); //显示游戏界面
};
#endif // SNAKE_H
using namespace std;
//测试成功
void Csnake::InitInstance()
{
snakeMapresize(line); // snakeMap[竖坐标][横坐标]
for(int i=0;i<line;i++)
{
snakeMap[i]resize(row);
for(int j=0;j<row;j++)
{
snakeMap[i][j]=' ';
}
}
for(int m=1;m<maxSize+1;m++)
{
//初始蛇身
snakeMap[line/2][m]='@';
//将蛇身坐标压入队列
snakeBodypush(Cmp(m,(line/2)));
//snakeBody[横坐标][竖坐标]
}
//链表头尾
firstSign=snakeBodyback();
secondSignsetPoint(maxSize-1,line/2);
}
//测试成功
int Csnake::GetDirections()const
{
if(GetKeyState(VK_UP)<0) return 1; //1表示按下上键
if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下键
if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左键
if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右键
return 0;
}
bool Csnake::UpdataGame()
{
//-----------------------------------------------
//初始化得分0
static int score=0;
//获取用户按键信息
int choice;
choice=GetDirections();
cout<<"Total score: "<<score<<endl;
//随机产生食物所在坐标
int r,l;
//开始初始已经吃食,产生一个食物
static bool eatFood=true;
//如果吃了一个,才再出现第2个食物
if(eatFood)
{
do
{
//坐标范围限制在(1,1)到(line-2,row-2)对点矩型之间
srand(time(0));
r=(rand()%(row-2))+1; //横坐标
l=(rand()%(line-2))+1;//竖坐标
//如果随机产生的坐标不是蛇身,则可行
//否则重新产生坐标
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇头和社颈的横坐标不相同,执行下面 *** 作
if(firstSignrSign!=secondSignrSign)nextSignsetPoint(firstSignrSign,firstSignlSign-1);
//否则,如下在原本方向上继续移动
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSignrSign!=secondSignrSign)nextSignsetPoint(firstSignrSign,firstSignlSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSignlSign!=secondSignlSign)nextSignsetPoint(firstSignrSign-1,firstSignlSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSignlSign!=secondSignlSign)nextSignsetPoint(firstSignrSign+1,firstSignlSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='' && !isDead(nextSign))
//如果没有碰到食物(且没有死亡的情况下),删除蛇尾,压入新的蛇头
{
//删除蛇尾
lastSign=snakeBodyfront();
snakeMap[lastSignlSign][lastSignrSign]=' ';
snakeBodypop();
//更新蛇头
secondSign=firstSign;
//压入蛇头
snakeBodypush(nextSign);
firstSign=snakeBodyback();
snakeMap[firstSignlSign][firstSignrSign]='@';
//没有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSignlSign][nextSignrSign]='@';
//只压入蛇头
snakeBodypush(nextSign);
firstSign=snakeBodyback();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
int main()
{
Csnake s(20);
sInitInstance();
//sShowGame();
int noDead;
do
{
sShowGame();
noDead=sUpdataGame();
}while (noDead);
system("pause");
return 0;
}
这个代码可以运行的,记得给分啦
程序设计及说明
1
、边墙(
Wall
)
该类规定游戏的范围大小。
2
、蛇类(
Snake
)
用该类生成一个实例蛇
snake
。
3
、移动(
Move
)
该类用于实现对蛇的 *** 作控制,即蛇头方向的上下左右的移动 *** 作。
4
、食物类(
Food
)
该类是游戏过程中食物随机产生的控制和显示。
5
、判断死亡(
Dead
)
该类是对游戏过程中判断玩家 *** 作是否导致蛇的死亡,其中包括蛇头咬食自己身体和蛇头是否触
及游戏“边墙”。
6
、蛇结点(
SnakeNode
)
该类是蛇吃下随机产生的食物从而增加长度的控制类,其中包括蛇长度增加和尾部的变化。
7
、计分统计(
Score
)
该类由于玩家的游戏成绩记录,及游戏结束时的得分输出。
部分函数及说明
1Char menu();
/
用于玩家选择的游戏速度,返回一个
char
值
/
2DELAY(char ch1);
/
用于控制游戏速度
/
3void drawmap();
/
绘制游戏地图函数
4
、
void menu()
/
游戏帮助信息的输出
部分类细节解说
1
、蛇的构建
—
Snake
class Snake{
public:
int x[n]
;
int y[n];
int node;
//
蛇身长度
int direction;//
蛇运动方向
int
life;//
蛇生命,判断死亡
}
2
、随机食物
Food
利用
rand(
)函数进行随机数产生,然后就行坐标定位
void Food(void){
int pos_x = 0;
int pos_y = 0;
pos_x = rand() % length;//x
坐标的确定
pos_y = rand() % (width-1);//y
坐标的确定
}
3
、蛇头方向确定
利用
switch
语句进行方向确定
switch(){
case VK_UP:{
OutChar2Y--;
y--;
break;
}
case VK_LEFT:{
OutChar2Y++;
y++;
break;
}
case VK_DOWN:{
OutChar2X---;
x--;
break;
}
case 'VK_RIGHT:{
OutChar2X++;
x++;
break;
}
}
代码
#include <iostream>
#include <ctime>
#include <conioh>
#include <windowsh>
#include <timeh>
using namespace std;
int score=0,t=300,f=1;//
得分与时间间隔
/ms
(控制贪吃蛇的速度)
double ss=0,tt=0;//
统计时间所用参数
class Node
{
Node(): x(0), y(0), prior(0), next(0) { }
int x;
int y;
Node prior;
Node next;
friend class Snake;
};
class Snake
{
public:
Snake();
~Snake();
void output();
void move();
void change_point(char);
private:
Node head;
Node tail;
enum p{ UP
, RIGHT, DOWN, LEFT }point; //
方向
int food_x, food_y; //
食物的坐标
static const int N = 23;
int game[N][N];
void add_head(int, int); //
添加坐标为
a,b
的结点
void delete_tail(); //
删除最后一个结点
void greate_food(); //
产生食物
void gotoxy(int, int);
};
void menu();
//
游戏 *** 作菜单
int main()
{
system("color a");
//
初始
cmd
窗口颜色为黑(背景)淡绿(文字)
cout<<"\n\n\n\n\n\n
";
for(int i=0;i<23;i++)
{char star[]={"Welcome To Snake Game!"};
cout<<star[i];
Sleep(170);}
cout<<"\n\n
祝你好运!
"<<endl;
Sleep(3000);
if(kbhit()){char kk=getch();if(kk==9)f=5;} //
如果执行,吃一颗星加
5
分
system("cls");
Snake s;
menu();
system("color 1a");
soutput();
while (true)
{
char keydown = getch();
if(keydown==32)getch();
if(keydown==27)return 0;
schange_point(keydown);
while (!kbhit())
{clock_t start,end;start=clock();
smove();
soutput();
Sleep(t);
end=clock();tt=(double)(end-start)/CLOCKS_PER_SEC;ss+=tt;
cout<<"
时间:
"<<(int)ss;
以上就是关于贪吃蛇游戏的C语言编程全部的内容,包括:贪吃蛇游戏的C语言编程、在dos环境下c语言编程编一个贪吃蛇游戏、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)