Advertisement
VictoriaLodochkina

lab 7 z2

May 5th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3.  
  4. Widget::Widget(QWidget *parent)
  5.     : QWidget(parent)
  6.     , ui(new Ui::Widget)
  7. {
  8.     ui->setupUi(this);
  9.     //все начальные координаты для таймера
  10.     /*int t0=0;
  11.     int Dt=333;
  12.     int x0=230;
  13.     int y0=210;
  14.     int R=200;
  15.     double x_cur1;
  16.     double y_cur1;
  17.     double x_cur2;
  18.     double y_cur2;*/
  19.     timer = new QTimer(this);
  20.     timer->setInterval(11);
  21.     pen.setColor(Qt::black);
  22.     pen.setStyle(Qt::SolidLine);
  23.     pen.setWidth(1);
  24.     connect(timer, SIGNAL(timeout()), this, SLOT(myTimerEvent()));
  25. }
  26.  
  27. Widget::~Widget()
  28. {
  29.     delete ui;
  30. }
  31.  
  32. void Widget::draw_mill(QPainter &paint){
  33.     paint.setPen(QPen(Qt::black, 1));
  34.     paint.setBrush(QBrush(Qt::gray));
  35.     QPolygon poly;
  36.     poly << QPoint(290, 285) << QPoint(290, 270) << QPoint(430, 300) << QPoint(430, 315);
  37.     paint.drawPolygon(poly);
  38.     paint.setBrush(QColor(85, 200, 255));
  39.     QPolygon polygon;
  40.     polygon << QPoint(170, 350) << QPoint(290, 350) << QPoint(330, 610) << QPoint(130, 610);
  41.     paint.drawPolygon(polygon);
  42.     //------------------------
  43.     paint.setBrush(QColor(249, 164, 70));
  44.     paint.drawRect(200, 510, 60, 100);
  45.     paint.setBrush(QBrush(Qt::black, Qt::VerPattern));
  46.     paint.drawRect(200, 510, 60, 100);//door
  47.     //------------------------
  48.     paint.setBrush(QColor(85, 200, 255));
  49.     paint.drawRect(170, 250, 120, 100);
  50.     paint.setBrush(QBrush(Qt::red));
  51.     QPolygon polygon1;
  52.     polygon1 << QPoint(150, 250) << QPoint(230, 150) << QPoint(310, 250);//roof
  53.     //-------------------------
  54.     paint.drawPolygon(polygon1);
  55.     paint.setBrush(QColor(128, 64, 64));
  56.     paint.drawEllipse(210, 190, 40, 40);
  57.     paint.setBrush(QColor(85, 200, 255));
  58.     paint.drawEllipse(223, 203, 14, 14);//ось мельницы центр -230/210
  59.     //--------------------------
  60.     paint.setBrush(QColor(253, 206, 128));
  61.     QPolygon hill;
  62.     hill << QPoint(x_hill1, y_hill1) << QPoint(x_hill2, y_hill2) << QPoint(x_hill3, y_hill3);
  63.     paint.drawPolygon(hill);
  64.     //---------------------------
  65.     paint.setBrush(QColor(128, 64, 64));
  66.     QPolygon polygon2;
  67.     polygon2 << QPoint(x_cur1, y_cur1) << QPoint(230, 210) << QPoint(x_cur2, y_cur2);
  68.     paint.drawPolygon(polygon2);
  69.     QPolygon polygon3;
  70.     polygon3 << QPoint(x_cur12, y_cur12) << QPoint(230, 210) << QPoint(x_cur22, y_cur22);
  71.     paint.drawPolygon(polygon3);
  72.     QPolygon polygon4;
  73.     polygon4 << QPoint(x_cur3, y_cur3) << QPoint(230, 210) << QPoint(x_cur4, y_cur4);
  74.     paint.drawPolygon(polygon4);
  75.     QPolygon polygon5;
  76.     polygon5 << QPoint(x_cur32, y_cur32) << QPoint(230, 210) << QPoint(x_cur42, y_cur42);
  77.     paint.drawPolygon(polygon5);
  78. }
  79.  
  80.  
  81. void Widget::myTimerEvent(){
  82.     //код
  83.     /*x_cur1=x0+R*qCos(t0*(M_PI/(180*16)));
  84.     y_cur1=y0+R*qSin(t0*(M_PI/(180*16)));
  85.     x_cur12=460-x_cur1;
  86.     y_cur12=420-y_cur1;
  87.     x_cur2=x0+R*qCos((t0+Dt)*(M_PI/(180*16)));
  88.     y_cur2=y0+R*qSin((t0+Dt)*(M_PI/(180*16)));
  89.     x_cur22=460-x_cur2;
  90.     y_cur22=420-y_cur2;
  91.     x_cur3=440-y_cur1;//x0+R*qCos((t0+667)*(M_PI/(180*16)));
  92.     y_cur3=x_cur1-20;//y0+R*qSin((t0+667)*(M_PI/(180*16)));
  93.     x_cur4=(x_cur3-230)*qCos(30*(M_PI/(180)))-(y_cur3-210)*qSin(30*(M_PI/(180)))+230;//x0+R*qCos((t0+667+Dt)*(M_PI/(180*16)));
  94.     y_cur4=(x_cur3-230)*qSin(30*(M_PI/(180)))+(y_cur3-210)*qCos(30*(M_PI/(180)))+210;//y0+R*qSin((t0+667+Dt)*(M_PI/(180*16)));
  95.     x_cur32=460-x_cur3;
  96.     y_cur32=420-y_cur3;
  97.     x_cur42=460-x_cur4;
  98.     y_cur42=420-y_cur4;*/
  99.  
  100.    /*x_cur1 = x0 + R*cos(0.0005*t0*M_PI);
  101.     y_cur1 = y0 + R*sin(0.0005*t0*M_PI);
  102.     x_cur2 = x0 + R*cos(0.0005*(t0+Dt)*M_PI);
  103.     y_cur2 = y0 + R*sin(0.0005*(t0+Dt)*M_PI);
  104.  
  105.     x_cur12 = x0 - R*sin(0.0005*t0*M_PI);
  106.     y_cur22 = y0 + R*cos(0.0005*t0*M_PI);
  107.     x_cur22 = x0 - R*sin(0.0005*(t0+Dt)*M_PI);
  108.     y_cur22 = y0 + R*cos(0.0005*(t0+Dt)*M_PI);
  109.  
  110.     x_cur3 = x0 - R*cos(0.0005*t0*M_PI);
  111.     y_cur3 = y0 - R*sin(0.0005*t0*M_PI);
  112.     x_cur4 = x0 - R*cos(0.0005*(t0+Dt)*M_PI);
  113.     y_cur4 = y0 - R*sin(0.0005*(t0+Dt)*M_PI);
  114.  
  115.     x_cur32 = x0 + R*sin(0.0005*t0*M_PI);
  116.     y_cur32 = y0 - R*cos(0.0005*t0*M_PI);
  117.     x_cur42 = x0 + R*sin(0.0005*(t0+Dt)*M_PI);
  118.     y_cur42 = y0 - R*cos(0.0005*(t0+Dt)*M_PI);*/
  119.  
  120.     x_hill1=x_hill1-0.1;
  121.     y_hill1=610;
  122.     x_hill2=430;
  123.     y_hill2=y_hill2-0.1;;
  124.     x_hill3=x_hill3+0.1;
  125.     y_hill3=610;
  126. //---------------------------
  127.     x_cur1=x0+R*cos(t0*(M_PI/(180)));
  128.     y_cur1=y0+R*sin(t0*(M_PI/(180)));
  129.     x_cur12=460-x_cur1;
  130.     y_cur12=420-y_cur1;
  131.     x_cur2=x0+R*cos((t0+Dt)*(M_PI/(180)));
  132.     y_cur2=y0+R*sin((t0+Dt)*(M_PI/(180)));
  133.     x_cur22=460-x_cur2;
  134.     y_cur22=420-y_cur2;
  135.     x_cur3=x0-R*sin(t0*(M_PI/180));
  136.     y_cur3=y0+R*cos(t0*(M_PI/180));
  137.     x_cur4=x0-R*sin((t0+Dt)*(M_PI/180));
  138.     y_cur4=y0+R*cos((t0+Dt)*(M_PI/180));
  139.     x_cur32=460-x_cur3;
  140.     y_cur32=420-y_cur3;
  141.     x_cur42=460-x_cur4;
  142.     y_cur42=420-y_cur4;
  143.     t0+=11;
  144.     repaint();
  145. }
  146.  
  147. void Widget::paintEvent(QPaintEvent*){
  148.     QPainter paint;
  149.     //paint.setRenderHint(QPainter::Antialiasing, true);
  150.     paint.begin(this);
  151.     draw_mill(paint);
  152.     paint.end();
  153. }
  154.  
  155. void Widget::on_pushButton_clicked()
  156. {
  157.     QSound sound("C:\\Users\\Константин\\Downloads\\Sound_06952.wav");
  158.     if(timer->isActive()){
  159.         ui->pushButton->setText("Start");
  160.         timer->stop();
  161.         sound.stop();
  162.     }
  163.     else{
  164.         ui->pushButton->setText("Stop");
  165.         timer->start();
  166.         //QSound::play("C:\\Users\\Константин\\Downloads\\Sound_06952.wav");
  167.         sound.setLoops(3);
  168.         sound.play();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement