go语言项目贪吃蛇 贪吃蛇编程python

c语言设计的贪吃蛇 帮我解释一下。。。

这个是代码

渝中网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

#include"time.h"

#include"stdio.h"

#include"conio.h"

#include"stdlib.h"

#define ESC 033

#define UP 0110

#define DOWN 0120

#define LEFT 0113

#define RIGHT 0115

#define X1 15

#define X2 40

#define Y1 2

#define Y2 20

#define N 15

/*************************************************************/

void food(int *b)

{

*b=random(X2-X1+1)+X1; b++; /*产生食物*/

*b=random(Y2-Y1+1)+Y1;

}

/*************************************************************/

void init(int (*a)[2],int *b)

{

int i,j;

food(b);

a[0][0]=random(X2-X1-5)+X1+3; /*随机生成一3断长的snack */

a[0][1]=random(Y2-Y1-5)+Y1+3;

switch(random(4))

{case 0:i=-1; j=0; break;

case 1:i=1; j=0; break;

case 2:i=0; j=-1; break;

case 3:i=0; j=1; break;}

a[1][0]=a[0][0]+i;

a[1][1]=a[0][1]+j;

a[2][0]=2*a[1][0]-a[0][0];

a[2][1]=2*a[1][1]-a[0][1];

window(1,1,80,25);

textbackground(BLACK);

clrscr();

window(X1,Y1,X2,Y2);

textbackground(BLUE);

clrscr();

window(1,1,80,25);

textbackground(YELLOW);

textcolor(YELLOW);

for(i=0;i3;i++)

{gotoxy(a[i][0],a[i][1]);

cprintf("%c",'%');}

gotoxy(b[0],b[1]);

cprintf("%c",'*');

}

/*************************************************************/

void putout(int a[][2],int n,int b[],int step)

{

textbackground(YELLOW);

gotoxy(a[0][0],a[0][1]);

cprintf("%c",'%');

gotoxy(b[0],b[1]);

cprintf("%c",'#');

textbackground(BLUE);

gotoxy(a[n][0],a[n][1]);

cprintf("%c",' '); /*清除蛇的尾部*/

gotoxy(X1,Y2+2);

cprintf("Length:%2d Delay:%3dms",n,step);

}

/*************************************************************/

void direction(int a[][2],int n)

{

char ch;

int i,across,down;

across=a[0][0]-a[1][0];

down=a[0][1]-a[1][1];

if(kbhit())

{ch=getch();

while(ch==0)

ch=getch();

if(ch==LEFT) {if(across!=1) {down=0; across=-1;}}

else if(ch==RIGHT) {if(across!=-1) {down=0; across=1; }}

else if(ch==UP) {if(down!=1) {down=-1; across=0; }}

else if(ch==DOWN) {if(down!=-1) {down=1; across=0; }}

else if(ch==ESC) exit(1);

}

for(i=n;i0;i--)

{a[i][0]=a[i-1][0];

a[i][1]=a[i-1][1];

}

a[0][0]+=across;

a[0][1]+=down;

}

/*************************************************************/

int die(int a[][2],int n)

{int i;

if(a[0][0]X2||a[0][0]X1||a[0][1]Y2||a[0][1]Y1) return 1;

for(i=4;i=n;i++)

{if(a[0][0]==a[i][0]a[0][1]==a[i][1])

return 1;

}

return 0;

}

/*************************************************************/

int meet(int a[][2],int b[])

{

if(a[0][0]==b[0]a[0][1]==b[1]) return 1;

return 0;

}

/*************************************************************/

void delay2(int step)

{float t;

static clock_t start=0;

clock_t end;

do{end=clock();

t=(end-start)/CLK_TCK;

}

while(t*1000step);

start=clock();

}

/*************************************************************/

void ifdie()

{

char ch;

gotoxy(X1,Y2+2);

printf("You die. Do you want to go on?(Y/N)");

ch=getch();

if(ch!='y') exit(1);

}

/*************************************************************/

void ifwin()

{

char ch;

gotoxy(X1,Y2+2);

puts("You win. Do you want to continue?(Y/N)");

ch=getch();

if(ch=='y') return;

exit(1);

}

/**************************************************************/

int main()

{

int a[N][2],b[2];

int n,step;

REPLAY:

for(step=300;1;step*=.9)

{randomize();

init(a,b);

for(n=3;nN;n++)

{do{putout(a,n,b,step);

delay2(step);

direction(a,n);

if(die(a,n))

{putout(a,n,b,step);

ifdie();

goto REPLAY;

}

}

while(!meet(a,b));

food(b);

}

ifwin();

}

return 0;

}

拓展训练项目贪吃蛇

拓展训练项目贪吃蛇

拓展训练项目贪吃蛇不知道大家是否玩过?下面由我为大家收集的关于拓展训练项目之贪吃蛇的介绍,欢迎大家阅读!

拓展训练项目之贪吃蛇简介:

可能我们大家在小的时候或者不久前,都玩过贪吃蛇的游戏。和别的游戏不同,很多游戏都是打通关打到底,而贪食蛇的结局却是死亡。不管你玩得多么纯熟,手艺多么高超,当手机上的小蛇越长越长,积分越来越高的时候,死亡也就越来越近。“贪食蛇”,要命的就在一个“贪”字上。

接下来我们将要操作的拓展训练项目之贪吃蛇:贪食蛇,跟我们的手机里玩得有些类似,主要是让大家体验这份快乐和信息高效传递。任务:就是在队友看不见、又不能沟通的情况下从目标区域内取出不同分值的球。

拓展训练拓展项目之贪吃蛇规则:

拓展训练项目之贪吃蛇规则一:拓展项目之贪吃蛇是以队为单位来进行,行进过程中手搭手行进,手不能断开;如若发现有手松开的情况,我们每发现一次将扣除5分的积分;

拓展训练项目之贪吃蛇规则二:行进中不能首尾相连,围成一个圆环;

拓展v项目之贪吃蛇规则三:除了蛇尾最后一名队友外,其它队友都须戴上眼罩,除了第一个队友可以说话以外,其它队友不可以讲话;再强调一下:第一名队友看不见,但能说话,最后一名队名,看得见,但不能说话;从第二名队友开始到倒数第二名队(教练做个示范,指定一下)是既看不见,又不能说话。这是一个君子游戏,如果发现有说话的、偷看的、每发现一次我们将扣除总积分的…分;

拓展训练项目之贪吃蛇规则四:第一队友捡了球之后就停下来,然后从前面往后面传,传到最后一名伙伴以后,放到指定区域,最后一名伙伴将球放到指定的'区域后,回到队伍,才能继续下一个任务;

拓展训练项目之贪吃蛇注意:其它队伍不能干扰正在场上操作的队伍,每个球的分值在任务上,每个队的分值是不一样的,就是在五分钟的时间里完成任务书的要求。

拓展训练项目之贪吃蛇拓展目的:

1、培养团队决策能力培养参与者相互沟通的意识。克服相互沟通障碍的能力。

2、培养参与者听从指挥,不断改进的心态。培训参与者系统思考的能力。

3、培养善于学习、总结,积极有效的把握信息、主动出击把握机会的能力以及感受团队的分工、搭配、协调,创造团队的学习能力。

;

贪吃蛇程序中如何实现游戏结束

#ifndef __COLORCONSOLE__H__

#define __COLORCONSOLE__H__

#include windows.h

#include iostream

#include conio.h

#include time.h

using namespace std;

HANDLE initiate();

BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString);

#endif

#ifndef __FOOD__H__ //防止重复引用该头文件

#define __FOOD__H__

#include "colorConsole.h"

class Food

{

public:

void initfood(int); //出始化食物函数

protected:

WORD goodcolor[1]; //定义正常食物的颜色

WORD badcolor[1]; //定义变质食物的颜色

int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标

int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标

};

#endif

#ifndef __FOOD__H__ //防止重复引用该头文件

#define __FOOD__H__

#include "colorConsole.h"

class Food

{

public:

void initfood(int); //出始化食物函数

protected:

WORD goodcolor[1]; //定义正常食物的颜色

WORD badcolor[1]; //定义变质食物的颜色

int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标

int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标

};

#endif

#ifndef __SNAKE__H__ //防止重复引用该头文件

#define __SNAKE__H__

#include "colorConsole.h"

class Snake

{

public:

void initsnake(); //初始化蛇的函数

protected:

WORD bColor[1]; //定义蛇身的颜色

WORD hColor[1]; //定义蛇头的颜色

int headx,heady,bodyx[100],bodyy[100]; //定义蛇头和蛇身的坐标

int node; //定义蛇身的节数

};

#endif

#include "colorConsole.h"

HANDLE initiate()

{

HANDLE hOutput;

hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

return hOutput;

}

BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)

{

DWORD cWritten;

BOOL fSuccess;

COORD coord;

coord.X = x; // start at first cell

coord.Y = y; // of first row

fSuccess = WriteConsoleOutputCharacter(

hOutput, // screen buffer handle

lpszString, // pointer to source string

lstrlen(lpszString), // length of string

coord, // first cell to write to

cWritten); // actual number written

if (! fSuccess)

cout"error:WriteConsoleOutputCharacter"endl;

for (;fSuccess coord.X lstrlen(lpszString)+x; coord.X += nColors)

{

fSuccess = WriteConsoleOutputAttribute(

hOutput, // screen buffer handle

wColors, // pointer to source string

nColors, // length of string

coord, // first cell to write to

cWritten); // actual number written

}

if (! fSuccess)

cout"error:WriteConsoleOutputAttribute"endl;

return 0;

}

#include "Food.h"

HANDLE handle;

void Food::initfood(int lev) //初始化食物

{

numgood=lev*5;

numbad=lev*2;

goodcolor[0]=FOREGROUND_RED|FOREGROUND_BLUE; //正常食物的颜色

badcolor[0]=FOREGROUND_RED|FOREGROUND_GREEN; //变质食物的颜色

srand((unsigned) time(NULL));

textout(handle,64,6,goodcolor,1,"正常食物为★"); //输出图例

textout(handle,64,8,badcolor,1,"变质食物为★");

for(int fgood=0;fgoodnumgood;fgood++) //输出正常食物

{

goodx[fgood]=((rand()%55+4)/2)*2;

goody[fgood]=rand()%26+4;

textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");

}

for(int fbad=0;fbadnumbad;fbad++) //输出变质食物

{

badx[fbad]=((rand()%56+4)/2)*2;

bady[fbad]=rand()%26+4;

textout(handle,badx[fbad],bady[fbad],badcolor,1,"★");

}

}

#include "Game.h"

HANDLE handle;

Game::Game()

{

node=3; //蛇的出始默认节数

headx=44,heady=4; //蛇头的出始默认坐标

controlx=-2,controly=0; //蛇的出始默认方向为向左

start=0;

scores=0;

self=0,wall=0,died=0; //游戏结束条件默认为否

sColor[0]=FOREGROUND_GREEN|FOREGROUND_INTENSITY; //围墙颜色

head[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //开始界面文字颜色

}

int Game::starting()

{

while(1)

{

textout(handle,22,16,head,1,"控制键: W-上 S-下 A-左 D-右"); //开始界面

textout(handle,16,18,head,1,"吃完所有正常食物升级 千万不要吃到变质的食物哦!");

textout(handle,30,20,head,1, "选择级数 1-9 ");

if(_kbhit())

{

int v=_getch(); //选择级数

switch(v)

{

case 49:

level=1;

break;

case 50:

level=2;

break;

case 51:

level=3;

break;

case 52:

level=4;

break;

case 53:

level=5;

break;

case 54:

level=6;

break;

case 55:

level=7;

break;

case 56:

level=8;

break;

default:

level=9;

break;

}

textout(handle,16,18,head,1," "); //清屏

textout(handle,30,20,head,1, " ");

textout(handle,22,16,head,1," ");

break;

}

}

//设置四周围墙

for(int wideup=2;wideup60;wideup+=2) //上边

{

textout(handle,wideup,2,sColor,1,"■");

}

for(int widedown=2;widedown60;widedown+=2) //下边

{

textout(handle,widedown,30,sColor,1,"■");

}

for(int longr=2;longr30;longr++) //左边

{

textout(handle,2,longr,sColor,1,"■");

}

for(int longl=2;longl31;longl++) //右边

{

textout(handle,60,longl,sColor,1,"■");

}

levelup=5*level;

return level;

}

void Game::ifstart() //由用户判断是否开始游戏

{

textout(handle,24,0,sColor,1,"按 y 开始游戏");

int v=_getch();

switch(v)

{

case 'y': start=1;break;

default : start=0;break;

}

textout(handle,24,0,sColor,1," "); //清屏

}

bool Game::getstart() //获得start

{

return start;

}

void Game::moventurn() //关于移动和转向的函数

{

int len1=0; //设置一个消除原有图形的计数器

for(int len=0;lennode;len++) //消除原有图形

{

textout(handle,bodyx[len],bodyy[len],bColor,1," ");

}

for(int len6=node-1;len60;len6--) //显示移动以后的图形

{

len1=len6-1;

bodyx[len6]=bodyx[len1]; //获得前一节body的坐标

bodyy[len6]=bodyy[len1];

textout(handle,bodyx[len6],bodyy[len6],bColor,1,"●"); //移动后的图形

}

bodyx[0]=headx;

bodyy[0]=heady;

textout(handle,bodyx[0],bodyy[0],bColor,1,"●"); //显示移动后的第一节

headx+=controlx;

heady+=controly;

textout(handle,headx,heady,hColor,1,"◎"); //显示移动后的头部

Sleep(165-level*17); //速度

if(kbhit()) //转向

{

char direction=getch(); //获取方向

if(direction=='w'direc!='s')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=0;

controly=-1;

direc='w';

}

if(direction=='s'direc!='w')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=0;

controly=1;

direc='s';

}

if(direction=='a'direc!='d')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=-2;

controly=0;

direc='a';

}

if(direction=='d'direc!='a')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=2;

controly=0;

direc='d';

}

}

}

bool Game::ifgameover() //判断是否结束游戏

{

if(headx==2||headx==60||heady==2||heady==30) //撞墙

{

wall=1;

return 1;

}

for(int sbody=1;sbodynode;sbody++) //咬到自己

{

if(headx==bodyx[sbody]heady==bodyy[sbody])

{

self=1;

return 1;

}

}

for(int countbad=0;countbadnumbad;countbad++) //吃到变质食物

{

if(headx==badx[countbad]heady==bady[countbad])

{

died=1;

return 1;

}

}

return 0;

}

void Game::gameover() //输出游戏结束界面

{

//sndPlaySound("DOWN.wav",SND_ASYNC);

if(wall==1)

textout(handle,20,14,sColor,1,"撞到墙了 游戏结束");

if(self==1)

textout(handle,20,14,sColor,1,"咬到自己了 游戏结束");

if(died==1)

textout(handle,19,14,sColor,1,"吃到变质食物了 游戏结束");

}

void Game::scorenlevelup()

{

char marks[5],Level[5]; //准备输出级数和分数

for(int eatgood=0;eatgoodnumgood;eatgood++) //判断是否咬到正常食物以加分

{

if(headx==goodx[eatgood]heady==goody[eatgood])

{

//sndPlaySound("LASER.wav",SND_ASYNC);

goodx[eatgood]=74; //防止以后走到已吃过食物点的时候会判断为吃到而加分以影响升级

goody[eatgood]=6;

node++; //长一截

scores+=10; //加十分

itoa(scores,marks,10); //转换数据类型,准备输出

itoa(level,Level,10);

textout(handle,64,11,hColor,1,"分数:"); //输出分数

textout(handle,70,11,hColor,1,marks);

textout(handle,64,13,hColor,1,"级数:"); //输出级数

textout(handle,70,13,hColor,1,Level);

levelup--;

if(levelup==4)

{

for(int fgood=0;fgoodnumgood;fgood++) //再次输出正常食物

{

textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");

}

}

}

}

if(levelup==0) //升级

{

level++;

levelup=5*level;

for(int i=4;i58;i+=2) //清屏

{

for(int j=4;j30;j++)

{

textout(handle,i,j,sColor,1," ");

}

}

initfood(level); //输出食物

textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头

for(int j=0;jnode;j++) //打印蛇身

{

textout(handle,bodyx[j],bodyy[j],bColor,1,"●");

}

textout(handle,18,0,hColor,1,"按任意键开始更高的等级");

itoa(level,Level,10);

textout(handle,70,13,hColor,1,Level); //输出新的级数

while(1) //由用户控制是否开始更高等级

{

if (_kbhit())

{

textout(handle,18,0,sColor,1, " ");

break;

}

}

}

}

#include "Snake.h"

extern HANDLE handle;

void Snake::initsnake() //初始化蛇

{

hColor[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //蛇头颜色

bColor[0]=FOREGROUND_BLUE|FOREGROUND_INTENSITY; //蛇身颜色

for(int i=0;inode;i++) //蛇身各节的坐标定义

{

bodyx[i]=(i+1)*2+headx;

bodyy[i]=heady;

}

textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头

for(int j=0;jnode;j++) //打印蛇身

{

textout(handle,bodyx[j],bodyy[j],bColor,1,"●");

}

}

#include "Snake.h"

#include "Food.h"

#include "Game.h"

Game game;

HANDLE handle; //窗口句柄

void main()

{

//sndPlaySound("BACKGROUND.wav",SND_LOOP|SND_ASYNC);

handle=initiate(); //初始化窗口句柄

game.initfood(game.starting()); //初始化食物

game.initsnake(); //初始化蛇

game.ifstart(); //玩家是否开始游戏

if(game.getstart())

{ while(1)

{

game.moventurn(); //蛇的移动和转向

game.scorenlevelup(); //得分和升级

if(game.ifgameover()) //判断是否中止游戏

break;

}

game.gameover(); //输出游戏结束原因

}

}

这是一个控制台的贪吃蛇 比较简单

只要楼主了解几个API函数就可以 至于算法 有好多 我这个不怎么样

我建议用链表来表示蛇体 下面是我的一个snake 类 函数体没给 楼主自己想吧

struct node

{node* pre;br Cpoint point;br node* next;br};

node::node()

{ point(0,0);

pre=NULL;

next=NULL;

};

class snake

{ private:

node* snakehead;

node snakebody[100];

int length

int direction;

public:

void go();

bool fail();

void textout();

}

贪吃蛇游戏设计的目的及内容

主要内容就是设计蛇的形状和方向,以及食物的产生与位置,先创建一个Point对象point,由多个point 组合成蛇身,当蛇吃到食物时,则增加一个point在蛇身上,使蛇身不断加长,同时利用随机函数在游戏面板上产生一个新的Food对象代替原来被“吃掉”的food,其次,玩家使用方向键来控制蛇的行进,当蛇吃到 food 就给玩家加分作为奖励,最后就可以得出用户的分数。

设计的目的在于学习Java程序设计基本技术,学习用MyEclipse开发Java程序的相关技术,以及熟悉项目开发的完整过程。学会怎样进行一个项目的需求分析、概要设计、详细设计等软件开发过程,熟练地掌握Java程序设计的基本技术和方法,以及熟练地掌握MyEclipse开发环境的使用方法,培养初步的项目分析能力和程序设计能力。

游戏实现的具体任务如下:

1) 游戏的开始,停止,暂停,继续。 根据用户单击的按钮(开始,停止,暂停,继续),游戏实现具体相应的功能。

2) 游戏难度的调整。 用户可以通过选择相应难度地形来设置游戏的难度级别,级别越高,游戏难度也越大。

3) 游戏帮助。单击游戏帮助按钮弹出一个消息框提供相应的游戏介绍。另外在游戏中提供了网格对每一块小方块进行包裹,玩家可以选择是否显示网格,可以帮助刚入门的玩家比对蛇头与食物的位置。

4) 蛇身和背景颜色的设置。可以通过单击相应的设置按钮让蛇身或背景的颜色以用户喜欢的颜色输出。另外如果玩家选择显示网格,也可以单击相应按钮设置网格线的颜色。

5) 游戏奖励。当蛇吃到一颗食物时则得到10分作为奖励,同时如果吃到

特殊的食物则会得到普通食物三倍的奖励。但特殊的食物会在一定时间内自动消失。

本文标题:go语言项目贪吃蛇 贪吃蛇编程python
当前网址:https://www.cdcxhl.com/article8/dojoiop.html

成都网站建设公司_创新互联,为您提供商城网站网页设计公司虚拟主机全网营销推广网站导航网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设