-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (64 loc) · 1.25 KB
/
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
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
#include"Snake.h"
#include"Wall.h"
#include"Food.h"
#include<conio.h>
#include<graphics.h>
#include <windows.h>
using namespace std;
void DetectPress(Snake& snake) {
if (_kbhit()) {
switch (_getch()) {
case 'a':
snake.ChangeOrientation(LEFT);
break;
case 's':
snake.ChangeOrientation(DOWN);
break;
case 'd':
snake.ChangeOrientation(RIGHT);
break;
case 'w':
snake.ChangeOrientation(UP);
break;
}
}
}
int flash = 1;
int main() {
Snake snake(DOWN, 15, 20, 10);
Wall wall(RED, DOWN, 20, 0, 0);
Food food(BIG, GREEN, 10, 10);
initgraph(800, 600);//窗口大小
setbkcolor(WHITE);//游戏背景色
while (1) {
if (wall.BeHit(snake)) {
cout << "撞墙" << endl;
break;
}
if (snake.BeHit(snake)) {
cout << "自身相撞" << endl;
break;
}
if (food.BeHit(snake)) {
}
cout << food.getColor() << ", " << food.getPositon(0).second << endl;
Sleep(300);
cleardevice();//清理窗口
DetectPress(snake);
snake.Move();
snake.DrawSnake();
wall.DrawWall();
if(flash == 1) {
food.DrawFood(10);
flash = 0;
}
else{
food.DrawFood(5);
flash = 1;
}
FlushBatchDraw();
}
cout << "游戏结束" << endl;
closegraph();
return 0;
}