C语言实现贪吃蛇源码
先放效果
源代码
//2016-2-12
//zhaoyu
//Gmail:zhaoyu1995.com@gmail.com
//Language: C
//Platform:Code::Blocks
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h> typedef struct snake
{
int x;
int y;
struct snake *next;
}Snake;
int X, Y;
enum STATUS{Up = 1, Down, Left, Right}; Snake *pHead, *pBody;//the head of the snake enum STATUS Direction;
int score=0, scorePerFood=10;
int gameStatus = 0;
int timeInterval = 200;
void gameEnd(void);
void setPosition(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
void hideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void creatInterface(void)
{
int i;
for(i=0;i<58;i+=2)
{
setPosition(i,0);
printf("¡ö");//a ¡ö occupy two character space
setPosition(i,26);
printf("¡ö");
}
for(i=1;i<26;i++)
{
setPosition(0,i);
printf("¡ö");
setPosition(56,i);
printf("¡ö");
}
setPosition(65, 10);
printf("Introduction");
setPosition(63,12);
printf("¡ü\tMove up.");
setPosition(63,14);
printf("¡ý\tMove Down.");
setPosition(63,16);
printf("¡û\tMove left.");
setPosition(63,18);
printf("¡ú\tMove right.");
setPosition(63,20);
printf("F1\tSpeed up.");
setPosition(63, 22);
printf("F2\tSlow down.");
setPosition(63, 24);
printf("Space\tGame pause.");
setPosition(63,26);
printf("ESC\tQuit the game.");
setPosition(63, 4);
printf("Score:");
setPosition(63, 6);
printf("Score Per Food:");
}
void initializeSnake(void)
{
Snake *pTail;
int i;
pTail = (Snake *)malloc(sizeof(Snake));
pTail->x = 24;
pTail->y = 5;
pTail->next = NULL;
for (i = 1; i < 4; i++)
{
pHead = (Snake *)malloc(sizeof(Snake));
pHead->next = pTail;
pHead->x = 24 + 2*i;
pHead->y = 5;
pTail = pHead;//Important
}
while (pTail != NULL)
{
setPosition(pTail->x, pTail->y);
printf("¡ö");
pTail = pTail->next;
}
}
int biteSelf(void)
{
Snake *pSelf = pHead->next;
while (pSelf != NULL)
{
if (pHead->x==pSelf->x && pHead->y==pSelf->y)
{
return 1;
}
pSelf = pSelf->next;
}
return 0;
}
void creatFood(void)
{
int flag = 0;
X = Y = 4;
srand((unsigned)time(NULL));
do{
X = rand()%52 + 2;
Y = rand()%24 + 1; if (X%2 != 0)
{
flag = 1;
}
else
{
flag = 0;
}//Important
pBody = pHead;
while (pBody->next != NULL)
{
if(pBody->x==X && pBody->y==Y)
{
flag = 1;
}
pBody = pBody->next;
}
}while(flag==1);
setPosition(X, Y);
printf("¡ö");
} void hitWall(void)
{
if (pHead->x==0 || pHead->x>=56 || pHead->y==0 || pHead->y>=26)
{
gameStatus = 1;
gameEnd();
}
}
void snakeMove(void)
{
Snake *pNextHead;
hitWall(); pNextHead = (Snake *)malloc(sizeof(Snake));
pNextHead->next = pHead;
switch(Direction)
{
case Up:
pNextHead->x = pHead->x;
pNextHead->y = pHead->y - 1;
break;
case Down:
pNextHead->x = pHead->x;
pNextHead->y = pHead->y + 1;
break;
case Right:
pNextHead->x = pHead->x + 2;
pNextHead->y = pHead->y;
break;
case Left:
pNextHead->x = pHead->x - 2;
pNextHead->y = pHead->y;
break;
default:
break;
}
pHead = pNextHead;
pBody = pHead;
if (pNextHead->x == X && pNextHead->y == Y)
{
while (pBody != NULL)
{
setPosition(pBody->x, pBody->y);
printf("¡ö");
pBody = pBody->next;
}
score += scorePerFood;
creatFood();
}
else
{
setPosition(pBody->x, pBody->y);
printf("¡ö");
while (pBody->next->next != NULL)
{
pBody = pBody->next;
}
setPosition(pBody->next->x, pBody->next->y);
printf(" ");
free(pBody->next);
pBody->next = NULL;
}
if (biteSelf() == 1)
{
gameStatus = 2;
gameEnd();
}
}
void pause(void)
{
while(1)
{
Sleep(300);
if(GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
void gameCircle(void)
{
Direction = Right;
while (1)
{
setPosition(72, 4);
printf("%d", score);
setPosition(80, 6);
printf("%d ", scorePerFood);//Attention space is needed
if (GetAsyncKeyState(VK_UP) && Direction!=Down)
{
Direction = Up;
}
else if (GetAsyncKeyState(VK_DOWN) && Direction!=Up)
{
Direction = Down;
}
else if (GetAsyncKeyState(VK_LEFT) && Direction!=Right)
{
Direction = Left;
}
else if (GetAsyncKeyState(VK_RIGHT) && Direction!=Left)
{
Direction = Right;
}
else if (GetAsyncKeyState(VK_SPACE))
{
pause();
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
gameStatus = 3;
break;
}
else if (GetAsyncKeyState(VK_F1))
{
if(timeInterval >= 70)
{
timeInterval -= 60;
}
}
else if (GetAsyncKeyState(VK_F2))
{
if (timeInterval <= 400)
{
timeInterval += 60;
} }
switch (timeInterval)
{
case 20: scorePerFood = 20;break;
case 80: scorePerFood = 15;break;
case 140: scorePerFood = 12;break;
case 200: scorePerFood = 10;break;
case 260: scorePerFood = 8;break;
case 320: scorePerFood = 6;break;
case 380: scorePerFood = 4;break;
case 440: scorePerFood = 2;break;
default: scorePerFood = 0;break;
}
Sleep(timeInterval);
snakeMove();
}
}
void welcomePage(void)
{
setPosition(35,2);
printf("Welcome to Snake");
setPosition(15, 5);
printf("Rules:");
setPosition(15, 7);
printf("1. Use ¡ü.¡ý.¡û.¡ú to control the movement of the Snake.");
setPosition(15, 9);
printf("2. Biting the snake itself is forbidden.");
setPosition(15, 11);
printf("3. Hit the wall is forbidden.");
setPosition(15, 13);
printf("Developeder: zhaoyu.");
setPosition(15, 15);
printf("Blog: http://blog.csdn.net/sinat_30046339");
setPosition(28, 23);
printf("Press any key to continue...");
setPosition(0, 28);
getchar();
system("cls");
}
void gameEnd(void)
{
system("cls");
setPosition(32, 10);
switch(gameStatus)
{
case 1:
printf("You hit the wall!");
break;
case 2:
printf("You bit yourself!");
break;
case 3:
printf("You chose to end the game.");
break;
default:
break;
}
setPosition(32, 14);
printf("Your final score is %d", score);
getchar();
setPosition(0, 25);
exit(0);
}
void gameStart(void)
{
system("mode con cols=100 lines=30");//no space around equal sign
welcomePage();
creatInterface();
initializeSnake();
creatFood();
} //Main Function
int main(void)
{
hideCursor();
gameStart();
gameCircle();
gameEnd();
return 0;
}
C语言实现贪吃蛇源码的更多相关文章
- Winfrom 极简版贪吃蛇源码
该源码是我在百度知识库借助前辈的的经验,加上自己的一点小改动写的一个非常简陋的贪吃蛇小程序.如果你们有更好的改动方案,欢迎评论. 进入主题吧! 1.创建一个桌面应运程序,拖一个定时器控件.这样,程序界 ...
- c# 贪吃蛇源码
using UnityEngine; using System.Collections;using System.Diagnostics;using UnityEngine.SceneManageme ...
- H5 贪吃蛇源码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js贪吃蛇源码
1.注意,自己引入jquery,这个demo基于jquery的,我的jquery是写的本地的 2.没有写注释,看不懂的再问我吧, <!DOCTYPE html><html> & ...
- 使用strace查看C语言级别的php源码
XCACHE XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接 ...
- c语言版贪吃蛇小游戏
编译环境:windows 7 64位 编译工具:codeblocks 13.12 备注:未使用graphics.h 声明:个人原创,未经允许,禁止转载!!! 数据结构:双向链表 1.程序未使用grap ...
- C语言实现贪吃蛇
日期:2018.9.11 用时:150min 项目:贪吃蛇(C语言--数组 结构体实现) 开发工具:vs2013 关键知识:数组,结构体,图形库,键位操作 源代码: #include<std ...
- 小迪安全 Web安全 基础入门 第六天 - 信息打点-Web架构篇&域名&语言&中间件&数据库&系统&源码获取
一 . Web架构 语言.常用的Web开发语言有PHP,Java,Python,JavaScript,.net等.具体可参考w3school的介绍. 中间件. (1)常见的Web服务器中间件:IIS. ...
- C语言之贪吃蛇
利用链表的贪吃蛇,感觉自己写的时候还是有很多东西不熟悉, 1.预编译 2.很多关于系统的头文件也不是很熟悉 3.关于内存 第一个是.h头文件 #ifndef _SNAKE_H_H_H #define ...
随机推荐
- C#中使用Log4net日志输出到本地文件、Textbox或Listview
网上很多配置log4net的方法,但是排行靠前的 根本就没有说明清除,导致浪费了两个小时来搞清楚如何配置,真是无语,特写此文,给那些刚接触log4net的朋友 1.参考链接:http://blog.s ...
- 如何优化 FineUI 控件库的性能,减少 80% 的数据上传量!
在开始正文之前,请帮忙为当前排名前 10 唯一的 .Net 开源软件 FineUI 投一票: 投票地址: https://code.csdn.net/2013OSSurvey/gitop/codevo ...
- QDir的mkdir和mkpath区别
mkdir:上层目录不存在时,创建会失败.比如创建“c:\\test\test”,如果test不存在,那test也创建不了.目录已经存在时会返回false. mkpath:上层目录不存在也没关系,自动 ...
- .Net分布式异常报警系统-服务端Service
服务端的2个Service 1. HandleService: 从Redis中获取异常信息, 入库并发送通知到相关责任人. 2. HealthyCheckService: 对站点指定页面进行模拟访问 ...
- linux 内存清理/释放命令
1.清理前内存使用情况 free -m 2.开始清理 echo 1 > /proc/sys/vm/drop_caches 3.清理后内存使用情况 free -m 4.完成! 查看内存条数命令: ...
- [BZOJ1264][AHOI2006]Match(DP+树状数组)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1264 分析: 考虑做一般的LCS的时候,更新结果的条件是a[i]==b[j]时候 于是 ...
- 关于document.getElement获取元素返回值的问题
获取网页元素有很多种方法,如下: document.all[];返回HTMLElement对象 document.all.tags[];返回NodeList对象,类似数组 document.getEl ...
- python内置数据类型-字典和列表的排序 python BIT sort——dict and list
python中字典按键或键值排序(我转!) 一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...
- redis HA高可用方案Sentinel和shard
1.搭建redis-master.redis-slave以及seninel哨兵监控 在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的” ...
- 第十五章:输入和输出(I/O)
一:流分类 抽象基类:InputStream和Reader 抽象类不能用于创建模板哦! OutputStream和Writer 方向: 以内存为中心! 输入流(读) 输出流(写) 数据 ...