資源簡介
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?????gotoxy(hOut?0?Y_BORDER);
????????for?(int?i?=?0;?i?????for?(int?i?=?0;?i?????????gotoxy(hOut?0?i);?putchar(‘X‘);}
????for?(int?i?=?0;?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:?;
????????????}
????????}
???????
- 上一篇:學生管理系統 根據數據結構的鏈表知識
- 下一篇:常用顏色大全中英文對照。
評論
共有 條評論