c语言实现贪吃蛇

      大一上学期马上就过去了,c语言课设的题目是实现一个贪吃蛇小游戏。对于一个小白,这是一场试炼!

      这次课设承包了我的很多第一次:第一次用到这么多头文件;第一次感受到链表的强大;第一次写了500行+的代码;第一次敲完代码后成就感满满.....(以至于立马就来炫耀了^_^)。创作的过程也是非常艰辛,去网上查阅了大量资料,翻看了很多版本的源码,才写出来一个差强人意的贪吃蛇

效果如下: 

 

      原谅我中二的把名字设成 ----“贪吃蛇_终章 ” (Hhhh因为实在有点小得意),至于源码嘛..嘿......嘿嘿.........

如果编译不了就把后缀名改成.cpp噢,我用的dev版本太低了好像不兼容c99,刚开始一直给我报错后面改成.cpp就好了(我也不知道什么原因……)。

代码很杂、很乱....大佬轻点喷

​
​
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

const int ESC=27;
const int SPACE='p';

static char temp=0;
static unsigned int MAX = 0;
static char NAME[20];

		
void gotoxy(int x, int y);
void madewell();
void welcome();
void setcolor(int );
char dongh(); 
void help(); 
void end();
char llll();
void baoc();

//蛇
typedef struct Lnode{
        int x;
        int y;
        struct Lnode *next;
}Lnode,*Linklist;

typedef struct{
        Linklist front;                                                      
        Linklist rear;                                                         
        int length;                                                                 
}Queue;

Queue snake;
//功能函数 
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 HideCursor()
{
	CONSOLE_CURSOR_INFO cursor;    
	cursor.bVisible = FALSE;    
	cursor.dwSize = sizeof(cursor);    
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    
	SetConsoleCursorInfo(handle, &cursor);
}



//界面函数;

void madewell()
{
	color(2);
	int i,j;
	for(j = 0; j <=29 ;j++)
	{
		for(i = 0; i <= 88; i++)
		{
			if(i == 0||j == 0||i == 88||j ==29)
			{
				gotoxy(i,j);
				printf("■");
			}
		}
	}
}
 
void welcome()
{
	system("cls");
	gotoxy(35,6);
	printf("---------------");
	gotoxy(35,7);
	printf("/ 1.开始游戏  \\");
	gotoxy(35,8);
	printf("/             \\");
	gotoxy(35,9);
	printf("/  2.最高分   \\");
	gotoxy(35,10);
	printf("/             \\");
	gotoxy(35,11);
	printf("/ 3.游戏帮助  \\");
	gotoxy(35,12);
	printf("/             \\"); 
	gotoxy(35,13);
	printf("/  ESC.退出   \\");
	gotoxy(34,14);
	printf("-----------------");
	
}

char dongh()
{
	int i;
	char c;
		for(i = 66; i >= 0; Sleep(150),i--)
		{
			if(i == 0) i = 88;
			welcome();
			gotoxy(i,21);
			color(2);
			printf("@  @");
			gotoxy(i,22); //                                                     l
			printf(" ■      ■      ■");
			gotoxy(i,23);
			printf("  ■  ■  ■  ■  ■");
			gotoxy(i,24);
			printf("    ■      ■      ■■■");
		
			gotoxy(0,25);
			color(5);
			printf("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
			gotoxy(0,26);
			printf("■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■");
			gotoxy(0,27);
			printf("  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■ ");
			gotoxy(0,28);
			printf("■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■");
			
			if(kbhit())
			{
				c = getch();
				return c;
			}
		}
	
}

char llll()
{
	if(snake.length > MAX)
	baoc();
	system("cls");
	char c;
	color(2);
	int i,j;
	for(i = 0,j = 29;j > 21; j--,i++)
	{
		gotoxy(0,i);
		printf("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
		Sleep(22);
		gotoxy(0,j);
		printf("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
		gotoxy(0,i+1);
		Sleep(22);
		printf("■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■");
		gotoxy(0,j-1);
		Sleep(22);
		printf("■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■");
		gotoxy(0,i+2);
		Sleep(22);
		printf("  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■ ");	
		gotoxy(0,j-2);
		Sleep(22);
		printf("  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■ ");	
		gotoxy(0,i+3);
		Sleep(22);
		printf("■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■");
		gotoxy(0,j-3);
		Sleep(22);
		printf("■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■");
		Sleep(22);
	}
	color(1);
	gotoxy(0,10);
	printf("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
	gotoxy(0,18);
	printf("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
		
	color(5);
	gotoxy(21,11);
	printf("■■■■  ■      ■  ■■■■  ■■■■");
	gotoxy(21,12);
	printf("■    ■   ■      ■  ■        ■    ■");
	gotoxy(21,13);
	printf("■    ■   ■      ■  ■        ■    ■");
	gotoxy(21,14);
	printf("■    ■     ■  ■    ■■■■  ■■■■");
	gotoxy(21,15);
	printf("■    ■     ■  ■    ■        ■■");
	gotoxy(21,16);
	printf("■    ■     ■  ■    ■        ■  ■");
	gotoxy(21,17);
	printf("■■■■       ■      ■■■■  ■   ■■");
	gotoxy(74,12);
	printf("得分:%d",snake.length);
	gotoxy(74,15);
	printf("1.再来一把");
	gotoxy(74,16);
	printf("2.溜了溜了");
	gotoxy(74,17);
	printf("3.返回菜单");

	while(c = getch())
	{
		if(c == '1'||c == '2'||c == '3')
			break;
	}
	return c;
}



void help()
{
	system("cls");
	
	gotoxy(20,14);
	printf("------------------------------------------------------");
	gotoxy(20,7);
	printf("------------------------------------------------------");
	gotoxy(20,13);
	printf("玩法说明:\n\n                    操控你的蛇去吞下豆子,吞下豆子会使蛇的长度增加,同时蛇\n                    的速度也会变快努力吃下更多的豆子吧!"); 
	gotoxy(20,6);
	printf("按键控制:\n\n                    移动:w(上) s(下) a(左) d(右)\n");
	gotoxy(20,9);
	printf("暂停:P");
	gotoxy(20,10);
	printf("快速退出:ESC");
	
	gotoxy(63,25);
	printf("......按回车键返回");
	char d;
	while(d = getch())
	{
		if(d == '\r')
			return ;
	}
}

void baoc()
{
	char *p = NAME;
	char ch;
	system("cls");
	gotoxy(29,9);
	printf("============================");
	gotoxy(29,17);
	printf("============================");
	for(int i = 10; i< 17; i++)
	{
	gotoxy(29,i);
	printf("|                          |");
	}
	gotoxy(32,11);
	printf("破纪录了!!!"); 
	gotoxy(32,14);
	printf("大佬,留下姓名________");
	gotoxy(47,14);
	scanf("%s",&NAME);
	MAX = snake.length+3;
	FILE *fp_n = fopen("name.txt","w");
	fprintf(fp_n,"%s",NAME);
	fclose(fp_n);
	FILE *fp_s = fopen("score.txt","w");
	fprintf(fp_s,"%d",MAX);
	
}
//蛇函数 
void enqueue(int x,int y)                               
{
        Linklist p;
        p=(Linklist)malloc(sizeof(Lnode));
        p->x=x;
        p->y=y;
        snake.rear->next=p;
        snake.rear=p;
        snake.rear->next=NULL;
        snake.length++;
}
void dequeue()                                         
{
        Linklist p=snake.front->next, q=snake.front;
        p->x=q->x;
        p->y=q->y;
        snake.front=p;
        free(q);
        snake.length--;
}



void init_snake()                                     
{
        snake.rear=snake.front=(Linklist)malloc(sizeof(Lnode));
        snake.rear->next=NULL;
        for(int i=2;i<5;i++)
        {
                enqueue(i,1);                                     
        }
        snake.length=0;
}



void show_food()                                    
{
        srand(time(NULL));
        Linklist p=snake.front->next, q=snake.front; 
        while(1)
        {
                q->x=rand()%(86)+2;
                q->y=rand()%(28)+1;
                while(p)                                                     
                {
                        if(p->x==q->x&&p->y==q->y) break;
                        p=p->next;
                }
                if(!p) break;
        }
        gotoxy(q->x,q->y);
        color(4);
        putchar('@');                                                  
}



void hua_snake(int i)                                
{
        Linklist p=snake.rear;
        gotoxy(p->x,p->y);
        if(i)
        {
        	color(1);
		    putchar('#');
		}
		else
        {
        	color(4);
        	putchar('*');
		}
}





void clear_wei()                                                   
{
        Linklist p=snake.front->next;
        gotoxy(p->x,p->y);
        putchar(' ');
        p=p->next;
        gotoxy(p->x,p->y);
        color(1);
        putchar('#');
}



int flag()                                                                          
{
        Linklist p=snake.front->next, q=snake.rear;
        while(p!=q)                             
        {
                if(p->x==q->x&&p->y==q->y) break;
                p=p->next;
        }
        if(p==q)
        {
                if(p->x>2&&p->x<88&&p->y>=1&&p->y<29) return 1;
        }
        return 0;
}


void snake_auto_move(char tmp)      
{
        int x, y;
        do{                                       
                clear_wei();                                              
                hua_snake(1);                                   
                x=snake.rear->x;
                y=snake.rear->y;
                switch(tmp)                                         
                {
                case 'w':y--;break;
                case 's':y++;break;
                case 'a':x--;break;
                case 'd':x++;break;
                }
                enqueue(x,y);                                          
                if(x==snake.front->x&&y==snake.front->y)         
                {
                        show_food();                                               
                }
                else dequeue();                                  
                hua_snake(0);                                        
                int speed=-5*snake.length + 157;         
                Sleep(speed);
        }while(!kbhit()&&flag());   
                      
}





void snake_move()              
{
        char c, c1=0;
        if(!temp)                             
        {
                temp='d';
                snake_auto_move('d'); 
        }
        while(1)
        {
                c=getch();
                while(c !='s'&&c != 'w'&&c != 'a' && c!='d'&&c != SPACE&& c !=ESC)
                {
                	snake_auto_move(temp);
                	c = getch();
                		continue;
				}
                if(c==ESC)                                   
                {
                        system("cls");
						color(5); 
                        puts("游戏退出成功!");
                        exit(0);
                }
                if(c1==SPACE)                         
                {
                        if(c==SPACE)
                        {
                                c1=0;
                                gotoxy(40,16);
                                printf("            ");
                                snake_auto_move(temp);
                        }
                        continue;
                }
                if(c==SPACE)                               
                {
                        c1=SPACE;
                        gotoxy(40,16);
                        printf("暂停中。。。");
                        continue; 
                }
                if(c=='s'||c=='w'||c=='a'||c=='d')  
                {
                        if(c=='s'&&temp=='w'||c=='w'&&temp=='s'||c=='a'&&temp=='d'||c=='d'&&temp=='a')
                        {
                                snake_auto_move(temp);
                                continue;
                        }
                }
                break;
        }
        temp=c;                                                             
        snake_auto_move(temp);
}


int main()
{
	FILE *fp_name = fopen("name.txt","r+");
	fscanf(fp_name,"%s",&NAME);
	fclose(fp_name);
	FILE *fp_score = fopen("score.txt","r+");
	fscanf(fp_score,"%d",&MAX);
	fclose(fp_score);
	system("title 贪吃蛇_终章");
	system("mode con cols=90 lines=30");
	system("color 05");
	HideCursor();
	while(1)
	{
		char c;
		c = dongh(); 
	
		switch(c)
		{
			case '1':
				{
					while(1)
					{
						temp = 0;
						char x;
						system("cls");
						madewell();
						init_snake();
        				show_food();
        				while(flag())
						{
           					snake_move();
						}
						x = llll();
						if(x == '1')
							continue; 
						if(x == '2')
						{
							system("cls");
							printf("退出游戏成功!");
							exit(0);
						}
						if(x == '3')
							break;
					}
					break;	
			    }
			case '2':
				{
					char c;
					system("cls");
					gotoxy(30,15);
					printf("名字:%s   得分:%d",NAME,MAX-3);
					gotoxy(63,25);
					printf("......按回车键返回");
					while(c=getch())
					{
						if(c == '\r')
						break;
					}
					break;
				}
			case '3':
				{
					help();
					break;
				}
			case 27:
				{
					color(5);
					system("cls");
					printf("游戏退出成功!"); 
					exit(0);
				}
			default : break;
	    }
			
	}
	
	getch();
	return 0;
}

​

​

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Front row sailor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值