-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceInvader.cpp
More file actions
155 lines (124 loc) · 4.36 KB
/
spaceInvader.cpp
File metadata and controls
155 lines (124 loc) · 4.36 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
#include "spaceInvader.h"
#include "ui_spaceInvader.h"
#include <iostream>
#include <QTimer>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QString>
using namespace std;
spaceInvader::spaceInvader(QWidget *parent) :
QDialog(parent),
m_ship(),
m_weapon(){
//create a spaceship and bullet object
//read in the initial x coordinate value
//and ship size from the cnofig file
int shipWidth = 120;
int shipHeight = 40;
QString initialxCoordinate;
QFile configFile(":/textFile/config.txt");
if(configFile.open(QIODevice::ReadOnly)){
QTextStream in(&configFile);
while(!in.atEnd()){
QString line = in.readLine();
//save x coordinate value to startxCoordinate
if(line == "[coordinate]"){
initialxCoordinate = in.readLine();
//THIS IS JUST A TEST -- print out the initial x coordinate
//value
}
if(line == "[size]"){
QString shipSize = in.readLine();
if(shipSize == "tiny"){
shipWidth = 90;
shipHeight = 30;
}
if(shipSize == "normal"){
shipWidth = 120;
shipHeight = 40;
}
if(shipSize == "large"){
shipWidth = 140;
shipHeight = 50;
}
if(shipSize == "giant"){
shipWidth = 170;
shipHeight = 60;
}
}
}
configFile.close();
}else{
cout << "File is not found or cannot be opened!" << endl;
}
string startxCoordinate = initialxCoordinate.toStdString();
cout << "value of initialxCoordinate is " << startxCoordinate << endl;
QPixmap temporary_shipImage;
temporary_shipImage.load(":/images/defender.png");
int maxX = this->width() - temporary_shipImage.width();
//construct the spaceship object and its image
spaceShip ship(initialxCoordinate.toInt(),350,3,maxX,
shipWidth,shipHeight);
//test the values of the ship that was just created
cout << "ship's initialxCoordinate is" << ship.getNewxCoordinate() << endl;
cout << "ship's initialyCoordinate is" << ship.getNewyCoordinate() << endl;
cout << "ship's speed is" << ship.getMovementSpeed()<< endl;
cout << "ship's shipWidth is" << shipWidth << endl;
cout << "ship's shipHeight is" << shipHeight<< endl;
cout << "ship's maxX is" << ship.getMaxX()<< endl;
weapon bullet(-1000,-1000,10,ship.getNewxCoordinate(),
ship.getNewyCoordinate(),ship.getSpaceShipImage());
//assign the spaceship and weapon objects to member variables
m_ship = ship;
m_weapon = bullet;
setStyleSheet("background-color: #112233;");
this->resize(600,420);
update();
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));
timer->start(33);
}
spaceInvader::~spaceInvader()
{
}
//the paintEvent method to paint objects on the screen
void spaceInvader::paintEvent(QPaintEvent *event){
QPainter painter(this);
painter.drawPixmap(m_ship.getNewxCoordinate(),
m_ship.getNewyCoordinate(),
m_ship.getSpaceShipImage());
painter.drawPixmap(m_weapon.getNewxCoordinate(),
m_weapon.getNewyCoordinate(),
m_weapon.getWeaponImage());
}
void spaceInvader::nextFrame(){
//negative for left movement, positive number for movement to the right
QString readLine;
int direction;
bool isCommandFound = false;
QFile configFile(":/textFile/config.txt");
//search for the commands in the config file
if(isCommandFound == false){
if(configFile.open(QIODevice::ReadOnly)){
QTextStream in(&configFile);
while(in.readLine() != "[commands]"){
QString line = in.readLine();
}
isCommandFound = true;
}else{
cout << "Cannot open the file!!" << endl;
}
}
QTextStream in(&configFile);
if(in.readLine() == "left"){
direction = -1;
}else if(in.readLine() == "right"){
direction = 1;
}
m_ship.setMovementDirection(direction);
//animate the spaceship
m_ship.nextFrame();
//animate the weapon
m_weapon.nextFrame();
}