学习了几天TouchGFX,我就想着做个打地鼠的游戏出来玩玩。说干就干,我先网上找了几张打地鼠的图片资源,然后开始了设计。 首先对游戏规划,计划2个场景,第一个背景图片,加按钮进入游戏。第2个场景设计游戏过程。
创建项目,第一个场景图片增加个button开始游戏。如图:
第二个场景找了一个12洞的图片,如图:
每个洞加一个地鼠图片的按钮Button控件,共12个按钮。通过控制按钮控件的显示和隐藏来模拟地鼠进出。如图:
增加的代码:
void Screen2View::handleTickEvent()
{
static int count;
static int MouseHole[12];
int Hole;
for(int i=0;i<12;i++)
{
if(MouseHole[i])
{
MouseHole[i] --;
if( MouseHole[i] == 0)
{
switch(i)
{
case 0:
button1.setVisible(false);
break;
case 1:
button2.setVisible(false);
break;
case 2:
button3.setVisible(false);
break;
case 3:
button4.setVisible(false);
break;
case 4:
button5.setVisible(false);
break;
case 5:
button6.setVisible(false);
break;
case 6:
button7.setVisible(false);
break;
case 7:
button8.setVisible(false);
break;
case 8:
button9.setVisible(false);
break;
case 9:
button10.setVisible(false);
break;
case 10:
button11.setVisible(false);
break;
case 11:
button12.setVisible(false);
break;
}
Screen2ViewBase::button1.invalidate();
Screen2ViewBase::button2.invalidate();
Screen2ViewBase::button3.invalidate();
Screen2ViewBase::button4.invalidate();
Screen2ViewBase::button5.invalidate();
Screen2ViewBase::button6.invalidate();
Screen2ViewBase::button7.invalidate();
Screen2ViewBase::button8.invalidate();
Screen2ViewBase::button9.invalidate();
Screen2ViewBase::button10.invalidate();
Screen2ViewBase::button11.invalidate();
Screen2ViewBase::button12.invalidate();
}
}
}
count++;
if(count==30)
{
Hole=rand()%12;
//Unicode::itoa(Hole,textScoresBuffer,5,10);//snprintf(textScoresBuffer,5,"S=%d",Hole);
//Screen2ViewBase::textScores.invalidate();
if(MouseHole[Hole])
{
Hole = 12;
}else MouseHole[Hole] = 50;
switch(Hole)
{
case 0:
button1.setVisible(true);
break;
case 1:
button2.setVisible(true);
break;
case 2:
button3.setVisible(true);
break;
case 3:
button4.setVisible(true);
break;
case 4:
button5.setVisible(true);
break;
case 5:
button6.setVisible(true);
break;
case 6:
button7.setVisible(true);
break;
case 7:
button8.setVisible(true);
break;
case 8:
button9.setVisible(true);
break;
case 9:
button10.setVisible(true);
break;
case 10:
button11.setVisible(true);
break;
case 11:
button12.setVisible(true);
break;
}
Screen2ViewBase::button1.invalidate();
Screen2ViewBase::button2.invalidate();
Screen2ViewBase::button3.invalidate();
Screen2ViewBase::button4.invalidate();
Screen2ViewBase::button5.invalidate();
Screen2ViewBase::button6.invalidate();
Screen2ViewBase::button7.invalidate();
Screen2ViewBase::button8.invalidate();
Screen2ViewBase::button9.invalidate();
Screen2ViewBase::button10.invalidate();
Screen2ViewBase::button11.invalidate();
Screen2ViewBase::button12.invalidate();
count = 0;
}
}
增加代码后保存文件,然后在右上角RUN Simulator编译,代码出现错误可以在右下角的Log查看原因。
最终的完成版如图:
做的很简陋。
模拟器运行EXE:
simulator.rar
(666.78 KB)
|