Arc* arcanoid;
#ifndef MAINCLASS_H#define MAINCLASS_H#include <QTimer>#include <QWidget>#include <QKeyEvent>#include "qt_windows.h"#include "arc.h"#include "ball.h"#include "blocks.h"class MainClass : public QWidget{ Q_OBJECTpublic: MainClass(QWidget *parent = 0); int screen; Arc* arcanoid; Ball* ball; bool isGame; QTimer* timer; int kx, ky; Blocks* field; int menuIndex; QList<QPixmap> menuList; QSize currentSize; qreal scaleParam; ~MainClass();private: virtual void paintEvent(QPaintEvent*); virtual void keyPressEvent(QKeyEvent* event); bool winEvent(MSG *message, long *result);private slots: void move();};#endif // MAINCLASS_H
#include "mainclass.h"#include <QPainter>#include <QDebug>#include <QApplication>#include <QColor>#include "qt_windows.h"/*увеличение скорости в течение игры; пропорции при изменение окна*/MainClass::MainClass(QWidget *parent) : QWidget(parent){ screen=0; currentSize=QSize(600, 600); scaleParam=(qreal)currentSize.width() / currentSize.height(); setMinimumSize(currentSize);}void MainClass::paintEvent(QPaintEvent *){ QPainter painter; painter.begin(this); painter.setRenderHint(QPainter::Antialiasing, true); if(screen==0){ painter.drawPixmap(0, 0, width(), height(), QPixmap(":image/Images/ScreenSaver.png")); }else if(screen==1){ painter.fillRect(0, 0, width(), height(), Qt::white); painter.drawPixmap(width()/2-menuList[menuIndex].width()/2, height()/2-menuList[menuIndex].height()/2, menuList[menuIndex].width(), menuList[menuIndex].height(), menuList[menuIndex]); } else if(screen==2){ painter.fillRect(0, 0, width(), height(), Qt::white); painter.drawPixmap(arcanoid->getPos().x(), arcanoid->getPos().y(), arcanoid->getLength(), arcanoid->getHeight(), arcanoid->getPix()); painter.drawPixmap(ball->getPos().x(), ball->getPos().y(), ball->getWidth(), ball->getHeight(), ball->getPix()); for(int i=0;i<5;i++){ for(int j=0;j<10;j++){ if(field->getElement(i, j)==1){ painter.drawPixmap(j*field->getWidth(), i*field->getHeight(), field->getWidth(), field->getHeight(), field->getPix(0)); }else if(field->getElement(i, j)==2){ painter.drawPixmap(j*field->getWidth(), i*field->getHeight(), field->getWidth(), field->getHeight(), field->getPix(1)); }else if(field->getElement(i, j)==3){ painter.drawPixmap(j*field->getWidth(), i*field->getHeight(), field->getWidth(), field->getHeight(), field->getPix(2)); }else if(field->getElement(i, j)==4){ painter.drawPixmap(j*field->getWidth(), i*field->getHeight(), field->getWidth(), field->getHeight(), field->getPix(3)); }else if(field->getElement(i, j)==5){ painter.drawPixmap(j*field->getWidth(), i*field->getHeight(), field->getWidth(), field->getHeight(), field->getPix(4)); } } } } painter.end();}void MainClass::keyPressEvent(QKeyEvent *event){ if(screen==0){ if(event->key()==Qt::Key_Return){ menuList.insert(0, QPixmap(":image/Images/start.png")); menuList.insert(1, QPixmap(":image/Images/exit.png")); menuIndex=0; screen=1; }else if(event->key()==Qt::Key_Escape){ emit qApp->quit(); } }else if(screen==1){ if(event->key()==Qt::Key_Down){ if(menuIndex==0){ menuIndex++; } }else if(event->key()==Qt::Key_Up){ if(menuIndex==1){ menuIndex--; } }else if(event->key()==Qt::Key_Return){ if(menuIndex==0){ isGame=false; kx=-1; ky=1; arcanoid=new Arc; ball=new Ball; field=new Blocks; timer=new QTimer(this); timer->setInterval(4); connect(timer, SIGNAL(timeout()), this, SLOT(move())); screen=2; }else{ emit qApp->quit(); } } }else if(screen==2){ if(event->key()==Qt::Key_Left){ if(arcanoid->getPos().x()>0){ if(0+arcanoid->getPos().x()>=width()/20){ arcanoid->setPosX(arcanoid->getPos().x()-width()/20); if(!isGame){ ball->setPosX(ball->getPos().x()-width()/20); } }else{ arcanoid->setPosX(0); if(!isGame){ ball->setPosX(0+(arcanoid->getLength()/2)-(ball->getWidth()/2)); } } } }else if(event->key()==Qt::Key_Right){ if(arcanoid->getPos().x()<this->width()-arcanoid->getLength()){ if(width()-(arcanoid->getPos().x()+arcanoid->getLength())>=width()/20){ arcanoid->setPosX(arcanoid->getPos().x()+width()/20); if(!isGame){ ball->setPosX(ball->getPos().x()+width()/20); } }else{ arcanoid->setPosX(width()-arcanoid->getLength()); if(!isGame){ ball->setPosX(width()-(arcanoid->getLength()/2)-(ball->getWidth()/2)); } } } }else if((event->key()==Qt::Key_Space)&(!isGame)){ isGame=true; timer->start(); }else if(event->key()==Qt::Key_Escape){ emit qApp->quit(); } } update();}void MainClass::move(){ if(ball->getPos().x()==this->width()-ball->getWidth()){ kx*=-1; } if(ball->getPos().x()==0){ kx*=-1; } if((ball->getPos().y()==this->height()-arcanoid->getHeight()-ball->getHeight())&(ball->getPos().x()+ball->getWidth()>=arcanoid->getPos().x())&(ball->getPos().x()<=arcanoid->getPos().x()+arcanoid->getLength())){ ky*=-1; } if(ball->getPos().y()<=0){ ky*=-1; } for(int i=0;i<5;i++){ for(int j=0;j<10;j++){ if(field->getElement(i,j)>0){ if((ball->getPos().x()+ball->getWidth()==j*field->getWidth())||(ball->getPos().x()==(j+1)*field->getWidth()-1)){ if((ball->getPos().y()+ball->getHeight()>=i*field->getHeight())&&(ball->getPos().y()<=(i+1)*field->getHeight())){ kx*=-1; field->setElement(i, j, field->getElement(i,j)-1); field->decNumOfBlocks(); break; } } if((ball->getPos().y()+ball->getHeight()==i*field->getHeight())||(ball->getPos().y()==(i+1)*field->getHeight())){ if(((ball->getPos().x())<=(j+1)*field->getWidth())&&(ball->getPos().x()+ball->getWidth()>=j*field->getWidth())){ ky*=-1; field->setElement(i, j, field->getElement(i,j)-1); field->decNumOfBlocks(); break; } } } } } ball->setPos(QPoint(ball->getPos().x()+(kx*1), ball->getPos().y()+(ky*1))); if((ball->getPos().y()>this->height())|(field->getNumOfBlocks()==0)){ timer->stop(); delete arcanoid; delete ball; delete field; disconnect(timer, SIGNAL(timeout()), this, SLOT(move())); delete timer; screen=1; } update();}bool MainClass::winEvent(MSG *message, long *result){ switch(message->message){ case(WM_SIZING):{ // Получаем прямоугольник окна, которым мы должны стать RECT* rect = (RECT*) message->lParam; // Вычленяем размеры рабочей области int fWidth = frameGeometry().width() - width(); int fHeight = frameGeometry().height() - height(); int nWidth = rect->right-rect->left - fWidth; int nHeight = rect->bottom-rect->top - fHeight; // Меняем размеры на нужные нам switch(message->wParam) { case WMSZ_BOTTOM: case WMSZ_TOP: rect->right = rect->left+(qreal)nHeight*m_scaleParamWtoH + fWidth; break; case WMSZ_BOTTOMLEFT: case WMSZ_BOTTOMRIGHT: if( (qreal)nWidth / nHeight > m_scaleParamWtoH ){ rect->bottom = rect->top+(qreal)nWidth/scaleParam +fHeight; }else{ rect->right = rect->left+(qreal)nHeight*scaleParam + fWidth; } break; case WMSZ_LEFT: case WMSZ_RIGHT: rect->bottom = rect->top+(qreal)nWidth/scaleParam +fHeight; break; case WMSZ_TOPLEFT: case WMSZ_TOPRIGHT: if( (qreal)nWidth / nHeight > scaleParam ){ rect->bottom = rect->top+(qreal)nWidth/scaleParam +fHeight; }else{ rect->right = rect->left+(qreal)nHeight*scaleParam + fWidth; } break; } } return true; default: return false; };}MainClass::~MainClass(){}
#ifndef ARC_H#define ARC_H#include <QPixmap>class Arc{public: Arc(); ~Arc(); QPixmap getPix(); QPoint getPos(); void setPosX(int value); void setPos(QPoint value); int getLength(); int getHeight(); void setLength(int value); void setHeight(int value);private: QPixmap pix; QPoint pos; int length, height;};#endif // ARC_H
#include "arc.h"#include <QDebug>Arc::Arc(): pix(":image/Images/arc.png"), length(100), height(20){ pos.setX(250); pos.setY(480);}QPixmap Arc::getPix(){ return pix;}QPoint Arc::getPos(){ return pos;}void Arc::setPosX(int value){ pos.setX(value);}void Arc::setPos(QPoint value){ pos=value;}int Arc::getLength(){ return length;}int Arc::getHeight(){ return height;}void Arc::setHeight(int value){ height=value;}void Arc::setLength(int value){ length=value;}Arc::~Arc(){}