在dos环境下c语言编程编一个贪吃蛇游戏

在dos环境下运行,要全部的代码

程序设计及说明

1
、边墙(
Wall


该类规定游戏的范围大小。

2
、蛇类(
Snake


用该类生成一个实例蛇
snake


3
、移动(
Move


该类用于实现对蛇的操作控制,即蛇头方向的上下左右的移动操作。

4
、食物类(
Food


该类是游戏过程中食物随机产生的控制和显示。

5
、判断死亡(
Dead


该类是对游戏过程中判断玩家操作是否导致蛇的死亡,其中包括蛇头咬食自己身体和蛇头是否触
及游戏“边墙”。

6
、蛇结点(
SnakeNode


该类是蛇吃下随机产生的食物从而增加长度的控制类,其中包括蛇长度增加和尾部的变化。

7
、计分统计(
Score


该类由于玩家的游戏成绩记录,及游戏结束时的得分输出。

...

部分函数及说明

1.Char menu();
/*
用于玩家选择的游戏速度,返回一个
char

*/
2.DELAY(char ch1);
/*
用于控制游戏速度
*/
3.void 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:{
OutChar2.Y--;
y--;
break;
}
case VK_LEFT:{
OutChar2.Y++;
y++;
break;
}
case VK_DOWN:{
OutChar2.X---;
x--;
break;
}
case 'VK_RIGHT:{
OutChar2.X++;
x++;
break;

}
}

代码

#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <time.h>
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");
s.output();

while (true)
{

char keydown = getch();

if(keydown==32)getch();

if(keydown==27)return 0;

s.change_point(keydown);

while (!kbhit())

{clock_t start,end;start=clock();

s.move();

s.output();

Sleep(t);

end=clock();tt=(double)(end-start)/CLOCKS_PER_SEC;ss+=tt;

cout<<"

时间:
"<<(int)ss;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-12
54444445

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网