C++ 贪吃蛇源码分享【动态空间+链表】
本帖最后由 yep96 于 2016-11-4 21:04 编辑这是上个学期的大作业,源码除了方向键控制外没有其他参考,应该和网上其他的源码区别挺大的吧。可以参考一下,有什么建议在好不过的了
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include <windows.h>
using namespace std;
int eat_food = 0;
float score = 0;
short int grade = 0;
short int timeout = 700;
int step = 0;
short int derect=77;//////////上下左右 asc
class SNAKE
{
short int x;
short int y;
SNAKE* Qian;
SNAKE* Hou;
public:
SNAKE(){x = 0; y = 0; Qian = NULL; Hou = NULL;}
SNAKE(int a, int b){x = a; y =b; Qian = NULL; Hou = NULL;}
void change(int a ,int b){if(a!=-1)x = a; if(b!=-1)y = b;}
short int getx(){return x;}
short int gety(){return y;}
SNAKE* qian(){return Qian;}
SNAKE* qian(SNAKE* a){Qian=a;return Qian;}
SNAKE* hou(){return Hou;}
SNAKE* hou(SNAKE* a){Hou=a;return Hou;}
};
class FOOD
{
public:
short int x;
short int y;
};
FOOD food;
SNAKE* end;//尾巴地址
SNAKE* head;///头地址
bool move(int,int,short int **);
bool randfood(int, int , short int &,short int &,short int **);
int main()
{
cout << "\n\t贪吃蛇\n\n\n\t\t Powered by xxxxxxx(差点忘了删)";
short int i,j, a, b;
cout<<"请输入行数:";
cin>>a; cin.sync(); cin.clear();
cout<<"请输入列数:";
cin>>b; cin.sync(); cin.clear();
a+=2;b+=2;
short int **point;
point = new short int* ; //////-1 墙 0 空 1 身子 2 食物 3头
for(i=0; i<a ;++i)
point=new short int;
for(i = 1; i < a-1; ++i)
for(j = 1; j < b-1; ++j)
point = 0;
for(i = 0; i < a; ++i)
point=point=-1;
for(i = 0; i < b; ++i)
point=point=-1;
cout << " 游戏马上开始,请按任意键继续\n PS:可随时以cmd选择模式暂停\n\n";
system("pause");
end = new SNAKE;
SNAKE* save_end = end;
for(i = 1; i < (a-2)*(b-2); ++i)
{
head = new SNAKE;
end -> qian(head);
head -> hou(end);
end = head;
}
head -> qian(save_end);
save_end -> hou(head);
head=head->qian();/////////////形成a*b的环状双向链表,已验证
end->change(a/2, b/2-1);
head->change(a/2, b/2);
point=1;
point=3;
randfood(a, b, food.x,food.y,point);
while(move(a,b,point));///传值边界含外框
for(i=0;i<a;++i)
delete[] point;
delete[] point;
SNAKE* temp = head;
for(i = 0; i < (a-2)*(b-2); ++i)
{
temp = head -> qian();
delete head;
head = temp;
}////////////delete
system("pause");
return 0;
}
bool randfood(int M, int N, short int &x,short int &y,short int **point)///////false时为全满,赢
{
int i,j;
srand(unsigned(time(0)));
x=rand()%(M-2)+1;
y=rand()%(N-2)+1;
i=x;
j=y;
for(++x;;++x)
{
if(x==M-1)
x-=M-2;
for(++y;;++y)
{
if(y==N-1)
y-=N-2;
if(point==0)
{
point=2;
return true;
}
if(j==y) break;
}
if(i==x) return false;
}
}
bool move(int M, int N, short int **point)
{
score=eat_food*10+step*0.1;
grade=score/100+1;
switch(grade)
{
case 2 : if(timeout==700){timeout=550;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
case 4 : if(timeout==550){timeout=400;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
case 7 : if(timeout==400){timeout=300;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
case 11: if(timeout==300){timeout=200;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
}
system("cls");
cout<<" 步数:"<<++step<<" 吃了:"<<eat_food<<" 分数:"<<(int)score<<" 等级:"<<grade<<" 时间间隔"<<timeout<<"ms"<<endl;
int i,j,time_1;
for(i=0;i<M;++i)
{
cout<<'\t';
for(j=0;j<N;++j)
{
if(point==0)
cout<<"";
if(point==1)
cout<<"▉";
if(point==-1)
cout<<"▓";
if(point==3)
cout<<"◆";
if(point==2)
cout<<"●";
}
cout<<endl;
}
int temp,key;
time_1=clock();
while(clock()-time_1<=timeout)
{
key=kbhit();/////////////////键盘输入时返回1
Sleep(50);
if(key!=0 && getch()==224)
{
temp=getch();
if((temp==80&&head->getx()+1!=head->hou()->getx())||(temp==72&&head->getx()-1!=head->hou()->getx())||(temp==75&&head->gety()-1!=head->hou()->gety())||(temp==77&&head->gety()+1!=head->hou()->gety()))
{ derect=temp; break;}
}
}
switch(derect)
{
case 80 : head->qian()->change(head->getx()+1, head->gety()); break;//xia
case 75 : head->qian()->change(head->getx(), head->gety()-1); break;//zuo
case 77 : head->qian()->change(head->getx(), head->gety()+1); break;//you
case 72 : head->qian()->change(head->getx()-1, head->gety()); break;//shang
}
head = head ->qian();
if(point[(int)head->getx()][(int)head->gety()]!=2)
{
point=0;
end = end ->qian();
}
else
{
++eat_food;
if(randfood(M, N, food.x,food.y,point)==0)
{
cout<<"\n\n\t\tYou Win!!!\n\n";//////////////赢了
system("color 1e");
return false;
}
}
if(point==1||point==-1)
{
cout<<"\n\n\t\tGame Over!\n\n";/////////////////输了
system("color 1C");
return false;
}
else
{
point=3;
point=1;
return true;
}
}
普通用户 发表于 2016-10-10 22:00
这个链表怎么做啊- -
我的教材,用的是类模板,可以参考一下。我上面贪吃蛇的链表老师应该不会喜欢。。
哪里卡着我可以帮忙看看。全部写出来,,我很懒的
普通用户 发表于 2016-10-10 22:00
这个链表怎么做啊- -
....都不想再写代码了
我的链表代码是自己编的,可能和老师教的听不一样的
一般来说,习惯于用class List和class Node。List用于创建、添加、修改和删除等操作,Node作为储存空间,添加List为friend,当然也可以用各种函数传参,但太过麻烦 6666.学习 学习 学习一下面向对象的思想 这个链表怎么做啊- -
我试了一下运行不了啊 45行出错了 me乄too 发表于 2016-10-10 22:13
我试了一下运行不了啊 45行出错了
我习惯于void main()
但是更正规的是int main(),而且g++编译不认void main() me乄too 发表于 2016-10-10 22:13
我试了一下运行不了啊 45行出错了
你用的是linux?不对,Linux没有windows.h头文件。。。 想知道楼主编c++用的什么编译器呀? 撒旦asd 发表于 2016-10-10 22:26
想知道楼主编c++用的什么编译器呀?
VS 2015 ent。我记得vc6.0好像也能编译