• 回答数

    7

  • 浏览数

    132

zoemai0505
首页 > 毕业论文 > 贪吃蛇毕业论文c语言

7个回答 默认排序
  • 默认排序
  • 按时间排序

人艰不拆XP

已采纳

/* 贪吃蛇程序 by champking */#define N 200#include <>#include <>#include <>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score = 0;/*得分*/int gamespeed = 100000;/*游戏速度自己调整*/struct Food{ int x;/*食物的横坐标*/ int y;/*食物的纵坐标*/ int yes;/*判断是否要出现食物的变量*/}food;/*食物的结构体*/struct Snake{ int x[N]; int y[N]; int node;/*蛇的节数*/ int direction;/*蛇移动方向*/ int life;/* 蛇的生命,0活着,1死亡*/}snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){ Init();/*图形驱动*/ DrawK();/*开始画面*/ GamePlay();/*玩游戏具体过程*/ Close();/*图形结束*/}/*图形驱动*/void Init(void){ int gd = DETECT, gm; initgraph(&gd, &gm, "c:\\tc"); cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/void DrawK(void){/*setbkcolor(LIGHTGREEN);*/ setcolor(11); setlinestyle(SOLID_LINE, 0, THICK_WIDTH);/*设置线型*/ for(i = 50; i <= 600; i += 10)/*画围墙*/ { rectangle(i, 40, i + 10, 49); /*上边*/ rectangle(i, 451, i + 10, 460);/*下边*/ } for(i = 40; i <= 450; i += 10) { rectangle(50, i, 59, i + 10); /*左边*/ rectangle(601, i, 610, i + 10);/*右边*/ }}/*玩游戏具体过程*/void GamePlay(void){ randomize();/*随机数发生器*/ = 1;/*1表示需要出现新食物,0表示已经存在食物*/ = 0;/*活着*/ = 1;/*方向往右*/ [0] = 100; [0] = 100;/*蛇头*/ [1] = 110; [1] = 100; = 2;/*节数*/ PrScore();/*输出得分*/ while(1)/*可以重复玩游戏,压ESC键结束*/ { while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/ { if( == 1)/*需要出现新食物*/ { = rand() % 400 + 60; = rand() % 350 + 60; while( % 10 != 0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/ ; while( % 10 != 0) ; = 0;/*画面上有食物了*/ } if( == 0)/*画面上有食物了就要显示*/ { setcolor(GREEN); rectangle(, , + 10, - 10); } for(i = - 1; i > 0; i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/ { [i] = [i-1]; [i] = [i-1]; } /*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/ switch() { case 1: [0] += 10; break; case 2: [0] -= 10; break; case 3: [0] -= 10; break; case 4: [0] += 10; break; } for(i = 3; i < ; i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/ { if([i] == [0] && [i] == [0]) { GameOver();/*显示失败*/ = 1; break; } } if([0]<55||[0]>595||[0]<55|| [0]>455)/*蛇是否撞到墙壁*/ { GameOver();/*本次游戏结束*/ ; /*蛇死*/ } if( == 1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/ break; if([0] == && [0] == )/*吃到食物以后*/ { setcolor(0);/*把画面上的食物东西去掉*/ rectangle(, , + 10, - 10); [] =- 20; [] =- 20; /*新的一节先放在看不见的位置,下次循环就取前一节的位置*/ ;/*蛇的身体长一节*/ = 1;/*画面上需要出现新的食物*/ score += 10; PrScore();/*输出新得分*/ } setcolor(4);/*画出蛇*/ for(i = 0; i < ; i++) rectangle([i], [i], [i] + 10, [i] - 10); delay(gamespeed); setcolor(0);/*用黑色去除蛇的的最后一节*/ rectangle([], [], [] + 10, [ - 1] - 10); } /*endwhile(!kbhit)*/ if( == 1)/*如果蛇死就跳出循环*/ break; key = bioskey(0);/*接收按键*/ if(key == ESC)/*按ESC键退出*/ break; else if(key == UP&&!=4) /*判断是否往相反的方向移动*/ ; else if(key == RIGHT && != 2) ; else if(key == LEFT && != 1) = 2; else if(key == DOWN && != 3) = 4; }/*endwhile(1)*/}/*游戏结束*/void GameOver(void){ cleardevice(); PrScore(); setcolor(RED); settextstyle(0, 0, 4); outtextxy(200, 200, "GAME OVER"); getch();}/*输出成绩*/void PrScore(void){ char str[10]; setfillstyle(SOLID_FILL, YELLOW); bar(50, 15, 220, 35); setcolor(6); settextstyle(0,0,2); sprintf(str, "score:%d", score); outtextxy(55, 20, str);}/*图形结束*/void Close(void){ getch(); closegraph();}

307 评论

西关少爷Billy

#include<> #include<> #include<> #include<> #include<> #include<> int grade=5,point=0,life=3; void set(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate(); struct bug { int x; int y; struct bug *last; struct bug *next; }; struct fd { int x; int y; int judge; }food={0,0,0}; struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL; void main() { char ch; initgraph(800,600); set(); init_insect(); while(1) { food_f(); Sleep(grade*10); setcolor(BLACK); circle(head_l->x,head_l->y,2); setcolor(WHITE); move_body(); if(kbhit()) { ch=getch(); if(ch==27) { ahead(); set(); } else if(ch==-32) { switch(getch()) { case 72:upon();break; case 80:down();break; case 75:left();break; case 77:right();break; } } else ahead(); } else { ahead(); } if(head_f->x==>y==) { Sleep(100); crate(); ; point=point+(6-grade)*10; if(<30||<30||>570||>570) life++; menu(); } if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595) { Sleep(1000); life--; ; init_graph(); init_insect(); menu(); } for(p1=head_f->next;p1!=NULL;p1=p1->next) { if(head_f->x==p1->x&&head_f->y==p1->y) { Sleep(1000); life--; ; init_graph(); init_insect(); menu(); break; } } if(life==0) { outtextxy(280,300,"游戏结束!"); getch(); return; } move(); }; } void init_graph() { clearviewport(); setlinestyle(PS_SOLID,1,5); rectangle(2,2,600,598); setlinestyle(PS_SOLID,1,1); } void set() { init_graph(); outtextxy(640,50,"1、开始 / 返回"); outtextxy(640,70,"2、退出"); outtextxy(640,90,"3、难度"); outtextxy(640,110,"4、重新开始"); switch(getch()) { case '1': menu();setcolor(GREEN);circle();setcolor(WHITE);return; case '2': exit(0);break; case '3': outtextxy(700,90,":1 2 3 4 5");grade=getch()-48;set();break; case '4': ;point=0;life=3;init_insect();menu();break; default: outtextxy(250,300,"输入错误!"); set();break; } } void menu() { char str[20],str1[]={"分数:"},str2[]={"难度:"},str3[]={"生命值:"}; init_graph(); sprintf(str,"%d",point); strcat(str1,str); outtextxy(640,50,str1); sprintf(str,"%d",grade); strcat(str2,str); outtextxy(640,70,str2); sprintf(str,"%d",life); strcat(str3,str); outtextxy(640,90,str3); outtextxy(640,110,"设置:ESC"); } void init_insect() { head_f=(struct bug *)malloc(sizeof(struct bug)); head_f->last=NULL; head_f->x=300; head_f->y=300; p2=head_f->next=p1=(struct bug *)malloc(sizeof(struct bug)); p1->last=head_f; p1->x=295; p1->y=300; p1=p1->next=(struct bug *)malloc(sizeof(struct bug)); p1->next=NULL; p1->x=290; p1->y=300; p1->last=p2; head_l=p1; } void move() { for(p1=head_f;p1!=NULL;p1=p1->next) { circle(p1->x,p1->y,2); } } void move_head() { } void move_body() { for(p1=head_l,p2=p1->last;p2!=NULL;p1=p2,p2=p2->last) { p1->x=p2->x; p1->y=p2->y; } } void ahead() { p1=head_f; p2=p1->next; p2=p2->next; if(p1->x==p2->x) { if(p1->y>p2->y) head_f->y+=5; else head_f->y-=5; } else { if(p1->x>p2->x) { head_f->x+=5; } else head_f->x-=5; } } void upon() { p1=head_f->next; p1=p1->next; head_f->y-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y+=5; ahead(); } } void down() { p1=head_f->next; p1=p1->next; head_f->y+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y-=5; ahead(); } } void left() { p1=head_f->next; p1=p1->next; head_f->x-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x+=5; ahead(); } } void right() { p1=head_f->next; p1=p1->next; head_f->x+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x-=5; ahead(); } } void food_f() { if(!) { (rand()%117+1)*5; (rand()%117+1)*5; ; if(<30||<30||>570||>570) { setcolor(RED); circle(f

246 评论

习习谷风

我调试了一下发现有三种情况:(断点在main 中的move函数处)1 每一次移动有输入,方向已改。2 每一次无输入。3 前几次有输入,到后几次无。结果:第一种直接在第6次就出错,斜着移动。 第二种几下就错了。 第三种,调试 一次输入一次方向,直到7次后,单句执行发现上一次蛇头(28,11),下一次蛇头(29,12)。看出问题了吧,就是在move的时候出错,第一次的值光标位置就成了(15,11),这个是不是就不符合原意呢?等有时间帮你再看。

331 评论

anne贝多芬

代码示例如下:

使用了4个自定义函数来实现

#include <>

#include <>

#include <>

#include <>

#define High 20  // 游戏画面尺寸

#define Width 30

// 全局变量

int moveDirection; // 小蛇移动位置,上下左右分别用1,2,3,4表示

int food_x,food_y; // 食物的位置

int canvas[High][Width] = {0}; // 二维数组存储游戏画布中对应的元素

// 0为空格0,-1为边框#,-2为食物F,1为蛇头@,大于1的正数为蛇身*

void gotoxy(int x,int y)  //光标移动到(x,y)位置

{

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

COORD pos;

= x;

= y;

SetConsoleCursorPosition(handle,pos);

}

// 移动小蛇

// 第一步扫描数组canvas所有元素,找到正数元素都+1

// 找到最大元素(即蛇尾巴),把其变为0

// 找到=2的元素(即蛇头),再根据输出的上下左右方向,把对应的另一个像素值设为1(新蛇头)

void moveSnakeByDirection()

{

int i,j;

for (i=1;i

for (j=1;j

if (canvas[i][j]>0)

canvas[i][j]++;

int oldTail_i,oldTail_j,oldHead_i,oldHead_j;

int max = 0;

for (i=1;i

for (j=1;j

if (canvas[i][j]>0)

{

if (max

{

max = canvas[i][j];

oldTail_i = i;

oldTail_j = j;

}

if (canvas[i][j]==2)

{

oldHead_i = i;

oldHead_j = j;

}

}

int newHead_i,newHead_j;

if (moveDirection==1) // 向上移动

{

newHead_i = oldHead_i-1;

newHead_j = oldHead_j;

}

if (moveDirection==2) // 向下移动

{

newHead_i = oldHead_i+1;

newHead_j = oldHead_j;

}

if (moveDirection==3) // 向左移动

{

newHead_i = oldHead_i;

newHead_j = oldHead_j-1;

}

if (moveDirection==4) // 向右移动

{

newHead_i = oldHead_i;

newHead_j = oldHead_j+1;

}

// 新蛇头如果吃到食物

if (canvas[newHead_i][newHead_j]==-2)

{

canvas[food_x][food_y] = 0;

// 产生一个新的食物

food_x = rand()%(High-5) + 2;

food_y = rand()%(Width-5) + 2;

canvas[food_x][food_y] = -2;

// 原来的旧蛇尾留着,长度自动+1

}

else // 否则,原来的旧蛇尾减掉,保持长度不变

canvas[oldTail_i][oldTail_j] = 0;

// 是否小蛇和自身撞,或者和边框撞,游戏失败

if (canvas[newHead_i][newHead_j]>0 || canvas[newHead_i][newHead_j]==-1)

{

printf("游戏失败!\n");

Sleep(2000);

system("pause");

exit(0);

}

else

canvas[newHead_i][newHead_j] = 1;

}

void startup() // 数据初始化

{

int i,j;

// 初始化边框

for (i=0;i

{

canvas[i][0] = -1;

canvas[i][Width-1] = -1;

}

for (j=0;j

{

canvas[0][j] = -1;

canvas[High-1][j] = -1;

}

// 初始化蛇头位置

canvas[High/2][Width/2] = 1;

// 初始化蛇身,画布中元素值分别为2,3,4,5....

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

canvas[High/2][Width/2-i] = i+1;

// 初始小蛇向右移动

moveDirection = 4;

food_x = rand()%(High-5) + 2;

food_y = rand()%(Width-5) + 2;

canvas[food_x][food_y] = -2;

}

void show()  // 显示画面

{

gotoxy(0,0);  // 光标移动到原点位置,以下重画清屏

int i,j;

for (i=0;i

{

for (j=0;j

{

if (canvas[i][j]==0)

printf(" ");   //   输出空格

else if (canvas[i][j]==-1)

printf("#");   //   输出边框#

else if (canvas[i][j]==1)

printf("@");   //   输出蛇头@

else if (canvas[i][j]>1)

printf("*");   //   输出蛇身*

else if (canvas[i][j]==-2)

printf("F");   //   输出食物F

}

printf("\n");

}

Sleep(100);

}

void updateWithoutInput()  // 与用户输入无关的更新

{

moveSnakeByDirection();

}

void updateWithInput()  // 与用户输入有关的更新

{

char input;

if(kbhit())  // 判断是否有输入

{

input = getch();  // 根据用户的不同输入来移动,不必输入回车

if (input == 'a')

{

moveDirection = 3;   // 位置左移

moveSnakeByDirection();

}

else if (input == 'd')

{

moveDirection = 4;  // 位置右移

moveSnakeByDirection();

}

else if (input == 'w')

{

moveDirection = 1;  // 位置上移

moveSnakeByDirection();

}

else if (input == 's')

{

moveDirection = 2;   // 位置下移

moveSnakeByDirection();

}

}

}

int main()

{

startup();  // 数据初始化

while (1) //  游戏循环执行

{

show();  // 显示画面

updateWithoutInput();  // 与用户输入无关的更新

updateWithInput();  // 与用户输入有关的更新

}

return 0;

}

C语言是一门通用计算机编程语言,广泛应用于底层开发。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

二十世纪八十年代,为了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。  目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。

C语言是一门面向过程的计算机编程语言,与C++,Java等面向对象的编程语言有所不同。

其编译器主要有Clang、GCC、WIN-TC、SUBLIME、MSVC、Turbo C等。

210 评论

taojia1988

p->x和p->y都是指向int的指针,不是数组,所以x[1]、y[1]等数据,使用的全是未申请的内存。你应该定将他们成一个整型数组,大小足够大,或者干脆使用链表。

339 评论

Lookiamycm

/*贪吃蛇程序bychampking*/#defineN200#include<>#include<>#include<>#defineLEFT0x4b00#defineRIGHT0x4d00#defineDOWN0x5000#defineUP0x4800#defineESC0x011binti,key;intscore=0;/*得分*/intgamespeed=100000;/*游戏速度自己调整*/structFood{intx;/*食物的横坐标*/inty;/*食物的纵坐标*/intyes;/*判断是否要出现食物的变量*/}food;/*食物的结构体*/structSnake{intx[N];inty[N];intnode;/*蛇的节数*/intdirection;/*蛇移动方向*/intlife;/*蛇的生命,0活着,1死亡*/}snake;voidInit(void);/*图形驱动*/voidClose(void);/*图形结束*/voidDrawK(void);/*开始画面*/voidGameOver(void);/*结束游戏*/voidGamePlay(void);/*玩游戏具体过程*/voidPrScore(void);/*输出成绩*//*主函数*/voidmain(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/voidInit(void){intgd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/voidDrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49);/*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10);/*左边*/rectangle(601,i,610,i+10);/*右边*/}}/*玩游戏具体过程*/voidGamePlay(void){randomize();/*随机数发生器*/;/*1表示需要出现新食物,0表示已经存在食物*/;/*活着*/;/*方向往右*/[0]=100;[0]=100;/*蛇头*/[1]=110;[1]=100;;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if()/*需要出现新食物*/{()%400+60;()%350+60;while(!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/;while(!=0);;/*画面上有食物了*/}if()/*画面上有食物了就要显示*/{setcolor(GREEN);rectangle();}for(i=;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/{[i]=[i-1];[i]=[i-1];}/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/switch(){case1:[0]+=10;break;case2:[0]-=10;break;case3:[0]-=10;break;case4:[0]+=10;break;}for(i=3;i<;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/{if([i]==[0]&&[i]==[0]){GameOver();/*显示失败*/;break;}}if([0]<55||[0]>595||[0]<55||[0]>455)/*蛇是否撞到墙壁*/{GameOver();/*本次游戏结束*/;/*蛇死*/}if()/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/break;if([0]==[0]==)/*吃到食物以后*/{setcolor(0);/*把画面上的食物东西去掉*/rectangle();[]=-20;[]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/;/*蛇的身体长一节*/;/*画面上需要出现新的食物*/score+=10;PrScore();/*输出新得分*/}setcolor(4);/*画出蛇*/for(i=0;i<;i++)rectangle([i],[i],[i]+10,[i]-10);delay(gamespeed);setcolor(0);/*用黑色去除蛇的的最后一节*/rectangle([],[],[]+10,[]-10);}/*endwhile(!kbhit)*/if()/*如果蛇死就跳出循环*/break;key=bioskey(0);/*接收按键*/if(key==ESC)/*按ESC键退出*/break;elseif(key==UP&&!=4)/*判断是否往相反的方向移动*/;elseif(key==RIGHT&&!=2);elseif(key==LEFT&&!=1);elseif(key==DOWN&&!=3);}/*endwhile(1)*/}/*游戏结束*/voidGameOver(void){cleardevice();PrScore();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAMEOVER");getch();}/*输出成绩*/voidPrScore(void){charstr[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"score:%d",score);outtextxy(55,20,str);}/*图形结束*/voidClose(void){getch();closegraph();}

214 评论

chocolate宸

没写过……用javap写过

236 评论

相关问答

  • html贪吃蛇游戏毕业论文

    能。1、贪吃蛇简单些,而且可扩展的不少,方面多,如果不好好考虑和设计,将难以成功开发出这个游戏。2、在这个游戏设计中,牵涉到图形界面的显示与更新、数据的收集与更

    詹姆斯尐雪 4人参与回答 2023-12-11
  • 基于贪吃蛇java毕业论文

    学生课程设计(论文)题 目: 贪吃蛇游戏程序设计指导学生姓名: 学 号:200910801001所在院(系): 计算机学院专

    肖小月半仔 3人参与回答 2023-12-10
  • 贪吃蛇毕业论文c语言

    /* 贪吃蛇程序 by champking */#define N 200#include #include #include #define LEFT 0x4

    zoemai0505 7人参与回答 2023-12-05
  • js设计贪吃蛇游戏毕业论文

    贪吃蛇 Snake v2.4 body{ font-size:9pt; } table{

    天天开心好好好 4人参与回答 2023-12-09
  • 贪吃蛇游戏毕业论文设计

    2.1程序功能介绍贪吃蛇游戏是一个经典小游戏,一条蛇在封闭围墙里,围墙里随机出现一个食物,通过按键盘四个光标键控制蛇向上下左右四个方向移动,蛇头撞倒食物,则食物

    民辉窗帘布艺 4人参与回答 2023-12-10