Qt-creator/C + + play hamster Games

Source: Internet
Author: User
Tags gopher

Say less nonsense first:

This game is purely the idea of the Earth Turtle, no use of any game engine.

1. Fill the window with a button or Qlabel.

2. Randomly dynamically set the background image of a button in a button matrix via a simple algorithm.

3. Synchronization 2 process repeatedly set multiple button background to achieve the image to move, pay attention to adjust the refresh time.

4. Override the button group mouse event, set the mouse to move into the time mark of the pattern, can be a small hammer.

5. Click on the event after the mouse to toggle the mouse pattern to lay and lift.

6. Add a little voice.

7. No more.

Related code:

Main.cpp#include "Widget.h" #include <qapplication>int widget::score;int main (int argc, char *argv[]) {    Qapplication A (argc, argv);    Widget W, *PW;    PW = &w;    W.setwindowtitle ("Hit the ground mouse   difficulty: ★   Score: 5 only");    W.setfixedsize (400,350);    W.show ();    Keep the window pointer    return a.exec ();}

  

Mybutton.cpp#include "Mybutton.h" #include <QWidget> #include <QMouseEvent> #include <qicon># Include <QSize> #include <QString> #include <QPixmap> #include <Qtimer> #include <QSound> #include "widget.h" Mybutton::mybutton (Qwidget *parent): Qpushbutton (parent) {    upTime = new Qtimer (this);//Restore Hammer State    uptime->connect (uptime,signal (timeout ()), This,slot (Upcz ()));   Btngroup = Widget ();      this->image = 0;} void Mybutton::mousepressevent (Qmouseevent *e) {     this->setcursor (qcursor (Qpixmap ("c:/src/c1.png"));     Uptime->start (+);     This->hit ();     Processing}void mybutton::hit () {    QSound::p Lay ("hit.wav");        if (this->image! = 0) {                 This->seticon (Qicon ("c:/src/hit.jpg"));                 QSound::p Lay ("dead.wav");//relative path                 widget::score++;        }} void Mybutton::upcz () {    this->setcursor (qcursor (Qpixmap ("c:/src/c0.png"));}

  

Mybutton.h#ifndef mybutton_h#define mybutton_h#include<qtimer> #include <QPushButton> #include < qwidget> #include <qmouseevent>class mybutton:public qpushbutton{    q_objectpublic:    int image;//picture count    MyButton (Qwidget *parent);    void hit ();    void Mousepressevent (qmouseevent *e);p rivate:    qtimer *uptime;    MyButton *btngroup;public Slots:    void Upcz ();//Lift the Hammer}; #endif//Mybutton_h

  

Widget.cpp#include "Widget.h" #include "mybutton.h" #include <QIcon> #include <QSize> #include <qtimer > #include <QDebug> #include <QCursor> #include <qpixmap>widget::widget (qwidget *parent): Qwidget (parent)   {initbtn (5);   Time= Qtime::currenttime ();   Qsrand (Time.msec () +time.second () *1000); Random number process srand[0] = 0;//location srand[1] = 0;//Gopher specific position srand[2] = 0;//Hamster specific position srand[3] = 0;//Gopher specific position srand[4] = 0;//ground   Mouse specific Position timer = new Qtimer (this);   Timer->connect (Timer,signal (timeout ()), This,slot (Startrun ()));   Timer->start (200);   Qpixmap QP (40,40);   Qp.load ("C:/src/c0.png");   Qp.scaled (60,60); This->setcursor (Qcursor (QP));}    void widget::initbtn (int num) {int counter = 0;            for (int i= 0;i < num;i++) {for (int j = 0, J < 5;j++) {Buttongroup[counter] = new MyButton (this);          Buttongroup[counter]->resize (80,70);//400/5=80 row of 5 buttongroup[counter]->move (80*j,70*i); Buttongroup[counter]->settext (Qstring::number (counter));            Buttongroup[counter]->setstylesheet ("border:0px;");            Buttongroup[counter]->seticon (Qicon ("c:/src/emptyhole.jpg"));            Buttongroup[counter]->seticonsize (Qsize (85,87));        counter++;    }}//Load image resource for (int i = 0; i < 6; i++) {Icogroup[i] = new Qicon ("c:/src/" +qstring::number (i) + ". jpg"); }}void Widget::startrun () {this->setcursor (Qcursor (Qpixmap ("c:/src/c0.png"));//mouse lifted for (int i = 0;i< 5;i++) {/ /One-time update 5 positions if (Buttongroup[srand[i]]->image < 6) {Buttongroup[srand[i]]->seticon (*icogroup[butto            ngroup[srand[i]]->image++]);        Buttongroup[srand[i]]->seticonsize (Qsize (85,87));            }else{Buttongroup[srand[i]]->seticon (Qicon ("c:/src/emptyhole.jpg"));            Buttongroup[srand[i]]->seticonsize (Qsize (85,87));            buttongroup[srand[i]]->image = 0;            Srand[i] = Qrand ()%25; Qdebug ("Gopher Location:%d", Srand[i]);        Score Setwindowtitle ("hit the Ground Mouse difficulty: ★ Score:" +qstring::number (Score) + "only"); }}}mybutton **widget::getbtngroup () {return buttongroup;} Widget::~widget () {}

  

Widget.h#ifndef widget_h#define widget_h#include <QWidget> #include "mybutton.h" #include <QIcon> #include <QTimer> #include <qtime>class widget:public qwidget{    q_objectpublic:    static int score;//fraction statistics    Widget (Qwidget *parent = 0);    void initbtn (int num);    MyButton **getbtngroup ();    ~widget ();p rivate:    MyButton *buttongroup[25];    Qtimer *timer;//Refresh interface transaction    Qicon *icogroup[6];    Qtime time;    int srand[5];//specific Location public slots:   void Startrun ();//Start Game}; #endif//Widget_h

Thinking primitive, the way ugly, the great god please ignore, beginners can learn under.

From Ctgu Homework

Qt-creator/C + + play hamster Games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.