-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
81 lines (73 loc) · 1.94 KB
/
widget.cpp
File metadata and controls
81 lines (73 loc) · 1.94 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
#include "widget.h"
#include "ui_widget.h"
#include<QKeyEvent>
#include<QMessageBox>
#include <QPainter>
#include <math.h>
#include <QTimer>
static const double Pi = 3.14159265358979323846264338327950288419717;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Tank=(QPixmap) *(ui->tank->pixmap());
pos=ui->tank->pos();
angle=90;
varX=0;
varY=0;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTank()));
timer->start(50);
}
Widget::~Widget()
{
delete ui;
}
void Widget::updateTank(){
int x,y;
x=ui->tank->pos().x();
y=ui->tank->pos().y();
if(ui->up->isDown() || ui->down->isDown() ){
varX+=cos((angle)*Pi/180);
if(abs(varX)>1){
int Xinc=round(varX);
varX = varX - Xinc;
if(ui->up->isDown() )
x=x+Xinc;
else
x=x-Xinc;
}
varY+=sin((angle)*Pi/180);
if(abs(varY)>1){
int Yinc=round(varY);
varY = varY - Yinc;
if(ui->up->isDown() )
y=y-Yinc;
else
y=y+Yinc;
}
ui->tank->move(x,y);
}else if(ui->right->isDown()){
angle--;
ui->tank->setPixmap(rotatePixmap((90-angle)));
if(angle == 360) angle = 0;
}else if(ui->left->isDown()){
angle++;
ui->tank->setPixmap(rotatePixmap((90-angle)));
if(angle == 0) angle = 360;
}
}
QPixmap Widget::rotatePixmap(int angle){
QSize size = Tank.size();
QPixmap rotatedPixmap(size);
rotatedPixmap.fill(QColor::fromRgb(0, 0, 0, 0)); //the new pixmap must be transparent.
QPainter* p = new QPainter(&rotatedPixmap);
p->translate(size.height()/2,size.height()/2);
p->rotate(angle);
p->translate(-size.height()/2,-size.height()/2);
p->drawPixmap(0, 0, Tank);
p->end();
delete p;
return rotatedPixmap;
}