项目描述: QT 设计的攻防闯关单机游戏,鼠标控制一 只羊在不接触 4 只(有规律移动)狼的情况下, 吃掉草原里所有的草方可通关,否则被狼吃掉。 使用技术: 采用 QPainter 绘图工具绘制和刷新画面;使用了 QT 图形界面设计工具;重写 QT 事件处理函数。
QT += core gui
QT += multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
gametime.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
gametime.h \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
rs.qrc \
srgame.qrc
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
#include <QMessageBox>
#include <QMediaPlayer>
#include "gametime.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void mousePressEvent(QMouseEvent *event); //按压鼠标
//void mouseReleaseEvent(QMouseEvent *event); //松开鼠标
//void mouseDoubleClickEvent(QMouseEvent *event); //双击
//void mouseMoveEvent(QMouseEvent *event); //鼠标移动
//void wheelEvent(QWheelEvent *event); //滚轮滑动
//void keyPressEvent(QKeyEvent *event); //键盘按下
//void keyReleaseEvent(QKeyEvent *event); //键盘松开
void enterEvent(QEvent *event); //鼠标进入
void leaveEvent(QEvent *event); //鼠标离开
void paintEvent(QPaintEvent *event);
public slots:
void threadRunSlotFun();
void btnAgainSlotFun();
void btnBeginSlotFun();
void positionChanged(qint64);
private:
Ui::MainWindow *ui;
//QPainter *pPainter;
int MouseKey,movSheep,movSheepLast;
bool movSheepkey;
int MouseMoveX,MouseMoveY;
int MouseX,MouseY;
GameTime *threadRun;
int grassNum[42];
int grassKey[42];
int gameWin,gameScore;
QMediaPlayer *player;
QPixmap *pix_7;
int customsPass;
};
#endif // MAINWINDOW_H
#ifndef GAMETIME_H
#define GAMETIME_H
#include <QObject>
#include <QThread>
#include <QAbstractItemView>
class GameTime :public QThread
{
Q_OBJECT
signals:
void repRun();
public:
GameTime(QObject *parent);
~GameTime();
void run();//线程
};
#endif // GAMETIME_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gametime.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
MouseKey=0;
//winKey=0;
threadRun = new GameTime(this);
connect(threadRun,SIGNAL(repRun()),this,SLOT(threadRunSlotFun()));
threadRun->start();
MouseX=700;
MouseY=500;
movSheep=0;
movSheepLast=0;
movSheepkey=0;
gameScore=0;
customsPass=0;
for(int i=0;i<42;i++){
grassNum[i]=rand()%4;
grassKey[i]=1;
}
connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(btnBeginSlotFun()));
ui->groupBox_2->setGeometry(190,150,380,230);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(btnAgainSlotFun()));
ui->groupBox->setGeometry(2000,1700,380,230);
player = new QMediaPlayer(this);
player->setMedia(QUrl("qrc:/rs/game.mp3"));
player->setVolume(100);
player->play();
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(MouseKey==0){
return;
}
MouseKey=2;
MouseMoveX=event->x();
MouseMoveY=event->y();
}
void MainWindow::enterEvent(QEvent *event)
{
MouseKey=1;
}
void MainWindow::leaveEvent(QEvent *event)//鼠标退出
{
MouseKey=0;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QString pixStr[8];
pixStr[0] = ":/rs/cao01.png";
pixStr[1] = ":/rs/cao02.png";
pixStr[2] = ":/rs/cao03.png";
pixStr[3] = ":/rs/cao04.png";
pixStr[4] = ":/rs/yang01.png";
pixStr[5] = ":/rs/yang02.png";
pixStr[6] = ":/rs/yang03.png";
pixStr[7] = ":/rs/yang04.png";
QPixmap pix_1(":/rs/000111.png");
int sheepCoor[2];
int wolfCoor[8];
if(customsPass==0){
sheepCoor[0]=MouseX-50;
sheepCoor[1]=MouseY-50;//羊轨迹
wolfCoor[0]=395;
wolfCoor[1]=movSheep*2+20;
wolfCoor[2]=600-movSheep*4;
wolfCoor[3]=10+movSheep*4;
wolfCoor[4]=movSheep*3+15;
wolfCoor[5]=3*movSheep;
wolfCoor[6]=250;
wolfCoor[7]=380-movSheep*3;//狼轨迹
}else if(customsPass==1){
sheepCoor[0]=MouseX-50;
sheepCoor[1]=MouseY-50;//羊轨迹
wolfCoor[0]=390-movSheep*3;
wolfCoor[1]=movSheep*2+20;
wolfCoor[2]=600-movSheep*4;
wolfCoor[3]=10+movSheep*movSheep/20;
wolfCoor[4]=movSheep*movSheep*movSheep/230+15;
wolfCoor[5]=3*movSheep;
wolfCoor[6]=movSheep;
wolfCoor[7]=380-movSheep*3;//狼轨迹
}
QPainter pPainter(this);
QPen pen; pen.setWidth(2); pen.setColor( QColor(0,255,0) ) ; pen.setStyle(Qt::SolidLine);
QBrush brush; brush.setColor(QColor(0,255,0)); brush.setStyle(Qt::SolidPattern);
pPainter.setPen(pen); pPainter.setBrush(brush);
//pPainter.translate((this->width())/2-400,(this->height())/2-300);
pPainter.drawPixmap(0,0,this->width(),this->height(),pix_1);
int k=0;
for(int i=0;i<7;i++){ //(i*100)<width();i++){
for(int j=0;j<6;j++){
if(grassKey[k]==0){
QPixmap pix(pixStr[ grassNum[k] ]);
pPainter.drawPixmap(i*100+1000,j*100+1000,50,50,pix);
k++;
qDebug()<<gameScore<<endl;
continue;
}
if(grassKey[k]==1 && ( ( (i*100+80-25) - (sheepCoor[0]))<20) && ((((sheepCoor[0])) - (i*100+80-25) )<20) && (((j*100+20-25) - ((sheepCoor[1])))<20) && ((((sheepCoor[1])) - (j*100+20-25))<20) ){
QPixmap pix(pixStr[ grassNum[k] ]);
pPainter.drawPixmap(i*100+1000,j*100+1000,50,50,pix);
grassKey[k]=0;
k++;
gameScore++;
qDebug()<<gameScore<<endl;
continue;
}
QPixmap pix(pixStr[ grassNum[k] ]);
pPainter.drawPixmap(i*100+80,j*100+20,50,50,pix);
k++;
qDebug()<<gameScore<<endl;
}
}
if(gameWin!=3){
return;
}
for(int i=0;i<4;i++){
if(i<2){
QPixmap pix(pixStr[ 4 ]);
pPainter.drawPixmap(wolfCoor[2*i],wolfCoor[2*i+1],100,100,pix);
}else{
QPixmap pix(pixStr[ 6 ]);
pPainter.drawPixmap(wolfCoor[2*i],wolfCoor[2*i+1],100,100,pix);
}
}
if(movSheepLast>sheepCoor[0]){
pix_7=new QPixmap(pixStr[ 5 ]);
}
else if(movSheepLast<sheepCoor[0]){
pix_7=new QPixmap(pixStr[ 7 ]);
}
pPainter.drawPixmap(sheepCoor[0],sheepCoor[1],100,100,*pix_7);
movSheepLast=sheepCoor[0];
for(int i=0;i<4;i++){
if( ( (wolfCoor[2*i] - sheepCoor[0])<50) && ((sheepCoor[0] - wolfCoor[2*i])<50) && ((wolfCoor[2*i+1] - sheepCoor[1])<50) && ((sheepCoor[1] - wolfCoor[2*i+1])<50) ){
gameWin=2;
}
}
if(gameScore>=42){
gameWin=1;
}
}
void MainWindow::threadRunSlotFun()
{
if(gameWin==2){
ui->groupBox->setGeometry(200,170,380,230);
ui->label_2->setText("被吃掉了。。。");
ui->pushButton_4->setText(QString("%1").arg(gameScore));
//update();
return;
}else if(gameWin==1){
ui->groupBox->setGeometry(200,170,380,230);
ui->label_2->setText("你胜利了。。。");
ui->pushButton_4->setText(QString("%1").arg(gameScore));
customsPass++;
if(customsPass>=2){customsPass=0;}
return;
}
if(movSheepkey==0){
movSheep++;
}else{
movSheep--;
}
if(movSheep>=100){movSheepkey=1;}
else if(movSheep<=0){movSheepkey=0;}
if(MouseKey!=2){
update();
return;
}
if((MouseMoveX/10)>(MouseX/10)){
MouseX+=10;
}else if((MouseMoveX/10)<(MouseX/10)){
MouseX-=10;
}
if((MouseMoveY/10>MouseY/10)){ // (MouseMoveX/10)==(MouseX/10) &&
MouseY+=10;
}else if( (MouseMoveY/10<MouseY/10)){ //(MouseMoveX/10)==(MouseX/10) &&
MouseY-=10;
}
update();
}
void MainWindow::btnBeginSlotFun()
{
gameWin=3;
gameScore=0;
ui->groupBox_2->setGeometry(2000,2000,380,230);//开始
}
void MainWindow::positionChanged(qint64)
{
player->play();
}
void MainWindow::btnAgainSlotFun()
{
gameWin=3;
gameScore=0;
ui->groupBox->setGeometry(2000,1700,380,230);//被吃
for(int i=0;i<42;i++){
grassNum[i]=rand()%4;
grassKey[i]=1;
}
MouseX=700;
MouseY=500;
movSheep=0;
movSheepkey=0;
MouseMoveX=700;
MouseMoveY=500;
}
#include "gametime.h"
GameTime::GameTime(QObject *parent):
QThread(parent)
{
}
GameTime::~GameTime()
{
}
void GameTime::run()
{
/*QTime current_time =QTime::currentTime();
int hour = current_time.hour();//当前的小时
int minute = current_time.minute();//当前的分
int second = current_time.second();//当前的秒
int msec = current_time.msec();//当前的毫秒*/
while(1){
msleep(50);
emit repRun();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>狡猾的灰太狼</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>390</x>
<y>0</y>
<width>381</width>
<height>231</height>
</rect>
</property>
<property name="title">
<string>游戏结束</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>390</width>
<height>230</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="srgame.qrc">:/rs/gamepix.png</pixmap>
</property>
</widget>
<widget class="QTextBrowser" name="textBrowser">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="html">
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun'; font-size:11pt; font-weight:600;">游戏结束</span></p></body></html></string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>80</x>
<y>70</y>
<width>231</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>你 被 吃 了。。。</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>130</x>
<y>160</y>
<width>141</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>再开一把</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>90</x>
<y>120</y>
<width>93</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>看看分数:</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_4">
<property name="geometry">
<rect>
<x>182</x>
<y>120</y>
<width>121</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>190</x>
<y>150</y>
<width>381</width>
<height>231</height>
</rect>
</property>
<property name="title">
<string>游戏结束</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>390</width>
<height>230</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="srgame.qrc">:/rs/gamepix.png</pixmap>
</property>
</widget>
<widget class="QTextBrowser" name="textBrowser_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="html">
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun'; font-size:11pt; font-weight:600;">游戏开始</span></p></body></html></string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>90</x>
<y>110</y>
<width>231</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>是兄弟就来吃草。。。</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>90</x>
<y>160</y>
<width>211</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>开始吧!</string>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="srgame.qrc"/>
</resources>
<connections/>
</ui>