-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
160 lines (138 loc) · 3.93 KB
/
mainwindow.cpp
File metadata and controls
160 lines (138 loc) · 3.93 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "mainwindow.h"
mainwindow::mainwindow(QWidget *parent) : QWidget(parent)
{
titlesound=new QSound(":/res/title.wav");
titlesound->play();
titlesound->setLoops(-1);
this->grabKeyboard();
resize(GM_WIDTH,GM_HEIGHT);
setMinimumSize(GM_WIDTH,GM_HEIGHT);
setMaximumSize(GM_WIDTH,GM_HEIGHT);
setAutoFillBackground(true);
QPalette palette;
QPixmap pixmap(":/res/title.png");
palette.setBrush(QPalette::Window, QBrush(pixmap));
setPalette(palette);
QFont font("Arial Black",30);//设置字体和大小
for(int i=0;i<2;i++){
PushButton[i] = new QPushButton(this);
PushButton[i]->move(800,50*(i+1));
switch(i){
case 0:
PushButton[i]->move(3*GM_WIDTH/5,GM_HEIGHT/4);
PushButton[i]->setText(tr("Game Start"));
break;
case BUTTON_CNT-1:
PushButton[i]->move(3*GM_WIDTH/5+30,GM_HEIGHT/4+50);
PushButton[i]->setText(tr("Quit"));
break;
default:break;
}
PushButton[i]->setFont(font);
PushButton[i]->hide();//不显示push button本身的文字,用painter绘制
}
now_button=0;//开始定位在 Game Start 上面
cs = new ChooseStage();
cp = new ChoosePlane();
connect(cs,&ChooseStage::chosen,this,&mainwindow::cs2cp);
connect(cp,&ChoosePlane::chosen,this,&mainwindow::StartGame);
connect(cp,&ChoosePlane::returned,this,&mainwindow::cp2cs);
connect(cs,&ChooseStage::returned,this,&mainwindow::cs2main);
update();
}
void mainwindow::paintEvent(QPaintEvent *){
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
QPen pen;
pen.setWidth(3);
pen.setColor(Qt::black);
painter.setPen(pen);
QLinearGradient linearGrad;
QFont font;
qreal x,y;
QString str;
for(int i=0;i<BUTTON_CNT;i++){
QPainterPath textPath;
str=PushButton[i]->text();
x=PushButton[i]->x();
y=PushButton[i]->y();
font=PushButton[i]->font();
if(i==now_button){
linearGrad.setColorAt(0,BUTTON_LIGHT);
}
else{
linearGrad.setColorAt(0,BUTTON_DARK);
}
textPath.addText(x,y,font,str);
painter.setBrush(linearGrad);
painter.drawPath(textPath);
}
}
void mainwindow::keyPressEvent(QKeyEvent *ke){
switch(ke->key()){
case Qt::Key_Up:
ke->accept();
if(now_button>0){
now_button--;
}
break;
case Qt::Key_Down:
ke->accept();
if(now_button<BUTTON_CNT-1){
now_button++;
}
break;
case Qt::Key_Return://ENTER键,Qt::Key_Enter为小键盘的ENTER键,原游戏中不能使用,在这里暂时不设置
ke->accept();
switch(now_button){
case 0:GameStartPress();break;
case 1:QuitPress();break;
default:break;
}
break;
case Qt::Key_Escape:
ke->accept();
if(now_button!=BUTTON_CNT-1){//Quit键在最后一个
now_button=BUTTON_CNT-1;
}
else{
QuitPress();
}
break;
default:
ke->ignore();
break;
}
update();
}
void mainwindow::GameStartPress(){
cs->show();
cs->grabKeyboard();
this->hide();
}
void mainwindow::QuitPress(){
this->close();
}
void mainwindow::cs2cp(){
cs->hide();
cp->grabKeyboard();
cp->show();
}
void mainwindow::cp2cs(){
cp->hide();
cs->grabKeyboard();
cs->show();
}
void mainwindow::cs2main(){
cs->hide();
this->grabKeyboard();
this->show();
}
void mainwindow::StartGame(){
Stage *s = new Stage(cp->now_plane,cs->now_stage);
titlesound->stop();
cs->close();
cp->close();
this->close();
s->show();
}