-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGameWidget.h
63 lines (49 loc) · 1.41 KB
/
SnakeGameWidget.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
#pragma once
#include <QKeyEvent>
#include <QList>
#include <QPainter>
#include <QRandomGenerator>
#include <QTimer>
#include <QWidget>
#include <qboxlayout.h>
class SnakeGameWidget : public QWidget
{
Q_OBJECT;
public:
SnakeGameWidget(QWidget *parent = nullptr);
~SnakeGameWidget() = default;
void start();
void end();
void init();
void moveSnake();
bool isEqual(QPoint *p1, QPoint *p2);
bool isContain(QPoint *p, QList<QPoint *> list);
void createFood();
void setWidgetSize(int x, int y);
void setBlockSize(int size);
int getBlockSize();
void setSpeed(int speed);
int getSpeed();
int getWidgetAllBlockWidth();
int getWidgetAllBlockHeight();
QString getSnakeDirection();
void setSnakeDirection(QString dir);
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
signals:
void gameOver(int mode);
void getScore(int score);
private:
QTimer *timer;
bool gameStatus = false;
int blockSize = 20;
int widgetAllBlockWidth = 10;
int widgetAllBlockHeight = 10;
QList<QPoint *> snake;
QPoint *snakeHead;
QString direction = "n";
QString preDirection = "n";
QPoint *food;
int score = 0;
int speed = 250;
};