先放效果

源代码

//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语言实现贪吃蛇源码的更多相关文章

  1. Winfrom 极简版贪吃蛇源码

    该源码是我在百度知识库借助前辈的的经验,加上自己的一点小改动写的一个非常简陋的贪吃蛇小程序.如果你们有更好的改动方案,欢迎评论. 进入主题吧! 1.创建一个桌面应运程序,拖一个定时器控件.这样,程序界 ...

  2. c# 贪吃蛇源码

    using UnityEngine; using System.Collections;using System.Diagnostics;using UnityEngine.SceneManageme ...

  3. H5 贪吃蛇源码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. js贪吃蛇源码

    1.注意,自己引入jquery,这个demo基于jquery的,我的jquery是写的本地的 2.没有写注释,看不懂的再问我吧, <!DOCTYPE html><html> & ...

  5. 使用strace查看C语言级别的php源码

    XCACHE XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接 ...

  6. c语言版贪吃蛇小游戏

    编译环境:windows 7 64位 编译工具:codeblocks 13.12 备注:未使用graphics.h 声明:个人原创,未经允许,禁止转载!!! 数据结构:双向链表 1.程序未使用grap ...

  7. C语言实现贪吃蛇

    日期:2018.9.11 用时:150min 项目:贪吃蛇(C语言--数组   结构体实现) 开发工具:vs2013 关键知识:数组,结构体,图形库,键位操作 源代码: #include<std ...

  8. 小迪安全 Web安全 基础入门 第六天 - 信息打点-Web架构篇&域名&语言&中间件&数据库&系统&源码获取

    一 . Web架构 语言.常用的Web开发语言有PHP,Java,Python,JavaScript,.net等.具体可参考w3school的介绍. 中间件. (1)常见的Web服务器中间件:IIS. ...

  9. C语言之贪吃蛇

    利用链表的贪吃蛇,感觉自己写的时候还是有很多东西不熟悉, 1.预编译 2.很多关于系统的头文件也不是很熟悉 3.关于内存 第一个是.h头文件 #ifndef _SNAKE_H_H_H #define ...

随机推荐

  1. 建立时间和保持时间(setup time 和 hold time)

    建立时间和保持时间贯穿了整个时序分析过程.只要涉及到同步时序电路,那么必然有上升沿.下降沿采样,那么无法避免setup-time 和 hold-time这两个概念.本文内容相对独立于该系列其他文章,是 ...

  2. 更便捷的Android多渠道打包方式

    本文先回顾了以往流行的多渠道打包方式,随后引入的mcxiaoke的packer-ng-plugin项目,介绍该项目在实际应用(配合友盟统计)中如何解决更方便的Android多渠道打包问题 多渠道打包方 ...

  3. AngularJS中实现无限级联动菜单

    多级联动菜单是常见的前端组件,比如省份-城市联动.高校-学院-专业联动等等.场景虽然常见,但仔细分析起来要实现一个通用的无限分级联动菜单却不一定像想象的那么简单.比如,我们需要考虑子菜单的加载是同步的 ...

  4. 理解IEnumerator+IEnumerable这种接口思想

    前言 本文不想过多篇幅来介绍IEnumerator和IEnumerable这两个接口的具体说明,只是把它作一个例子作引言而已,本文将根据自己的理解来描述微软为何要这样设计这种关联风格的接口.这种风格的 ...

  5. [BZOJ1143][CTSC2008]祭祀river(最长反链)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1143 分析: 最长反链==最小路径覆盖==n-二分图最大匹配数 某神犇对二分图的总结: ...

  6. C# 退出应用程序办法

    Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵   this.Close();//只是关闭当前窗体.   Application.E ...

  7. 用户 'IIS APPPOOL\***' 登录失败

    用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中安装了iis之后添加了我做的网站打开之后提示用户 'IIS APPPOOL\DefaultAppPoo ...

  8. SSH框架 sequence diagram

  9. myeclipse下java文件乱码问题解决

    中文乱码是因为编码格式不一致导致的.1.进入Eclipse,导入一个项目工程,如果项目文件的编码与你的工具编码不一致,将会造成乱码.2.如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文 ...

  10. 屠龙之路_狭路相逢勇者胜_EighthDay

    屠龙天团的少年们追着Alpha恶龙沿路留下的粪便,一路狂奔追到了福州大学生活区三十号楼4层活动室,空气中弥漫着恶龙的臭味!屠龙少年对恶龙的隐身遁迹之术心知肚明,于是点头示意,四下散开.各自拿出了电脑, ...