QT-- eat moon game

QT-- eat moon game

Summary:

  • The design uses mouse events , keyboard events , graphics events , the characters may be controlled by moving the mouse or arrow keys to eat moon

Code:

widget.h file:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPaintEvent>  //绘图事件
#include <QMouseEvent>   //鼠标
#include <QPoint>
#include <QKeyEvent>   //键盘
#include <QPainter>
#include <QList>
#include <QLineEdit>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget (); 

public :
     int random_x;                              // Save the Moon random coordinates 
    int random_y;
     int direction;                             // save keyboard events 
    int Score = 0 ;                             // score 
    QString Score_data;                        // save the score 


    BOOL Meet = false ; 
    QPainter * Painter; 
    QLineEdit * Line; 

    void Wall_x ( int & );
     void Wall_y (int &);                        // meets the wall 
    void meet_s ();                             // encountered the moon, the moon redraw 
    int Xrandom ();                             // generate random numbers 
    int Yrandom ();                             // generate random numbers 
    void Direction ();                          // control People change direction 


    void paintEvent (QPaintEvent * Event );       // drawing 
    void mousePressEvent (QMouseEvent * Event ); // mouse click 
    void mouseMoveEvent (QMouseEvent * Event );   //Drag the mouse 
    void keyPressEvent (QKeyEvent * Event );      // keyboard events 
Private : 
    QPoint Point; 

Private : 
    Ui :: Widget * ui; 
}; 

#endif  // WIDGET_H
widget.cpp file:
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QPainter>  //绘图画家
#include <QTime>
#include <QMutableListIterator>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setMinimumSize(500,500);   //窗口大小



    point.setX(this->width()/2);                     // initialize the figure in the center of the window 
    point.setY ( the this -> width () / 2 ); 

    random_x = Xrandom ();                           // initialize moon 
    random_y = Yrandom (); 

} 

void the Widget :: mousePressEvent (QMouseEvent * Event )   // mouse click 
{ 
    qDebug () << Event -> POS (); 
    Point = Event -> POS (); 
    Update (); 
} 

void the Widget ::. mouseMoveEvent (QMouseEvent * Event )   //Mouse drag 
{ 
    qDebug () << Event -> POS (); 
    Point = Event -> POS (); 
    Update (); 
} 

void the Widget :: Wall_x ( int & X)        // met wall 
{
     IF (X <= 20 is ) 
    { 
        X + = 50 ;          // met spring back 
        Score - = . 3 ; 
    } 
    the else  IF (X> = the this -> width () - 40 ) 
    { 
        X - =50;
        Score -= 3;
    }
}
void Widget::Wall_y(int &y)      //遇见墙壁
{
    if(y <= 20)
    {
        y += 50;
        Score -= 3;
    }
    else if (y >= this->height()-40)
    {
        y -= 50;
        Score -= 3;
    }
}
void:: keyPressEvent the Widget (QKeyEvent * Event )   // keyboard events, character movement control 
{
     int X = point.x ();
     int Y = point.y (); 
    qDebug () << Event -> Key (); 
    direction = Event -> Key ();
     Switch ( Event -> Key ()) 
    { 
        Case the Qt :: KEY_UP:      // on 
            qDebug () << " on " ; 
            Y - = 10 ; 
            Wall_y (Y); 
            BREAK;
        case Qt::Key_Down:   //
            qDebug() <<"";
            y += 10;
            Wall_y(y);
            break;
        case Qt::Key_Left:   //
            qDebug() <<"";
             x -= 10;
             Wall_x(x);
            break;
        case Qt::Key_Right:  //
            qDebug() <<"";
             x += 10;
             Wall_x(x);
            break;

        default :
             break;
    }
    point.setX(x) ;
    point.setY(y);

    qDebug() <<x <<":" <<y;
}

int Widget::Xrandom()                    //产生随机数
{
    int random_q;
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);

    random_q = qrand() % (this->width()-40);
    if(random_q < 20)
        random_q += 20;
    return random_q;
}
int Widget::Yrandom()                    //产生随机数
{
    int random_q;
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);

    random_q = qrand() % (this->height()-40);
    if(random_q < 20)
        random_q += 20;
    return random_q;
}


void Widget::meet_s()    //遇到月亮
{

    if((point.x()-random_x < 20 && point.x()-random_x > -20)&& (point.y() - random_y < 20 &&point.y() - random_y > -20) )
        meet = true;
    if(!meet)
    {
        /*  豆豆  */
        QRectF target_Peas(random_x,random_y, 100, 100);
        QRectF source_Peas(0,0, 400 , 400 ); 
        QPixmap pixmap_Peas ( " : /new/prefix1/peas3.jpg " );          // here to use the resource files that you added pictures within a folder 
        painter-> drawPixmap (target_Peas, pixmap_Peas, source_Peas); 
    } 
    the else 
    { 
        qDebug () << " met " ; 
        random_x = Xrandom (); 
        random_y = Yrandom (); 
        Score ++ ; 
        meet = to false ; 
    } 
} 

void the Widget :: the Direction ()           // the control to change the direction of the character 
{ 
    QRectF target ( - 10 , - 10 , 40.0 , 40.0 ); 
    QRectF Source ( 0.0 , 0.0 , 400.0 , 400.0 ); 
    QPixmap pixmap_right ( " : /new/prefix1/character_right.jpg " );         / / used herein pictures folder in the resource files that you added 
    QPixmap pixmap1_left ( " : /new/prefix1/character_left.jpg " );          // characters move to the left is a picture 

    IF (Qt :: KEY_UP == direction)                               // on
     {
         painter->rotate(-90);
         painter->drawPixmap(target, pixmap_right, source);
    }

    else if(Qt::Key_Down == direction)                      //
    {
         painter->rotate(90);
         painter->drawPixmap(target, pixmap_right, source);
    }
    else if(Qt::Key_Left == direction)                      //
    {
         painter->drawPixmap(target, pixmap1_left, source);
    }
    else                                                    //
    {
        painter->drawPixmap(target, pixmap_right, source);
    }
}
void Widget::paintEvent(QPaintEvent *event)
{
    painter = new QPainter(this);

    /*  画笔    */
    QPen pen (Qt::red,10);
    QPen pen1 (Qt::green,5);
    painter->setPen(pen1);

    /*  边框  */
    QRectF rectangle(0, 0,this->width(),this->width());
    QRectF rectangle1(10, 10, this->width()-20, this->height()-20);

    /*  背景  */
    QRectF target(0,0, this->width(), this->width());
    QRectF source(0,0, this->width(), this->width());
    QPixmap pixmap_background(":/new/prefix1/background.jpg");
    painter->drawPixmap(target, pixmap_background, source);


    painter->setRenderHint(QPainter::Antialiasing,true);  //抗锯齿

    /*  画边框  */
    painter->drawRect(rectangle);
    painter->drawRect(rectangle1);

    /*  得分  */
    Score_data = "得分:" + QString::number(Score);
    painter->drawText(0,0,100,100,Qt::AlignCenter,Score_data);
    painter->setPen(pen);


    meet_s ();                                          // draw the moon 
    painter-> Translate (point.x () + 10 , point.y () + 10 );     // Mobile painter 

    Direction ();                                       // draw the characters 
    Update ();                                          // must be refresh 

} 

the Widget :: ~ the Widget () 
{ 
    Delete UI; 
}
Run effect diagram:

Guess you like

Origin www.cnblogs.com/IntelligencePointer/p/11519704.html