废话少说先上图:

这个游戏纯属土鳖思路,没有用到什么游戏引擎。

1.使用按钮或QLabel铺满窗口。

2.通过简单算法随机动态的设置按钮矩阵中某个按钮的背景图像。

3.同步2过程反复设置多个按钮背景实现图像动起来,注意调节刷新时间。

4.重写按钮组鼠标事件,设置鼠标移入时光标的图案,可以是个小锤子。

5.单击事件后鼠标同理切换鼠标图案打下和抬起。

6.加入一点声音。

7.没有了。

相关代码:

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("打地鼠 难度:★ 成绩:5只 ");
w.setFixedSize(400,350);
w.show();
////保持窗口指针
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);//恢复锤子状态
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(100);
this->hit();
//处理
}
void MyButton::hit(){
QSound::play("hit.wav");
if(this->image != 0){
this->setIcon(QIcon("C:/src/hit.jpg"));
QSound::play("dead.wav");//相对路径
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_OBJECT
public: int image;//图片张数
MyButton(QWidget *parent);
void hit();
void mousePressEvent(QMouseEvent *e); private:
QTimer *upTime;
MyButton *btnGroup;
public slots:
void upCZ();//抬起锤子
}; #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);
//随机数进程
srand[0] = 0;//地鼠具体位置
srand[1] = 0;//地鼠具体位置
srand[2] = 0;//地鼠具体位置
srand[3] = 0;//地鼠具体位置
srand[4] = 0;//地鼠具体位置
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一行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++;
}
} //载入图像资源
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")));//鼠标抬起
for(int i = 0;i< 5;i++){//一次性更新5个位置
if(buttonGroup[srand[i]]->image < 6){
buttonGroup[srand[i]]->setIcon(*icoGroup[buttonGroup[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("地鼠位置:%d",srand[i]);
//分数
setWindowTitle("打地鼠 难度:★ 成绩:"+QString::number(score)+"只 ");
}
} } 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_OBJECT public:
static int score;//分数统计
Widget(QWidget *parent = 0);
void initBtn(int num);
MyButton **getBtnGroup();
~Widget(); private:
MyButton *buttonGroup[25];
QTimer *timer;//刷新界面事务
QIcon *icoGroup[6];
QTime time;
int srand[5];//具体位置
public slots:
void startRun();//开始游戏
}; #endif // WIDGET_H

思路原始,写法丑陋,大神请无视,初学者可以学习下。

FROM CTGU HOMEWORK

QT-Creator C/C++ 打地鼠小游戏的更多相关文章

  1. JavaScript+HTML5 实现打地鼠小游戏

    一.游戏简介 打地鼠这个游戏相信大家都不陌生,也是童年时候一款经典的游戏.本次游戏的编写是以html文件形式完成的,并且使用HBulider软件进行编写,使用谷歌浏览器展示效果,游戏将会采用JavaS ...

  2. [Cocos Creator]安卓上微信小游戏加载到100%就进不去了

    最近用Cocos Creator 开发微信小游戏,构建发布到微信上,用安卓手机预览打开后加载到100%就不动了,开始以为微信开发工具版本的问题,后来用苹果的手机测试了一下,发现也有同样的问题. 仔细分 ...

  3. 一个基于Myeclipse开发的Java打地鼠小游戏(Appletcation)

    package javaes.zixue.wangshang.daima; 2 3 import java.awt.Cursor; import java.awt.Image; import java ...

  4. 屏幕坐标和世界坐标的转换+对象池技术(3D打地鼠小游戏)

    游戏中可能经常会遇到需要某个物体跟着鼠标移动,然后又需要把物体放在某个鼠标指定的位置 实现方式 Camera.main.WorldToScreenPoint Camera.main.ScreenToW ...

  5. 原生js完成打地鼠小游戏

    :这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...

  6. 用html+css+js做打地鼠小游戏

    html 代码 first.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  7. 用原生javascript做的一个打地鼠的小游戏

    学习javascript也有一段时间了,一直以来分享的都是一些概念型的知识,今天有空做了一个打地鼠的小游戏,来跟大家分享一下,大家也可以下载来增加一些生活的乐趣,下面P出代码:首先是HTML部分代码: ...

  8. Qt Quick实现的疯狂算数游戏

    使用 Qt Quick 写了个小游戏:疯狂算数.支持 Windows 和 Android 两个平台. 游戏简单,但牵涉到下面你的 Qt Quick 主题: 自己实现一个按钮 自适应分辨率 国际化 QM ...

  9. Demo_2:Qt实现猜字小游戏

    1  环境 系统:windows 10 代码编写运行环境:Qt Creator 4.4.1 (community) Github: 2  简介 参考视频:https://www.bilibili.co ...

随机推荐

  1. 【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL

    Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. WEB标准:标准定义、好处、名词解释、常用术语、命名习惯、浏览器兼容、代码书写规范

    1. WEB标准是什么? “WEB标准”是一系列标准的总称.一般的误区经常把WEB标准说成DIV+CSS.准确的说法应该是:采用W3C推荐的WEB标准中的XHTML1.1结合CSS2.0 样式表制作页 ...

  3. C++标准程序库读书笔记-第二章新的语言特性

    1.基本类型的显式初始化 如果采用不含参数.明确的constructor(构造函数)调用语法,基本型别会被初始化为零: int i1; //undefined value int i2 = int() ...

  4. 浅谈设计模式在GIS中的应用

    设计模式在GIS中的应用 一.设计模式概述 随着面向对象技术的广泛应用,软件复用在越来越多的开发工程中被采用.在研究软件复用的过程中,设计模式的概念被提了出来.所谓设计模式就是一些设计面向对象的软件的 ...

  5. 利用ajax做的柱状图,线性统计图,饼状图

    柱状图,两个不同类型的数据 以下是html页面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  6. Go 语言读写 Excel

    Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于微软的 Office OpenXML 标准.可以使用它来读取.写入 XLSX 文件.相比较其他的开源类库 ...

  7. spring下配置dbcp,c3p0,proxool[转]

    不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不但可以通过JNDI获取应用 ...

  8. Javascript自定义类

    JavaScript并不是严格的面向对象的语言,但是带有面向对象的一些特性,我们可以通过这些特性创建js中的自定义类. JavaScript中的类其实是function关键字包裹的一系列变量和方法. ...

  9. CodeForces 190A Vasya and the Bus

    本题是比较简单的,有几个坑要注意一下: 1.n==0&&m!=0  时输出 "Impossible" : 2.n==0&&m==0 时输出 ”0 0 ...

  10. TensorFlow 深度学习笔记 卷积神经网络

    Convolutional Networks 转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Is ...