QT-Creator C/C++ 打地鼠小游戏
废话少说先上图:
这个游戏纯属土鳖思路,没有用到什么游戏引擎。
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++ 打地鼠小游戏的更多相关文章
- JavaScript+HTML5 实现打地鼠小游戏
一.游戏简介 打地鼠这个游戏相信大家都不陌生,也是童年时候一款经典的游戏.本次游戏的编写是以html文件形式完成的,并且使用HBulider软件进行编写,使用谷歌浏览器展示效果,游戏将会采用JavaS ...
- [Cocos Creator]安卓上微信小游戏加载到100%就进不去了
最近用Cocos Creator 开发微信小游戏,构建发布到微信上,用安卓手机预览打开后加载到100%就不动了,开始以为微信开发工具版本的问题,后来用苹果的手机测试了一下,发现也有同样的问题. 仔细分 ...
- 一个基于Myeclipse开发的Java打地鼠小游戏(Appletcation)
package javaes.zixue.wangshang.daima; 2 3 import java.awt.Cursor; import java.awt.Image; import java ...
- 屏幕坐标和世界坐标的转换+对象池技术(3D打地鼠小游戏)
游戏中可能经常会遇到需要某个物体跟着鼠标移动,然后又需要把物体放在某个鼠标指定的位置 实现方式 Camera.main.WorldToScreenPoint Camera.main.ScreenToW ...
- 原生js完成打地鼠小游戏
:这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...
- 用html+css+js做打地鼠小游戏
html 代码 first.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- 用原生javascript做的一个打地鼠的小游戏
学习javascript也有一段时间了,一直以来分享的都是一些概念型的知识,今天有空做了一个打地鼠的小游戏,来跟大家分享一下,大家也可以下载来增加一些生活的乐趣,下面P出代码:首先是HTML部分代码: ...
- Qt Quick实现的疯狂算数游戏
使用 Qt Quick 写了个小游戏:疯狂算数.支持 Windows 和 Android 两个平台. 游戏简单,但牵涉到下面你的 Qt Quick 主题: 自己实现一个按钮 自适应分辨率 国际化 QM ...
- Demo_2:Qt实现猜字小游戏
1 环境 系统:windows 10 代码编写运行环境:Qt Creator 4.4.1 (community) Github: 2 简介 参考视频:https://www.bilibili.co ...
随机推荐
- wpf msdn在线地址http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
- mvc 日历控件
第二个是日历控件,在网上查了一个普通的日历控件,也生成了下拉的日历样子,但是一些脚本比如选择年月,需要一些时间,最后只好套用了My97 DatePicker,这样以来其实简单多了. 第一步:下载 My ...
- 去掉firefox点击按钮时的虚线边框
去掉火狐里面点击按钮时候的虚线边框 button::-moz-focus-inner, input[type="reset"]::-moz-focus-inner, input[t ...
- 《JavaScript 闯关记》之语句
表达式在 JavaScript 中是短语,那么语句就是整句命令.表达式用来计算出一个值,语句用来执行以使某件事发生.从本质上看,语句定义了 JavaScript 中的主要语法,语句通常使用一或多个关键 ...
- Windows7中安装内存与可用内存不一致的解决办法
转载:http://blog.sina.com.cn/s/blog_56741a420100h9d1.html 问题现象: 安装完Windows7后,在计算机->属性中,会看到安装内存.但有时在 ...
- 笔记本开通手机WiFI热点
启用虚拟wifi: netsh wlan set hostednetwork mode=allow ssid=liuyuduen key=liuyuduen 关闭虚拟Wifi: netsh wlan ...
- CMD下修改IP地址
@echo off netsh interface ip set address name="本地连接" static 192.168.1.55 255.255.255.0 192 ...
- UITabBarController+微博简单模拟1
UITabBarController是IOS中很常用的一个viewController.UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的c ...
- G - 密码 kmp、贪心、manachar等等等等
G - 密码 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit Status ...
- 无法启动计算机"."上的服务w3svc
在启动IIS服务的时候出现错误: 无法启动计算机"."上的服务w3svc 解决方法: /* 修复错误 运行命令提示符 fsutil resource setautoreset tr ...