-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainScene.cpp
More file actions
182 lines (165 loc) · 4.38 KB
/
Copy pathmainScene.cpp
File metadata and controls
182 lines (165 loc) · 4.38 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// mainwindow.cpp
#include "mainScene.h"
#include "config.h"
#include "QPainter"
#include "ctime"
#include "QSound"
MainScene::MainScene(QWidget *parent) :
QMainWindow(parent) {
initScene();
playGame();
}
void MainScene::initScene() {
setFixedSize(GAME_WIDTH, GAME_HEIGHT);
setWindowTitle(GAME_TITLE);
//设置图标资源
setWindowIcon(QIcon( GAME_ICON)); //加头文件 #include <QIcon>
m_Timer.setInterval(GAME_RATE);
srand((unsigned int) time(NULL));
m_recoder = 0;
}
void MainScene::playGame()
{
//启动定时器
m_Timer.start();
QSound::play(SOUND_BACKGROUND);
//监听定时器
connect(&m_Timer,&QTimer::timeout,[=](){
//更新游戏中元素的坐标
updatePosition();
//敌机出场
enemyToScene();
//重新绘制图片
update();
//碰撞检测
collisionDetection();
});
}
void MainScene::updatePosition()
{
//更新地图坐标
m_map.mapPosition();
temp_bullet.m_Free = false;
temp_bullet.updatePosition();
m_hero.shoot();
for(int i = 0; i < BULLET_NUM; i++)
{
if ( !m_hero.m_bullets[i].m_Free)
{
m_hero.m_bullets[i].updatePosition();
}
}
for (int i = 0; i < ENEMY_NUM; ++i) {
if (m_enemys[i].m_Free == false)
{
m_enemys[i].updatePosition();
}
}
for (int i = 0; i < BOMB_NUM; ++i) {
if(m_bombs[i].m_Free == false)
{
m_bombs[i].updateInfo();
}
}
}
void MainScene::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//绘制地图
painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);
//绘制英雄
painter.drawPixmap(m_hero.m_X, m_hero.m_Y, m_hero.m_Plane);
painter.drawPixmap(temp_bullet.m_X, temp_bullet.m_Y, temp_bullet.m_Bullet);
for(int i = 0; i<BULLET_NUM; i++)
{
if(!m_hero.m_bullets[i].m_Free)
{
painter.drawPixmap(m_hero.m_bullets[i].m_X, m_hero.m_bullets[i].m_Y, m_hero.m_bullets[i].m_Bullet);
}
}
for(int i=0; i<ENEMY_NUM; i++){
if (m_enemys[i].m_Free == false)
{
painter.drawPixmap(m_enemys[i].m_X, m_enemys[i].m_Y, m_enemys[i].m_enemy);
}
}
for (int i = 0; i < BOMB_NUM; ++i) {
if(m_bombs[i].m_Free == false)
{
painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
}
}
}
void MainScene::mouseMoveEvent(QMouseEvent *event) {
int x = event->x() - m_hero.m_Rect.width()*0.5;
int y = event->y() - m_hero.m_Rect.height()*0.5;
//边界检测
if(x <= 0)
{
x = 0;
}
if(x >= GAME_WIDTH - m_hero.m_Rect.width())
{
x = GAME_WIDTH - m_hero.m_Rect.width();
}
if(y <=0)
{
y = 0;
}
if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
{
y = GAME_HEIGHT - m_hero.m_Rect.height();
}
m_hero.setPosition(x, y);
}
void MainScene::enemyToScene() {
m_recoder ++;
if(m_recoder < ENEMY_INTERVAL)
{
return;
}
m_recoder = 0;
for (int i = 0; i < ENEMY_NUM; ++i) {
if(m_enemys[i].m_Free)
{
m_enemys[i].m_Free = false;
//设置坐标
m_enemys[i].m_X = rand() % (GAME_WIDTH-m_enemys[i].m_Rect.width());
m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
break;
}
}
}
void MainScene::collisionDetection() {
for(int i =0; i<ENEMY_NUM; i++)
{
if(m_enemys[i].m_Free){
continue;
}
//遍历子弹
for(int j = 0; j < BULLET_NUM; j++)
{
if(m_hero.m_bullets[j].m_Free){
continue;
}
if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
{
m_enemys[i].m_Free = true;
m_hero.m_bullets[j].m_Free = true;
QSound::play(SOUND_BOMB);
for(int k = 0; k<BOMB_NUM; k++){
if(m_bombs[k].m_Free)
{
m_bombs[k].m_Free = false;
m_bombs[k].m_X = m_enemys[i].m_X;
m_bombs[k].m_Y = m_enemys[i].m_Y;
break;
}
}
}
}
}
}
MainScene::~MainScene() {
}