贪吃蛇源代码作文3700字

五年级作文

#include<windows.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#include<cstring>

#include<cstdio>

#include<iostream>

#define N 22

using namespace std;

int gameover;

int x1,y1;

int x,y;

long start;

class snake_position

{

public:

int x,y;

snake_position(){};

void initialize(int &);

};

snake_position position[(N-2)*(N-2)+1];

void snake_position::initialize(int &j)

{

x=1;

y=j;

}

class snake_map

{

char s[N][N];

int grade,length;

int gamespeed;

char direction;

int head,tail;

int score;

bool gameauto;

public:

snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s)//这里可能有问题 {}

void initialize();

void show_game();

int updata_game();

void setpoint();

void getgrade();

void display();

};

void snake_map::initialize()

{

int i,j;

for(i=1;i<=3;i++)

s[1][i]='*';

s[1][4]='#';

for(i=1;i<=N-2;i++)

for(j=1;j<=N-2;j++)

s[i][j]=' ';

for(i=0;i<=N-1;i++)

s[0][i]=s[N-1][i]='-';

for(i=1;i<N-2;i++)

s[i][0]=s[i][N-1]='|';

}

void snake_map::show_game()

{

system("cls");

int i,j;

cout<<endl;

for(i=0;i<N;i++)

{

cout<<'\t';

for(j=0;j<N;j++)

cout<<s[i][j]<<' ';

if(i==2) cout<<"\t等级:"<<grade;

if(i==6) cout<<"\t速度:"<<gamespeed;

if(i==10) cout<<"\t得分:"<<score<<"分";

if(i==14) cout<<"\t暂停:按一下空格键";

if(i==18) cout<<"\t继续:按两下空格键";

cout<<endl;

}

}

void snake_map::getgrade()

{

cin>>grade;

while(grade>7||grade<1)

{

cout<<"请输入数字1—7选择等级,输入其他数字无效"<<endl; cin>>grade;

}

switch(grade)

{

case 1:gamespeed=1000;gameauto=0;break;

case 2:gamespeed=800;gameauto=0;break;

case 3:gamespeed=600;gameauto=0;break;

case 4:gamespeed=400;gameauto=0;break;

case 5:gamespeed=200;gameauto=0;break;

case 6:gamespeed=100;gameauto=0;break;

case 7:grade=1;gamespeed=1000;gameauto=1;break;

}

}

void snake_map::display()

{

cout<<"\n\t\t\t\t等级:"<<grade;

cout<<"\n\n\n\t\t\t\t速度:"<<gamespeed;

cout<<"\n\n\n\t\t\t\t得分:"<<score<<"分"; }

void snake_map::setpoint()

{

srand(time(0));

do

{

x1=rand()%(N-2)+1;

y1=rand()%(N-2)+1;

}while(s[x1][y1]!=' ');

s[x1][y1]='*';

}

char key;

int snake_map::updata_game()

{

gameover=1;

key=direction;

start=clock();

while((gameover=(clock()-start<=gamespeed))&&!kbhit()); if(gameover)

{

getch();

key=getch();

}

if(key==' ')

{

while(getch()!=' '){};

}

else direction=key;

switch(direction)

{

case 72:x=position[head].x-1;y=position[head].y;break;

case 80:x=position[head].x+1;y=position[head].y;break; case 75:x=position[head].x;y=position[head].y-1;break;

case 77:x=position[head].x;y=position[head].y+1;break;

}

if(!(direction==72||direction==80||direction==75||direction==77)) gameover=0;

else if(x==0||x==N-1||y==0||y==N-1)

gameover=0;

else if(s[x][y]!=' '&&!(x==x1&&y==y1))

gameover=0;

else if(x==x1&&y==y1)

{

length++;

if(length>=8&&gameauto)

{

length-=8;

grade++;

if(gamespeed>=200)

gamespeed-=200;

else gamespeed=100;

}

s[x][y]='#';

s[position[head].x][position[head].y]='*';

head=(head+1)%((N-2)*(N-2));

position[head].x=x;

position[head].y=y;

show_game();

gameover=1;

score+=grade*20;

setpoint();

}

else

{

s[position[tail].x][position[tail].y]=' ';

tail=(tail+1)%((N-2)*N-2);

s[position[head].x][position[head].y]='*';

head=(head+1)%((N-2)*(N-2));

position[head].x=x;

position[head].y=y;

s[position[head].x][position[head].y]='#';

gameover=1;

}

return gameover;

}

int main()

{

char ctn='y';

int nodead;

cout<<"\n\n\n\n\n\t\t\t我是小黑"<<endl;

cout<<"\n\n\n\t\t\t按任意键马上开始。。。"<<endl;

getch();

while(ctn=='y')

{

system("cls");

snake_map snake;

snake.initialize();

cout<<"\n\n请输入数字选择游戏等级:"<<endl;

cout<<"\n\n\n\t\t\t1.等级一:速度 1000 \n\n\t\t\t2.等级二:速度800 \n\n\t\t\t3.等级三:速度600";

cout<<"\n\n\t\t\t4.等级四:速度 400 \n\n\t\t\t5.等级五:速度 200 \n\n\t\t\t6.等级六: 速度 100 \n\n\t\t\t7.自动升级模式"<<endl;

snake.getgrade();

for(int i=1;i<=4;i++)

{

position[i].initialize(i);

}

snake.setpoint();

do

{

snake.show_game();

nodead=snake.updata_game();

}while(nodead);

system("cls");

cout<<"\n\n\n\t\t\t\tGameover!\n\n"<<endl;

snake.display();

cout<<"\n\n\n\t\t 是否选择继续游戏? 输入继续yes,n 退出"<<endl; cin>>ctn;

}

return 0;

}

#include<windows.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#include<cstring>

#include<cstdio>

#include<iostream>

#define N 22

using namespace std;

int gameover;

int x1,y1;

int x,y;

long start;

class snake_position

{

public:

int x,y;

snake_position(){};

void initialize(int &);

};

snake_position position[(N-2)*(N-2)+1];

void snake_position::initialize(int &j)

{

x=1;

y=j;

}

class snake_map

{

char s[N][N];

int grade,length;

int gamespeed;

char direction;

int head,tail;

int score;

bool gameauto;

public:

snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s)//这里可能有问题 {}

void initialize();

void show_game();

int updata_game();

void setpoint();

void getgrade();

void display();

};

void snake_map::initialize()

{

int i,j;

for(i=1;i<=3;i++)

s[1][i]='*';

s[1][4]='#';

for(i=1;i<=N-2;i++)

for(j=1;j<=N-2;j++)

s[i][j]=' ';

for(i=0;i<=N-1;i++)

s[0][i]=s[N-1][i]='-';

for(i=1;i<N-2;i++)

s[i][0]=s[i][N-1]='|';

}

void snake_map::show_game()

{

system("cls");

int i,j;

cout<<endl;

for(i=0;i<N;i++)

{

cout<<'\t';

for(j=0;j<N;j++)

cout<<s[i][j]<<' ';

if(i==2) cout<<"\t等级:"<<grade;

if(i==6) cout<<"\t速度:"<<gamespeed;

if(i==10) cout<<"\t得分:"<<score<<"分";

if(i==14) cout<<"\t暂停:按一下空格键";

if(i==18) cout<<"\t继续:按两下空格键";

cout<<endl;

}

}

void snake_map::getgrade()

{

cin>>grade;

while(grade>7||grade<1)

{

cout<<"请输入数字1—7选择等级,输入其他数字无效"<<endl; cin>>grade;

}

switch(grade)

{

case 1:gamespeed=1000;gameauto=0;break;

case 2:gamespeed=800;gameauto=0;break;

case 3:gamespeed=600;gameauto=0;break;

case 4:gamespeed=400;gameauto=0;break;

case 5:gamespeed=200;gameauto=0;break;

case 6:gamespeed=100;gameauto=0;break;

case 7:grade=1;gamespeed=1000;gameauto=1;break;

}

}

void snake_map::display()

{

cout<<"\n\t\t\t\t等级:"<<grade;

cout<<"\n\n\n\t\t\t\t速度:"<<gamespeed;

cout<<"\n\n\n\t\t\t\t得分:"<<score<<"分"; }

void snake_map::setpoint()

{

srand(time(0));

do

{

x1=rand()%(N-2)+1;

y1=rand()%(N-2)+1;

}while(s[x1][y1]!=' ');

s[x1][y1]='*';

}

char key;

int snake_map::updata_game()

{

gameover=1;

key=direction;

start=clock();

while((gameover=(clock()-start<=gamespeed))&&!kbhit()); if(gameover)

{

getch();

key=getch();

}

if(key==' ')

{

while(getch()!=' '){};

}

else direction=key;

switch(direction)

{

case 72:x=position[head].x-1;y=position[head].y;break;

case 80:x=position[head].x+1;y=position[head].y;break; case 75:x=position[head].x;y=position[head].y-1;break;

case 77:x=position[head].x;y=position[head].y+1;break;

}

if(!(direction==72||direction==80||direction==75||direction==77)) gameover=0;

else if(x==0||x==N-1||y==0||y==N-1)

gameover=0;

else if(s[x][y]!=' '&&!(x==x1&&y==y1))

gameover=0;

else if(x==x1&&y==y1)

{

length++;

if(length>=8&&gameauto)

{

length-=8;

grade++;

if(gamespeed>=200)

gamespeed-=200;

else gamespeed=100;

}

s[x][y]='#';

s[position[head].x][position[head].y]='*';

head=(head+1)%((N-2)*(N-2));

position[head].x=x;

position[head].y=y;

show_game();

gameover=1;

score+=grade*20;

setpoint();

}

else

{

s[position[tail].x][position[tail].y]=' ';

tail=(tail+1)%((N-2)*N-2);

s[position[head].x][position[head].y]='*';

head=(head+1)%((N-2)*(N-2));

position[head].x=x;

position[head].y=y;

s[position[head].x][position[head].y]='#';

gameover=1;

}

return gameover;

}

int main()

{

char ctn='y';

int nodead;

cout<<"\n\n\n\n\n\t\t\t我是小黑"<<endl;

cout<<"\n\n\n\t\t\t按任意键马上开始。。。"<<endl;

getch();

while(ctn=='y')

{

system("cls");

snake_map snake;

snake.initialize();

cout<<"\n\n请输入数字选择游戏等级:"<<endl;

cout<<"\n\n\n\t\t\t1.等级一:速度 1000 \n\n\t\t\t2.等级二:速度800 \n\n\t\t\t3.等级三:速度600";

cout<<"\n\n\t\t\t4.等级四:速度 400 \n\n\t\t\t5.等级五:速度 200 \n\n\t\t\t6.等级六: 速度 100 \n\n\t\t\t7.自动升级模式"<<endl;

snake.getgrade();

for(int i=1;i<=4;i++)

{

position[i].initialize(i);

}

snake.setpoint();

do

{

snake.show_game();

nodead=snake.updata_game();

}while(nodead);

system("cls");

cout<<"\n\n\n\t\t\t\tGameover!\n\n"<<endl;

snake.display();

cout<<"\n\n\n\t\t 是否选择继续游戏? 输入继续yes,n 退出"<<endl; cin>>ctn;

}

return 0;

}


相关作文

  • 贪吃蛇代码C#作文2100字
  • 一个C#小程序,贪吃蛇游戏 using System; using System.Collections; using System.Drawing; using System.Windows.Forms; namespace WSnake { /// <summary> /// Sna ...

  • 贪吃蛇c语言代码作文11800字
  • 贪吃蛇c 语言代码 #include <graphics.h> #include <conio.h> #include <stdlib.h> #include <dos.h> #define NULL 0 #define UP 18432 #defin ...

  • iOS培训-斗鱼直播APP之项目部署作文500字
  • 玩转[斗鱼直播APP ]系列之项目部署 作者:小码哥教育 项目部署目的 任何一个项目最好使用源代码管理工具 源代码管理工具可以帮助我们解决备份.版本回退等等问题 因为是非基础篇,因此具体好处不在累述 常见的源代码管理工具: SVN: 集中式源代码管理工具 GIT: 分布式源代码管理工具 在公司开发者 ...

  • 征文[我因代码而与众不同]作文900字
  • 我因代码而与众不同 有一句话说得好"世界上没有两片完全相同的树叶",人也是这样,每个人都有属于自己的性格.外貌.特点,每个人都有与众不同的地方.我也不例外,我也有很多与众不同的地方,但是要说最与众不同的地方,还是不得不谈到一个很专业的名词--代码. 代码这两个字从听说到认识最后到 ...

  • 小甲鱼python课后题作文26700字
  • 第一节课 0. Python 是什么类型的语言? Python 是脚本语言 脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序.以简单的方式快速完成某些复杂的事情通常是创造脚本语言的重要原则,基于这项原则,使得脚本语言通常比 C 语言.C+ ...

  • 爱的密码作文1500字
  • 母乌龟的头脑中有没有爱的代码?它把蛋下在沙滩上,也懂得埋起来,可然后不管了!母鸡的头脑里有没有爱的代码?不知道它为什么带小鸡?后来为什么又不带了?不知道是小鸡长大了不跟它了,还是它自己不想带了,或者是不爱了? 很多人认为所有母亲的头脑中肯定有爱的代码.这个我觉得勉强对,因为绝大多数数的母亲都爱她们的 ...

  • 验证码的前世今生(今生篇)作文2300字
  • 验证码的前世今生(今生篇) 看完<验证码的前世今生(前世篇)>也许第一感觉就是Winter is coming,互联网的人机对抗到了最黑暗的时刻.柳暗花明又一村,最黑暗的时刻也是光明即将来临的时刻--在传统验证码的末日新的反向图灵测试机制浴火重生. 作者:阿里聚安全来源:segmentf ...

  • JSP基本指令和动作作文4300字
  • JSP 基本指令和动作 JSP 基本指令 jsp 命令指令用来设置与整个jsp 页面相关的属性,它并不直接产生任何可见的输出, 而只是告诉引擎如何处理其余JSP 页面.其一般语法形式为: <%@ 指令名称 属性="值"%> 三种命令指令分别是page .include ...

  • 代码作文17700字
  • 事件 ID 类别 事件类型 值或描述 1000 服务 信息 Forefront Server Security 服务正在运行. 1001 服务 信息 Forefront Server Security 服务已停止. 1002 服务 信息 FSEIMC 服务已启动. 1003 服务 信息 FSEIMC ...

你可能喜欢
聚合