貪食蛇-遊戲主體


創造完遊戲基本的物件後,就可以開始做遊戲實際運行的畫面了。

先來回顧一下之前寫的物件

C++
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
class Snake{
private:
int x;
int y;
int player;
bool eat = false;
class Body{
private:
int x;
int y;
public:
int xpos(){
return x;
}
int ypos(){
return y;
}
void setxy(int a, int b){
x = a;
y = b;
}
};
deque<Body> body;
public:
Body GetBody(int i){
return body[i];
}
void gotoxy(int xpos, int ypos)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void Init(){
player = 4;
Body head;
head.setxy(1, 1);
body.clear();
body.push_front(head);
}
int speed(){
return (50 + length()*10);
}
void move(char key){
Body head = body.front();
if(!eat){
Body tail = body.back();
gotoxy(tail.xpos() + Left_Edge, tail.ypos());
cout << " ";
body.pop_back();
}
if(key == 'a'){
head.setxy(head.xpos()-1, head.ypos());
}
else if(key == 'd'){
head.setxy(head.xpos()+1, head.ypos());
}
else if(key == 'w'){
head.setxy(head.xpos(), head.ypos()-1);
}
else if(key == 's'){
head.setxy(head.xpos(), head.ypos()+1);
}
gotoxy(head.xpos() + Left_Edge, head.ypos());
cout << "S";
body.push_front(head);
eat = false;
}
int length(){
return body.size();
}
int HP(){
return player;
}
void hurt(){
--player;
}
void grow(){
eat = true;
}
bool die(){
Body head = body.front();
if(player <= 0) return true;
if(head.xpos() < 1 || head.xpos() > Map_Width-1 || head.ypos() < 1 || head.ypos() > Map_Height-1) return true;
for(int i = 1; i < body.size(); ++i){
if(head.xpos() == body[i].xpos() && head.ypos() == body[i].ypos()){
return true;
}
}
return false;
}
}snake;

食物

C++
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
class Food{
private:
bool flag;
int x;
int y;
public:
int xpos(){
return x;
}
int ypos(){
return y;
}
void gotoxy(int xpos, int ypos)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void InitFood(){
flag = true;
}
void CreateFood(){
if(flag){
x = rand() % (Map_Width-2) + 1;
y = rand() % (Map_Height-2) + 1;
for(int i = 0; i < snake.length(); ++i){
if(x == snake.GetBody(i).xpos() && y == snake.GetBody(i).ypos()){
x = rand() % (Map_Width-2) + 1;
y = rand() % (Map_Height-2) + 1;
}
}
bool CreateError = false;
for(int i = 0; i < snake.length(); ++i){
if(x == snake.GetBody(i).xpos() && y == snake.GetBody(i).ypos()){
CreateError = true;
break;
}
}
if(CreateError) flag = true;
else {
gotoxy(x + Left_Edge, y);
cout << "f";
flag = false;
}
}
}
void EatFood(){
if(x == snake.GetBody(0).xpos() && y == snake.GetBody(0).ypos()){
snake.grow();
flag = true;
}
}
bool exist(){
if(flag) return false;
else return true;
}
}food;

回顧完畢

由於蛇撞到邊界會死亡,所以勢必要有一張有限的地圖,這裡我們可以先定義地圖的長、寬,以及地圖左邊界的位置(讓遊戲畫面盡量靠中)。

1
2
3
#define Map_Width 50 //寬
#define Map_Height 30 //長
#define Left_Edge 30 //左邊界x值

地圖的邊界可以利用一些簡單的符號以及 gotoxy() 函式來繪製。寫成一個函式之後要用再呼叫。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void InitScene(){
for(int i = 0; i < Map_Width + 1; ++i){
gotoxy(Left_Edge + i, 0); //上邊界
cout << "-";
gotoxy(Left_Edge + i, Map_Height); //下邊界
cout << "-";
}
for(int i = 1; i < Map_Height; ++i){
gotoxy(Left_Edge - 1, i); //左邊界
cout << "|";
gotoxy(Left_Edge + Map_Width, i); //右邊界
cout << "|";
}
}

結果

接著就可以想辦法將蛇和食物顯示在螢幕上,並且依照指令行動,就是變成可以玩的樣子。

先將蛇和食物初始化,接著使蛇可以一直根據指令行動,過程中要不斷判斷食物是否需要重新生成(有沒有被吃),若死亡則遊戲結束。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){
char Key = 'd'; //默認向右移動
HideCursor(); //隱藏光標
InitScene(); //繪製地圖
snake.Init(); //蛇初始化
food.InitFood(); //食物初始化
while(true){ //開始遊戲
if(_kbhit()){ //若有按鍵則接收輸入
Key = _getch();
}
food.CreateFood(); //是否需要生成食物
snake.move(Key); //移動
food.EatFood(); //食物是否被吃
if(snake.die()){ //是否死亡
break;
}
Sleep(100); //移動間隔時間
}
return 0;
}

最後來簡單做個遊戲結束的畫面好了,順便在遊戲過程中顯示當前分數(蛇的長度)。

將遊戲結束畫面寫成一個函式,在死亡時呼叫即可。

1
2
3
4
5
6
void die_UI(){
gotoxy(Left_Edge + Map_Width/2, 5);
cout << "你已經死了!!";
gotoxy(Left_Edge + Map_Width/2, 7);
cout << "分數: " << snake.length();
}

一樣寫成一個函式,在無窮迴圈中呼叫。

1
2
3
4
void update(){
gotoxy(Left_Edge + Map_Width + 1, 2);
cout << "得點: " << snake.length() << " ";
}

最終的程式碼

C++
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;

#define Map_Width 50
#define Map_Height 30
#define Left_Edge 30

class Snake{
private:
int x;
int y;
int player;
bool eat = false;
class Body{
private:
int x;
int y;
public:
int xpos(){
return x;
}
int ypos(){
return y;
}
void setxy(int a, int b){
x = a;
y = b;
}
};
deque<Body> body;
public:
Body GetBody(int i){
return body[i];
}
void gotoxy(int xpos, int ypos)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void Init(){
player = 4;
Body head;
head.setxy(1, 1);
body.clear();
body.push_front(head);
}
void move(char key){
Body head = body.front();
if(!eat){
Body tail = body.back();
gotoxy(tail.xpos() + Left_Edge, tail.ypos());
cout << " ";
body.pop_back();
}
if(key == 'a'){
head.setxy(head.xpos()-1, head.ypos());
}
else if(key == 'd'){
head.setxy(head.xpos()+1, head.ypos());
}
else if(key == 'w'){
head.setxy(head.xpos(), head.ypos()-1);
}
else if(key == 's'){
head.setxy(head.xpos(), head.ypos()+1);
}
gotoxy(head.xpos() + Left_Edge, head.ypos());
cout << "S";
body.push_front(head);
eat = false;
}
int length(){
return body.size();
}
void grow(){
eat = true;
}
bool die(){
Body head = body.front();
if(player <= 0) return true;
if(head.xpos() < 1 || head.xpos() > Map_Width-1 || head.ypos() < 1 || head.ypos() > Map_Height-1) return true;
for(int i = 1; i < body.size(); ++i){
if(head.xpos() == body[i].xpos() && head.ypos() == body[i].ypos()){
return true;
}
}
return false;
}
}snake;

class Food{
private:
bool flag;
int x;
int y;
public:
int xpos(){
return x;
}
int ypos(){
return y;
}
void gotoxy(int xpos, int ypos)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void InitFood(){
flag = true;
}
void CreateFood(){
if(flag){
x = rand() % (Map_Width-2) + 1;
y = rand() % (Map_Height-2) + 1;
for(int i = 0; i < snake.length(); ++i){
if(x == snake.GetBody(i).xpos() && y == snake.GetBody(i).ypos()){
x = rand() % (Map_Width-2) + 1;
y = rand() % (Map_Height-2) + 1;
}
}
bool CreateError = false;
for(int i = 0; i < snake.length(); ++i){
if(x == snake.GetBody(i).xpos() && y == snake.GetBody(i).ypos()){
CreateError = true;
break;
}
}
if(CreateError) flag = true;
else {
gotoxy(x + Left_Edge, y);
cout << "f";
flag = false;
}
}
}
void EatFood(){
if(x == snake.GetBody(0).xpos() && y == snake.GetBody(0).ypos()){
snake.grow();
flag = true;
}
}
}food;

void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int xpos, int ypos)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}

void InitScene();
void die_UI();
void update();

int main(){
char Key = 'd';
HideCursor();
InitScene();
snake.Init();
food.InitFood();
while(true){
if(_kbhit()){
Key = _getch();
}
food.CreateFood();
snake.move(Key);
food.EatFood();
update();
if(snake.die()){
system("CLS");
die_UI();
break;
}
Sleep(100);
}
return 0;
}
void InitScene(){
for(int i = 0; i < Map_Width + 1; ++i){
gotoxy(Left_Edge + i, 0);
cout << "-";
gotoxy(Left_Edge + i, Map_Height);
cout << "-";
}
for(int i = 1; i < Map_Height; ++i){
gotoxy(Left_Edge - 1, i);
cout << "|";
gotoxy(Left_Edge + Map_Width, i);
cout << "|";
}
}

void die_UI(){
gotoxy(Left_Edge + Map_Width/2, 5);
cout << "你已經死了!!";
gotoxy(Left_Edge + Map_Width/2, 7);
cout << "得點: " << snake.length();
}

void update(){
gotoxy(Left_Edge + Map_Width + 1, 2);
cout << "得點: " << snake.length() << " ";
}

開玩

最基礎的貪食蛇終於完工啦,可以交作業了,但是這樣你滿足了嗎?

Are you satisfied?

No, I am curious and interested!!

當然不能止步於此,還有各種介面、關卡流程,甚至是 BGM 等著我們來做,你以為全都結束了嗎?

不,這只是最初的起點。

這不是很戲劇化的發展嗎