//Sets the angle of the ship
void CannonField::setAngle(int angle)
{
if (currentAngle == angle)
return;
currentAngle = angle;
update(cannonRect());
emit angleChanged(currentAngle);
}
void CannonField
::paintEvent(QPaintEvent * /* event */) {
if (gameEnded) {
painter.setPen(Qt::black);
painter.
setFont(QFont("Courier",
48,
QFont::Bold));
painter.drawText(rect(), Qt::AlignCenter, tr("Game Over"));
}
paintCannon(painter);
if (isShooting())
paintShot(painter);
if (!gameEnded)
paintTarget(painter);
}
void CannonField
::paintCannon(QPainter &painter
) {
//setPen determines if there is an outline I believe.
painter.setPen(Qt::NoPen);
//setBrush determines color.
painter.setBrush(Qt::blue);
painter.save();
//Translate the coordinates to that the center of the play
//field is (0, 0)
painter.translate(width()/2, height()/2);
//Rotates the ship
painter.rotate(-currentAngle);
//creates the dimensions for Polygon
//draws the ship
painter.drawPolygon(polygon);
painter.restore();
}
QRect CannonField
::cannonRect() const {
//cannonRect should be around the ship, as a
//sort of hit box and way to move the ship.
QRect result
(0,
0,
500,
500);
//we should use something like this to move the box
//and our ship would automatically move with it, since
//it would always paint inside the box
result.moveBottomLeft(rect().bottomLeft());
return result;
}
QRect CannonField
::targetRect() const {
//QRect is set up for the target/asteroid, and it starts at the bottom
//left corner, and is 20 wide and 10 high.
QRect result
(0,
0,
20,
10);
//It is moved from it's center by a certain amount.
result.
moveCenter(QPoint(target.
x(), height
() - 1 - target.
y()));
return result;
}
//Sets the angle of the ship
void CannonField::setAngle(int angle)
{
if (currentAngle == angle)
return;
currentAngle = angle;
update(cannonRect());
emit angleChanged(currentAngle);
}
void CannonField::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
if (gameEnded) {
painter.setPen(Qt::black);
painter.setFont(QFont("Courier", 48, QFont::Bold));
painter.drawText(rect(), Qt::AlignCenter, tr("Game Over"));
}
paintCannon(painter);
if (isShooting())
paintShot(painter);
if (!gameEnded)
paintTarget(painter);
}
void CannonField::paintCannon(QPainter &painter)
{
//setPen determines if there is an outline I believe.
painter.setPen(Qt::NoPen);
//setBrush determines color.
painter.setBrush(Qt::blue);
painter.save();
//Translate the coordinates to that the center of the play
//field is (0, 0)
painter.translate(width()/2, height()/2);
//Rotates the ship
painter.rotate(-currentAngle);
//creates the dimensions for Polygon
QPolygon polygon;
polygon << QPoint(-10, 0) << QPoint(10, 0) << QPoint(0, -20);
//draws the ship
painter.drawPolygon(polygon);
painter.restore();
}
QRect CannonField::cannonRect() const
{
//cannonRect should be around the ship, as a
//sort of hit box and way to move the ship.
QRect result(0, 0, 500, 500);
//we should use something like this to move the box
//and our ship would automatically move with it, since
//it would always paint inside the box
result.moveBottomLeft(rect().bottomLeft());
return result;
}
QRect CannonField::targetRect() const
{
//QRect is set up for the target/asteroid, and it starts at the bottom
//left corner, and is 20 wide and 10 high.
QRect result(0, 0, 20, 10);
//It is moved from it's center by a certain amount.
result.moveCenter(QPoint(target.x(), height() - 1 - target.y()));
return result;
}
To copy to clipboard, switch view to plain text mode
Bookmarks