📄 func.cpp
字号:
#include"mywidget.h"#include<math.h>#include<iostream.h>//two steps ghost may walk int ghost_step[16][4]={ {1,0,0,0},{1,0,1,0}, {1,0,0,1},{1,0,0,-1}, {-1,0,0,0},{-1,0,-1,0}, {-1,0,0,1},{-1,0,0,-1}, {0,1,0,0},{0,1,1,0}, {0,1,-1,0},{0,1,0,1}, {0,-1,0,0},{0,-1,1,0}, {0,-1,-1,0},{0,-1,0,-1} }; //check the ascii of the button pressedvoid MyWidget::setButton(int ascii){ switch(ascii) { case Qt::Key_W: case Qt::Key_Up: step[0]=0; step[1]=-1; state=0; emit walk(step); break; case Qt::Key_S: case Qt::Key_Down: step[0]=0; step[1]=1; state=2; emit walk(step); break; case Qt::Key_A: case Qt::Key_Left: step[0]=-1; step[1]=0; state=3; emit walk(step); break; case Qt::Key_D: case Qt::Key_Right: step[0]=1; step[1]=0; state=1; emit walk(step); break; case Qt::Key_F: step[0]=0; step[1]=0; state=4; emit walk(step); break; default: button=0; break; } }//judge whether the charactor is trying to walk through wall or doorbool MyWidget::judgement(int who,int *ppos,int *sstep){ int posH,posV=0; if(sstep[0]==0&&sstep[1]==0) return true; if(sstep[1]==0&&sstep[0]!=0) { posH=ppos[1]; if(sstep[0]==1) posV=ppos[0]+1; else posV=ppos[0]; if(wallH_vector[posH][posV]==1) return false; else if(who==0&&wallH_vector[posH][posV]==2) { emit good(); return false; } else return true; } if(sstep[0]==0&&sstep[1]!=0) { posH=ppos[0]; if(sstep[1]==1) posV=ppos[1]+1; else posV=ppos[1]; if(wallV_vector[posH][posV]==1) return false; else if(who==0&&wallV_vector[posH][posV]==2) { emit good(); return false; } else return true; } }//animate the charactor when they are walking//here,use the method of repainting to delay void MyWidget::go(int who,int *ppos,int *sstep){ QPainter paint; QPixmap character("./pic/character.png"); QLabel *c=new QLabel(r); if(who==0) { state=man_state; c=m; } else { state=ghost_state; c=g; } int i; for(i=0;i<3;i++) { distH=24+16*ppos[0]+4*i*sstep[0]; distV=21+16*ppos[1]+4*i*sstep[1]; c->move(distH,distV); paint.begin(c); paint.drawPixmap(0,0,*pic,distH,distV,16,16); paint.drawPixmap(0,0,character,state+16*i,32*who,16,16); paint.end(); int n=4000; while((--n)!=0) { paint.begin(c); paint.drawPixmap(0,0,character,state+16*i,32*who,16,16); paint.end(); } } for(i=1;i>=0;i--) { distH=24+16*ppos[0]+16*sstep[0]-4*i*sstep[0]; distV=21+16*ppos[1]+16*sstep[1]-4*i*sstep[1]; c->move(distH,distV); paint.begin(c); paint.drawPixmap(0,0,*pic,distH,distV,16,16); paint.drawPixmap(0,0,character,state+16*i,32*who,16,16); paint.end(); int n=4000; while((--n)!=0) { paint.begin(c); paint.drawPixmap(0,0,character,state+16*i,32*who,16,16); paint.end(); } }}//ghost goes horizontally first,and choose the position closest to girlint gStep1[2],gStep2[2];void MyWidget::ghost_walk(){ gStep1[0]=0; gStep1[1]=0; gStep2[0]=0; gStep2[1]=0; int temp_step1[2],temp_step2[2]; int s1[2],s2[2]; int distance,tempDist,mov=0; int state1=4,state2=4; distance=abs(ghost_position[0]-man_position[0])+ abs(ghost_position[1]-man_position[1]); int i; for(i=0;i<8;i++) { temp_step1[0]=ghost_step[i][0]; temp_step1[1]=ghost_step[i][1]; if(judgement(1,ghost_position,temp_step1)) { s1[0]=ghost_position[0]+temp_step1[0]; s1[1]=ghost_position[1]+temp_step1[1]; temp_step2[0]=ghost_step[i][2]; temp_step2[1]=ghost_step[i][3]; if(judgement(1,s1,temp_step2)) { s2[0]=temp_step2[0]+s1[0]; s2[1]=temp_step2[1]+s1[1]; tempDist=abs(s2[0]-man_position[0])+ abs(s2[1]-man_position[1]); if(tempDist<distance) { gStep1[0]=temp_step1[0]; gStep1[1]=temp_step1[1]; gStep2[0]=temp_step2[0]; gStep2[1]=temp_step2[1]; if(ghost_step[i][0]==1) state1=1; else state1=3; if(ghost_step[i][3]==0) { if(ghost_step[i][2]==-1) state2=3; else if(ghost_step[i][2]==1) state2=1; else state2=4; } else { if(ghost_step[i][3]==1) state2=2; else state2=0; } distance=tempDist; mov=1; } } } } if(mov==0) { for(i=8;i<16;i++) { temp_step1[0]=ghost_step[i][0]; temp_step1[1]=ghost_step[i][1]; if(judgement(1,ghost_position,temp_step1)) { s1[0]=ghost_position[0]+temp_step1[0]; s1[1]=ghost_position[1]+temp_step1[1]; temp_step2[0]=ghost_step[i][2]; temp_step2[1]=ghost_step[i][3]; if(judgement(1,s1,temp_step2)) { s2[0]=temp_step2[0]+s1[0]; s2[1]=temp_step2[1]+s1[1]; tempDist=abs(s2[0]-man_position[0])+ abs(s2[1]-man_position[1]); if(tempDist<distance) { gStep1[0]=temp_step1[0]; gStep1[1]=temp_step1[1]; gStep2[0]=temp_step2[0]; gStep2[1]=temp_step2[1]; if(ghost_step[i][1]==1) state1=2; else state1=0; if(ghost_step[i][3]==0) { if(ghost_step[i][2]==-1) state2=3; else if(ghost_step[i][2]==1) state2=1; else state2=4; } else { if(ghost_step[i][3]==1) state2=2; else state2=0; } distance=tempDist; } } } } } if(state1<4) { ghost_state=48*state1; go(1,ghost_position,gStep1); ghost_position[0]+=gStep1[0]; ghost_position[1]+=gStep1[1]; } if(state2<4) { ghost_state=48*state2; go(1,ghost_position,gStep2); ghost_position[0]+=gStep2[0]; ghost_position[1]+=gStep2[1]; } distH=24+16*man_position[0]; distV=21+16*man_position[1]; //record ghost's path p1->ghostPath1[0]=gStep1[0]; p1->ghostPath1[1]=gStep1[1]; p1->ghostPath2[0]=gStep2[0]; p1->ghostPath2[1]=gStep2[1]; p1->ghostS1=state1; p1->ghostS2=state2; p.push(p1); if(man_position[0]==ghost_position[0]&& man_position[1]==ghost_position[1]) emit over(); button=0;}void MyWidget::mousePressEvent(QMouseEvent *event){ curPos[0]=event->x(); curPos[1]=event->y()-85; //get the mouse's position in the map emit cursor_pos(curPos);}void MyWidget::keyReleaseEvent(QKeyEvent *event){ if(button==0) { ascii=event->key(); button=1; setButton(ascii); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -