本文主要是介绍C语言程序简单实现贪吃蛇小游戏—不需要graphics.h头文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近学习了一些数据结构的知识,然后试着用链表实现一个小游戏(贪吃蛇),在网上看了一些别人写的代码,然后自己重新改写(以前是一个主函数,现在分成了几个小函数方便查看功能,加了内存释放函数)了。在这里发表自己的 第一篇技术博客,希望大家一起讨论、学习,如果有错误,大家积极指出,这样才会有进步!
开发平台是:win7(32位)+vs2008。
snake.h 头文件
#ifndef _SNAKE_H_H_H #define _SNAKE_H_H_H//调节游戏界面大小,这里设置为15*15 int const COL = 15; int const ROW = 15;//蛇节点 typedef struct Node {int x;int y;struct Node *pre;struct Node *next; }NODE, *pNODE;//食物 typedef struct Food {int x;int y;char c; }FOOD, *pFOOD;//初始化,创建蛇的第一节 pNODE InitSnake(void);//初始化食物成员 FOOD InitFood(void);//蛇运动函数 int MoveSnake(pNODE pHead, char c);//检查键盘按键 char KbHit(char orient);//蛇吃到食物处理函数 pNODE SnakeEatFood(pNODE pHead, pFOOD pFood);//打印游戏界面函数 void Print(pNODE pHead, FOOD food);//游戏结束,释放内存函数 void FreeMemory(pNODE *ppHead);#endif
snake.c 源文件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <Windows.h>
#include "snake.h"
//主函数
int main(void)
{char orien = 'a', get_char;int game_over = 0;FOOD food = InitFood();pNODE head = InitSnake();while (1){head = SnakeEatFood(head, &food);get_char = KbHit(orien);if (27 == get_char){game_over = 1;break;}elseorien = get_char; game_over = MoveSnake(head, orien);if (game_over)break;system("cls");Print(head, food);Sleep(200); //ms级,刷屏间隔时间}if (game_over){printf("游戏结束!\n");FreeMemory(&head);if (NULL == head)printf("内存释放成功!\n");elseprintf("内存释放失败!\n");}getch();return 0;
}
//初始化,创建蛇的第一节
pNODE InitSnake(void)
{pNODE pHead = (pNODE)malloc(sizeof(NODE));srand((unsigned)(time(NULL)+1));if (NULL == pHead){printf("内存分配失败!\n");exit(-1);}pHead->x = rand() % ROW;pHead->y = rand() % COL;pHead->next = NULL;pHead->pre = NULL;return pHead;
}
//初始化食物成员
FOOD InitFood(void)
{FOOD food;srand((unsigned)time(NULL));food.x = rand() % ROW;food.y = rand() % COL;food.c = 65 + rand()%26;return food;
}
//蛇吃到食物处理函数
pNODE SnakeEatFood(pNODE pHead, pFOOD pFood)
{pNODE p_add = NULL, pt = NULL, rear = NULL;if (pFood->x == pHead->x && pFood->y == pHead->y){p_add = (pNODE)malloc(sizeof(NODE));if (NULL == p_add){printf("内存分配失败!\n");exit(-1);}pt = pHead;while (pt->next != NULL){pt = pt->next;}p_add->pre = pt;p_add->next = NULL;pt->next = p_add;*pFood = InitFood();//不让食物出现在蛇的位置上pt = pHead;while (pt != NULL){if (pFood->x == pHead->x && pFood->y == pHead->y){*pFood = InitFood();break;}pt = pt->next;}}return pHead;
}
//检查键盘按键
char KbHit(char orient)
{ char c;if (kbhit()){c = getch(); if (orient != 'd' && c == 'a'){orient = c;}else if (orient != 'a' && c == 'd'){orient = c;}else if (orient != 'w' && c == 's'){orient = c;}else if (orient != 's' && c == 'w'){orient = c;}else if (27 == c){orient = c;}}return orient;
}
//蛇运动函数
int MoveSnake(pNODE pHead, char c)
{INT game_over = 0;pNODE pt = pHead;//让pt指向蛇尾while (pt->next != NULL){pt = pt->next;}//从蛇尾向向蛇头前进while(pt != pHead){pt->x = pt->pre->x;pt->y = pt->pre->y;pt = pt->pre;}if ('d' == c){pHead->x += 1;if (pHead->x >= ROW){pHead->x -= ROW;}}if ('a' == c){pHead->x -= 1;if (pHead->x < 0){pHead->x += ROW;}}if ('s' == c){pHead->y += 1;if (pHead->y >= COL){pHead->y -= COL;}}if ('w' == c){pHead->y -= 1;if (pHead->y < 0){pHead->y += COL;}}//当蛇头碰到蛇身,游戏结束pt = pHead->next;while (pt != NULL){if (pt->x == pHead->x && pt->y == pHead->y){game_over = 1;}pt = pt->next;}return game_over;
}
//打印游戏界面函数
void Print(pNODE pHead, FOOD food)
{int row = 0, col = 0, flag = 0;pNODE pt = NULL;printf(" 方向控制——上:w 下:s 左:a 右:d(Esc退出)\n ");for (row=0; row<ROW; row++){printf("—");}putchar('\n');for (col=0; col<COL; col++){printf(" |");for (row=0; row<ROW; row++){pt = pHead;flag = 0;//打印出蛇while (pt != NULL){if (row == pt->x && col == pt->y){if (pt == pHead)printf("■");elseprintf("□");flag = 1;break;}pt = pt->next;}//打印出食物或两个空格if (0 == flag){if (row == food.x && col == food.y){putchar(food.c);putchar(food.c);continue;}printf(" ");} }printf("|");putchar('\n');}printf(" ");for (row=0; row<ROW; row++){printf("—");}putchar('\n');
}
//释放内存函数
void FreeMemory(pNODE *ppHead)
{pNODE p_delete = NULL, pt = NULL;while (*ppHead != NULL){pt = (*ppHead)->next;if (pt != NULL){pt->pre = NULL;} p_delete = *ppHead;free(p_delete);p_delete = NULL;*ppHead = pt;}
}
最后附测试结果图片一张:
这篇关于C语言程序简单实现贪吃蛇小游戏—不需要graphics.h头文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!