-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path贪吃蛇.cpp
118 lines (98 loc) · 2.8 KB
/
贪吃蛇.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 贪吃蛇.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include"wall.h"
#include"snake.h"
#include"food.h"
#include<ctime>
#include<conio.h>
#include<Windows.h>
using namespace std;
//光标定位
void gotoxy1(HANDLE hOut1, int x, int y)
{
COORD pos;
pos.X = x; //横坐标
pos.Y = y; //纵坐标
SetConsoleCursorPosition(hOut1, pos);
}
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //定义显示器句柄变量
int main()
{
//添加随机数种子
srand((unsigned int)time(NULL));
char preKey = NULL;
Wall wall;
wall.initWall();
wall.drawWall();
Food food(wall);
food.setFood();
Snake snake(wall,food);
snake.initSnake();
//是否死亡表示
bool isDead = false;
/*snake.move('w');
snake.move('w');
snake.move('a'); */
gotoxy1(hOut, 0, Wall::ROW+1);
cout << "得分:" << snake.getScore() << "分" << endl;
while (!isDead)
{
//接受用户输入
char key = _getch();
//第一次按了左键,才不能激活游戏
if (preKey == NULL && key == snake.LEFT)
{
continue;
}
do
{
if (key == snake.UP || key == snake.DOWN || key == snake.LEFT || key == snake.RIGHT)
{
//判断本次按键是否与上次按键冲突
if ((key == snake.LEFT && preKey == snake.RIGHT) ||
(key == snake.UP && preKey == snake.DOWN) ||
(key == snake.RIGHT && preKey == snake.LEFT) ||
(key == snake.DOWN && preKey == snake.UP)
)
{
key = preKey;
}
else
{
preKey = key;
}
if (snake.move(key) == true)
{
////移动成功
//system("cls");
//wall.drawWall();
gotoxy1(hOut, 0, Wall::ROW);
cout << "得分:" << snake.getScore() << "分" << endl;
Sleep(snake.getSleepTime());
}
else
{
isDead = true;
break;
}
}
else
{
//强制将错误按键变为上一次移动方向
key = preKey;
}
} while (!_kbhit());
}
// //测试
// wall.setWall(5, 4, '=');
// wall.setWall(5, 5, '=');
// wall.setWall(5, 6, '@');
//
// wall.drawWall();
//
// cout << wall.getWall(0, 0) << endl;
// cout << wall.getWall(5, 4) << endl;
// cout << wall.getWall(5, 6) << endl;
// cout << wall.getWall(1, 1) << endl;
}