服务器之家

服务器之家 > 正文

C++控制台循环链表实现贪吃蛇

时间:2021-09-03 14:54     来源/作者:PPPeyton

本文实例为大家分享了C++控制台循环链表实现贪吃蛇的具体代码,供大家参考,具体内容如下

-stdafx.h 为了简化程序定义一些宏和全局变量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef __STDAFX_H__
#define __STDAFX_H__
 
// ============上下左右=============
const int UP = 72;
const int DOWN = 80;
const int LEFT = 75;
const int RIGHT = 77;
 
// ==============宽高===============
#define HEIGHT 20
#define WIDTH 39
 
// ==============输出===============
#define cout_food std::cout<<"*"
#define cout_snake std::cout<<"■"
#define cout_space std::cout << " "
#define cout_snake_xy(x,y) SnakeUI::gotoXY(x,y);cout_snake
#define cout_food_xy(x,y) SnakeUI::gotoXY(x,y);cout_food
#define cout_space_xy(x,y) SnakeUI::gotoXY(x,y);cout_space
 
// =============结束?==============
#define OVER false
#define RUN true
 
#endif

-SnakeUI.h 

主要是初始化UI,初始化蛇,还有生产食物,判断食物和蛇有没有相撞,还有对界面的一些操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef __SNAKE_UI_H__
#define __SNAKE_UI_H__
 
#include <iostream>
#include <Windows.h>
#include "Snake.h"
 
struct Food {
 int x;
 int y;
};
 
class SnakeUI {
public:
 static void initUI();
 static void initSnake();
 static void gotoXY(int x, int y);
 
 static void productFood(Snake& snake);
 static bool meetWithFood(int x, int y);
 
private:
 static Food food;
 
};
 
 
#endif

-SnakeUI.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include "SnakeUI.h"
#include <ctime>
using namespace std;
 
Food SnakeUI::food = { 0, 0 };
 
// init UI
void SnakeUI::initUI() {
 cout << "┏";
 for (int i = 1; i < WIDTH; ++i) cout << "━";
 cout << "┓";
 gotoXY(0, HEIGHT);
 cout << "┗";
 for (int i = 1; i < WIDTH; ++i) cout << "━";
 cout << "┛";
 for (int y = 1; y < HEIGHT; ++y) {
 gotoXY(0, y); cout << "┃";
 gotoXY(WIDTH, y); cout << "┃";
 }
}
 
// init snake: three points
void SnakeUI::initSnake() {
 gotoXY(2, 10); cout_snake;
 gotoXY(3, 10); cout_snake;
 gotoXY(4, 10); cout_snake;
}
 
// goto point(x, y) in console
void SnakeUI::gotoXY(int x, int y) {
 COORD coord = { x * 2, y };
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
 
// random product food
void SnakeUI::productFood(Snake& snake) {
 srand((unsigned)time(NULL));
 int x, y; // x from 1 to 38, y from 1 to 19
 bool productOK;
 for (;;) {
 productOK = true;
 x = rand() % 38 + 1;
 y = rand() % 19 + 1;
 // 不和蛇身碰撞1->检查身体和尾部
 for (SnakeNode* sn = snake.last; sn != snake.first; sn = sn->prev) {
  if (sn->x == x && sn->y == y) {
  productOK = false;
  break;
  }
 }
 // 不和蛇身碰撞2->检查头部
 if (x == snake.first->x && y == snake.first->y)
  productOK = false;
 
 if (productOK)
  break;
 }
 food.x = x;
 food.y = y;
 cout_food_xy(food.x, food.y);
}
 
// is snake's head meet with food?
bool SnakeUI::meetWithFood(int x, int y) {
 return (food.x == x && food.y == y);
}

-Snake.h
蛇类,蛇的移动和状态

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef __SNAKE_H__
#define __SNAKE_H__
 
struct SnakeNode {
 int x;
 int y;
 SnakeNode* prev;
 SnakeNode(int x_t, int y_t){ x = x_t; y = y_t; }
 SnakeNode(){}
};
 
class Snake {
 friend class SnakeUI;
 
public:
 Snake();
 ~Snake();
 bool snakeMove(char& dir);
 
private:
 void getKey(char& dir);
 
private:
 SnakeNode* first;
 SnakeNode* last;
 char state;
};
 
#endif

-Snake.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "stdafx.h"
#include "Snake.h"
#include "SnakeUI.h"
 
Snake::Snake() {
 // 状态:向右
 state = RIGHT;
 // 创建循环链表
 first = new SnakeNode(4, 10);
 last = new SnakeNode(2, 10);
 last->prev = new SnakeNode(3, 10);
 last->prev->prev = first;
 first->prev = last;
 // UI
 SnakeUI::initSnake();
 SnakeUI::productFood(*this);
}
 
Snake::~Snake() {
 SnakeNode* tmp = last;
 while (last != last) {
 last = last->prev;
 delete tmp;
 tmp = last;
 }
 delete last;
}
 
bool Snake::snakeMove(char& dir) {
 int x = first->x;
 int y = first->y;
 getKey(dir);
 // 撞墙->Game Over
 switch (state)
 {
 case UP: --y; if (y == 0) return OVER; break;
 case DOWN: ++y; if (y == HEIGHT) return OVER; break;
 case LEFT: --x; if (x == 0) return OVER; break;
 case RIGHT: ++x; if (x == WIDTH) return OVER; break;
 }
 
 // 撞到了自己
 SnakeNode* tmp = last;
 for (; tmp != first; tmp = tmp->prev) {
 if (first->x == tmp->x && first->y == tmp->y)
  return OVER;
 }
 
 // 吃食物
 if (SnakeUI::meetWithFood(x, y)) {
 SnakeNode* newHead = new SnakeNode(x, y);
 first->prev = newHead;
 newHead->prev = last;
 first = newHead;
 cout_snake_xy(x, y);
 SnakeUI::productFood(*this);
 }
 else {
 cout_space_xy(last->x, last->y);
 last->x = x;
 last->y = y;
 first = last;
 last = last->prev;
 cout_snake_xy(x, y);
 }
 
 return RUN;
}
 
void Snake::getKey(char& dir) {
 switch (dir)
 {
 case UP: if (state == LEFT || state == RIGHT) state = UP; return;
 case DOWN: if (state == LEFT || state == RIGHT) state = DOWN; return;
 case LEFT: if (state == UP || state == DOWN) state = LEFT; return;
 case RIGHT: if (state == UP || state == DOWN) state = RIGHT; return;
 }
}

-main.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include "SnakeUI.h"
#include "Snake.h"
#include <iostream>
#include <conio.h>
using namespace std;
 
DWORD WINAPI ThreadProc1(LPVOID lpParameter);
 
char dir = RIGHT;
 
int main()
{
 SnakeUI::initUI();
 Snake snake;
 
 CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
 while (snake.snakeMove(dir)) Sleep(100);
 
 system("pause");
 return 0;
}
 
 
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
 for (;;) {
 dir = _getch();
 }
 return 1;
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq1263292336/article/details/49964011

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部