QT 俄罗斯方块源码.doc
文本预览下载声明
QT 俄罗斯方块源码,用复制过去整理一下各个源文件用qmake工具编译一下即可
Main.cpp:
#include QtGui/QApplication
#include widget.h
#include QTextCodec
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Widget w;
w.show();
return a.exec();
}
widget.cpp:
#include widget.h
#include ui_widget.h
#include QMessageBox
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui-setupUi(this);
this-resize(800,500);
this-gameArea = new GameArea(this);
this-timer = new QTimer(this);
connect(this-timer,SIGNAL(timeout()),this,SLOT(timer_upDate()));
score =0;
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e-type()) {
case QEvent::LanguageChange:
ui-retranslateUi(this);
break;
default:
break;
}
}
void Widget::timer_upDate() //定时器溢出处理
{
this-gameArea-moveOneStep(); //先移动一步,这时并没有显示出来
if(this-gameArea-isMoveEnd()) //如果无法移动,到底了或结束了
{
if(this-gameArea-isGame_Over()) //如果是结束了
{
this-timer-stop(); //停止计时
QMessageBox::warning(this,tr(warning),tr(Game Over!),QMessageBox::Yes);
//弹出对话框
this-score =0; //清空分数
this-gameArea-init_Game(); //重新开始游戏
this-gameArea-gameStart();
this-timer-start(500);
}
else //如果是移动到底了
{
this-gameArea-nextItem(); //出现下一个图形
int num = this-gameArea-getFullRowNum(); //获得已满的行数
this-doScore(num); //显示分数
this-gameArea-gameStart(); //继续游戏
}
}
else //如果没有到底
{
this-gameArea-do_MoveNext(); //显示方块下移一步后的界面
}
}
void Widget::on_pushButton_clicked() //开始按钮
{
this-gameArea-init_Game(); //第一次进入游戏时进行的初始化
this-gameArea-gameStart(); //开始游戏
this-timer-start(500); //开启定时器
this-gameArea-setFocus(); //让游戏区域获得焦点,这样才能响应键盘
}
void Widget::doScore(int num) //
显示全部