⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slot.cpp

📁 本人这两天在linux下用qt作的益智小游戏
💻 CPP
字号:
#include"mywidget.h"#include"stagewidget.h"#include<iostream.h>//use mouse to make the girl walk,this function judge whether the girl could walk and where she goesvoid MyWidget::IsWalking(int* cursorPos){        if(cursorPos[0]>distH&&cursorPos[0]<(distH+16)	&&cursorPos[1]>distV&&cursorPos[1]<(16+distV))      {	  if(click_counter==0)	  {	      click_counter=1;	      return;	  }          }//click the girl     if(click_counter==1)    {	click_counter=0;	//must be 0	if(cursorPos[0]<(distH-16)||cursorPos[0]>(distH+32)	    ||cursorPos[1]<(distV-16)||cursorPos[1]>(32+distV))	    return;  	        //if click the wrong position,need retry	if(cursorPos[0]>(16+distH))	{	    step[0]=1;	    step[1]=0;	    state=1;	}//go right	else if(cursorPos[0]<distH)	{	    step[0]=-1;	    step[1]=0;	    state=3;	}//go left	else if(cursorPos[1]>(distV+16))	{	    step[0]=0;	    step[1]=1;	    state=2;	}//go up	else if(cursorPos[1]<distV)	{	    step[0]=0;	    step[1]=-1;	    state=0;	}//go down	else	{	    step[0]=0;	    step[1]=0;	    state=4;	}//don't want to move	emit walk(step);    }    }void MyWidget::man_walk(int *man_step){        p1=new struct path;       p1->manPath[0]=man_step[0];    p1->manPath[1]=man_step[1];    //record the path the girl walked    p1->manS=state;        if(state<4)		//the girl don't dare to have a rest    {   	man_state=48*state;	distH=24+16*man_position[0];	distV=21+16*man_position[1];	m->move(distH,distV);	QPainter paint;	QPixmap character("./pic/character.png");	paint.begin(m);	paint.drawPixmap(0,0,*pic,distH,distV,16,16);	paint.drawPixmap(0,0,character,man_state,0,16,16);	paint.end();	if(judgement(0,man_position,man_step))	{	    p1->manChange=1;	    go(0,man_position,man_step);	    man_position[0]+=man_step[0];	    man_position[1]+=man_step[1];	 }	else	    p1->manChange=0;	    }      ghost_walk();}//if player has passed a stage,he can play next stagevoid MyWidget::win(){   if(stag<19)       mess1->setText("Good job!Congratulations!\n"		      "       Go on!");   else       mess1->setText("Excellent!Congratulations!\n"		      "You can delete this game!");   mess1->show();   //a convinient method for goodlooking interface   man_position[0]=-10;   man_position[1]=-10;   //change stage   if(stag!=19)       stag++;   begin();   lcd->display(stag);}//ghost has captured girl,player can retryvoid MyWidget::gameOver(){      mess2=new QMessageBox("You Lost",			  "You lost!\nDo you want to retry?", 			  QMessageBox::Question,			  QMessageBox::Yes|QMessageBox::Default,			  QMessageBox::No|QMessageBox::Escape,			  0);      mess2->setFont(QFont("Times",9,QFont::Normal));      if(mess2->exec()==QMessageBox::Yes)      {	  QPaintEvent*event;	  paintEvent(event);    }    }void MyWidget::begin(){   QPaintEvent*event;   paintEvent(event);}void MyWidget::setback(){    if(!p.isEmpty())    {		p1=p.pop();	step[0]=0-p1->ghostPath2[0];	step[1]=0-p1->ghostPath2[1];		if(p1->ghostS2<4)	{ 	    p1->ghostS2+=2;	    if(p1->ghostS2>=4)		p1->ghostS2-=4;	    ghost_state=48*p1->ghostS2;	    go(1,ghost_position,step);//**	    ghost_position[0]+=step[0];	    ghost_position[1]+=step[1];	}	step[0]=0-p1->ghostPath1[0];	step[1]=0-p1->ghostPath1[1];	if(p1->ghostS1<4)	{	    p1->ghostS1+=2;	    if(p1->ghostS1>=4)		p1->ghostS1-=4;	    	    ghost_state=48*p1->ghostS1;	    go(1,ghost_position,step);//**	    ghost_position[0]+=step[0];	    ghost_position[1]+=step[1];	}	step[0]=0-p1->manPath[0];	step[1]=0-p1->manPath[1];	   		if(p1->manS<4&&p1->manChange==1)	{	    p1->manS+=2;	    if(p1->manS>=4)		p1->manS-=4;	    man_state=48*p1->manS;	    go(0,man_position,step);//**	    man_position[0]+=step[0];	    man_position[1]+=step[1];	}    }}//codec translatesvoid MyWidget::introduce(){    QTabDialog*tdialog=new QTabDialog();    tdialog->setFont(QFont("Times",10,QFont::Normal));    tdialog->resize(250,250);        QWidget*page1=new QWidget();    QWidget*page2=new QWidget();    QWidget*page3=new QWidget();        QMultiLineEdit *edit1=new QMultiLineEdit(page1);    QMultiLineEdit *edit2=new QMultiLineEdit(page2);    QMultiLineEdit *edit3=new QMultiLineEdit(page3);    edit1->resize(270,150);    edit2->resize(270,150);    edit3->resize(270,150);    QString string1="\n\n"		    "           女孩要躲避幽灵的抓捕,若\n"                    "      从红门逃脱,则闯关成功;若被\n"                    "      幽灵抓到,则闯关失败.\n";    QString string2="    1.女孩先走,每次走一步(也可\n"		    "      选择停止);幽灵后走,每次走\n"		    "      两步.之后,两者轮流走.\n"		    "    2.幽灵先走横,后走竖.如果横\n"		    "      走可接近女孩,则选择横走,\n"		    "      即使竖走可以更接近.\n";    QString string3="鼠标:\n"		    "     先点击左键选中女孩,再点击\n"		    "     要走到的格子,女孩走的范围\n"		    "     是她所在位置的上下左右或是\n"		    "     不动.\n"		    "键盘: (可用方向键) 或者\n"		    "     上:      'w'\n"		    "     下:      's'\n"		    "     左:      'a'\n"		    "     右:      'd'\n";    QString string10="游戏玩法:";                QString string20="游戏规则:";		        QString string30="游戏控制:";     QTextCodec *codec10 = QTextCodec::codecForName("GBK");     QString encoded_string10=codec10->toUnicode(string10);          QTextCodec *codec20 = QTextCodec::codecForName("GBK");     QString encoded_string20=codec20->toUnicode(string20);           QTextCodec *codec30 = QTextCodec::codecForName("GBK");     QString encoded_string30=codec30->toUnicode(string30);           QTextCodec *codec1 = QTextCodec::codecForName("GBK");     QString encoded_string1=codec1->toUnicode(string1);     edit1->insert(encoded_string1);     QTextCodec *codec2 = QTextCodec::codecForName("GBK");     QString encoded_string2=codec2->toUnicode(string2);     edit2->insert(encoded_string2);     QTextCodec *codec3 = QTextCodec::codecForName("GBK");     QString encoded_string3=codec3->toUnicode(string3);     edit3->insert(encoded_string3);          tdialog->insertTab(page1,encoded_string10);     tdialog->insertTab(page2,encoded_string20);     tdialog->insertTab(page3,encoded_string30);        tdialog->show();}void MyWidget::about(){    QMessageBox *mess=new QMessageBox		      ("About",		       "No Version!Only one Version!\n"		       "Copyright(c)  2006-~   03EE.\n"		       "     All rights reserved!\n"		       "------------------------------------------\n"		       "       Completed on February the 1st\n"		       "                    kingapoji",		       QMessageBox::NoIcon,		       QMessageBox::Ok|QMessageBox::Escape,		       0,0);    if(mess->exec()==QMessageBox::Ok)	;		       }void MyWidget::choose_stage(){    StageWidget *w=new StageWidget();    connect(w->b1,SIGNAL(clicked()),w,SLOT(respond()));    connect(w->b2,SIGNAL(clicked()),w,SLOT(close()));     connect(w,SIGNAL(getVal(int)),lcd,SLOT(display(int)));    connect(w,SIGNAL(getVal(int)),SIGNAL(stageChange(int)));}void MyWidget::changeStage(int val){    stag=val;}void MyWidget::add_stage(){    if(stag!=19)    {	stag++;	emit stageChange(stag);    }}void MyWidget::sub_stage(){    if(stag!=1)    {	stag--;	emit stageChange(stag);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -