贪吃蛇源代码(VC++6.0实现)
贪吃蛇源代码
#include <>#include <>#include <>#include <>#define LEFT 'a'#define RIGHT 'd'#define DOWN 's'#define UP 'w'#define ESC 27#define N 200 /*蛇的最大长度*/int i;char key;int score=0; /*得分*/int gamespeed=100; /*游戏速度自己调整*/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=9,gm=2;initgraph(&gd,&gm," ");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/void DrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(LIGHTCYAN);setlinestyle(PS_SOLID,0,1); /*设置线型*/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){srand(time(NULL)); /*随机数发生器*/=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,;}for(i=;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(); /*本次游戏结束*/=1; /*蛇死*/}if==1) /*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/break;if[0]==&&[0]==/*吃到食物以后*/{setcolor(BLACK); /*把画面上的食物东西去掉*/rectangle,,+10,;[]=-20;[]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/++; /*蛇的身体长一节*/=1; /*画面上需要出现新的食物*/score+=10;PrScore(); /*输出新得分*/}setcolor(RED); /*画出蛇*/for(i=0;i<;i++)rectangle[i],[i],[i]+10,[i]-10);Sleep(gamespeed);setcolor(BLACK); /*用黑色去除蛇的的最后一节*/rectangle[],[],[]+10,[]-10);} /*endwhile(!kbhit)*/if==1) /*如果蛇死就跳出循环*/break;key=getch(); /*接收按键*/if (key == ESC) break; /*按ESC键退出*/switch(key){case UP:if!=4) /*判断是否往相反的方向移动*/=3;break;case RIGHT:if!=2)=1;break;case LEFT:if!=1)=2;break;case DOWN:if!=3)=4;break;}}/*endwhile(1)*/}/*游戏结束*/void GameOver(void){cleardevice();PrScore();setcolor(RED);setfont(56,0,"黑体");outtextxy(200,200,"GAME OVER"); getch();}/*输出成绩*/void PrScore(void){char str[10];setfillstyle(YELLOW);bar(50,15,220,35);setcolor(BROWN);setfont(16,0,"宋体");sprintf(str,"score:%d",score); outtextxy(55,16,str);}/*图形结束*/void Close(void){closegraph();}。
贪吃蛇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)
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);}。
(完整word版)C语言最简洁的贪吃蛇源代码
C语言最简洁的贪吃蛇源代码.txt每天早上起床都要看一遍“福布斯”富翁排行榜,如果上面没有我的名字,我就去上班。
谈钱不伤感情,谈感情最他妈伤钱。
我诅咒你一辈子买方便面没有调料包。
#include〈graphics.h>#include<conio。
h〉#include〈dos.h〉#include<bios。
h>#include<stdlib。
h〉#define STATIC 0#define TRUE 1#define FALSE 0#define UP 1#define RIGHT 2#define DOWN 3#define LEFT 4#define VK_LEFT 0x4b00 /*上下左右键的值*/#define VK_RIGHT 0x4d00#define VK_DOWN 0x5000#define VK_UP 0x4800#define VK_ESC 0x011bint board[22][22];int snakelength=0;struct snake{public:int x=0;int y=0;int direction;}body[20];snake food;void makefood();/*产生一个食物*/int eatfood(); /*蛇吃掉食物*/void right(); /*上下左右的函数了*/void down();void left();void up();void getdirection(); /*判断蛇的方向*/move(snake *body)/*让蛇动起来*/{int x=body[0].x,y=body[0].y;if(body—>direction==RIGHT&&board[y][x+1]!=1)right();else if(body—>direction==DOWN&&board[y+1][x]!=1)down(); else if(body->direction==LEFT&&board[y][x—1]!=1)left(); else if(body—>direction==UP&&board[y-1][x]!=1)up();return 0;}void print() /*在屏幕上显示蛇*/{int i,j,x=0,y=0;for(i=1;i〈21;i++)for(j=1;j<21;j++)board[i][j]=0;for(i=0;i〈20;i++){x=body[i]。
彩版_贪吃蛇C语言版(VC++6.0中运行完美通过)
// (彩版——贪吃蛇代码)在vc++6.0 中运行通过~~// 本屌花了整整3天才弄透,然后自己小小改编了的贪吃蛇代码,// 特色在于可以自己输入初始蛇身长度,以及食物总数,// 二是可以加速、减速。
// 第一次挂代码,望对大家有微末帮助!!~//以下为运行效果图://好了,现在上源文件,,哈哈# include <stdio.h># include <stdlib.h># include <malloc.h># include <windows.h># include <conio.h># include <time.h># define ESC 27# define SPACE 32# define MAX_X 25# define MAX_Y 50int max_food;// 食物总数int length = 0;// 蛇身长度char sna_dir = 'd';// 蛇头方向char temp;int snaDir_x, snaDir_y;// 蛇头方向坐标clock_t now_time;// 取系统时钟double wait_time = 300;// 限制蛇的速度typedef struct Node// 结构体{int x;int y;struct Node *pNext;}NODE, *PNODE;typedef struct Queue// queue 队列{PNODE front;PNODE rear;}QUEUE, *PQUEUE;PQUEUE pSnake = (PQUEUE)malloc(sizeof(QUEUE));// 全局HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄void hideCursor()// 隐藏光标{CONSOLE_CURSOR_INFO cursor_info = {1, 0};SetConsoleCursorInfo(hConsole, &cursor_info);}void Setcolor(int color)// 实现彩色的函数{SetConsoleTextAttribute(hConsole, color);}void gotoxy(int x, int y)// 确定坐标{COORD loc;loc.X = y;loc.Y = x;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HAND LE), loc);}void destroy()// 销毁{PNODE p = pSnake->front;PNODE t = pSnake->front;while (NULL != p)//最后使pSnake->front 和p 都为NULL{p = p->pNext;free(t);t = p;}}void game_over()// 游戏结束{gotoxy(11, 20);destroy();printf("Game Over");system("pause>nul");exit(0);}void enqueue(int x, int y)// 进队{PNODE pNew = (PNODE)malloc(sizeof(NODE));pNew->x = x;pNew->y = y;pSnake->rear->pNext = pNew;pSnake->rear = pNew;pSnake->rear->pNext = NULL;length++;}void dequeue()// 出队{PNODE p = pSnake->front;pSnake->front = pSnake->front->pNext;pSnake->front->x = p->x;pSnake->front->y = p->y;free(p);length--;}void pri_sna_info()// 打印信息{int i;Setcolor(13);gotoxy(4, 58);printf("Length: %d",length);gotoxy(6, 58);printf("还差%d 就通关了~~!", max_food+1);for (i=51; i<80; i++){gotoxy(10, i);printf("-");}Setcolor(13);gotoxy(15, 58);printf("按1 加速");gotoxy(16, 58);printf("按2 减速");gotoxy(19, 58);printf("空格键暂停");gotoxy(20, 58);printf("ESC 退出");Setcolor(15);}void draw_wall()// 画墙{int i;for (i=1; i<MAX_Y; i++){gotoxy(0, i);printf("=");gotoxy(MAX_X, i);printf("=");}for (i=0; i<MAX_X+1; i++){gotoxy(i, 0);printf("|");gotoxy(i, MAX_Y);printf("|");}Setcolor(15);}void rand_food()// 随机产生食物并显示{PNODE p;while(1){pSnake->front->x = rand()%(MAX_X-1) + 1;pSnake->front->y = rand()%(MAX_Y-1) + 1;p = pSnake->front->pNext;while(NULL != p){if(pSnake->front->x==p->x && pSnake->front->y==p->y) break;elsep = p->pNext;}if(NULL == p)break;}max_food--;Setcolor(14);gotoxy(pSnake->front->x, pSnake->front->y);printf("@");fflush(stdout);Setcolor(15);}void judge()// 判断赢或继续或挂{PNODE p;p = pSnake->front->pNext;if (max_food+1 == 0){system("cls");Setcolor(13);gotoxy(11, 25);printf("==== 哈哈,你赢啦!!!====");system("pause>nul");exit(0);}while (pSnake->rear != p){if (pSnake->rear->x==p->x && pSnake->rear->y==p->y){game_over();}elsep = p->pNext;}if (p->x==pSnake->front->x && p->y==pSnake->front->y) {rand_food();pri_sna_info();}elsedequeue();}void clear_tail()// 清除蛇尾{PNODE p = pSnake->front->pNext;gotoxy(p->x, p->y);printf(" ");}void print_sna()// 打印蛇{PNODE p = pSnake->front->pNext;int color;Setcolor(0xe);gotoxy(p->x, p->y);printf("z");while (pSnake->rear != p){color = rand()%15 + 1;if(color == 14)color -= rand()%13 + 1;Setcolor(color);p = p->pNext;gotoxy(p->x, p->y);printf("r");}Setcolor(0xf);gotoxy(p->x, p->y);printf("o");Setcolor(15);}void head(char c)// 蛇头方向{int x;char ch = 0;PNODE p = pSnake->front->pNext;while (pSnake->rear != p)p = p->pNext;if (c == 32){x = 1;gotoxy(11, 20);printf("--Pause--");while (ch != 32){ch = getch();}c = temp;gotoxy(11, 20);printf(" ");}if (x == 1)c = temp;if (temp=='a'&&c=='d' || temp=='d'&&c=='a' || temp=='s'&&c=='w' || temp=='w'&&c=='s')// 避免按与当前方向相反的键而挂掉,也就是增强游戏健壮性了c = temp;if (c!='a' && c!='w' && c!='d' && c!='s' && c!=27)c = temp;switch (c){case 'a':sna_dir = 'a';snaDir_x = p->x;snaDir_y = p->y - 1;if (snaDir_y < 1)game_over();break;case 's':sna_dir = 's';snaDir_x = p->x + 1;snaDir_y = p->y;if (snaDir_x == MAX_X)game_over();break;case 'w':sna_dir = 'w';snaDir_x = p->x - 1;snaDir_y = p->y;if (snaDir_x < 1)game_over();break;case 'd':sna_dir = 'd';snaDir_x = p->x;snaDir_y = p->y + 1;if (snaDir_y == MAX_Y)game_over();break;default:break;}enqueue(snaDir_x, snaDir_y); if (c == 27){system("cls");gotoxy(11, 35);printf("退出成功");system("pause>nul");exit(0);}}void move_sna()// 蛇的移动,,{if (kbhit())//这个函数去百度下就知道了——判断键盘是否有输入{temp = sna_dir;sna_dir = getch();if (sna_dir == '1'){sna_dir = temp;if (wait_time >= 50)wait_time -= 50;}if (sna_dir == '2'){sna_dir = temp;if (wait_time <= 800)wait_time += 50;}head(sna_dir);}else{head(sna_dir);}judge();print_sna();}void con_sna()// 控制蛇{if (clock()-now_time >= wait_time) // 通过wait_time控制蛇的速度{clear_tail();move_sna();now_time = clock();}}void init() // 这个是初始化,大家都知道的{int i;int len;system("title orrrrrrrrrrrrz——膜拜");system("mode con: cols=80 lines=26");PNODE (pSnake->front) = (PNODE)malloc(sizeof(NODE));pSnake->rear = pSnake->front;Setcolor(13);gotoxy(11, 20);printf("You Can Set The Length Of Your Snake: ");scanf("%d", &len);system("cls");gotoxy(11, 10);printf("You Can Set The Number Of Customs Clearance Of Foods: ");scanf("%d", &max_food);system("cls");Setcolor(15);hideCursor();// 隐藏光标for (i=0; i<len; i++){enqueue(11, i+20);}draw_wall();rand_food();pri_sna_info();}int main(void){init();srand((unsigned)time(NULL));// 随机函数,为rand()函数服务的,一样的,去百度这个函数~~now_time = clock();while (1){con_sna();}return 0;}。
贪吃蛇C语言源代码
#include <stdio.h>#include <stdlib.h>#include <Windows.h>//windows编程头文件#include <time.h>#include <conio.h>//控制台输入输出头文件#ifndef __cplusplustypedef char bool;#define false 0#define true 1#endif//将光标移动到控制台的(x,y)坐标点处void gotoxy(int x, int y){COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }#define SNAKESIZE 100//蛇的身体最大节数#define MAPWIDTH 78//宽度#define MAPHEIGHT 24//高度//食物的坐标struct {int x;int y;}food;//蛇的相关属性struct {int speed;//蛇移动的速度int len;//蛇的长度int x[SNAKESIZE];//组成蛇身的每一个小方块中x的坐标int y[SNAKESIZE];//组成蛇身的每一个小方块中y的坐标}snake;//绘制游戏边框void drawMap();//随机生成食物void createFood();//按键操作void keyDown();//蛇的状态bool snakeStatus();//从控制台移动光标void gotoxy(int x, int y);int key = 72;//表示蛇移动的方向,72为按下“↑”所代表的数字//用来判断蛇是否吃掉了食物,这一步很重要,涉及到是否会有蛇身移动的效果以及蛇身增长的效果int changeFlag = 0;int sorce = 0;//记录玩家的得分int i;void drawMap(){//打印上下边框for (i = 0; i <= MAPWIDTH; i += 2)//i+=2是因为横向占用的是两个位置{//将光标移动依次到(i,0)处打印上边框gotoxy(i, 0);printf("■");//将光标移动依次到(i,MAPHEIGHT)处打印下边框gotoxy(i, MAPHEIGHT);printf("■");}//打印左右边框for (i = 1; i < MAPHEIGHT; i++){//将光标移动依次到(0,i)处打印左边框gotoxy(0, i);printf("■");//将光标移动依次到(MAPWIDTH, i)处打印左边框gotoxy(MAPWIDTH, i);printf("■");}//随机生成初试食物while (1){srand((unsigned int)time(NULL));food.x = rand() % (MAPWIDTH - 4) + 2;food.y = rand() % (MAPHEIGHT - 2) + 1;//生成的食物横坐标的奇偶必须和初试时蛇头所在坐标的奇偶一致,因为一个字符占两个字节位置,若不一致//会导致吃食物的时候只吃到一半if (food.x % 2 == 0)break;}//将光标移到食物的坐标处打印食物gotoxy(food.x, food.y);printf("*");//初始化蛇的属性snake.len = 3;snake.speed = 200;//在屏幕中间生成蛇头snake.x[0] = MAPWIDTH / 2 + 1;//x坐标为偶数snake.y[0] = MAPHEIGHT / 2;//打印蛇头gotoxy(snake.x[0], snake.y[0]);printf("■");//生成初试的蛇身for (i = 1; i < snake.len; i++){//蛇身的打印,纵坐标不变,横坐标为上一节蛇身的坐标值+2snake.x[i] = snake.x[i - 1] + 2;snake.y[i] = snake.y[i - 1];gotoxy(snake.x[i], snake.y[i]);printf("■");}//打印完蛇身后将光标移到屏幕最上方,避免光标在蛇身处一直闪烁gotoxy(MAPWIDTH - 2, 0);return;}void keyDown(){int pre_key = key;//记录前一个按键的方向if (_kbhit())//如果用户按下了键盘中的某个键{fflush(stdin);//清空缓冲区的字符//getch()读取方向键的时候,会返回两次,第一次调用返回0或者224,第二次调用返回的才是实际值key = _getch();//第一次调用返回的不是实际值key = _getch();//第二次调用返回实际值}/**蛇移动时候先擦去蛇尾的一节*changeFlag为0表明此时没有吃到食物,因此每走一步就要擦除掉蛇尾,以此营造一个移动的效果*为1表明吃到了食物,就不需要擦除蛇尾,以此营造一个蛇身增长的效果*/if (changeFlag == 0){gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);printf(" ");//在蛇尾处输出空格即擦去蛇尾}//将蛇的每一节依次向前移动一节(蛇头除外)for (i = snake.len - 1; i > 0; i--){snake.x[i] = snake.x[i - 1];snake.y[i] = snake.y[i - 1];}//蛇当前移动的方向不能和前一次的方向相反,比如蛇往左走的时候不能直接按右键往右走//如果当前移动方向和前一次方向相反的话,把当前移动的方向改为前一次的方向if (pre_key == 72 && key == 80)key = 72;if (pre_key == 80 && key == 72)key = 80;if (pre_key == 75 && key == 77)key = 75;if (pre_key == 77 && key == 75)key = 77;/***控制台按键所代表的数字*“↑”:72*“↓”:80*“←”:75*“→”:77*///判断蛇头应该往哪个方向移动switch (key){case 75:snake.x[0] -= 2;//往左break;case 77:snake.x[0] += 2;//往右break;case 72:snake.y[0]--;//往上break;case 80:snake.y[0]++;//往下break;}//打印出蛇头gotoxy(snake.x[0], snake.y[0]);printf("■");gotoxy(MAPWIDTH - 2, 0);//由于目前没有吃到食物,changFlag值为0changeFlag = 0;return;}void createFood(){if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇头碰到食物{//蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物while (1){int flag = 1;srand((unsigned int)time(NULL));food.x = rand() % (MAPWIDTH - 4) + 2;food.y = rand() % (MAPHEIGHT - 2) + 1;//随机生成的食物不能在蛇的身体上for (i = 0; i < snake.len; i++){if (snake.x[i] == food.x && snake.y[i] == food.y){flag = 0;break;}}//随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成if (flag && food.x % 2 == 0)break;}//绘制食物gotoxy(food.x, food.y);printf("*");snake.len++;//吃到食物,蛇身长度加1sorce += 10;//每个食物得10分snake.speed -= 5;//随着吃的食物越来越多,速度会越来越快changeFlag = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果}return;}bool snakeStatus(){//蛇头碰到上下边界,游戏结束if (snake.y[0] == 0 || snake.y[0] == MAPHEIGHT)return false;//蛇头碰到左右边界,游戏结束if (snake.x[0] == 0 || snake.x[0] == MAPWIDTH)return false;//蛇头碰到蛇身,游戏结束for (i = 1; i < snake.len; i++){if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])return false;}return true;}int main(){drawMap();while (1){keyDown();if (!snakeStatus())break;createFood();Sleep(snake.speed);}gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2);printf("Game Over!\n");gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2 + 1);printf("本次游戏得分为:%d\n", sorce);Sleep(5000);return 0;}。
贪吃蛇源代码(需要easy x,vc6.0可以成功编译运行)
#include <graphics.h>#include <stdlib.h>#include <time.h>//#include <dos.h>#include <conio.h>#include <stdio.h>//#include <winnt.h>#define MAX_JOINTS 200#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b#define MV_RIGHT 1#define MV_LEFT 2#define MV_UP 3#define MV_DOWN 4void InitGraph(void); /*图形驱动初始化函数*/void DrawFence(void); /*绘制游戏场景*/void GameOver(int score); /*结束游戏*/void GamePlay(void); /*玩游戏具体过程*/void PrScore(int score); /*输出成绩*/struct Food /*食物的结构体定义*/{int x; /*食物的横坐标*/int y; /*食物的纵坐标*/int addFood; /*判断是否要出现食物的变量*/};struct Snake /*蛇的结构体定义*/{int x[MAX_JOINTS]; /*保存蛇身每一节位于屏幕上的列坐标*/ int y[MAX_JOINTS]; /*保存蛇身每一节位于屏幕上的行坐标*/ int joint; /*蛇的节数*/int direction; /*蛇移动方向*/int life; /*蛇的生命,0活着,1死亡*/};/*主函数*/void main(void){InitGraph(); /*图形驱动*/DrawFence(); /*游戏场景*/GamePlay(); /*玩游戏具体过程*/closegraph(); /*图形结束*/}/*图形驱动初始化函数*/void InitGraph(void){int gd = DETECT, gm;initgraph(&gd, &gm, "");cleardevice();setbkcolor(BLUE);cleardevice();setcolor(WHITE);//settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);setfont(32, 0, "宋体");outtextxy(170, 150, "Greedy Snake");outtextxy(219, 254, "Ready?");setcolor(BLUE);cleardevice();}/*游戏开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/ void DrawFence(void){int i;setbkcolor(LIGHTGREEN);setcolor(11);//setlinestyle(SOLID_LINE, 0, THICK_WIDTH);setlinestyle(PS_SOLID, 0, 3);/*画围墙*/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){int i, key;//int gamespeed = 22000; /*控制游戏速度*/int gamespeed = 200;int score = 0; /*记录游戏得分*/struct Food food; /*食物结构体变量*/struct Snake snake; /*蛇结构体变量*/food.addFood = 1; /*1表示需要出现新食物,0表示已经存在食物*/ snake.life = 0; /*置蛇的生命状态为活着*/snake.direction = MV_RIGHT; /*置蛇头方向往右*/snake.x[0] = 100; snake.y[0] = 100; /*置蛇头初始位置*/snake.x[1] = 110; snake.y[1] = 100;snake.joint = 2; /*置蛇的初始节数为2*/PrScore(score); /*显示游戏得分*//*重复玩游戏,直到按Esc键结束*/srand(time(NULL));while (1){while (!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if (food.addFood == 1) /*需要出现新食物*/{food.x = rand() % 400 + 60;food.y = rand() % 350 + 60;/*食物出现后必须在整格内才能让蛇吃到*/while (food.x%10 != 0){food.x++;}while (food.y%10 != 0){food.y++;}food.addFood = 0; /*画面上有食物*/}if (food.addFood == 0) /*画面上有食物,则显示*/{setcolor(GREEN);rectangle(food.x, food.y, food.x+10, food.y-10);}for (i=snake.joint-1; i>0; i--) /*蛇的每个节往前移动*/{snake.x[i] = snake.x[i-1];snake.y[i] = snake.y[i-1];}/*1,2,3,4 表示右,左,上,下四个方向,来决定蛇头的移动*/switch(snake.direction){case MV_RIGHT: snake.x[0] += 10; break;case MV_LEFT: snake.x[0] -= 10; break;case MV_UP: snake.y[0] -= 10; break;case MV_DOWN: snake.y[0] += 10; break;}/*从蛇的第四节开始判断是否撞到自己,因为蛇头为两节,第三节不可能拐过来*/for (i=3; i<snake.joint; i++){if (snake.x[i]==snake.x[0] && snake.y[i]==snake.y[0]){GameOver(score); /*显示失败*/snake.life = 1; /*蛇死*/break;}}/*判断蛇是否撞到墙壁*/if (snake.x[0]<55 || snake.x[0]>595|| snake.y[0]<55 || snake.y[0]>455){GameOver(score); /*本次游戏结束*/snake.life = 1; /*蛇死*/}/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/if (snake.life == 1) break;if (snake.x[0]==food.x && snake.y[0]==food.y) /*吃到食物后*/{/*把画面上的食物清除*/setcolor(0);rectangle(food.x, food.y, food.x+10, food.y-10);/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/snake.x[snake.joint] =-20; snake.y[snake.joint] =-20;snake.joint++; /*蛇的身体长一节*/food.addFood = 1; /*画面上需要出现新食物*/score += 10;PrScore(score); /*输出新得分*/}/*画蛇*/setcolor(4);for (i=0; i<snake.joint; i++){rectangle(snake.x[i], snake.y[i],snake.x[i]+10, snake.y[i]-10);}//delay(gamespeed); /*延时控制蛇的速度*/Sleep(gamespeed);/*去除蛇的最后一节*/setcolor(BLUE);rectangle(snake.x[snake.joint-1], snake.y[snake.joint-1],snake.x[snake.joint-1]+10,snake.y[snake.joint-1]-10);} /*end of while(!kbhit)*/if (snake.life == 1) break; /*如果蛇死,则跳出循环*///key = bioskey(0); /*接收按键*/key = getch();/*if (kbhit()){key = getch();}*//*判断按键,是否往相反方向移动,按Esc键则退出*/ /* if (key == ESC) break;else if (key==UP && snake.direction!=4)snake.direction = MV_UP;else if (key==RIGHT && snake.direction!= MV_LEFT)snake.direction = MV_RIGHT;else if (key==LEFT && snake.direction!= MV_RIGHT)snake.direction = MV_LEFT;else if (key==DOWN && snake.direction!= MV_UP)snake.direction = MV_DOWN;*/if (key == ESC) break;else if (key=='W' && snake.direction!=4)snake.direction = MV_UP;else if (key=='D' && snake.direction!= MV_LEFT)snake.direction = MV_RIGHT;else if (key=='A' && snake.direction!= MV_RIGHT)snake.direction = MV_LEFT;else if (key=='S' && snake.direction!= MV_UP)snake.direction = MV_DOWN;} /*end of while(1)*/ }/*结束游戏*/void GameOver(int score){cleardevice();PrScore(score);setcolor(RED);//settextstyle(0, 0, 4);setfont(60, 0,"黑体");outtextxy(200, 200, "GAME OVER");getch();}/*输出成绩*/void PrScore(int score){char str[10];setfillstyle(SOLID_FILL, YELLOW);bar(50, 15, 220, 35);setcolor(6);//settextstyle(0, 0, 2);setfont(16, 0,"宋体");sprintf(str, "score:%d", score);outtextxy(55, 20, str);}。
C语言实现贪吃蛇代码
C语⾔实现贪吃蛇代码本⽂实例为⼤家分享了C语⾔实现贪吃蛇代码的具体代码,供⼤家参考,具体内容如下#include"stdafx.h"#include<stdio.h>#include<time.h>#include<windows.h>#include<stdlib.h>#include<conio.h>#define U 1#define D 2#define L 3#define R 4 //蛇的状态 U:上 D:下 L:左 R:右typedef struct snake //蛇⾝的⼀个节点{int x; //节点的x坐标int y; //节点的y坐标struct snake *next; //蛇⾝的下⼀个节点}snake;int score=0,add=10; //总得分和每吃⼀次⾷物的得分int highscore=0; //最⾼分int status,sleeptime=200; //蛇前进状态,每次运⾏的时间间隔snake *head,*food; //蛇头指针,⾷物指针snake *q; //遍历蛇时⽤的指针int endgamestatus=0; //游戏结束时的状态HANDLE hOut; //控制台句柄void gotoxy(int x,int y);int color(int c);void printsnake();void wlcome();void createmap();void scoreandtips();void initsnake();void createfood();int biteself();void cantcrosswall();void speedup();void speeddown();void snakemove();void keyboardcontrol();void lostdraw();void endgame();void choose();void file_out();void file_in();void explation();main(){system("mode con cols=100 lines=30");printsnake();wlcome();file_out();keyboardcontrol();endgame();}void gotoxy(int x,int y)//设置光标位置{COORD c;c.X=x;c.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); //定位光标的位置}int color(int c)//设置颜⾊{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);return 0;}void printsnake()//打印字符蛇{color(3);printf("学号:1910101099"); gotoxy(35,1);color(6);printf("/^\\/^\\");gotoxy(34,2);printf("|_| o|");gotoxy(33,2);color(2);printf("_");gotoxy(25,3);color(12);printf("\\/");gotoxy(31,3);color(2);printf("/");gotoxy(37,3);color(6);printf("\\_/");gotoxy(41,3);color(10);printf(" \\");gotoxy(26,4);color(12);printf("\\____");gotoxy(32,4);printf("_________");gotoxy(31,4);color(2);printf("|");gotoxy(43,4);color(10);printf("\\");gotoxy(32,5);color(2);printf("\\_______");gotoxy(44,5);color(10);printf("\\");gotoxy(39,6);printf("| | \\");gotoxy(38,7);printf("/ / \\");gotoxy(37,8);printf("/ / \\ \\");gotoxy(35,9);printf("/ / \\ \\");gotoxy(34,10);printf(" / / \\ \\");gotoxy(33,11);printf("/ / _----_ \\ \\");gotoxy(32,12);gotoxy(32,14);printf("\\ ~-____-~ _-~ ~-_ ~-_-~ /"); gotoxy(33,15);printf("~-_ _-~ ~-_ _-~");gotoxy(35,16);printf("~--____-~ ~-___-~");}void wlcome()//欢迎界⾯{int n;int i,j=1;gotoxy(43,18);color(11);printf("贪吃蛇⼤作战");color(14);for(i=20;i<=26;i++){for(j=27;j<=74;j++){gotoxy(j,i);if(i==20||i==26){printf("-");}else if(j==27||j==74){printf("|");}}}color(12);gotoxy(35,22);printf("1.开始游戏");gotoxy(55,22);printf("2.游戏说明");gotoxy(35,24);printf("3.退出游戏");gotoxy(29,27);color(3);printf("请选择1 2 3\n");color(14);scanf("%d",&n);switch(n){case 1:system("cls");//清屏createmap();initsnake();createfood();keyboardcontrol();break;case 2:explation();break;break;case 3:exit(0);break;}}void createmap()//创建地图{int i,j;for(i=0;i<58;i+=2){gotoxy(i,0);color(5);printf("□");gotoxy(i,26);gotoxy(0,i);printf("□");gotoxy(56,i);printf("□");}for(i=2;i<56;i+=2){for(j=1;j<26;j++){gotoxy(i,j);color(3);printf("■\n\n");}}}void scoreandtips()//游戏界⾯右侧的得分和⼩提⽰ {file_out();gotoxy(64,4);color(11);printf("*最⾼纪录*: %d",highscore);gotoxy(64,8);color(14);printf("得分: %d ",score);color(13);gotoxy(73,11);printf("⼩提⽰");gotoxy(60,13);color(6);printf("+---------------------+");gotoxy(60,25);printf("+---------------------+");color(3);gotoxy(64,14);printf("每个⾷物得分:%d分",add);gotoxy(64,16);printf("不能穿墙,不能咬到⾃⼰");gotoxy(64,18);printf("⽤↑↓←→分别控制蛇的移动");gotoxy(64,20);printf("F1为加速,F2为减速");gotoxy(64,22);printf("space: 暂停游戏");gotoxy(64,24);printf("ESC:退出游戏");}void file_out()//打开⽂件记录最⾼分{FILE *fp;fp=fopen("save.txt","a+");fscanf(fp,"%d",&highscore);fclose(fp);}void initsnake(){snake *tail;int i;tail=(snake*)malloc(sizeof(snake));tail->x=24;tail->y=5;tail->next=NULL;for(i=1;i<=4;i++){head=(snake*)malloc(sizeof(snake));head->next=tail;head->x=24+2*i;head->y=5;tail=head;}while(tail!=NULL){gotoxy(tail->x,tail->y);}void createfood()//随机出现⾷物{snake *food_1;srand((unsigned)time(NULL));food_1=(snake*)malloc(sizeof(snake));while((food_1->x%2!=0)){food_1->x=rand()%52+2;}food_1->y=rand()%24+1;q=head;while(q->next==NULL){if(q->x==food_1->x&&q->y==food_1->y){free(food_1);createfood();}q=q->next;}gotoxy(food_1->x,food_1->y);food=food_1;color(12);printf("@");}int biteself(){snake *self; //定义self为蛇⾝上除蛇头以外的节点self=head->next;while(self!=NULL){if(self->x==head->x&&self->y==head->y){return 1;}self=self->next;}return 0;}void cantcrosswall(){if(head->x==0||head->x==56||head->y==0||head->y==26) {endgamestatus=1;endgame();}}void speedup()//加速{if(sleeptime>=50){sleeptime=sleeptime-10;add=add+2;}}void speeddown()//减速{if(sleeptime<350){sleeptime=sleeptime+30;add=add-2;if(sleeptime==350){add=1;}}}void snakemove()//控制⽅向if(status==U)//上{nexthead->x=head->x; //向上前进时,x不变,y-1nexthead->y=head->y-1;nexthead->next=head;head=nexthead;q=head;//如果下⼀个位置上有⾷物,下⼀个位置的坐标和⾷物坐标相同if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL)//如果没有遇见⾷物{gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}//经过上⾯的循环,q指向蛇尾,蛇尾的下⼀步就是蛇⾛过去的位置 gotoxy(q->next->x,q->next->y);color(3);printf("■");//恢复⾛过的位置free(q->next);q->next=NULL;}}if(status==D){nexthead->x=head->x; //向下前进时,x不变,y+1nexthead->y=head->y+1;nexthead->next=head;head=nexthead;q=head;//如果下⼀个位置上有⾷物,下⼀个位置的坐标和⾷物坐标相同if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL)//如果没有遇见⾷物{gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}//经过上⾯的循环,q指向蛇尾,蛇尾的下⼀步就是蛇⾛过去的位置 gotoxy(q->next->x,q->next->y);color(3);if(status==L)//左{nexthead->x=head->x-2; //向左前进时,x不变,y+1nexthead->y=head->y;nexthead->next=head;head=nexthead;q=head;//如果下⼀个位置上有⾷物,下⼀个位置的坐标和⾷物坐标相同if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y); //⾷物变成蛇⾝上的⼀部分color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL)//如果没有遇见⾷物{gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}//经过上⾯的循环,q指向蛇尾,蛇尾的下⼀步就是蛇⾛过去的位置 gotoxy(q->next->x,q->next->y);color(3);printf("■");//恢复⾛过的位置free(q->next);q->next=NULL;}}if(status==R){nexthead->x=head->x+2; //向上前进时,x不变,y-1nexthead->y=head->y;nexthead->next=head;head=nexthead;q=head;//如果下⼀个位置上有⾷物,下⼀个位置的坐标和⾷物坐标相同if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL)//如果没有遇见⾷物{gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}//经过上⾯的循环,q指向蛇尾,蛇尾的下⼀步就是蛇⾛过去的位置 gotoxy(q->next->x,q->next->y);color(3);if(biteself()==1){endgamestatus=2;endgame();}}void keyboardcontrol(){status=R;while(1){scoreandtips();//GetAsyncKeyState函数⽤来判断函数调⽤指定虚拟键的状态 if(GetAsyncKeyState(VK_UP)&&status!=D){status=U;}else if(GetAsyncKeyState(VK_DOWN)&&status!=U){status=D;}else if(GetAsyncKeyState(VK_LEFT)&&status!=R){status=L;}else if(GetAsyncKeyState(VK_RIGHT)&&status!=L){status=R;}if(GetAsyncKeyState(VK_SPACE)){while(1){//调⽤sleep函数,令进程停⽌,直到达到其中设定的参数时间 Sleep(300);if(GetAsyncKeyState(VK_SPACE)){break;}}}else if(GetAsyncKeyState(VK_ESCAPE)){endgamestatus=3;break;}else if(GetAsyncKeyState(VK_F1)){speedup();}else if(GetAsyncKeyState(VK_F2)){if(sleeptime<350){sleeptime=sleeptime+30;add=add-2;if(sleeptime==350){add=1;}}}Sleep(sleeptime);snakemove();}}void lostdraw(){system("cls");int i,j;gotoxy(35,5);color(14);printf("o00o");gotoxy(39,5);color(11);printf("----------");gotoxy(48,5);color(14);printf("---");gotoxy(51,5);color(11);printf("----------");gotoxy(61,5);color(14);printf("o00o");gotoxy(65,5);color(11);printf("-----------------+");for(i=6;i<=19;i++){gotoxy(17,i);printf("|");gotoxy(82,i);printf("|");}gotoxy(17,20);printf("+----------------------------------");gotoxy(52,20);color(11);printf("-----------------------------+");}void endgame(){system("cls");if(endgamestatus==1){lostdraw();gotoxy(35,9);color(12);printf("对不起,您撞到墙了。
基于C语言实现的贪吃蛇游戏完整实例代码
基于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);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;}}if (f==1)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||scenterx==sx&&scentery==sy)}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,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++)}/******************主场景**********************/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);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;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();case ESC:flag=1;break;}/* if 判断是否吃蛇*/if(flag!=1){ if(head->newx==scenterx&&head->newy==scentery){ p3=head;while(p3!=NULL){ p4=p3;p3=p3->next;}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;p5=p5->next; /****从第五个节点判断是否⾃杀************/while(p5!=NULL){if(head->newx==p5->centerx&&head->newy==p5->centery){ 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(far3);free(far4);closegraph(); return 0;}。
用visual c++开发的贪吃蛇游戏
贪吃蛇完整设计过程和完整源代码简介一直在学VC编程,但是感觉自己的进步甚微,还是什么都不懂,还是什么都不会编,再加上身心不适,学习总是断断续续的。
期中考试过后,放六天假,闲来无事,在网上看到了别人编的贪吃蛇游戏,自己下载两个源代码,一个界面做的太不人性化,感觉那蛇不像蛇,都是断断续续的小点,感觉不是很好;另一个是在TC中编的,好像现在很少有人用TC,我想大家都应该跟我一样,都是用VC,没办法,需要画图,这个程序在VC中没办法运行,一DEBUG错误连篇,看都不想看了。
所以自己有了编写这个游戏的念头。
首先自己看了看别人编写贪吃蛇的思路(其实这就是这个游戏的精髓,我很惭愧啊)。
就是使用链表来实现的。
-----到这里,是不是很感慨我们学过的数据结构原来是这么有用途啊?链表?用链表就可以实现吗?------没错!就是用链表(队列)实现的,在这个游戏程序中除了画图,定时,产生随机数之外,那就是链表的创建,扫描,入队和出队了。
如果你懂链表的这些基本的操作,那没问题,这个游戏程序编写出来是没有问题的。
当然还是得知道点(一点点)WIN32(API,我是用API,没用MFC,相比较API开始比MFC好学些哦)应用程序的基本框架的基本知识。
比方说某些消息,WM_CREATE , WM_PAINT , WM_CHAR , WM_TIMER ;HPEN (画笔), HBRUSH(画刷),HDC(设备上下文环境) ;某些设置画笔、画刷的函数:CreatePen() , SelectObject() ;获取DC的函数,在WM_PAINT 中要使用:BeginPaint() ..... EndPaint() ,在其他地方就要使用GetDC() ......ReleaseDC() ;设置定时器函数SetTimer() ,销毁定时器函数KillTimer() 。
好了,这就是编写出此程序所需要的全部知识,下面的过程要看个人的逻辑思维能力,编程经验。
贪吃蛇 C++源代码
if ((cmd & CMD_A_RIGHT) && g_offset_a.y != 0) { g_offset_a.x = 1; g_offset_a.y = 0; }
#define CMD_B_RIGHT 0x80 控制命令:游戏者 B 向右
#define CMD_QUIT um ITEM { EMPTY = 0, WALL, PLAYER_A, PLAYER_B, PLAYER_DEAD, PLAYER_A_NEXT, PLAYER_B_NEXT };
case 'D' case 'd' if (PLAYER_A_CMD.size() 16) PLAYER_A_CMD.push(CMD_A_RIGHT); break;
case 0 case 0xE0
switch(_getch())
{
case 72 if (PLAYER_B_CMD.size() 16) PLAYER_B_CMD.push(CMD_B_UP); break;
按确定开始游戏
MessageBox(GetHWnd(), _T(对战贪吃蛇 游戏说明:nn)
_T(游戏目标:两条蛇,先碰到墙壁或碰到任何蛇的身体就算输。n)
_T(Player A 使用 A S D W 控制蓝色小蛇移动方向。n)
_T(Player B 使用上下左右控制红色小蛇移动方向。nn)
}
for(y = 1; y HEIGHT - 1; y++)
C语言写的简单贪吃蛇,vc6.0编译通过
#include<iostream>using namespace std;#include<windows.h>#include<stdlib.h>#include<time.h>#define ssize 20#define vsize 40 //地图的水平和垂直尺寸#define SL 200int key=3,count=0,life=0,whefd=0,score=0,level=1;//k是控制键,count是时间延迟计数器,p死亡证明,m地图上是否有食物。
score是分数,level等级。
struct snake{int body[100][2],lenght,direction;} snake;struct map{ char pla[ssize][vsize];} map;void init_map(){ //初始化地图for(int i=0;i<ssize;i++)for(int j=0;j<vsize;j++){map.pla[i][j]=' ';}}void init_snake(){ //初始化蛇,初始长度为3,方向为右。
snake.lenght=3;for(int i=0;i<snake.lenght;i++){snake.body[0][0]=ssize/2;snake.body[0][1]=snake.lenght-1;map.pla[ssize/2][snake.lenght-1]='@';}snake.direction=3;}void print_map(){ //打印地图和蛇for(int i=-1;i<=ssize;i++){for(int j=-1;j<=vsize;j++){if((i==-1&&j==-1)||(i==ssize&&j==-1)||(j==vsize&&i==-1)||(j==vsize&&i==ssize))cout<<"+";else if(i==-1||i==ssize)cout<<"-";else if(j==-1||j==vsize)cout<<"|";elsecout<<map.pla[i][j];}cout<<endl;}}void dead(){life=1;}void food(){ //出现食物int x,y,k=1;while(k){x=rand()%ssize;y=rand()%ssize;if(map.pla[x][y]!='@'){map.pla[x][y]=14;k=0;whefd=1;}}}void move(){ //移动int x,y,n=0;x=snake.body[snake.lenght-1][0]; //记录蛇尾y=snake.body[snake.lenght-1][1];for(int i=snake.lenght-1;i>0;i--){ //将除蛇头外的每一节向前移动一位snake.body[i][0]=snake.body[i-1][0];snake.body[i][1]=snake.body[i-1][1];}if((key+snake.direction)!=5) //按键方向与当前方向相反则无效snake.direction=key; //按键方向有效时,改变蛇的方向。
C语言贪吃蛇源代码
C语言贪吃蛇源代码 TTA standardization office【TTA 5AB- TTAK 08- TTA 2C】#include<stdio.h>#include<process.h>#include<windows.h>#include<conio.h>#include<time.h>#include<stdlib.h>#define WIDTH 40#define HEIGH 12enum direction{//方向LEFT,RIGHT,UP,DOWN};struct Food{//食物int x;int y;};struct Node{//画蛇身int x;int y;struct Node *next;};struct Snake{//蛇属性int lenth;//长度enum direction dir;//方向};struct Food *food; //食物struct Snake *snake;//蛇属性struct Node *snode,*tail;//蛇身int SPEECH=200;int score=0;//分数int smark=0;//吃食物标记int times=0;int STOP=0;void Initfood();//产生食物void Initsnake();//构造snakevoid Eatfood();//头部前进void Addnode(int x, int y);//增加蛇身void display(struct Node *shead);//显示蛇身坐标void move();//蛇移动void draw();//画蛇void Homepage();//主页void keybordhit();//监控键盘按键void Addtail();//吃到食物void gotoxy(int x, int y)//定位光标{COORD pos;pos.X = x - 1;pos.Y = y - 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); }void Initsnake()//构造snake{int i;snake=(struct Snake*)malloc(sizeof(struct Snake));tail=(struct Node*)malloc(sizeof(struct Node));food = (struct Food*)malloc(sizeof(struct Food));snake->lenth=5;//初始长度 5snake->dir=RIGHT;//初始蛇头方向右for(i=2;i<=snake->lenth 2;i )//增加 5 个结点{Addnode(i,2);}}void Initfood()//产生食物{struct Node *p=snode;int mark=1;srand((unsigned)time(NULL));//以时间为种子产生随机数while(1){food->x=rand()%(WIDTH-2) 2;//食物X坐标food->y=rand()%(HEIGH-2) 2;//食物Y坐标while(p!=NULL){if((food->x==p->x)&&(food->y==p->y))//如果食物产生在蛇身上{//则重新生成食物mark=0;//食物生成无效break;}p=p->next;if(mark==1)//如果食物不在蛇身上,生成食物,否则重新生成食物{gotoxy(food->x,food->y);printf("%c",3);break;}mark=1;p=snode;}}void move()//移动{struct Node *q, *p=snode;if(snake->dir==RIGHT){Addnode(p->x 1,p->y);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}if(snake->dir==LEFT){Addnode(p->x-1,p->y);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}if(snake->dir==UP){Addnode(p->x,p->y-1);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}if(snake->dir==DOWN){Addnode(p->x,p->y 1);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}}void Addnode(int x, int y)//增加蛇身{struct Node *newnode=(struct Node *)malloc(sizeof(struct Node)); struct Node *p=snode;newnode->next=snode;newnode->x=x;newnode->y=y;snode=newnode;//结点加到蛇头if(x<2||x>=WIDTH||y<2||y>=HEIGH)//碰到边界{STOP=1;gotoxy(10,19);printf("撞墙,游戏结束,任意键退出!\n");//失败_getch();free(snode);//释放内存free(snake);exit(0);}while(p!=NULL)//碰到自身{if(p->next!=NULL)if((p->x==x)&&(p->y==y)){STOP=1;gotoxy(10,19);printf("撞到自身,游戏结束,任意键退出!\n");//失败_getch();free(snode);//释放内存free(snake);exit(0);}p=p->next;}}void Eatfood()//吃到食物{Addtail();score ;}void Addtail()//增加蛇尾{struct Node *newnode=(struct Node *)malloc(sizeof(struct Node)); struct Node *p=snode;tail->next=newnode;newnode->x=50;newnode->y=20;newnode->next=NULL;//结点加到蛇头tail=newnode;//新的蛇尾}void draw()//画蛇{struct Node *p=snode;int i,j;while(p!=NULL){gotoxy(p->x,p->y);printf("%c",2);tail=p;p=p->next;}if(snode->x==food->x&&snode->y==food->y)//蛇头坐标等于食物坐标{smark=1;Eatfood();//增加结点Initfood();//产生食物}if(smark==0){gotoxy(tail->x,tail->y);//没吃到食物清除之前的尾结点printf("%c",' ');//如果吃到食物,不清楚尾结点}else{times=1;}if((smark==1)&&(times==1)){gotoxy(tail->x,tail->y);//没吃到食物清除之前的尾结点printf("%c",' ');//如果吃到食物,不清楚尾结点smark=0;}gotoxy(50,12);printf("食物: %d,%d",food->x,food->y);gotoxy(50,5);printf("分数: %d",score);gotoxy(50,7);printf("速度: %d",SPEECH);gotoxy(15,14);printf("按o键加速");gotoxy(15,15);printf("按p键减速");gotoxy(15,16);printf("按空格键暂停");}void HideCursor()//隐藏光标{CONSOLE_CURSOR_INFO cursor_info = {1, 0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }void Homepage()//绘主页{int x,y;HideCursor();//隐藏光标printf("----------------------------------------\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("|\t\t\t\t |\n");printf("----------------------------------------\n");gotoxy(5,13);printf("任意键开始游戏!按W.A.S.D控制方向");_getch();Initsnake();Initfood();gotoxy(5,13);printf(" ");}void keybordhit()//监控键盘{char ch;if(_kbhit()){ch=getch();switch(ch){case 'W':case 'w':if(snake->dir==DOWN)//如果本来方向是下,而按相反方向无效{break;}elsesnake->dir=UP;break;case 'A':case 'a':if(snake->dir==RIGHT)//如果本来方向是右,而按相反方向无效{break;}elsesnake->dir=LEFT;break;case 'S':case 's':if(snake->dir==UP)//如果本来方向是上,而按相反方向无效{break;}elsesnake->dir=DOWN;break;case 'D':case 'd':if(snake->dir==LEFT)//如果本来方向是左,而按相反方向无效{break;}elsesnake->dir=RIGHT;break;case 'O':case 'o':if(SPEECH>=150)//速度加快{SPEECH=SPEECH-50;}break;case 'P':case 'p':if(SPEECH<=400)//速度减慢{SPEECH=SPEECH 50;}break;case ' '://暂停gotoxy(15,18);printf("游戏已暂停,按任意键恢复游戏"); system("pause>nul");gotoxy(15,18);printf(" "); break;default:break;}}}int main(void)//程序入口{Homepage();while(!STOP){keybordhit();//监控键盘按键move();//蛇的坐标变化draw();//蛇的重绘Sleep(SPEECH);//暂时挂起线程}return 0;}。
贪吃蛇游戏代码(C语言编写)
贪吃蛇游戏代码(C语言编写)#include "graphics.h"#include "stdio.h"#define MAX 200#define MAXX 30#define MAXY 30#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181#define PAGEUP 18688#define PAGEDOWN 20736#define KEY_U 5749#define KEY_K 9579#define CTRL_P 6512#define TRUE 1#define FALSE 0#define GAMEINIT 1#define GAMESTART 2#define GAMEHAPPY 3#define GAMEOVER 4struct SPlace{int x;int y;int st;} place[MAX];int speed;int count;int score;int control;int head;int tear;int x,y;int babyx,babyy;int class;int eat;int game;int gamedelay[]={5000,4000,3000,2000,1000,500,250,100}; int gamedelay2[]={1000,1};static int hitme=TRUE,hit = TRUE; void init(void);void nextstatus(void);void draw(void);void init(void){int i;for(i=0;i<max;i++)< p="">{place[i].x = 0;place[i].y = 0;place[i].st = FALSE;}place[0].st = TRUE;place[1].st = TRUE;place[1].x = 1;speed = 9;count = 0;score = 0;control = 4;head = 1;tear = 0;x = 1;y = 0;babyx = rand()%MAXX;babyy = rand()%MAXY;eat = FALSE;game = GAMESTART;}void nextstatus(void){int i;int exit;int xx,yy;xx = x;yy = y;switch(control){case 1: y--; yy = y-1; break;case 2: y++; yy = y+1; break;case 3: x--; xx = x-1; break;case 4: x++; xx = x+1; break;}hit = TRUE;if ( ((control == 1) || (control ==2 )) && ( (y < 1) ||(y >= MAXY-1)) || (((control == 3) || (control == 4)) && ((x < 1) ||(x >= MAXX-1) ) ) ){}if ( (y < 0) ||(y >= MAXY) ||(x < 0) ||(x >= MAXX) ){game = GAMEOVER;control = 0;return;}for (i = 0; i < MAX; i++){if ((place[i].st) &&(x == place[i].x) &&(y == place[i].y) ){game = GAMEOVER;control = 0;return;}if ((place[i].st) &&(xx == place[i].x) &&(yy == place[i].y) ){hit = FALSE;goto OUT;}}OUT:if ( (x == babyx) && (y == babyy) ) {count ++;score += (1+class) * 10;}head ++;if (head >= MAX) head = 0;place[head].x = x;place[head].y = y;place[head].st= TRUE;if (eat == FALSE){place[tear].st = FALSE;tear ++;if (tear >= MAX) tear = 0;}else{eat = FALSE;exit = TRUE;while(exit){babyx = rand()%MAXX;babyy = rand()%MAXY;exit = FALSE;for( i = 0; i< MAX; i++ )if( (place[i].st)&&( place[i].x == babyx) && (place[i].y == babyy))exit ++;}}if (head == tear) game = GAMEHAPPY;}void draw(void){char temp[50];int i,j;for (i = 0; i < MAX; i++ ){setfillstyle(1,9);if (place[i].st)bar(place[i].x*15+1,place[i].y*10+1,place[i].x*15+14,place[i]. y*10+9);}setfillstyle(1,4);bar(babyx*15+1,babyy*10+1,babyx*15+14,babyy*10+9);setcolor(8);setfillstyle(1,8);bar(place[head].x*15+1,place[head].y*10+1,place[head].x*1 5+14,place[head].y*10+9); /* for( i = 0; i <= MAXX; i++ ) line( i*15,0, i*15, 10*MAXY);for( j = 0; j <= MAXY; j++ )line( 0, j*10, 15*MAXX, j*10); */rectangle(0,0,15*MAXX,10*MAXY);sprintf(temp,"Count: %d",count);settextstyle(1,0,2);setcolor(8);outtextxy(512,142,temp);setcolor(11);outtextxy(510,140,temp);sprintf(temp,"1P: %d",score);settextstyle(1,0,2);setcolor(8);outtextxy(512,102,temp); setcolor(12);outtextxy(510,100,temp); sprintf(temp,"Class: %d",class); setcolor(8);outtextxy(512,182,temp); setcolor(11);outtextxy(510,180,temp);}main(){int pause = 0;char temp[50];int d,m;int key;int p;static int keydown = FALSE; int exit = FALSE;int stchange = 0;d = VGA;m = VGAMED;initgraph( &d, &m, "" ); setbkcolor(3);class = 3;init();p = 1;while(!exit){if (kbhit()){key = bioskey(0);switch(key){case UP: if( (control != 2)&& !keydown)control = 1;keydown = TRUE;break;case DOWN: if( (control != 1)&& !keydown)control = 2;keydown = TRUE;break;case LEFT: if( (control != 4)&& !keydown)control = 3;keydown = TRUE;break;case RIGHT: if( (control != 3)&& !keydown)control = 4;keydown = TRUE;break;case ESC: exit = TRUE;break;case ENTER: init();break;case PAGEUP: class --; if (class<0) class = 0; break;case PAGEDOWN: class ++;if (class>7) class = 7; break;case KEY_U: if( ( (control ==1) ||(control ==2))&& !keydown) control = 3;else if(( (control == 3) || (control == 4))&& !keydown)control = 1;keydown = TRUE;break;case KEY_K: if( ( (control ==1) ||(control ==2))&& !keydown) control = 4;else if(( (control == 3) || (control == 4))&& !keydown)control = 2;keydown = TRUE;break;case CTRL_P:pause = 1 - pause; break;}}stchange ++ ;putpixel(0,0,0);if (stchange > gamedelay[class] + gamedelay2[hit]){stchange = 0;keydown = FALSE;p = 1 - p;setactivepage(p);cleardevice();if (!pause)nextstatus();else{settextstyle(1,0,4);setcolor(12);outtextxy(250,100,"PAUSE");}draw();if(game==GAMEOVER){settextstyle(0,0,6);setcolor(8);outtextxy(101,101,"GAME OVER"); setcolor(15);outtextxy(99,99,"GAME OVER"); setcolor(12);outtextxy(100,100,"GAME OVER"); sprintf(temp,"Last Count: %d",count); settextstyle(0,0,2);outtextxy(200,200,temp);}if(game==GAMEHAPPY){settextstyle(0,0,6);setcolor(12);outtextxy(100,300,"YOU WIN"); sprintf(temp,"Last Count: %d",count); settextstyle(0,0,2);outtextxy(200,200,temp);}setvisualpage(p);}}closegraph();}</max;i++)<>。
贪吃蛇C语言代码实现(难度可选)
贪吃蛇C语⾔代码实现(难度可选)本⽂实例为⼤家分享了C语⾔实现贪吃蛇的具体代码,供⼤家参考,具体内容如下/*****************************************************************************贪吃蛇(难度可选)**********************************制作者:Xu Lizi ⽇期:2012/12/31****************************部分函数有借鉴**********************************************************************************/#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>#include<time.h>int snakey[100]={5,4,3,2,1}; /*定义蛇的横坐标*/int snakex[100]={1,1,1,1,1}; /*定义蛇的纵坐标,蛇头起始位置为(5,1)*/int life=0; /*定义蛇的⽣命,0表⽰存活,1表⽰死亡*/int lenght=5; /*定义蛇的长度,初始为5节*/char map[12][24]={"***********************", /*y*/"* *","* *","* *","* *","* *","* *","* *","* *","* *","* *",/*x*/ "***********************"};void put_money(int i,int j) /*放钱函数,使⽤随机数,随机出现⾷物*/{int x=0,y=0;srand(time(NULL));while ( (map[y][x]==003) || (map[y][x]==002) || (map[y][x]=='*') || ((x==i)&&(y==j)) ){x=rand()%21+1;y=rand()%10+1;}map[y][x]='$';return;}void output() /*输出*/{system("cls");int i,j;for(i=0; i<12; i++){for(j=0; j<23; j++) printf("%c", map[i][j]);printf("\n");}return;}void gameover() /*游戏结束*/{life=1;printf("笨蛋,输了吧\n");return;}void turn_up() /*向上移动*/{system("cls");int i;if ( (snakex[0]==1) || (map[snakex[0]-1][snakey[0]]==003) ) gameover(); else { if (map[snakex[0]-1][snakey[0]]=='$'){put_money( snakey[0], snakex[0]-1 );lenght++;map[snakex[lenght-1]][snakey[lenght-1]]=003;}for(i=lenght; i>0; i--){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}map[snakex[lenght]][snakey[lenght]]=' ';snakex[0]--;for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;map[snakex[0]][snakey[0]]=002;output();}return;}void turn_down() /*向下*/{system("cls");int i;if ( (snakex[0]==10) || (map[snakex[0]+1][snakey[0]]==003) ) gameover();else { if (map[snakex[0]+1][snakey[0]]=='$'){put_money(snakey[0],snakex[0]+1);lenght++;map[snakex[lenght-1]][snakey[lenght-1]]=003;}for(i=lenght; i>0; i--){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}snakex[0]++;map[snakex[lenght]][snakey[lenght]]=' ';for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;map[snakex[0]][snakey[0]]=002;output();}return;}void turn_left() /*向左*/{system("cls");int i;if ( (snakey[0]==1) || (map[snakex[0]][snakey[0]-1]==003) ) gameover();else { if (map[snakex[0]][snakey[0]-1]=='$'){put_money(snakey[0]-1,snakex[0]);lenght++;map[snakex[lenght-1]][snakey[lenght-1]]=003;}for(i=lenght; i>0; i--){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}map[snakex[lenght]][snakey[lenght]]=' ';snakey[0]--;for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;map[snakex[0]][snakey[0]]=002;output();}return;}void turn_right() /*向右*/{system("cls");int i;if ( (snakey[0]==21) || (map[snakex[0]][snakey[0]+1]==003) ) gameover();else {if (map[snakex[0]][snakey[0]+1]=='$'){put_money(snakey[0]+1,snakex[0]);lenght++;map[snakex[lenght-1]][snakey[lenght-1]]=003;}for(i=lenght; i>0; i--){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}map[snakex[lenght]][snakey[lenght]]=' ';snakey[0]++;for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;map[snakex[0]][snakey[0]]=002;output();}return;}int main(){int i,timeover,hard;long start;char name , direcation;printf("\n 向上移动:W ;向下移动:S ; 向左移动:A ;向右移动:D \n");printf("\t请选择难度(数字)\n\t分1~5级,分别代表\n\t1难,2中上,3中,4中下5,易:\n");scanf("%d",&hard);system("cls");for(i=1;i<5;i++) map[1][i]=003; /*输出蛇⾝*/map[1][5]=002; /*输出蛇头*/put_money(0,0);output();while(life!=1) /*当蛇死亡时结束循环*/{/*让蛇⾃动运⾏的函数******有借鉴*/timeover=1;start=clock();while((timeover=(clock()-start<=hard*100))&&!kbhit()); //难度设定if(timeover){direcation=getch();}/*让蛇⾃动运⾏的函数******有借鉴*/switch(direcation){case 'w':turn_up();break;case 's':turn_down();break;case 'a':turn_left();break;case 'd':turn_right();break;}}return 0;}更多有趣的经典⼩游戏实现专题,分享给⼤家:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
基于VC的贪吃蛇游戏的C语言代码
基于VC的贪吃蛇游戏的C语言代码#include#include#include#include#define U 1#define D 2#define L 3#define R 4 //蛇的状态,U:上;D:下;L:左 R:右typedef struct SNAKE //蛇身的一个节点{int x;int y;struct SNAKE *next;}snake;//全局变量//int score=0,add=10;//总得分与每次吃食物得分。
int status,sleeptime=200;//每次运行的时间间隔snake *head, *food;//蛇头指针,食物指针snake *q;//遍历蛇的时候用到的指针int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
//声明全部函数//void Pos();void creatMap();void initsnake();int biteself();void createfood();void cantcrosswall();void snakemove();void pause();void gamecircle();void welcometogame();void endgame();void gamestart();void Pos(int x,int y)//设置光标位置{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput,pos);}void creatMap()//创建地图{int i;for(i=0;i<58;i+=2)//打印上下边框{Pos(i,0);printf("■");Pos(i,26);printf("■");}for(i=1;i<26;i++)//打印左右边框{Pos(0,i);printf("■");Pos(56,i);printf("■");}}void initsnake()//初始化蛇身{snake *tail;int i;tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//tail->x=24;tail->y=5;tail->next=NULL;for(i=1;i<=4;i++){head=(snake*)malloc(sizeof(snake));head->next=tail;head->x=24+2*i;head->y=5;tail=head;}while(tail!=NULL)//从头到为,输出蛇身{Pos(tail->x,tail->y);printf("■");tail=tail->next;}}int biteself()//判断是否咬到了自己{snake *self;self=head->next;while(self!=NULL){if(self->x==head->x && self->y==head->y){return 1;}self=self->next;}return 0;}void createfood()//随机出现食物{snake *food_1;srand((unsigned)time(NULL));food_1=(snake*)malloc(sizeof(snake));while((food_1->x%2)!=0) //保证其为偶数,使得食物能与蛇头对其 {food_1->x=rand()%52+2;}food_1->y=rand()%24+1;q=head;while(q->next==NULL){if(q->x==food_1->x && q->y==food_1->y) //判断蛇身是否与食物重合{free(food_1);createfood();}q=q->next;}Pos(food_1->x,food_1->y);food=food_1;printf("■");}void cantcrosswall()//不能穿墙{if(head->x==0 || head->x==56 ||head->y==0 || head->y==26) {endgamestatus=1;endgame();}}void snakemove()//蛇前进,上U,下D,左L,右R{snake * nexthead;cantcrosswall();nexthead=(snake*)malloc(sizeof(snake));if(status==U){nexthead->x=head->x;nexthead->y=head->y-1;if(nexthead->x==food->x && nexthead->y==food->y)//如果下一个有食物//{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //如果没有食物//{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(status==D){nexthead->x=head->x;nexthead->y=head->y+1;if(nexthead->x==food->x && nexthead->y==food->y) //有食物{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //没有食物{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}if(status==L){nexthead->x=head->x-2;nexthead->y=head->y;if(nexthead->x==food->x && nexthead->y==food->y)//有食物{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //没有食物{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(status==R){nexthead->x=head->x+2;nexthead->y=head->y;if(nexthead->x==food->x && nexthead->y==food->y)//有食物nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //没有食物 {nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(biteself()==1) //判断是否会咬到自己{endgamestatus=2;endgame();}}void pause()//暂停{while(1){Sleep(300);if(GetAsyncKeyState(VK_SPACE)) {}}}void gamecircle()//控制游戏{Pos(64,15);printf("不能穿墙,不能咬到自己\n");Pos(64,16);printf("用↑.↓.←.→分别控制蛇的移动.");Pos(64,17);printf("F1 为加速,F2 为减速\n");Pos(64,18);printf("ESC :退出游戏.space:暂停游戏.");Pos(64,20);printf("C语言研究中心/doc/1d5620851.html,");status=R;while(1){Pos(64,10);printf("得分:%d ",score);Pos(64,11);printf("每个食物得分:%d分",add);if(GetAsyncKeyState(VK_UP) && status!=D){status=U;}else if(GetAsyncKeyState(VK_DOWN) && status!=U) {status=D;}else if(GetAsyncKeyState(VK_LEFT)&& status!=R) {status=L;}else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) {status=R;}else if(GetAsyncKeyState(VK_SPACE))}else if(GetAsyncKeyState(VK_ESCAPE)) {endgamestatus=3;break;}else if(GetAsyncKeyState(VK_F1)) {if(sleeptime>=50){sleeptime=sleeptime-30;add=add+2;if(sleeptime==320){add=2;//防止减到1之后再加回来有错}}}else if(GetAsyncKeyState(VK_F2)) {if(sleeptime<350){sleeptime=sleeptime+30;add=add-2;if(sleeptime==350){add=1; //保证最低分为1}}Sleep(sleeptime);snakemove();}}void welcometogame()//开始界面{Pos(40,12);system("title C语言研究中心/doc/1d5620851.html,");printf("欢迎来到贪食蛇游戏!");Pos(40,25);printf(" C语言研究中心/doc/1d5620851.html,.\n");system("pause");system("cls");Pos(25,12);printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n"); Pos(25,13);printf("加速将能得到更高的分数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
T.tail=(++T.tail)%ML;
}
}
void aaa()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
sum=T.body[(T.head-1+ML)%ML]+1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
www();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
T.tail=(++T.tail)%ML;
}
}
void sss()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
}
if(order=='w')
{
jud=order;
//printf("-->w\n");
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
ddd();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<conio.h>
#include<time.h>
#include<windows.h>
#define ML 100
using namespace std;
break;
}
}
else if(order=='a')
{
jud=order;
//printf("-->a\n");
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
srand(time(0));
sum=rand()%10000+101;
}*/
T.body[T.head++]=101;
//printf("%d\n",sum);
T.head%=ML;
sum=getnum();
map[sum/100][sum%100]=1;
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void ddd()
{
int sum,i;
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
T_T=0;
char jud='s';
int TM=300,start;
while(1)
{
TM=300-T.length*15;
start=clock();
while(clock()-start<=TM&&!kbhit())
{
;
}
if(kbhit()&&(order=getch(),order=='w'||order=='s'||order=='a'||order=='d'))
break;
}
}
else if(order=='d')
{
jud=order;
//printf("-->d\n");
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
while(temp--)
random_shuffle(card,card+19*19);
for(i=0;i<=20;i++)
map[i][0]=map[20][i]=map[i][20]=map[0][i]=-1;
init();
T.length=1;
/*while(sum/100>19||sum%100>19||map[sum/100][sum%100]==-1)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
sss();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)