-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysnakefoodtest.cpp
97 lines (94 loc) · 2.94 KB
/
mysnakefoodtest.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
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
#include "mysnakefoodtest.h"
mysnakefoodtest::mysnakefoodtest(QWidget *parent) :
QWidget(parent)
{
resize(930,930);
setWindowTitle(tr("我是蛇"));
}
mysnakefoodtest::~mysnakefoodtest()
{
this->close();
}
void mysnakefoodtest::keyPressEvent(QKeyEvent *key)
{switch(key->key())
{case Qt::Key_Up: snake1.direction=1;break;
case Qt::Key_Down: snake1.direction=2;break;
case Qt::Key_Left: snake1.direction=3;break;
case Qt::Key_Right: snake1.direction=4;break;
case Qt::Key_P: snake1.isPause=!snake1.isPause;break;
case Qt::Key_R: if(snake1.isOver) snake1.reset();break;
case Qt::Key_Escape: if(snake1.isOver) {snake1.istrulyover=true;}break;
default:;
}
}
void mysnakefoodtest::paintEvent(QPaintEvent *event)
{
if(snake1.istrulyover) {this->hide();}
QPainter painter(this);
if(!snake1.isRun)
{snake1.init_Snake();
food1.adjustfood();
display="play game now";
scoreLabel="Score:";}
//playarea
//把背景换了
QPixmap image1;
image1.load("../mysnaketest/4.jpg");
painter.drawPixmap(0,0,930,930,image1);
//wall
painter.setPen(Qt::black);
painter.setBrush(Qt::black);
painter.drawRect(30,30,470,15);
painter.drawRect(30,30,15,870);
painter.drawRect(485,30,15,870);
painter.drawRect(30,885,470,15);
painter.setBrush(Qt::white);
painter.drawRect(530,100,920,400);
//显示开始、结束
QFont font1("Microsoft YaHei",12);
painter.setFont(font1);
painter.setPen(Qt::red);
painter.setBrush(Qt::red);
painter.drawText(622,200,display);
//计分
QFont font2("Microsoft YaHei",12);
painter.setFont(font2);
painter.setPen(Qt::black);
painter.setBrush(Qt::black);
painter.drawText(622,300,scoreLabel);
painter.drawText(722,300,QString::number(snake1.returnscore()));
painter.drawText(622,400,"press p to pause");
//画蛇头
painter.setPen(Qt::black);
painter.setBrush(Qt::yellow);
painter.drawRect(snake1.returnhead());
//画蛇
painter.setPen(Qt::black);
painter.setBrush(Qt::black);
painter.drawRects(snake1.returnbody()+1,snake1.returnlens()-1);
//画吃的
painter.setPen(Qt::green);
painter.setBrush(Qt::green);
painter.drawRect(food1.returnfood());
//更新蛇
if(food1.returnfood().contains(snake1.returnhead())) eat();
snake1.Snake_update();
if(snake1.isOver) display="game over now!";
update();
}
void mysnakefoodtest::eat()
{
if(food1.returnfoodtype()==0) snake1.isEat();
else {
for(int i=0;i<10;++i) snake1.isEat();
}
food1.adjustfood();
}
bool mysnakefoodtest::overornot()
{
return snake1.returntrulyover();
}
void mysnakefoodtest::rebegin()
{
snake1.rebegin();
}