본문 바로가기
과거에 공부했던 것들(저장용)/학부생

QT) Timer를 이용하여 공움직이기

by under_coverzzz 2015. 7. 23.
반응형

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QGraphicsEllipseItem>

#include <QGraphicsView>

#include <QBrush>

#include <QPen>

#include <math.h>

#include <QtWidgets>

#include <QPropertyAnimation>

#include <math.h>

 

int ox,oy;

int xx,yy;

 

 

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

scene = new QGraphicsScene(this);

QBrush yellowBrush(Qt::yellow);

QBrush redBrush(Qt::red);

QPen blackpen(Qt::black);

 

blackpen.setWidth(1);

 

x = 700;

y = 400;

scene->setSceneRect(20,0,x+20,y);

 

QLineF TopLine(scene->sceneRect().topLeft(),scene->sceneRect().topRight());

QLineF LeftLine(scene->sceneRect().topLeft(),scene->sceneRect().bottomLeft());

QLineF RightLine(scene->sceneRect().topRight(),scene->sceneRect().bottomRight());

QLineF BottomLine(scene->sceneRect().bottomLeft(),scene->sceneRect().bottomRight());

 

scene->addLine(TopLine,blackpen);

scene->addLine(LeftLine,blackpen);

scene->addLine(RightLine,blackpen);

scene->addLine(BottomLine,blackpen);

connect(ui->yellowButton2,SIGNAL(clicked()),this,SLOT(yTimer_2()));

connect(ui->yellowButton4,SIGNAL(clicked()),this,SLOT(yTimer_4()));

connect(ui->yellowButton6,SIGNAL(clicked()),this,SLOT(yTimer_6()));

connect(ui->yellowButton8,SIGNAL(clicked()),this,SLOT(yTimer_8()));

 

x1 = 100;

y1 = 50;

ox = x1;

oy = y1;

 

x3 = 300;

y3 = 50;

 

ellipse_yellow = scene->addEllipse(x1,y1,40,40,blackpen,yellowBrush);

ellipse_red = scene->addEllipse(x3,y3,40,40,blackpen,redBrush);

 

ui->view->setScene(scene);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::yellowButton2()

{

y1 = y1 -1;

x1 = x1;

 

ellipse_yellow->setRect(x1,y1,40,40);

ellipse_red->setRect(x3,y3,40,40);

}

void MainWindow::yellowButton4()

{

x1 = x1 -1;

y1 = y1;

 

ellipse_yellow->setRect(x1,y1,40,40);

ellipse_red->setRect(x3,y3,40,40);

}

void MainWindow::yellowButton6()

{

x1 = x1 + 1;

y1 = y1;

if(sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1))<=40)

{

bounce();

}

ellipse_yellow->setRect(x1,y1,40,40);

ellipse_red->setRect(x3,y3,40,40);

}

 

void MainWindow::yellowButton8()

{

x1 = x1;

y1 = y1 + 1;

 

ellipse_yellow->setRect(x1,y1,40,40);

ellipse_red->setRect(x3,y3,40,40);

}

void MainWindow::bounce()

{

hello = new QTimer(this);

connect(hello,SIGNAL(timeout()),this,SLOT(dangu()));

hello->start(100);

}

void MainWindow::dangu()

{

if(x3 <700)

{

flag =1;

check();

}

else if (x3>=700)

{

flag_2 =1;

check();

}

}

void MainWindow :: check()

{

if(flag == 1)

{

x3 = x3 +1;

}

else if (flag_2 == 1)

{

while(x3-20==0)

{

x3 = x3 -1;

}

}

}

void MainWindow::yTimer_2()

{

yTimer2 = new QTimer(this);

connect(yTimer2,SIGNAL(timeout()),this,SLOT(yellowButton2()));

yTimer2->start(10);

}

 

void MainWindow::yTimer_4()

{

yTimer4 = new QTimer(this);

connect(yTimer4,SIGNAL(timeout()),this,SLOT(yellowButton4()));

yTimer4->start(10);

}

void MainWindow::yTimer_6()

{

yTimer6 = new QTimer(this);

connect(yTimer6,SIGNAL(timeout()),this,SLOT(yellowButton6()));

yTimer6->start(10);

}

 

void MainWindow::yTimer_8()

{

yTimer8 = new QTimer(this);

connect(yTimer8,SIGNAL(timeout()),this,SLOT(yellowButton8()));

yTimer8->start(10);

}


반응형

'과거에 공부했던 것들(저장용) > 학부생' 카테고리의 다른 글

월세예측 프로그램  (0) 2015.07.23
판다의 성장과정 맞추기  (0) 2015.07.23
QT) ThreadScaler  (0) 2015.07.23
QT) Mouse 움직임 나타내기  (0) 2015.07.23
C언어 Call by Pointer  (0) 2015.07.23