贪吃蛇代码-C语言-VC++6.0
贪吃蛇代码(C语言)

UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
// TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK Win_tetris_Proc(
rect.left = 0;
rect.top = 0;
rect.right = 16;
rect.bottom = 8;
hbr= CreateSolidBrush(RGB(255,0,0));
int nCmdShow // show state
)
{
snk_1.Head_X = 0x01;//head x
snk_1.Head_Y = 0x00;//head y
snk_1.Tail_X = 0x00;//tail x
snk_1.Tail_Y = 0x00;//tail y
{
snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
s_count[snk_1.h_index].direction = GO_UP;
s_count[snk_1.h_index].cnt = 1;
}
}
else if(40 == wParam)
{
if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT))
贪吃蛇c++语言程序,C++语言贪吃蛇游戏源代码.doc

贪吃蛇c++语⾔程序,C++语⾔贪吃蛇游戏源代码.docC语⾔贪吃蛇游戏源代码/*Author : 柳印奇email : liuyinqi2012@Description : 蛇年来临创作⼀个与蛇有关的经典游戏《贪吃蛇》,与同学们分享蛇年美好的时光。
欢迎交流与探讨,直接将代码粘贴到VC6.0的环境下即可运⾏。
← 左转→ 右转↓ 向下↑ 向上积分达到⼀定程度,会有换命的活动,命最多6条。
难度会随积分的上升逐渐上升,最多到6的难度。
*/#include#include#include#include#pragma comment(lib, "winmm.lib")using namespace std;#define GameW 20#define GameH 11const int CtrlLeft = GameW*2+4 + 3;struct Point {Point(){}Point& operator=(const Point& rhs) {_x = rhs._x;_y = rhs._y;return *this;}Point(int x, int y) {_x = x, _y = y;}int _x, _y;};HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);HANDLE g_hInput = GetStdHandle(STD_INPUT_HANDLE); Point g_ptCursor(0,0);BOOL isChecking = FALSE;BOOL g_bGameOver = FALSE;BOOL bCreateEgg = FALSE;int g_nGameBack[GameH][GameW];int nowKeyInfo = -1;int g_nDiff = 1;int g_nLife = 3;int g_nScore = 0;void SetCursor(COORD cd) {SetConsoleCursorPosition(g_hOutput, cd);}void SetCursor(int x, int y){COORD cd = {x, y};SetCursor(cd);}void SetBlockCursor(int x, int y){COORD cd = {2*x + 2, y + 1};SetCursor(cd);}void SetBack(int x, int y, BOOL bk) {SetBlockCursor(x, y);if (bk)printf("%s", "■");elseprintf(" ");}bool Out(int x, int y) {return x < 0 || y < 0 || x >= GameW || y >= GameH;}struct Egg {int x;int y;Point fangxiang;Egg() {}Egg(int px, int py) {x = px;y = py;fangxiang._x = 1;fangxiang._y = 0;}Egg(const Egg& rhs) {x = rhs.x;y = rhs.y;fangxiang = rhs.fangxiang;}Egg& operator=(const Egg& rhs) { x = rhs.x;y = rhs.y;fangxiang = rhs.fangxiang;return *this;}void DrawMe() {if (!Out(x,y)) {SetBack(x, y, TRUE);}}void eraseMe() {if (!Out(x,y)) {SetBack(x, y, FALSE);}}};Egg egg;class Snake {public:vector List;Snake(){}Snake(Egg& eg) { List.push_back(eg); }void move() {。
C语言小游戏源代码《贪吃蛇》

void init(void){/*构建图形驱动函数*/ int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice(); }
欢迎您阅读该资料希望该资料能给您的学习和生活带来帮助如果您还了解更多的相关知识也欢迎您分享出来让我们大家能共同进步共同成长
C 语言小游戏源代码《贪吃பைடு நூலகம்》
#define N 200/*定义全局常量*/ #define m 25 #include <graphics.h> #include <math.h> #include <stdlib.h> #include <dos.h> #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define Esc 0x011b int i,j,key,k; struct Food/*构造食物结构体*/ { int x; int y; int yes; }food; struct Goods/*构造宝贝结构体*/ { int x; int y; int yes; }goods; struct Block/*构造障碍物结构体*/ { int x[m]; int y[m]; int yes; }block; struct Snake{/*构造蛇结构体*/ int x[N]; int y[N]; int node; int direction; int life; }snake; struct Game/*构建游戏级别参数体*/ { int score; int level; int speed;
贪吃蛇代码-c语言-vc6.0

#include <conio.h>
#define N 21
int apple[3];
char score[3];
char tail[3];
void gotoxy(int x, int y) //输出坐标
{
COORD pos;
pos.X = x;
pos.Y = y;
/*这是一个贪吃蛇代码,运行环境VC++6.0(亲测完美运行)*/
/*该程序在dos系统下运行,不需要graphics.h头文件*/
/*该程序由C语言小方贡献,谢谢您的支持*/
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
apple[0] = rand()%N + 1;
apple[1] = rand()%N + 1;
apple[2] = 1;
}
Sleep(200-score[3]*10);
setbuf(stdin, NULL);
if (kbhit())
{
gotoxy(0, N+2);
ch = getche();
}
snake = Move(snake, ch, &len);
}
int File_in() //取记录的分数
{
FILE *fp;
if((fp = fopen("C:\\tcs.txt","a+")) == NULL)
完整版C语言贪吃蛇设计思路和代码详解

完整版C语言贪吃蛇设计思路和代码详解到了本章,你已经学完了大部分C语言的基础知识,详细讲解贪吃蛇代码的条件就具备了。
本教程教你编写的贪吃蛇不依赖TC环境,不依赖任何第三方库,可以在VC 6.0、VS等常见IDE中编译通过,请看下图:更多效果图请查看:游戏初始化、游戏进行中、游戏结束。
请大家先把贪吃蛇的源码下载下来浏览一下,我们再具体分析,这样将会有更好的效果。
贪吃蛇源代码下载:提取密码:81qm贪吃蛇设计思路上图中的红色空心方框(?)表示边框,是贪吃蛇的边界,贪吃蛇不能碰到它,否则就“死掉”,游戏结束。
绿色实心方框(?)表示贪吃蛇的活动范围,贪吃蛇可以自由移动,食物(苹果)也会随机出现在这个区域。
我们不妨将贪吃蛇的活动范围称为“贪吃蛇地图”,而加上边框就称为“全局地图”。
我们需要记录地图中每一个节点的信息,包括:位置:也就是第几行几列;类型:这个节点出现的是贪吃蛇、食物、边框,还是什么都没有(绿色的背景)。
索引:也就是数组下标,稍后会说明是什么意思。
所以需要定义一个结构体二维数组:struct{char type;int index;}globalMap[MAXWIDTH][MAXHEIGHT];用一维下标和二维下标表示位置;用 type 表示类型,不同的类型用不同的数字代表;用 index 表示索引。
直观上讲,应该将 type 定义为int类型,不过int占用四个字节,而节点类型的取值范围非常有限,一个字节就足够了,所以为了节省内存才定义为char类型。
同时,再建立一个足够大的一维数组,让贪吃蛇在数组内活动:struct{int x;int y;} snakeMap[ (MAXWIDTH-2)*(MAXHEIGHT-2) ]x、y 表示行和列,也就是 globalMap 数组的两个下标。
globalMap 数组中的索引 index 就是 snakeMap 数组的下标。
globalMap 表示了所有节点的信息,而 snakeMap 只表示了贪吃蛇的活动区域。
基于C语言实现的贪吃蛇游戏完整实例代码

基于C语言实现的贪吃蛇游戏完整实例代码#include <graphics.h>#include <conio.h>#include <stdlib.h>#include <dos.h>#define NULL 0#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181struct snake{int centerx;int centery;int newx;int newy;struct snake *next;};struct snake *head;int grade=60; /*控制速度的*******/int a,b; /* 背静遮的位置*/void *far1,*far2,*far3,*far4; /* 蛇身指针背静遮的指针虫子*/int size1,size2,size3,size4; /* **全局变量**/int ch=RIGHT; /**************存按键开始蛇的方向为RIGHT***********/int chy=RIGHT;int flag=0; /*********判断是否退出游戏**************/int control=4; /***********判断上次方向和下次方向不冲突***/int nextshow=1; /*******控制下次蛇身是否显示***************/int scenterx; /***************随即矩形中心坐标***************/int scentery;int sx; /*******在a b 未改变前得到他们的值保证随机矩形也不在此出现*******/int sy;/************************蛇身初始化**************************/ void snakede(){struct snake *p1,*p2;head=p1=p2=(struct snake *)malloc(sizeof(struct snake));p1->centerx=80;p1->newx=80;p1->centery=58;p1->newy=58;p1=(struct snake *)malloc(sizeof(struct snake));p2->next=p1;p1->centerx=58;p1->newx=58;p1->centery=58;p1->newy=58;p1->next=NULL;}/*******************end*******************/void welcome() /*************游戏开始界面,可以选择速度**********/{int key;int size;int x=240;int y=300;int f;void *buf;setfillstyle(SOLID_FILL,BLUE);bar(98,100,112,125);setfillstyle(SOLID_FILL,RED);bar(98,112,112,114);setfillstyle(SOLID_FILL,GREEN);bar(100,100,110,125);size=imagesize(98,100,112,125);buf=malloc(size);getimage(98,100,112,125,buf);cleardevice();setfillstyle(SOLID_FILL,BLUE);bar(240,300,390,325);outtextxy(193,310,"speed:");setfillstyle(SOLID_FILL,RED);bar(240,312,390,314);setcolor(YELLOW);outtextxy(240,330,"DOWN");outtextxy(390,330,"UP");outtextxy(240,360,"ENTER to start..." );outtextxy(270,200,"SNAKE");fei(220,220);feiyang(280,220);yang(340,220);putimage(x,y,buf,COPY_PUT);setcolor(RED);rectangle(170,190,410,410);while(1){ if(bioskey(1)) /********8选择速度部分************/key=bioskey(0);switch(key){case ENTER:f=1;break;case DOWN:if(x>=240){ putimage(x-=2,y,buf,COPY_PUT);grade++;key=0;break;}case UP:if(x<=375){ putimage(x+=2,y,buf,COPY_PUT);grade--;key=0;break;}}break;} /********** end ****************/free(buf);}/*************************随即矩形*****************//***********当nextshow 为1的时候才调用此函数**********/ void ran(){ int nx;int ny;int show; /**********控制是否显示***********/int jump=0;struct snake *p;p=head;if(nextshow==1) /***********是否开始随机产生***************/while(1){show=1;randomize();nx=random(14);ny=random(14);scenterx=nx*22+58;scentery=ny*22+58;while(p!=NULL){if(scenterx==p->centerx&&scentery==p->centery||scenter x==sx&&scentery==sy){show=0;break;}elsep=p->next;if(jump==1)break;}if(show==1){putimage(scenterx-11,scentery-11,far3,COPY_PUT); nextshow=0;break;}}}/***********过关动画**************/void donghua(){ int i;cleardevice();setbkcolor(BLACK);randomize();while(1){for(i=0;i<=5;i++){putpixel(random(640),random(80),13);putpixel(random(640),random(80)+80,2); putpixel(random(640),random(80)+160,3); putpixel(random(640),random(80)+240,4); putpixel(random(640),random(80)+320,1); putpixel(random(640),random(80)+400,14);}setcolor(YELLOW);settextstyle(0,0,4);outtextxy(130,200,"Wonderful!!");setfillstyle(SOLID_FILL,10);bar(240,398,375,420);feiyang(300,400);fei(250,400);yang(350,400);if(bioskey(1))if(bioskey(0)==ESC){flag=1;break;}}}/*************************end************************//***********************初始化图形系统*********************/ void init(){int a=DETECT,b;int i,j;initgraph(&a,&b,"");}/***************************end****************************//***画立体边框效果函数******/void tline(int x1,int y1,int x2,int y2,int white,int black) { setcolor(white);line(x1,y1,x2,y1);line(x1,y1,x1,y2);setcolor(black);line(x2,y1,x2,y2);line(x1,y2,x2,y2);}/****end*********//*************飞洋标志**********/int feiyang(int x,int y){int feiyang[18][18]={ {0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0}, {0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0},{0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,0,1,1,1,1,1,0,0,1,0,0,1,1,0,0,0,0},{0,0,1,1,1,0,0,0,0,1,0,1,1,1,0,0,0,0},{0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0},{0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (feiyang[i][j]==1)putpixel(j+x,i+y,RED);}}/********"飞"字*************/int fei(int x,int y){int fei[18][18]={{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (fei[i][j]==1)putpixel(j+x,i+y,BLUE);}}/*********"洋"字**************/int yang(int x,int y){int yang[18][18]={{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0},{0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0},{0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0},{0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0},{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (yang[i][j]==1)putpixel(j+x,i+y,BLUE);}}/******************主场景**********************/int bort(){ int a;setfillstyle(SOLID_FILL,15);bar(49,49,71,71);setfillstyle(SOLID_FILL,BLUE);bar(50,50,70,70);size1=imagesize(49,49,71,71);far1=(void *)malloc(size1);getimage(49,49,71,71,far1);cleardevice();setfillstyle(SOLID_FILL,12);bar(49,49,71,71);size2=imagesize(49,49,71,71);far2=(void *)malloc(size2);getimage(49,49,71,71,far2);setfillstyle(SOLID_FILL,12);bar(49,49,71,71);setfillstyle(SOLID_FILL,GREEN);bar(50,50,70,70);size3=imagesize(49,49,71,71);far3=(void *)malloc(size3);getimage(49,49,71,71,far3);cleardevice(); /*取蛇身节点背景节点虫子节点end*/setbkcolor(8);setfillstyle(SOLID_FILL,GREEN);bar(21,23,600,450);tline(21,23,600,450,15,8); /***开始游戏场景边框立体效果*******/tline(23,25,598,448,15,8);tline(45,45,379,379,8,15);tline(43,43,381,381,8,15);tline(390,43,580,430,8,15);tline(392,45,578,428,8,15);tline(412,65,462,85,15,8);tline(410,63,464,87,15,8);tline(410,92,555,390,15,8);tline(412,94,553,388,15,8);tline(431,397,540,420,15,8);tline(429,395,542,422,15,8);tline(46,386,377,428,8,15);tline(44,384,379,430,8,15);setcolor(8);outtextxy(429,109,"press ENTER ");outtextxy(429,129,"---to start"); /*键盘控制说明*/ outtextxy(429,169,"press ESC ");outtextxy(429,189,"---to quiet");outtextxy(469,249,"UP");outtextxy(429,289,"LEFT");outtextxy(465,329,"DOWN");outtextxy(509,289,"RIGHT");setcolor(15);outtextxy(425,105,"press ENTER ");outtextxy(425,125,"---to start");outtextxy(425,165,"press ESC ");outtextxy(425,185,"---to quiet");outtextxy(465,245,"UP");outtextxy(425,285,"LEFT");outtextxy(461,325,"DOWN");outtextxy(505,285,"RIGHT"); /*******end*************/setcolor(8);outtextxy(411,52,"score");outtextxy(514,52,"left");setcolor(15);outtextxy(407,48,"score");outtextxy(510,48,"left");size4=imagesize(409,62,465,88); /****分数框放到内存********/far4=(void *)malloc(size4);getimage(409,62,465,88,far4);putimage(500,62,far4,COPY_PUT); /*******输出生命框***********/setfillstyle(SOLID_FILL,12);setcolor(RED);outtextxy(415,70,"0"); /***************输入分数为零**********/outtextxy(512,70,"20"); /*************显示还要吃的虫子的数目*********/bar(46,46,378,378);feiyang(475,400);fei(450,400);yang(500,400);outtextxy(58,390,"mailto:************************");outtextxy(58,410,"snake game");outtextxy(200,410,"made by yefeng");while(1){ if(bioskey(1))a=bioskey(0);if(a==ENTER)break;}}/******************gameover()******************/void gameover(){ char *p="GAME OVER";int cha;setcolor(YELLOW);settextstyle(0,0,6);outtextxy(100,200,p);while(1){if(bioskey(1))cha=bioskey(0);if(cha==ESC){flag=1;break;}}}/***********显示蛇身**********************/void snakepaint(){struct snake *p1;p1=head;putimage(a-11,b-11,far2,COPY_PUT);while(p1!=NULL){putimage(p1->newx-11,p1->newy-11,far1,COPY_PUT);p1=p1->next;}}/****************end**********************//*********************蛇身刷新变化游戏关键部分*******************/void snakechange(){struct snake *p1,*p2,*p3,*p4,*p5;int i,j;static int n=0;static int score;static int left=20;char sscore[5];char sleft[1];p2=p1=head;while(p1!=NULL){ p1=p1->next;if(p1->next==NULL){a=p1->newx;b=p1->newy; /************记录最后节点的坐标************/sx=a;sy=b;}p1->newx=p2->centerx;p1->newy=p2->centery;p2=p1;}p1=head;while(p1!=NULL){p1->centerx=p1->newx;p1->centery=p1->newy;p1=p1->next;}/********判断按键方向*******/if(bioskey(1)){ ch=bioskey(0);if(ch!=RIGHT&&ch!=LEFT&&ch!=UP&&ch!=DOWN&&ch!= ESC) /********chy为上一次的方向*********/ch=chy;}switch(ch){case LEFT: if(control!=4){head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}else{ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}chy=ch;break;case DOWN:if(control!=1){ head->newy=head->newy+22; head->centery=head->newy; control=3;if(head->newy>377) gameover();}else{ head->newy=head->newy-22; head->centery=head->newy; control=1;if(head->newy<47) gameover();}chy=ch;break;case RIGHT: if(control!=2){ head->newx=head->newx+22; head->centerx=head->newx; control=4;if(head->newx>377) gameover();}else{ head->newx=head->newx-22; head->centerx=head->newx; control=2;if(head->newx<47) gameover();}chy=ch;break;case UP: if(control!=3){ head->newy=head->newy-22;head->centery=head->newy;control=1;if(head->newy<47)gameover();}else{ head->newy=head->newy+22;head->centery=head->newy;control=3;if(head->newy>377)gameover();}chy=ch;break;case ESC:flag=1;break;}/* if 判断是否吃蛇*/if(flag!=1){ if(head->newx==scenterx&&head->newy==scentery) { p3=head;while(p3!=NULL){ p4=p3;}p3=(struct snake *)malloc(sizeof(struct snake));p4->next=p3;p3->centerx=a;p3->newx=a;p3->centery=b;p3->newy=b;p3->next=NULL;a=500;b=500;putimage(409,62,far4,COPY_PUT); /********** 分数框挡住**************/putimage(500,62,far4,COPY_PUT); /*********把以前的剩下虫子的框挡住********/score=(++n)*100;left--;itoa(score,sscore,10);itoa(left,sleft,10);setcolor(RED);outtextxy(415,70,sscore);outtextxy(512,70,sleft);nextshow=1;if(left==0) /************判断是否过关**********/donghua(); /*******如果过关,播放过关动画*********************/}p5=head; /*********************判断是否自杀***************************/p5=p5->next;p5=p5->next;p5=p5->next; /****从第五个节点判断是否自杀************/ while(p5!=NULL){if(head->newx==p5->centerx&&head->newy==p5->cent ery){ gameover();break;}elsep5=p5->next;}}}/************snakechange()函数结束*******************//*****************************主函数******************************************/int main(){ int i;init(); /**********初始化图形系统**********/welcome(); /*********8欢迎界面**************/bort(); /*********主场景***************/snakede(); /**********连表初始化**********/while(1){ snakechange();if(flag==1)break;snakepaint();ran();for(i=0;i<=grade;i++) delay(3000);}free(far1);free(far2);free(far3);free(far4); closegraph();return 0;}。
vc贪吃蛇c语言代码

#include "stdio.h"#include "stdio.h"#include "windows.h"#include "time.h"#include "setjmp.h"#define MAXNOD 500#define UP 1#define DOWN -1#define LEFT -2#define RIGHT 2#define YES 1#define NO 0jmp_buf retry;typedef struct{int x;int y;int status;}Food;typedef struct {int *px;int *py;int direction;int nodlen;int score;}Snack;int gotoxy(int x, int y){COORD cd;cd.X = x;cd.Y = y;return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cd); }void initialization(Snack *pss){system("color 0e");pss->px=(int *)malloc(MAXNOD*sizeof(int));pss->py=(int *)malloc(MAXNOD*sizeof(int));memset(pss->px,0,MAXNOD);memset(pss->py,0,MAXNOD);pss->px[0]=0;pss->py[0]=0;pss->px[1]=1;pss->py[1]=0;pss->direction=RIGHT;pss->nodlen=2;pss->score=0;}void getscoresys(Snack scr){gotoxy(68,7);cprintf("score: %d",scr.score);}int ctrltoi(char ctr){switch(ctr){case 'w':return UP;case 's':return DOWN;case 'a':return LEFT;case 'd':return RIGHT ;}}void boundary(){int cnt,y;gotoxy(15,3);for (cnt=0;cnt<45;cnt++){cprintf("%c",4);}for (y=4;y<20;y++){gotoxy(15,y);cprintf("%c",219);gotoxy(59,y);cprintf("%c",219);}gotoxy(15,20);for (cnt=0;cnt<45;cnt++){cprintf("%c",4);}gotoxy(68,9);cprintf("UP: W");gotoxy(68,10);cprintf("DOWN: S");gotoxy(68,11);cprintf("LEFT: A");gotoxy(68,12);cprintf("RIGHT: D");gotoxy(68,14);cprintf("PAUSE: BLANK");gotoxy(68,15);cprintf("EXIT :ESC");}int isdead(Snack ts){int i;char select;for(i=0;i<ts.nodlen-1;i++)if((ts.px[i]==ts.px[ts.nodlen-1]&&ts.py[i]==ts.py[ts.nodlen-1])||((ts.px[ts.nodlen-1]<0))||(ts.px[ts.nodl en-1]>42)||(ts.py[ts.nodlen-1]<0)||(ts.py[ts.nodlen-1]>15)){system("cls");gotoxy(15,12);cprintf("Game Over! Press ESC to exit, any other key to retry\a\n");flushall();select=getch();if(select==27) exit(0);system("cls");longjmp(retry,1);}}void getfood(Snack s,Food *pf){int i;cnt: do{pf->x=rand()%43;pf->y=rand()%16;for (i=0;i<s.nodlen;i++){if (s.px[i]==pf->x&&s.py[i]==pf->y){goto cnt;}}break;}while(1);pf->status=YES;}int main(){Snack ss;Food foo;int i,j,dire;char ctrl;srand((unsigned)time(NULL));setjmp(retry);initialization(&ss);getfood(ss,&foo);do{gotoxy(foo.x+16,foo.y+4);if (foo.status==NO)getfood(ss,&foo);cprintf("%c",3);boundary();if(_kbhit()){ctrl=getch();if (ctrl=='w'||ctrl=='s'||ctrl=='a'||ctrl=='d'){dire=ctrltoi(ctrl);if(ss.direction==(0-dire)) ;elsess.direction=dire;}else if (ctrl==' ')system("pause");else if (ctrl==27)exit(0);}for (i=0;i<ss.nodlen-1;i++){ss.px[i]=ss.px[i+1];ss.py[i]=ss.py[i+1];}switch(ss.direction){case UP:ss.py[ss.nodlen-1]=ss.py[ss.nodlen-1]-1;break;case DOWN:ss.py[ss.nodlen-1]=ss.py[ss.nodlen-1]+1;break;case LEFT:ss.px[ss.nodlen-1]=ss.px[ss.nodlen-1]-1;break;case RIGHT:ss.px[ss.nodlen-1]=ss.px[ss.nodlen-1]+1;}for(i=0;i<ss.nodlen;i++){gotoxy(ss.px[i]+16,ss.py[i]+4);cprintf("%c",4);}isdead(ss);if (ss.px[ss.nodlen-1]==foo.x&&ss.py[ss.nodlen-1]==foo.y) {for (j=0;j<ss.nodlen;j++){ss.px[ss.nodlen-j]=ss.px[ss.nodlen-j-1];ss.py[ss.nodlen-j]=ss.py[ss.nodlen-j-1];}ss.score +=10;ss.nodlen++;foo.status=NO;}getscoresys(ss);_sleep(199);system("cls");flushall();}while(1);}。
C语言贪吃蛇代码适合VC6.0

#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>typedef struct snake{int a;int b;struct snake *u;struct snake *n;}snake,*snake1;typedef struct food{int a;int b;}food;void main(){char c,c0 = 'd';int i,j,k,n=1,t,at;snake p,q;snake *dd,*dd0,*dd1,*dd2; food f;srand(time(NULL));p.u = NULL;p.n = &q;p.a = 5;p.b = 6;q.a = 5;q.b = 5; q.u = &p;q.n = NULL;dd=dd2= &q;f.a=(rand()%15+1);f.b=(rand()%15+1);while(1){srand(time(NULL));system("cls");for(i = 0;i < 17;i ++){for(j = 0; j < 17;j++){if(i == 0 )printf("▁");else if(i == 16)printf("▔");else if(j == 0)printf("▕");else if(j == 16)printf("▏");else if(i == p.a && j == p.b) printf("■");else if(i == f.a && j == f.b) printf("★");else{t = 0;dd = dd2;for(k = 0; k < n ;k++){if(i == dd->a && j == dd->b) {printf("□");t = 1;break;}dd = dd->u;}if(t == 0)printf(" ");}}printf("\n");}at = 0;dd =dd2;for(i=0;i<n;i++){if(p.a == dd->a && p.b == dd->b) {printf("game over!!\n");exit(0);}dd = dd->u;}if(p.a == f.a && p.b == f.b){dd = dd2;at =1;f.a = (rand()%15+1);f.b = (rand()%15+1);for(i=0;i<n;i++){if(f.a == dd->a && f.b == dd->b){f.a = dd2->a;f.b = dd2->b;break;}}n++;}if(kbhit()){c = getch();dd = dd2;if(c == 'w' && c0 != 's'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.a == 1)p.a = 15;elsep.a = (p.a-1)%15;}else if(c == 's' && c0 != 'w'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.a = (p.a%15)+1;}else if(c == 'a' && c0 != 'd'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.b == 1)p.b = 15;elsep.b = (p.b-1)%15;}else if(c == 'd' && c0 != 'a'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.b = (p.b%15)+1;}else{goto qq;}c0 = c;}else{qq: if(c0 == 'w'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.a == 1)p.a = 15;elsep.a=(p.a-1)%15;}else if(c0 == 's'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.a=(p.a%15)+1;}else if(c0 == 'a'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b; dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.b == 1)p.b = 15;elsep.b=(p.b-1)%15;}else if(c0 == 'd'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake)); dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2; dd2=dd0;}dd = dd2;for(i = 0; i<n ; i++){dd1 = dd->u;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.b=(p.b%15)+1;}}fflush(stdin);dd = &q;_sleep(200);}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*这是一个贪吃蛇代码,运行环境VC++6.0(亲测完美运行)*//*该程序在dos系统下运行,不需要graphics.h头文件*//*该程序由C语言小方贡献,谢谢您的支持*/#include <windows.h>#include <stdlib.h>#include <time.h>#include <stdio.h>#include <string.h>#include <conio.h>#define N 21int apple[3];char score[3];char tail[3];void gotoxy(int x, int y) //输出坐标{COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }void color(int b) //颜色函数{HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;SetConsoleTextAttribute(hConsole,b) ;}int Block(char head[2]) //判断出界{if ((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))return 1;return 0;}int Eat(char snake[2]) //吃了苹果{if ((snake[0] == apple[0]) && (snake[1] == apple[1])){apple[0] = apple[1] = apple[2] = 0;gotoxy(N+44,10);color(13);printf("%d",score[0]*10);color(11);return 1;}return 0;}void Draw(char **snake, int len) //蛇移动{if (apple[2]) {gotoxy(apple[1] * 2, apple[0]);color(12);printf("●");color(11);}gotoxy(tail[1] * 2, tail[0]);if (tail[2]){ color(14);printf("★");color(11);}elseprintf("■");gotoxy(snake[0][1] * 2, snake[0][0]);color(14);printf("★");color(11);putchar('\n');}char** Move(char **snake, char dirx, int *len) //控制方向{int i, full = Eat(snake[0]);memcpy(tail, snake[(*len)-1], 2);for (i = (*len) - 1; i > 0; --i)memcpy(snake[i], snake[i-1], 2);switch (dirx){case 'w': case 'W': --snake[0][0]; break;case 's': case 'S': ++snake[0][0]; break;case 'a': case 'A': --snake[0][1]; break;case 'd': case 'D': ++snake[0][1]; break;default: ;}if (full){snake = (char **)realloc(snake, sizeof(char *) * ((*len) + 1));snake[(*len)] = (char *)malloc(sizeof(char) * 2);memcpy(snake[(*len)], tail, 2);++(*len);++score[0];if(score[3] < 16)++score[3];tail[2] = 1;}elsetail[2] = 0;return snake;}void init(char plate[N+2][N+2], char ***snake_x, int *len) //初始化{int i, j;char **snake = NULL;*len = 3;score[0] = score[3] =3;snake = (char **)realloc(snake, sizeof(char *) * (*len));for (i = 0; i < *len; ++i)snake[i] = (char *)malloc(sizeof(char) * 2);for (i = 0; i < 3; ++i){snake[i][0] = N/2 + 1;snake[i][1] = N/2 + 1 + i;}for (i = 1; i <= N; ++i)for (j = 1; j <= N; ++j)plate[i][j] = 1;apple[0] = rand()%N + 1; apple[1] = rand()%N + 1;apple[2] = 1;for (i = 0; i < N + 2; ++i){gotoxy(0, i);for (j = 0; j < N + 2; ++j){switch (plate[i][j]){case 0:color(12);printf("□");color(11); continue;case 1: printf("■"); continue;default: ;}}putchar('\n');}for (i = 0; i < (*len); ++i){gotoxy(snake[i][1] * 2, snake[i][0]);printf("★");}putchar('\n');*snake_x = snake;}void Manual(){gotoxy(N+30,2);color(10);printf("按W S A D 移动方向");gotoxy(N+30,4);printf("按space 键暂停");gotoxy(N+30,8);color(11);printf("历史最高分为: ");color(12);gotoxy(N+44,8);printf("%d",score[1]*10);color(11);gotoxy(N+30,12);printf("你现在得分为: 0");}int File_in() //取记录的分数{FILE *fp;if((fp = fopen("C:\\tcs.txt","a+")) == NULL){gotoxy(N+18, N+2);printf("文件不能打开\n");exit(0);}if((score[1] = fgetc(fp)) != EOF);elsescore[1] = 0;return 0;}int File_out() //存数据{FILE *fp;if(score[1] > score[0]){gotoxy(10,10);color(12);puts("闯关失败加油耶");gotoxy(0,N+2);return 0;}if((fp = fopen("C:\\tcs.txt","w+")) == NULL){printf("文件不能打开\n");exit(0);}if(fputc(--score[0],fp)==EOF)printf("输出失败\n");gotoxy(10,10);color(12);puts("恭喜您打破记录");gotoxy(0,N+2);return 0;}void Free(char **snake, int len) //释放空间{int i;for (i = 0; i < len; ++i)free(snake[i]);free(snake);}int main(void){int len;char ch = 'g';char a[N+2][N+2] = {{0}};char **snake;srand((unsigned)time(NULL));color(11);File_in();init(a, &snake, &len);Manual();while (ch != 0x1B) // 按ESC 结束{Draw(snake, len);if (!apple[2]) {apple[0] = rand()%N + 1;apple[1] = rand()%N + 1;apple[2] = 1;}Sleep(200-score[3]*10);setbuf(stdin, NULL);if (kbhit()){gotoxy(0, N+2);ch = getche();}snake = Move(snake, ch, &len);if (Block(snake[0])==1){gotoxy(N+2, N+2);puts("你输了");File_out();Free(snake, len);getche();exit(0);}}Free(snake, len);exit(0);}。