当前位置:首页 >> 建筑/土木 >>

简单Snake源代码


main.C 源代码: #include <stdio.h> #include "pcc32.h" #define FALSE 0 #define TURE 1 #define OVER 2 #define WIDTH 22 #define HEIGH 18 #define BASETIME 300 #define TIME(x) BASETIME

-23*(x) enum DIR{ LEFT, RIGHT, UP, DOWN }; struct COOR{ uint8 x; uint8 y; }; struct SNAKE{ uint16 length; struct COOR body[328]; enum DIR dir; }; struct SNAKE snake={0,{0,0},0}; struct COOR food; uint16 score=0; void Ready(); void Play(); void Over(); void Wall(); void InitSnake(); void InitFood(); uint8 IsOver(uint8 x, uint8 y); uint8 InSnake(uint8 x, uint8 y); uint8 MoveSnake();

void Ready() { printf("说明:\n"); printf("开始游戏前请将输入法切换成英文输入法。\n"); printf("↑↓←→分别控制蛇上下左右移动。\n"); printf("回车键暂停或继续游戏。\n"); printf("\n\n 请按任意键开始游戏"); jkGetKey(); } void Play() { uint8 level=1; system("cls"); Wall(); setTextColor(YELLOW); InitSnake(); InitFood(); do { if(level<10) level=(snake.length+5)/10; gotoTextPos(0,HEIGH+1); printf("SCORE: %5u LEVEL: %u",score,level); sleep(TIME(level)); if(jkHasKey()) switch(jkGetKey()) { case JK_LEFT: if(snake.dir!=RIGHT) snake.dir=LEFT; break; case JK_RIGHT: if(snake.dir!=LEFT) snake.dir=RIGHT; break; case JK_UP: if(snake.dir!=DOWN) snake.dir=UP; break; case JK_DOWN: if(snake.dir!=UP) snake.dir=DOWN; break;

case JK_ENTER: for(;jkGetKey()!=JK_ENTER;); break; default:; } }while(MoveSnake()); } void Over() { gotoTextPos(WIDTH-10,HEIGH/2-1); setTextColor(WHITE); printf("Your score is:%u",score); sleep(1500); jkGetKey(); } void Wall() { uint8 x=0,y=0; setTextColor(RED_ORANGE); gotoTextPos(0,0); while(x++<WIDTH) printf("■"); while(++y<HEIGH) { gotoTextPos(0,y); printf("■"); gotoTextPos((WIDTH-1)*2,y); printf("■"); } gotoTextPos(0,HEIGH-1); while(--x>0) printf("■"); } void InitFood() { srand((unsigned)time(NULL)); do { food.x=rand()%(WIDTH-2)+1; food.y=rand()%(HEIGH-2)+1; }while(InSnake(food.x, food.y)); gotoTextPos(food.x*2, food.y ); printf("●"); }

void InitSnake() { uint8 i=0; snake.length=5; snake.dir=RIGHT; setTextColor(YELLOW); do { snake.body[i].x=5-i; snake.body[i].y=4; gotoTextPos(snake.body[i].x*2,snake.body[i].y); printf("■"); }while(++i<snake.length); } uint8 IsOver(uint8 x,uint8 y) { if(InSnake(x,y)||0==x||WIDTH-1==x||0==y||HEIGH-1==y) return TURE; return FALSE; } uint8 InSnake(uint8 x, uint8 y) { uint8 i= snake.length; while(i--) if(snake.body[i].x==x&&snake.body[i].y==y) return TRUE; return FALSE; } uint8 MoveSnake() { uint8 i,x=snake.body[0].x, y=snake.body[0].y; switch(snake.dir) { case LEFT: --x; break; case RIGHT: ++x; break; case UP: --y; break; default: ++y; } if(x==food.x&&y==food.y) { ++snake.length; InitFood(); score+=50; }

else { gotoTextPos(snake.body[snake.length-1].x*2,snake.body[snake.length-1].y); printf(" "); } gotoTextPos(x*2,y); printf("■"); for(i=snake.length-1;i>0;--i) snake.body[i]=snake.body[i-1]; if(IsOver(x,y)) return FALSE; snake.body[i].x=x; snake.body[i].y=y; return TRUE; } int main(void) { setConsoleTitle("Snake 贪吃蛇 貌似掉线 QQ 群:31066229"); setCursorVisible(0); fixConsoleSize(WIDTH*2,HEIGH+2); Ready(); Play(); Over(); return 0; }

pcc32.h 源代码: #ifndef PCC32_H_INCLUDED #define PCC32_H_INCLUDED #include <stdlib.h> #include <windows.h> typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; /* 按键定义 */ #define JK_FUNC_KEY 0x00 #define JK_CTRL_KEY 0xE0 #define JK_ESC 0x001B #define JK_ENTER 0x000D #define JK_SPACE 0x0020

#define JK_BKSPACE 0x0008 #define JK_TAB 0x0009 #define JK_CTRL_Z 0x001A #define JK_CTRL_X 0x0018 #define JK_CTRL_C 0x0003 #define JK_CTRL_A 0x0001 #define JK_CTRL_S 0x0013 #define JK_CTRL_D 0x0004 #define JK_LEFT 0xE04B #define JK_RIGHT 0xE04D #define JK_UP 0xE048 #define JK_DOWN 0xE050 #define JK_INSERT 0xE052 #define JK_HOME 0xE047 #define JK_PGUP 0xE049 #define JK_DELETE 0xE053 #define JK_END 0xE04F #define JK_PGDW 0xE051 #define JK_F1 0xFF3B #define JK_F2 0xFF3C #define JK_F3 0xFF3D #define JK_F4 0xFF3E #define JK_F5 0xFF3F #define JK_F6 0xFF40 #define JK_F7 0xFF41 #define JK_F8 0xFF42 #define JK_F9 0xFF43 #define JK_F10 0xFF44 #define JK_F11 0xE085 #define JK_F12 0xE086 /* 控制台尺寸定义 */ #define MIN_CONSOLE_WIDTH 14 #define MIN_CONSOLE_HEIGHT 1 /* 颜色定义 */ typedef enum _PCCOLOR { BLACK = 0, BLUE = 1, DARK_GREEN = 2, LIGHT_BLUE = 3, RED = 4, PURPLE = 5, ORANGE = 6, GREY = 7,

DARKER_GREY = 8, MEDIUM_BLUE = 9, LIGHT_GREEN = 10, TEAL = 11, RED_ORANGE = 12, LIGHT_PURPLE = 13, YELLOW = 14, WHITE = 15 }PCCOLOR; #ifdef __cplusplus extern "C" { #endif /* 延时,以毫秒计 */ void delayMS(uint32 d); /* 清除文字 */ void clearText(void); /* 发出简单的响铃声(阻塞,慎用) */ int simpleRing(uint16 freq, uint16 len); /* 设置文本颜色,0~15 */ int setTextColor(uint8 fColor); /* 获取文本颜色,0~15 */ PCCOLOR getTextColor(void); /* 设置文本背景颜色,0~15 */ int setBackColor(uint8 bColor); /* 获取文本背景颜色,0~15 */ PCCOLOR getBackColor(void); /* 设置文本及其背景颜色,0~15 */ int setColors(uint8 fColor, uint8 bColor); /* 设置/取消前/背景颜色的交换解析 */ int setSwapColors(int b); /* 设定/取消文字的下划线 */ int setUnderLine(int b); /* 获取控制台文本行的最大长度[默认为 80] */ uint8 getLineWidth(void); /* 获取控制台文本行的最大行数 */ uint8 getLineNum(void); /* 获取光标的横坐标[列数] */ uint8 getCursorX(void); /* 获取光标的纵坐标[行数] */ uint8 getCursorY(void); /* 屏幕光标定位,x 为列(横),y 为行(纵) */ int gotoTextPos(uint8 x, uint8 y); /* 设置光标的可见性 */ int setCursorVisible(int b);

/* 设置光标的(厚度)尺寸,1-100 */ int setCursorSize(uint8 s); /* 获取控制台的标题字符串 */ int getConsoleTitle(char *title, uint8 len); /* 设置控制台的标题字符串 */ int setConsoleTitle(char *title); /* 设置一个没有滚动条的控制台窗口尺寸 */ int fixConsoleSize(uint16 width, uint16 height); /* 输出控制台的相关信息(仅作调试用) */ int showConsoleInfo(); /* 获取控制台的键输入 */ uint16 jkGetKey(void); /* 判断控制台是否有键按下 */ int jkHasKey(void); #ifdef __cplusplus } #endif #endif // PCC32_H_INCLUDED void delayMS(uint32 d) { Sleep(d); return ; } void clearText(void) { system("cls"); return ; } int setTextColor(uint8 fColor) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hd, &csbInfo); return SetConsoleTextAttribute(hd, fColor | (csbInfo.wAttributes&~0x0F)); } PCCOLOR getTextColor(void) { CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo); return (PCCOLOR)(csbInfo.wAttributes&0x0F); } int setBackColor(uint8 bColor) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hd, &csbInfo); return SetConsoleTextAttribute(hd, (bColor << 4) | (csbInfo.wAttributes&~0xF0)); } PCCOLOR getBackColor(void) { CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo); return (PCCOLOR)((csbInfo.wAttributes&0xF0) >> 4); } int setColors(uint8 fColor, uint8 bColor) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hd, &csbInfo); return SetConsoleTextAttribute(hd, fColor | (bColor << 4) (csbInfo.wAttributes&~0xFF)); } int setSwapColors(int b) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hd, &csbInfo); if (!!b) return SetConsoleTextAttribute(hd, csbInfo.wAttributes | 0x4000); else return SetConsoleTextAttribute(hd, csbInfo.wAttributes & ~0x4000); } int setUnderLine(int b) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hd, &csbInfo); if (!!b) return SetConsoleTextAttribute(hd, csbInfo.wAttributes | 0x8000); else return SetConsoleTextAttribute(hd, csbInfo.wAttributes & ~0x8000); } uint8 getLineWidth(void) { CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo); return csbInfo.dwSize.X; }

|

uint8 getLineNum(void) { CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo); return csbInfo.dwSize.Y; } uint8 getCursorX(void) { CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo); return csbInfo.dwCursorPosition.X; } uint8 getCursorY(void) { CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo); return csbInfo.dwCursorPosition.Y; } int gotoTextPos(uint8 x, uint8 y) { COORD cd; cd.X = x; cd.Y = y; return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd); } int setCursorVisible(int b) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ccInfo; GetConsoleCursorInfo(hd, &ccInfo); ccInfo.bVisible = !!b; return SetConsoleCursorInfo(hd, &ccInfo); } int setCursorSize(uint8 s) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ccInfo; GetConsoleCursorInfo(hd, &ccInfo); ccInfo.dwSize = s; return SetConsoleCursorInfo(hd, &ccInfo); } int getConsoleTitle(char *title, uint8 len) { return GetConsoleTitle(title, len);

} int setConsoleTitle(char *title) { return SetConsoleTitle(title); } int fixConsoleSize(uint16 width, uint16 height) { int ret = 0; int fixX = 0, fixY = 0; COORD cd; SMALL_RECT srctWindow; CONSOLE_SCREEN_BUFFER_INFO csbiInfo; HANDLE hWin = GetStdHandle(STD_OUTPUT_HANDLE); if (!hWin) return 0; // 调整最小范围 fixX = (width < MIN_CONSOLE_WIDTH) ? MIN_CONSOLE_WIDTH : width; fixY = (height < MIN_CONSOLE_HEIGHT) ? MIN_CONSOLE_HEIGHT : height; // 先将 BUF 尺寸扩大到最大 cd.X = 512; cd.Y = 512; ret = SetConsoleScreenBufferSize(hWin, cd); if (!ret) return ret; //puts("Set Max Buf Error."); // 测试屏幕允许的最大尺寸 GetConsoleScreenBufferInfo(hWin, &csbiInfo); cd = csbiInfo.dwMaximumWindowSize; //printf("Max Win Size[%d, %d].\n", cd.X, cd.Y); fixX = (fixX > cd.X) ? cd.X : fixX; fixY = (fixY > cd.Y) ? cd.Y : fixY; //printf("Fix Win Size[%d, %d].\n", fixX, fixY); // 确定适当的窗口尺寸 srctWindow.Left = 0; srctWindow.Right = fixX - 1; srctWindow.Top = 0; srctWindow.Bottom = fixY - 1; ret = SetConsoleWindowInfo(hWin, 1, &srctWindow); if (!ret) return ret; //puts("Set Size Error."); // 确定适当的 BUF 尺寸 cd.X = fixX; cd.Y = fixY;

ret = SetConsoleScreenBufferSize(hWin, cd); if (!ret) return ret; //puts("Set Buff Error."); //printf("Fix Win Size[%d, %d]: %d.\n", fixX, fixY, ret); return ret; } int showConsoleInfo() { int ret; HANDLE hWin = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbiInfo; ret = GetConsoleScreenBufferInfo(hWin, &csbiInfo); if (ret) { printf("Buffer Size: [X]%d - [Y]%d\n", csbiInfo.dwSize.X, csbiInfo.dwSize.Y); printf("Cursor Pos : [X]%d - [Y]%d\n", csbiInfo.dwCursorPosition.X, csbiInfo.dwCursorPosition.Y); printf("Attributes : %d\n", csbiInfo.wAttributes); printf("Current Win: [L]%d - [R]%d - [T]%d - [B]%d\n", \ csbiInfo.srWindow.Left, csbiInfo.srWindow.Right, csbiInfo.srWindow.Top, csbiInfo.srWindow.Bottom); printf("Maximum Win: [X]%d - [Y]%d\n", csbiInfo.dwMaximumWindowSize.X, csbiInfo.dwMaximumWindowSize.Y); puts("Over."); } return ret; } int simpleRing(uint16 freq, uint16 len) { return Beep(freq, len); } uint16 jkGetKey(void) { uint16 rk = 0; uint8 k = getch(); if (k == JK_FUNC_KEY) { k = getch(); rk = 0xFF00 | k; return rk; } if (k == JK_CTRL_KEY) {

k = getch(); rk = 0xE000 | k; return rk; } rk = 0x0000 | k; return rk; } int jkHasKey(void) { return (kbhit()); } //End of pcc32.c


相关文章:
简单Snake源代码
简单Snake源代码简单Snake源代码隐藏>> main.C 源代码: #include <stdio.h> #include "pcc32.h" #define FALSE 0 #define TURE 1 #define OVER 2 #define...
C语言最简洁的贪吃蛇源代码
[22]; int snakelength=0; struct snake{ public: int x=0; int y=0; ...贪吃蛇C语言源代码 8页 1下载券 一个C语言写的简单贪吃蛇... 8页 免费 贪吃...
贪吃蛇简易代码
#include<windows.h> #include "resource1.h" #include <stdio.h> #include <stdlib.h> #include //蛇的结构体 typedef struct SNAKE { int x; int y;...
贪吃蛇源代码
贪吃蛇源代码.txt 这世界上除了我谁都没资格陪在你身边。 听着,我允许你喜欢...(snake.direction!=2) snake.direction=1; break; case LEFT: if(snake....
贪吃蛇源代码
贪吃蛇源代码_计算机软件及应用_IT/计算机_专业资料。贪吃蛇C语言源代码,可执行程序...(snake.direction!=2) snake.direction=1; break; case LEFT: if(snake....
贪吃蛇游戏实现思路及源代码
贪吃蛇游戏实现思路及源代码_计算机软件及应用_IT/计算机...如果只是一个方块的话显然很简单。但是当蛇的 长度...snake.push(head); window.addEventListener('keydown...
贪吃蛇小游戏源代码
贪吃蛇小游戏源代码_计算机软件及应用_IT/计算机_专业资料。/*贪吃蛇*/ #...("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n"); printf("...
游戏源码
但是如果连这样简单的编程环境都不了解,如何 能熟练的使用更为高级的编程工具!...代码*/ { int key,i,last_x,last_y; snake.jie=2; score=0; snake....
C语言代码
简单的C语言代码 8页 1下载券 C语言代码9 6页 1下载券C​语​言​代...{ snake[i][0]=snake[i-1][0]; snake[i][1]=snake[i-1][1]; ...
贪吃蛇源码分析
Android ---snake 源代码分析代码结构分析: Snake : 主游戏窗口 SnakeView : ...这是一个包括两个参数,用于记录 X 轴和 Y 轴简单类,其中包括一个比较 函数...
更多相关标签:
java简单小游戏源代码 | 安卓简单小游戏源代码 | java简单计算器源代码 | html简单网页源代码 | 简单文本编辑器源代码 | ios简单小游戏源代码 | 一个简单的网页源代码 | java简单记事本源代码 |