资源简介
retro_snake.c
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#define X_BORDER 70
#define Y_BORDER 30
#define INTERVAL 150
#define FAST_INTERVAL 50
void gotoxy(HANDLE int int);
void drawBorderUI(HANDLE);
void gameStart(HANDLE);
void initMoveSnake(HANDLE int);
void generateBean(HANDLE int);
void gameOver(HANDLE);
struct beans {
int x;
int y;
};
struct snakeNode {
int x;
int y;
};
struct beans bean;
struct snakeNode node[X_BORDER * Y_BORDER];
int score = 0 highscore = 0 snakeLength = 2 retry = 0;
/******************************************/
int main(int argc char* argv[])
{
int direction = 0;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
gameStart(hOut);
RETRY:
drawBorderUI(hOut);
initMoveSnake(hOut direction);
if (retry) {
system(“cls“);
goto RETRY;
}
gotoxy(hOut 0 Y_BORDER + 1);
return 0;
}
void gotoxy(HANDLE hOut int x int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut pos);
}
void gameStart(HANDLE hOut)
{
CONSOLE_CURSOR_INFO cursor_info = {1 0};
SetConsoleCursorInfo(hOut &cursor_info); // hide cursor
gotoxy(hOut X_BORDER / 2 - 11 Y_BORDER / 2);
printf(“Press Space to Start Game“);
gotoxy(hOut 0 0);
while (getch() != VK_SPACE) ;
system(“cls“);
}
void drawBorderUI(HANDLE hOut)
{
gotoxy(hOut 0 0);
for (int i = 0; i < X_BORDER + 1; i++) putchar(‘X‘);
gotoxy(hOut 0 Y_BORDER);
for (int i = 0; i < X_BORDER + 1; i++) putchar(‘X‘);
for (int i = 0; i < Y_BORDER + 1; i++) {
gotoxy(hOut 0 i); putchar(‘X‘);}
for (int i = 0; i < Y_BORDER + 1; i++) {
gotoxy(hOut X_BORDER i); putchar(‘X‘);}
gotoxy(hOut X_BORDER + 5 Y_BORDER / 2 - 3);
printf(“W S A D to Control Snake “);
gotoxy(hOut X_BORDER + 5 Y_BORDER / 2 - 1);
printf(“ Space to Switch Speed“);
gotoxy(hOut X_BORDER + 5 Y_BORDER / 2 + 1);
printf(“ Highscore %d“ highscore);
}
void initMoveSnake(HANDLE hOut int direction)
{
int fast = 0;
memset(node 0 sizeof(node));
gotoxy(hOut X_BORDER / 2 - 1 Y_BORDER / 2);
putchar(‘O‘);
putchar(‘@‘);
node[0].x = X_BORDER / 2;
node[1].x = X_BORDER / 2 - 1;
node[0].y = node[1].y = Y_BORDER / 2;
direction = 6; // 6 right 2 down 8 up 4 left
generateBean(hOut snakeLength);
for (; ;) {
while (kbhit()) {
switch (getch()) {
case ‘w‘: if (direction != 2) direction = 8; break;
case ‘s‘: if (direction != 8) direction = 2; break;
case ‘a‘: if (direction != 6) direction = 4; break;
case ‘d‘: if (direction != 4) direction = 6; break;
case VK_SPACE: fast = 1 - fast;
default: ;
}
}
- 上一篇:学生管理系统 根据数据结构的链表知识
- 下一篇:常用颜色大全中英文对照。
相关资源
- f83s72.pdf
- haimianjie2012_10485189.zip
- WeatherForecast.zip
- 世界河流_简化.shp
- DataVisualization-master.zip
- sigar.txt
- spc统计过程控制软件破解版.XLS
- license_R2015b.lic
- NVIDIA楂樼鏄惧崱PCB鏂囦欢GF106鎸_2涓
- LEGEND.rar
- guosenyuan_1539042.zip
- xilinxvivado2017.1版安装包20GBlicense支持
- RFID.zip
- 文件过大,请参考这里.docx
- generateUserBankInfo.rar
- 驾校任务书.doc
- nr24c6.rar
- rls.doc.doc
- DES加解密演算法.rar
- SceneTest.zip
- readAPI破解key加源码.zip
- 小牛大数据资源分享.txt
- 碳足迹计算公式及注释.pdf
- ssmshiro.rar
- [北派心皇]邮箱伪造器.zip
- networkViewDEMO.unitypackage
- H5棋牌游戏.txt
- Sniffer_CardTool超级卡软件.exe
- oppo密码破解工具,云盘地址.txt
- _冯建.rar
评论
共有 条评论