-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
103 lines (92 loc) · 1.74 KB
/
snake.cpp
File metadata and controls
103 lines (92 loc) · 1.74 KB
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
#include "snake.h"
#include<graphics.h>
#include<conio.h>
Snake::Snake(int length, int score, int direction)
{
s_length = length;
s_score = score;
s_direction = direction;
p[0].p_x = 50;
p[0].p_y = 10;
p[1].p_x = 40;
p[1].p_y = 10;
p[2].p_x = 30;
p[2].p_y = 10;
s_score = 0;
}
int Snake::GetLength()
{
return s_length;
}
void Snake::SetLength()
{
s_length++;
}
void Snake::DrawSnake()
{
setfillcolor(RGB(60, 179, 113));
for (int i = 1; i < GetLength(); i++)
{
fillrectangle(p[i].p_x, p[i].p_y, p[i].p_x + 10, p[i].p_y + 10);
}
setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
//fillrectangle(p[0].p_x, p[0].p_y, p[0].p_x + 10, p[0].p_y + 10);
fillcircle(p[0].p_x + 5, p[0].p_y + 5, 7);
}
void Snake::GetCommand(int command)
{
int flag = -1;
if (_kbhit())
{
switch (_getch())
{
case 'A': case 'a': case 75: flag = 0; break; //方向向左
case 'S': case 's': case 80: flag = 1; break; //方向向下
case 'D': case 'd': case 77: flag = 2; break; //方向向右
case 'W': case 'w': case 72: flag = 3; break; //方向向上
}
if (flag != -1 && abs(command - flag) != 2)
{
SetDirection(flag);
}
}
}
int Snake::GetDirection()
{
return s_direction;
}
void Snake::SetDirection(int dire)
{
s_direction = dire;
}
void Snake::SnakeMove(int direction)
{
for (int i = GetLength() - 1; i > 0; i--)
{
p[i].p_x = p[i - 1].p_x;
p[i].p_y = p[i - 1].p_y;
}
switch (direction)
{
case 2: //向右
p[0].p_x += 10;
break;
case 0: //向左
p[0].p_x -= 10;
break;
case 1: //向下
p[0].p_y += 10;
break;
case 3: //向上
p[0].p_y -= 10;
break;
}
}
int Snake::GetScore()
{
return s_score;
}
void Snake::SetScore()
{
s_score++;
}