-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.h
82 lines (68 loc) · 1.88 KB
/
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
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
#ifndef SNAKE_H
#define SNAKE_H
#pragma execution_character_set("utf-8")
//区域大小
const int MAX_X=20;
const int MAX_Y=20;
const int BLOCK_SIZE=30;
//枚举Label
enum Label
{
NORMAL_LABEL,//普通类型,背景
BORDER_LABEL,//边界
SNAKE_LABEL,//蛇节点
FOOD_LABEL,//食物
};
#include <QMainWindow>
#include <QLabel>
#include <QList>
#include <QWidget>
#include <QPushButton>
#include <QTimer>
//蛇的节点
class snakeNode
{
public:
QLabel *label;
int type;
int x;
int y;
};
class Snake :public QMainWindow
{
Q_OBJECT
public:
explicit Snake (QWidget *parent=0 );//表明Snake没有父窗口
void init(); //初始化游戏
void drawBorder(); //画边界
void initSnake(); //初始化蛇身
void drawViewArea(); //分数区域
void moveSnake(); //移动蛇
void getHeadTail(); //获取头指针
void createFood(); //创建食物
void gameover(); //游戏结束
void showViewArea(); //显示分数
bool up(); //判断能否上下左右
bool down();
bool left();
bool right();
QTimer timer;
QPushButton *startGame;
~Snake();
private slots:
void snakeMoveSlots();
void startGameSlots();
protected:
void keyPressEvent(QKeyEvent *event);
private:
snakeNode *matrix[MAX_X][MAX_Y]; //存储游戏画面
QList<snakeNode*> snake;
int level,score; //分数等级
int foodCount,moveSpeed; //食物数 移动速度
int directionX,directionY;
QLabel *mLabel; //level score标签
QString viewText; //显示区域文本
snakeNode *head;
snakeNode *tail; //蛇头尾指针
};
#endif // SNAKE_H