资源简介
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: ;
}
}
- 上一篇:学生管理系统 根据数据结构的链表知识
- 下一篇:常用颜色大全中英文对照。
相关资源
- PID_AutoTune_v0.rar
- vspd7.2.308.zip
- 价值2k的H漫画小说系统
- Pythonamp;课堂amp;笔记(高淇amp;400;集第
- ddos压力测试工具99657
- UML建模大全
- 开源1A锂电池充电板TP4056原理图+PCB
- m1卡 ic卡可选择扇区初始化加密软件
- TSCC.exe
- FTP课程设计(服务端+客户端)
- 计算机图形学 边填充算法实现代码
- 电力系统潮流计算程序集合
- oracle数据迁移项目实施方案
- Web Api 通过文件流 文件到本地
- Visio图标-最新最全的网络通信图标库
- Spire API文档
- OpenGL参考手册
- Python中Numpy库最新教程
- SPD博士V5.3.exe
- 直流无刷电机方波驱动 stm32 例程代码
- layui后台管理模板
- 仿知乎界面小程序源代码
- 云平台-阿里云详细介绍
- photoshop经典1000例
- scratch垃圾分类源码(最终版本).sb
- IAR ARM 7.8破解
- TI CCS V5.4 安装步骤及破解文件
- 松下plc FP-XH的驱动
- 局域网硬件信息收集工具
- 加快Windows XP操作系统开机速度
评论
共有 条评论