贪吃蛇(c++版)
這個小游戲是博主一年前寫的,大概是2020年1月份左右,現在發在csdn里面記錄一下生活,當時c++功底很low,雖然現在也很low,所以寫得很簡陋,還請各位大佬多多諒解小白
第一步:墻的cpp代碼
#include"wall.h"void Wall::initWall() {for (int i = 0; i < ROW; i++){for (int j = 0; j < COL; j++){if (i == 0 || j == 0 || i == ROW - 1 || j == COL - 1||((i == (ROW-1)/2)&&(j>=(COL - 1)/5 && j<= (COL - 1)/1.3))|| ((j == (COL - 1) / 2) && (i >= (ROW - 1) /5 && i <= (ROW - 1) / 1.3))|| ((i == (ROW - 1) /5) && (j >= (COL - 1) / 3 && j <= (COL - 1) /1.5)) || ((i == (ROW - 1) / 1.3) && (j >= (COL - 1) / 5 && j <= (COL - 1) / 1.3))){gameArray[i][j] = '*';}else {gameArray[i][j] = ' ';}}} }void Wall::drawwall() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {cout << gameArray[i][j]<<" ";}if (i == 5) {cout << "英文鍵盤下:d鍵,w鍵,s鍵開始!come on!";}if (i == 6) {cout << "a鍵向左";}if (i == 7) {cout << "d鍵向右";} if (i == 8) {cout << "w鍵向上";}if (i == 11) {cout << "制作人:尋夢 ";}if (i == 9) {cout << "s鍵向下";}cout << endl;}}void Wall::setwall(int x, int y,char c) {gameArray[x][y] = c; }char Wall::getwall(int x,int y) {return gameArray[x][y];}第二步:墻的頭文件代碼
#pragma once #ifndef WALL #define WALL #include<iostream> using namespace std; class Wall { public:enum {ROW = 26,COL =26};void initWall();void drawwall();void setwall(int x, int y, char c);char getwall(int x, int y); private:char gameArray[ROW][COL];};#endif WALL第三步:關于蛇的一系列cpp代碼
#include "snake.h" #include<windows.h>using namespace std;/*光標定位代碼1*/ void gotoxy(HANDLE hOut, int x, int y) {COORD c;c.X = x;c.Y = y;SetConsoleCursorPosition(hOut, c); } HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);Snake::Snake(Wall&tempwall,Food & tempfood):wall(tempwall),food(tempfood) {pHead = NULL;isrool = false; }void Snake::initSnake() {destroyPoint();addPoint(5, 3);addPoint(5, 4);addPoint(5,5); }void Snake::destroyPoint() {Point* pcur = pHead;while (pHead != NULL) {pcur = pHead->next;delete pHead;pHead = pcur;}}void Snake::addPoint(int x, int y) {Point* newpoint = new Point;newpoint->x = x;newpoint->y = y;newpoint->next = NULL;if (pHead != NULL) {wall.setwall(pHead->x, pHead->y, '=');gotoxy( hOut, pHead->y*2, pHead->x);cout << "=";}newpoint->next = pHead;pHead = newpoint;wall.setwall(pHead->x, pHead->y, '@');gotoxy(hOut, pHead->y * 2, pHead->x);cout << "@"; }void Snake::delpoint() {if (pHead == NULL || pHead->next == NULL){return;}Point* pcur = pHead->next;Point* pre = pHead;while (pcur->next != NULL) {pre = pre->next;pcur = pcur->next;}wall.setwall(pcur->x, pcur->y, ' ');gotoxy(hOut, pcur->y * 2, pcur->x);cout << " ";delete pcur;pcur = NULL;pre->next = NULL; }bool Snake::move(char keys) {int x = pHead->x;int y = pHead->y; switch (keys){case UP:x--;break;case DOWN:x++;break;case RIGHT:y++;break;case LEFT:y--;break;}Point* pcur = pHead->next;Point* pre = pHead;while (pcur->next != NULL) {pre = pre->next;pcur = pcur->next;}if (pcur->x == x && pcur->y == y) {isrool = true;}else {if (wall.getwall(x, y) == '*' || wall.getwall(x, y) == '='){addPoint(x, y);delpoint();system("cls");wall.drawwall();cout << "你的得分為:" << getscore() << "分" << endl;if (getscore() <= 300) {cout << "再接再厲哦!";}else if (500 > getscore() >300) {cout << "恭喜你呀,超過了百分之四十的人,繼續加油!";}else {cout << "你真優秀!"<<endl;}cout << "GAME OVER!!" << endl;return false;}}if (wall.getwall(x, y) == '#'){addPoint(x, y);food.setfood();}else {addPoint(x, y);delpoint();if (isrool == true){wall.setwall(x, y, '@');gotoxy(hOut, y * 2, x);cout << "@";}}return true;}int Snake::getSleeptime() {int sleeptime = 0;int size = countlist();if (size < 4) {sleeptime = 180;}else if (size >=4 && size <= 15) {sleeptime = 160;}else if (size > 15 && size <=20) {sleeptime = 155;}else if (size > 20 && size <=25) {sleeptime = 150;}else if (size >25 && size <= 40) {sleeptime = 145;}else if (size >40 && size <= 50) {sleeptime = 138;}else if(size > 50 && size <= 80) {sleeptime = 130;}else if (size >80 && size <= 90) {sleeptime = 125;}else if (size > 90 && size <=100) {sleeptime = 120;}else if (size > 100 && size <= 120) {sleeptime = 110;}else {sleeptime = 100;}return sleeptime; }int Snake::countlist() {int bodylong = 0;Point* curpoint = pHead;while (curpoint != NULL){bodylong++;curpoint = curpoint->next;}return bodylong; }int Snake::getscore() {int size = countlist();int score = (size-3) * 10;return score; }第四步:蛇的頭文件代碼
#pragma once #include<iostream> #include"wall.h" #include"foods.h"using namespace std; class Snake { public:Snake(Wall&tempwall,Food& food);enum {UP='w',DOWN='s',LEFT='a',RIGHT='d',};struct Point{int x;int y;Point* next;};void initSnake();void destroyPoint();void addPoint(int x,int y);void delpoint();bool move(char keys);int getSleeptime();int countlist();int getscore();Point* pHead;Wall& wall;Food & food;bool isrool; };第五步:食物的cpp代碼
#include"foods.h" #include<Windows.h> void gotoxy2(HANDLE hOut2, int x, int y) {COORD c;c.X = x;c.Y = y;SetConsoleCursorPosition(hOut2, c); } HANDLE hOut2 = GetStdHandle(STD_OUTPUT_HANDLE);Food::Food(Wall& tempWall):wall(tempWall) {}void Food::setfood() {while (true) {xfood = rand() % (Wall::ROW - 2) + 1;yfood = rand() % (Wall::COL - 2) + 1;if (wall.getwall(xfood, yfood) == ' '){wall.setwall(xfood, yfood, '#');gotoxy2( hOut2, yfood*2, xfood);cout << "#";break;}}}第六步:食物的頭文件代碼
#pragma once #include"wall.h" #include<iostream> using namespace std;class Food { public:Food(Wall& tempWall);void setfood();int xfood;int yfood;Wall& wall;private:};第七步:主函數代碼
#define CRT #include<iostream> using namespace std; #include"wall.h" #include"snake.h" #include"foods.h" #include<ctime> #include<conio.h> #include<Windows.h> void gotoxy1(HANDLE hOut1, int x, int y) {COORD c;c.X = x;c.Y = y;SetConsoleCursorPosition(hOut1, c); } HANDLE hOut1 = GetStdHandle(STD_OUTPUT_HANDLE);/*光標定位代碼2*/ /*void gotoxy(int x,int y){COORD c;c.X = x - 1;c.Y = y - 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);}*/int main() {l:char ctn = 'y';srand((unsigned int)time(NULL));bool isDead = false;char prekeys = NULL;Wall wall;wall.initWall();wall.drawwall();Food food(wall);food.setfood();Snake snake(wall, food);snake.initSnake();gotoxy1(hOut1, 0, Wall::ROW);cout << "你的得分為:" << snake.getscore() << "分" << endl;while (!isDead) {char keys = _getch();if (keys == snake.LEFT && prekeys == NULL){continue;}do{if (keys == snake.UP || keys == snake.DOWN || keys == snake.RIGHT || keys == snake.LEFT){if ((keys == snake.LEFT && prekeys == snake.RIGHT) || (keys == snake.RIGHT && prekeys == snake.LEFT)|| (keys == snake.DOWN && prekeys == snake.UP) || (keys == snake.UP && prekeys == snake.DOWN)){keys = prekeys;}else {prekeys = keys;}if (snake.move(keys) == true){//system("cls");//wall.drawwall();gotoxy1(hOut1, 0, Wall::ROW);cout << "你的得分為:" << snake.getscore() << "分"<<endl;if (snake.getscore() <=300) {cout << "再接再厲哦!" << endl;}else if (500 >= snake.getscore() > 300) {cout << "恭喜你呀,超過了百分之四十的人,繼續加油!" << endl;}else {cout << "你真是太優秀了!" << endl;}Sleep(snake.getSleeptime()); //毫秒}else {isDead = true;break;}}else {keys = prekeys;}} while (!_kbhit());//_kbhit當沒有鍵盤輸入時返回0;}system("pause");system("cls");goto l;system("pause");} 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 常用ARM指令总结(未完待续)
- 下一篇: 链表操作总结