#include "gamescene.h"
GameScene::GameScene(QWidget *parent) : QWidget(parent) {
setWindowTitle("超级玛丽");//设置标题
setFixedSize(800, 545);//设置窗口大小
Game_Init();
Pause_Init();
QTimer::singleShot(1500, this, [=]() {
timer1 = startTimer(15);
timer3 = startTimer(40);
game_start = true;
});
}
void GameScene::Pause_Init() {
Pause = new GamePause();//初始化暂停窗口
MyPushButton *btn_continue = new MyPushButton(":/photo/continueGame.png");//添加继续按钮
btn_continue->setParent(Pause);
btn_continue->setFixedSize(150, 75);
btn_continue->setIconSize(QSize(150, 75));
btn_continue->move(20, 10);
connect(btn_continue, &QPushButton::clicked, this, [=]() {
btn_continue->zoom1();//加载动画
btn_continue->zoom2();
QTimer::singleShot(500, this, [=]() {
timer1 = startTimer(15);
timer3 = startTimer(40);
mary->walk_state = 0;//初始化mary的行走状态
key = "null";
Pause->close();
});
});
MyPushButton *initGame = new MyPushButton(":/photo/initGame.png");//添加初始化窗口
initGame->setParent(Pause);
initGame->setFixedSize(150, 75);
initGame->setIconSize(QSize(150, 75));
initGame->move(20, 90);
connect(initGame, &QPushButton::clicked, this, [=]() {
initGame->zoom1();
initGame->zoom2();
QTimer::singleShot(500, this, [=]() {
Pause_Game_Init();//游戏初始化
Pause->close();
QTimer::singleShot(1500, this, [=]() {
timer1 = startTimer(15);//开启定时器
timer3 = startTimer(40);
game_start = true;
});
});
});
MyPushButton *btn_exit = new MyPushButton(":/photo/start.png");//添加离开按钮
btn_exit->setParent(Pause);
btn_exit->setFixedSize(150, 75);
btn_exit->setIconSize(QSize(150, 75));
btn_exit->move(20, 170);
connect(btn_exit, &QPushButton::clicked, this, [=]() {
btn_exit->zoom1();
btn_exit->zoom2();
QTimer::singleShot(500, this, [=]() {
this->close();
});
});
}
void GameScene::timerEvent(QTimerEvent *event) {
if (event->timerId() == timer1 && mary->is_die) {
mary->Mary_die();
Die_Init();
update();
return;
}
if (event->timerId() == timer1) {
mary->Mary_Move(key);
mary->Jump_And_Down();
Jump_Collision();
Move_Collision();
brick->ShatterState();
mushroom->Move_state();
master->Master_Move();
Die_Init();
Fall_Down(mary->y);
fire->Fire_state();
update();
}
if (event->timerId() == timer2) {
mary->Mary_Move(key);
}
if (event->timerId() == timer3) {
time -= 0.04;
unknown->Unknown_State();
unknown->Crash_state();
}
}
void GameScene::keyPressEvent(QKeyEvent *event) {
if (!mary->is_die) {
switch (event->key()) {
case Qt::Key_Right:
mary->direction = key = "right";
break;
case Qt::Key_Left:
mary->direction = key = "left";
break;
case Qt::Key_Z:
timer2 = startTimer(25);
is_kill_timer2 = false;
break;
case Qt::Key_Space:
mary->is_jump = true;
break;
case Qt::Key_B:
if (game_start) {
killTimer(timer1);
if (is_kill_timer2) {
killTimer(timer2);
}
killTimer(timer3);
Pause->setParent(this);
Pause->open();
}
break;
case Qt::Key_C:
if (mary->life < 8) {
mary->life++;
}
case Qt::Key_X:
if (!is_press_x && !mary->is_jump && mary->is_jump_end && mary->colour == 3) {
is_press_x = true;
fire->Fire_xy();
}
break;
}
}
}
void GameScene::keyReleaseEvent(QKeyEvent *event) {
if (!mary->is_die) {
switch (event->key()) {
case Qt::Key_Right:
mary->walk_state = 0;
key = "null";
break;
case Qt::Key_Left:
mary->walk_state = 0;
key = "null";
break;
case Qt::Key_Z:
is_kill_timer2 = true;
killTimer(timer2);
break;
case Qt::Key_Space:
mary->is_jump = false;
mary->is_space_release = true;
break;
case Qt::Key_X:
is_press_x = false;
break;
}
}
}
void GameScene::paintEvent(QPaintEvent *) {
QPainter painter(this);
if (!game_start) {
painter.drawPixmap(0, 0, 800, 550, QPixmap(":/photo/blackground2.png"));
painter.drawPixmap(300, 250, 40, 40, QPixmap(":/photo/life.png"));
painter.setPen(QColor(255, 255, 255));
QFont font;
font.setPointSize(35);
painter.setFont(font);
painter.drawText(360, 280, "x");
painter.drawText(110, 30, "times:");
painter.drawText(220, 32, QString::number(time, 'f', 1));
painter.drawText(600, 30, "coin:");
painter.drawText(680, 32, QString::number(unknown->coin));
font.setPointSize(45);
painter.setFont(font);
painter.drawText(400, 287, QString::number(mary->life));
return;
}
painter.drawPixmap(0, 0, 800, 550, QPixmap(":/photo/sky.png"));//画背景
painter.drawPixmap(230, 10, QPixmap(":/photo/coin.png"), 0, 0, 30, 30);
painter.drawPixmap(380, 10, 30, 30, QPixmap(":/photo/score.png"));
painter.setFont(QFont("Times", 45, QFont::Bold));
painter.drawText(280, 38, QString::number(unknown->coin));
painter.drawText(430, 38, QString::number(score));
for (int i = 1; i <= mary->life; i++) {
painter.drawPixmap(800 - i * 35, 10, 30, 30, QPixmap(":/photo/life.png"));
}
painter.drawPixmap(10, 10, 30, 30, QPixmap(":/photo/time.png"));
painter.drawText(50, 38, QString::number(time, 'f', 1));
painter.drawPixmap(0, 500, QPixmap(":/photo/ground.png"), mary->ground_state, 0, 800, 45);//画地板
if (mary->x > 7800) {
QVector < QVector < int >> ::iterator
it = castle->m.begin()->begin();
painter.drawPixmap(*it->begin() - mary->x, *(it->begin() + 1), 200, 200, QPixmap(":/photo/castle.png"));
}
if (mushroom->mushroom_state != 0) {
painter.drawPixmap(mushroom->mushroom_x - mary->x, mushroom->mushroom_y, 40, 40,
QPixmap(":/photo/mushroom" + QString::number(mary->colour) + ".png"));
}
for (QVector < QVector < int >> ::iterator it = brick->m.begin()->begin(); it != brick->m.begin()->end();
it++)
{
if (*(it->begin()) - mary->x > -50 && *(it->begin()) - mary->x < 800 && *(it->begin() + 2) == 1) {
painter.drawPixmap(*(it->begin()) - mary->x, *(it->begin() + 1), 50, 40, QPixmap(":/photo/brick1.png"));
}
}
for (QVector < QVector < int >> ::iterator it = unknown->m.begin()->begin(); it != unknown->m.begin()->end();
it++)
{
if (*(it->begin()) - mary->x > -50 && *(it->begin()) - mary->x < 800 && *(it->begin() + 2) != 0) {
painter.drawPixmap(*(it->begin()) - mary->x, *(it->begin() + 1), QPixmap(":/photo/unknown.png"),
unknown->unknown_state, 0, 50, 40);
} else if (*(it->begin()) - mary->x > -50 && *(it->begin()) - mary->x < 800 && *(it->begin() + 2) == 0) {
painter.drawPixmap(*(it->begin()) - mary->x, *(it->begin() + 1), 50, 40,
没有合适的资源?快使用搜索试试~ 我知道了~
基于c++和qt实现2D超级玛丽游戏项目源码.zip
共77个文件
png:43个
cpp:14个
h:13个
需积分: 0 11 下载量 113 浏览量
2023-09-18
14:45:27
上传
评论 1
收藏 8.42MB ZIP 举报
温馨提示
基于c++和qt实现2D超级玛丽游戏项目源码.zip 一个简单的超级玛丽游戏,学习Qt时编写的,需要优化的地方有很多,代码写的也不太严谨,目前只实现了第一关,后面的关卡需要在Vector中存储地图信息。 实现了吃蘑菇,怪物,火球,跳跃等功能 基于QT,使用C++编写 开发环境为:MacOS(Intel) + QtCreator 5.12,请使用QtCreator打开super_mary.pro文件
资源推荐
资源详情
资源评论
收起资源包目录
基于c++和qt实现2D超级玛丽游戏项目源码.zip (77个子文件)
super_mary.pro.user 24KB
mypushbutton.h 346B
unknown.cpp 2KB
mainwindow.h 492B
master.cpp 4KB
castle.cpp 210B
fire.h 512B
mainwindow.cpp 2KB
pipe.h 289B
gamehelp.cpp 816B
castle.h 246B
mushroom.cpp 2KB
main.cpp 236B
brick.cpp 3KB
mypushbutton.cpp 1KB
super_mary.pro 1KB
mainwindow.ui 644B
pipe.cpp 762B
gamepause.h 270B
mushroom.h 548B
fire.cpp 6KB
gamepause.cpp 620B
master.h 515B
gamescene.cpp 20KB
brick.h 450B
gamehelp.h 305B
gamescene.h 1KB
res.qrc 2KB
mary.cpp 2KB
photo
master_1.png 3KB
background1.jpg 334KB
flag2.png 15KB
walk_left2.png 69KB
flag.png 40KB
ground.png 199KB
mushroom2.png 157KB
end.png 11KB
gamehelp.png 1.68MB
coin.png 16KB
continueGame.png 13KB
walk_right3.png 75KB
openVoice.png 6KB
time.png 36KB
walk_left3.png 75KB
castle.png 490KB
gameover.png 21KB
walk_left1.png 68KB
FlagMary.png 21KB
life.png 237KB
walk_right2.png 75KB
sky.png 1.09MB
help.png 11KB
walk_right1.png 75KB
pipe_long.png 21KB
blackground2.png 2KB
closeVoice.png 7KB
mushroom1.png 156KB
pipe_short.png 19KB
master_0.png 4KB
set.png 11KB
unknown.png 43KB
back.png 6KB
mary_die.png 105KB
brick1.png 92KB
start.png 11KB
initGame.png 9KB
fire.png 61KB
unknown_after.png 6KB
dlg.png 71KB
brick2.png 2KB
icon.icns 1.46MB
score.png 30KB
icon.png 66KB
brick3.png 2KB
icon.icns 1.46MB
mary.h 888B
unknown.h 474B
共 77 条
- 1
资源评论
manylinux
- 粉丝: 4535
- 资源: 2485
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功