本文主要是介绍C和C++混写的贪吃蛇,巴适的板,直接COPY运行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
自己写的贪吃蛇代码
先上效果图
直接COPY到VS上,运行不起直播吃翔。
目前只有单人模式,空格暂停游戏,ESC退出到主界面
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
void laotie();
void initconsole();
void createMap();
void initSnake();
bool moveSnake();
void keyStatues();
void playGame();
void createFood();
int SnakeRushItSelf();
int SnakeRushWall();
typedef struct Snake
{Snake(int x = 15, int y = 20, Snake* next = nullptr) :x(x), y(y), next(next) {}int x;int y;Snake* next;
}*pSnake;
pSnake head = new Snake();
pSnake food = new Snake();
int flag = 1,sta=1;
using namespace std;void exitCursor()
{CONSOLE_CURSOR_INFO cursor;cursor.dwSize = 100;cursor.bVisible = TRUE;HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorInfo(handle,&cursor);
}
void hideCursor()
{CONSOLE_CURSOR_INFO cursor;cursor.bVisible = FALSE;cursor.dwSize = sizeof(cursor);HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorInfo(handle, &cursor);
}
void gotoxy(int x, int y)
{_COORD cursor;cursor.X = x;cursor.Y = y;HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(handle, cursor);
}
void changColor(int x)
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(handle, x);
}void createMap()
{//〓hideCursor();system("cls");system("mode con cols=120 lines=40");changColor(6);for (int i = 0; i < 40; i++){gotoxy(0, i);cout << "〓";gotoxy(118, i);cout << "〓";}for (int i = 2; i < 118; i++){gotoxy(i, 0);cout << "〓";gotoxy(i, 39);cout << "〓";}
}
void initSnake()
{flag = 1, sta =1;head = new Snake();pSnake pMove = head;for (int i = 0; i < 3; i++){pSnake Node = new Snake();pMove->next = Node;Node->x = pMove->x - 2;pMove = Node; }pMove = head;gotoxy(pMove->x, pMove->y);cout << "●";pMove = pMove->next;while(pMove){gotoxy(pMove->x, pMove->y);cout << "◆";pMove = pMove->next;}
}
bool moveSnake()
{hideCursor();while (1){if (SnakeRushItSelf() == 0||SnakeRushWall()==0){system("cls");initconsole();break;}/*system("cls");*/changColor(11);if (sta == 1)//向右{if (SnakeRushItSelf() == 0){system("cls");initconsole();break;}else{if (head->x == food->x && head->y == food->y){pSnake p = head;pSnake Node = new Snake();Node->x = p->x + 2;Node->y = p->y;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p){gotoxy(p->x, p->y);cout << "◆";p = p->next;}createFood();Sleep(100);}else{pSnake p = head, q;pSnake Node = new Snake();Node->x = p->x + 2;Node->y = p->y;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p->next->next){gotoxy(p->x, p->y);cout << "◆";p = p->next;}q = p->next;p->next = nullptr;gotoxy(p->x, p->y);cout << "◆"; changColor(0);gotoxy(q->x, q->y);cout << "◆";delete(q);Sleep(100);}return true;}}if (sta == 2)//向上{if (head->x == food->x && head->y == food->y){pSnake p = head, q;pSnake Node = new Snake();Node->x = p->x ;Node->y = p->y-1;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p){gotoxy(p->x, p->y);cout << "◆";p = p->next;}createFood();Sleep(100);}else{pSnake p = head, q;pSnake Node = new Snake();Node->y = p->y - 1;Node->x = p->x;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p->next->next){gotoxy(p->x, p->y);cout << "◆";p = p->next;}q = p->next;p->next = nullptr;gotoxy(p->x, p->y);cout << "◆";changColor(0);gotoxy(q->x, q->y);cout << "◆";Sleep(100);free(q);}if (!SnakeRushItSelf()){system("cls");initconsole();break;}return true;}if (sta == 3)//向左{if (head->x == food->x && head->y == food->y){pSnake p = head, q;pSnake Node = new Snake();Node->x = p->x - 2;Node->y = p->y;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p){gotoxy(p->x, p->y);cout << "◆";p = p->next;}createFood();Sleep(100);}else{pSnake p = head, q;pSnake Node = new Snake();Node->x = p->x - 2;Node->y = p->y;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p->next->next){gotoxy(p->x, p->y);cout << "◆";p = p->next;}q = p->next;p->next = nullptr;gotoxy(p->x, p->y);cout << "◆";changColor(0);gotoxy(q->x, q->y);cout << "◆";free(q);Sleep(100);}if (!SnakeRushItSelf()){system("cls");initconsole();break;}return true;}if (sta == 4)//向下{if (head->x == food->x && head->y == food->y){pSnake p = head, q;pSnake Node = new Snake();Node->x = p->x ;Node->y = p->y+1;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p){gotoxy(p->x, p->y);cout << "◆";p = p->next;}createFood();Sleep(100);}else{pSnake p = head, q;pSnake Node = new Snake();Node->y = p->y + 1;Node->x = p->x;Node->next = head;head = Node;//●◆p = head;gotoxy(p->x, p->y);cout << "●";p = p->next;while (p->next->next){gotoxy(p->x, p->y);cout << "◆";p = p->next;}q = p->next;p->next = nullptr;gotoxy(p->x, p->y);cout << "◆";changColor(0);gotoxy(q->x, q->y);cout << "◆";free(q);Sleep(100);}if (!SnakeRushItSelf()){system("cls");initconsole();break;}return true;}}
}void keyStatues()
{while (1){hideCursor();if (GetAsyncKeyState(VK_UP) && sta != 4)sta = 2;elseif (GetAsyncKeyState(VK_DOWN) && sta != 2)sta = 4;elseif (GetAsyncKeyState(VK_LEFT) && sta != 1)sta = 3;elseif (GetAsyncKeyState(VK_RIGHT) && sta != 3)sta = 1;if (GetAsyncKeyState(VK_SPACE)){while (1){Sleep(1000);if (GetAsyncKeyState(VK_SPACE))break;if (GetAsyncKeyState(VK_ESCAPE)){system("cls");initconsole();}}}if (GetAsyncKeyState(VK_ESCAPE)){system("cls");initconsole();break;}if (moveSnake() == 0){system("cls");initconsole();break;}}
}
void playGame()
{system("cls");createMap();initSnake();createFood();keyStatues();}void initconsole()
{changColor(3);exitCursor();gotoxy(5, 16);system("mode con cols=90 lines=30");gotoxy(5, 16);cout << "◆开始游戏";gotoxy(5, 18);cout << "◆退出游戏";gotoxy(5, 16);//laotie();int a = 16;int key = _getch();while (key != 0x0d || key == 0xE0){key = _getch();if (key == 0x48 && a > 16){a -= 2;gotoxy(5, a);}if (key == 0x50 && a < 18){a += 2;gotoxy(5, a);}}switch (a){case 16:playGame(); break;case 18: exit(0); break;}
}
void createFood()
{srand(time(0));pSnake pFood = food;pFood->x = 2 + rand() % 100;while (pFood->x % 2 == 0){pFood->x = 2 + rand() % 100;}pFood->y = 1 + rand() % 38;gotoxy(food->x, food->y);changColor(5);pSnake pMove = head;while (pMove){if (food->x == pMove->x && food->y == pMove->y)createFood();pMove = pMove->next;}cout << "◆";
}
int SnakeRushItSelf()
{pSnake pMove = head->next->next;while (pMove){if (pMove->x == head->x && pMove->y == head->y){flag = 0;return flag;}pMove=pMove->next;}return flag;
}
int SnakeRushWall()
{if (head->x == 1 || head->x == 117 || head->y == 1 || head->y == 39)flag = 0;return flag;
}
int main()
{initconsole();system("pause");return 0;
}
这篇关于C和C++混写的贪吃蛇,巴适的板,直接COPY运行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!