📄 xinvader.cpp
字号:
Engine.ClearScreen(SUR_BACK,NULL);
DrawText(str,250,200);
if(Timer.IsTimeUp(1000))
{
m_State=G_RUN;
Timer.Reset();
TimerTP.Reset();
}
}
break;
case G_GAMEOVER:
Engine.ClearScreen(SUR_BACK,NULL);
DrawText("GAME OVER",250,200);
Input.Update();
int x=0;
if(Input.GetKeyDown(KD_DOWN,NULL,x)!=0)
{
m_State=G_MENU;
Timer.Reset();
TimerTP.Reset();
}
break;
}
}
void ShowMenu()
{
static int frame=0,select=0;
Input.Update();
sur_menu.SetBltRect(0,0,640,480);
sur_menu.DrawFast(NULL,SUR_BACK,0,0,false);
sur_menu.SetBltRect(frame<<5,480,(frame+1)<<5,500);
sur_menu.DrawFast(NULL,SUR_BACK,150,275+select*40,true);
DrawText("START GAME",190,280);
DrawText("CREDITS",190,320);
DrawText("HELP",190,360);
DrawText("QUIT",190,400);
if(Timer.IsTimeUp(20))
if(++frame>19)
frame=0;
if(Input.IsKeyPress(DIK_ESCAPE,KD_DOWN))
RunGame=false;
else if(Input.IsKeyPress(DIK_UP,KD_DOWN))
{
snd_sel.Play();
if(--select<0)
select=3;
}
else if(Input.IsKeyPress(DIK_DOWN,KD_DOWN))
{
snd_sel.Play();
if(++select>3)
select=0;
}
else if(Input.IsKeyPress(DIK_RETURN,KD_DOWN))
{
switch(select)
{
case 0:
m_State=G_RUN;
ResetGame();
InitLevel();
m_State=G_LEVEL;
break;
case 1:
m_State=G_CREDITS;
break;
case 2:
m_State=G_HELP;
break;
case 3:
RunGame=false;
PostQuitMessage(0);
break;
}
frame=0;
select=0;
Timer.Reset();
TimerTP.Reset();
}
}
void ShowCredits()
{
Input.Update();
Engine.ClearScreen(SUR_BACK,NULL);
char buffer[25];
strcpy(buffer,"THIS GAME IS FREEWARE");
DrawText(buffer,145,60);
strcpy(buffer,"Programed by Johnhans");
DrawText(buffer,130,120);
strcpy(buffer,"@J--S Studio");
DrawText(buffer,210,180);
strcpy(buffer,"I WILL BE VERY PLEASED");
DrawText(buffer,140,240);
strcpy(buffer,"IF YOU WANT TO MAIL ME");
DrawText(buffer,130,300);
strcpy(buffer,"JOHNHANSSZ@HOTMAIL.COM");
DrawText(buffer,130,360);
strcpy(buffer,"Thanks to MAURICIORITTER");
DrawText(buffer,130,420);
int x=0;
if(Input.GetKeyDown(KD_DOWN,NULL,x))
{
m_State=G_MENU;
Timer.Reset();
TimerTP.Reset();
}
}
void ShowHelp()
{
static int frame = 0;
static bool show = true;
Input.Update();
Engine.ClearScreen(SUR_BACK,NULL);
DrawText("PHOTON AMMO",90,45);
DrawText("WEAPON ADVANCE",90,95);
DrawText("100 POINTS BONUS",90,145);
DrawText("LASER AMMO",90,195);
DrawText("SHIELD CHARGE",90,245);
DrawText("LEFT, RIGHT - MOVE SHIP",50,290);
DrawText("DOWN OR ENTER - STOP SHIP",50,325);
DrawText("CTRL - CHANGE WEAPON",50,360);
if(show)
DrawText("PRESS ANY KEY TO CONTINUE",140,450);
sur_Extras.SetBltRect(frame*25,0,(frame+1)*25,25);
sur_Extras.DrawFast(NULL,SUR_BACK,50,40,false);
sur_Extras.SetBltRect(frame*25,25,(frame+1)*25,50);
sur_Extras.DrawFast(NULL,SUR_BACK,50,90,false);
sur_Extras.SetBltRect(frame*25,50,(frame+1)*25,75);
sur_Extras.DrawFast(NULL,SUR_BACK,50,140,false);
sur_Extras.SetBltRect(frame*25,75,(frame+1)*25,100);
sur_Extras.DrawFast(NULL,SUR_BACK,50,190,false);
sur_Extras.SetBltRect(frame*25,100,(frame+1)*25,125);
sur_Extras.DrawFast(NULL,SUR_BACK,50,240,false);
if(Timer.IsTimeUp(20))
if(++frame>18)
frame=0;
if(TimerTP.IsTimeUp(1000))
show=!show;
int x=0;
if(Input.GetKeyDown(KD_DOWN,NULL,x)!=0)
{
m_State=G_MENU;
Timer.Reset();
TimerTP.Reset();
}
}
void DrawBkgnd()
{
static int y=455;
sur_bkgnd.SetBltRect(0,y,640,455);
sur_bkgnd.DrawFast(NULL,SUR_BACK,0,0,false);
sur_bkgnd.SetBltRect(0,0,640,y);
sur_bkgnd.DrawFast(NULL,SUR_BACK,0,455-y,false);
if(--y<0)
y=455;
}
void DrawStatus()
{
sur_bkgnd.SetBltRect(0,455,640,480);
sur_bkgnd.DrawFast(NULL,SUR_BACK,0,455,false); // 画框架
sur_bkgnd.SetBltRect(iWeapon*87,480,(iWeapon+1)*87,500);
sur_bkgnd.DrawFast(NULL,SUR_BACK,84,458,false); // 画武器
char buf[8];
unsigned int rem;
int iAmmo;
if (iWeapon < 3)
iAmmo = iLaserAmmo;
else
iAmmo = iPhotonAmmo;
buf[0] = iAmmo/100 + '0';
rem = iAmmo % 100;
buf[1] = rem/10 + '0';
rem = iAmmo % 10;
buf[2] = rem + '0';
buf[3] = '\0';
DrawText(buf, 241, 460); // 画弹药数
buf[0] = rem/1000000 + '0';
rem = iScore % 1000000;
buf[1] = rem/100000 + '0';
rem = iScore % 100000;
buf[2] = rem/10000 + '0';
rem = iScore % 10000;
buf[3] = rem/1000 + '0';
rem = iScore % 1000;
buf[4] = rem/100 + '0';
rem = iScore % 100;
buf[5] = rem/10 + '0';
rem = iScore % 10;
buf[6] = rem + '0';
buf[7] = '\0';
DrawText(buf,533,460); // 画得分
BYTE cr,cg;
static bool show=true;
if(iShield>60 && iShield<=100)
{ cr=0;cg=255;show=true; }
else if(iShield>40 && iShield<=60)
{ cr=255;cg=255;show=true; }
else if(iShield>0 && iShield<=40)
{
cr=255;cg=0;
if(TimerTP.IsTimeUp(50))
show=!show;
}
RECT rc={361,458,361+iShield,478};
if(show)
Engine.ClearScreen(SUR_BACK,&rc,cr,cg,0); // 画防护层
}
void DrawShip()
{
static int step=1;
int x=iShipframe%5;
int y=iShipframe/5;
if(iShield>0)
{
iShipPos+=iShipMov;
if (iShipPos <= 1) { iShipMov = 0; iShipPos = 1; }
else if (iShipPos >= 569) { iShipMov = 0; iShipPos = 569; }
sur_Ship.SetBltRect(x*70,y*70,(x+1)*70,(y+1)*70);
sur_Ship.DrawFast(NULL,SUR_BACK,iShipPos,380,true);
iShipframe+=step;
if(iShipframe>=24 || iShipframe<=0)
step=-step;
}
else if(iShipframe<21)
{
sur_Ship.SetBltRect(x*70,(y+5)*70,(x+1)*70,(y+6)*70);
sur_Ship.DrawFast(NULL,SUR_BACK,iShipPos,380,true);
iShipframe++;
}
}
bool DrawUFO()
{
bool bNone=true;
for(int i=0;i<MAX_UFO;i++)
{
if((m_UFO[i].type>>2) == _UFO_)
{
bNone=false;
int x=m_UFO[i].frame%5;
int y=m_UFO[i].frame/5;
sur_UfoN->SetBltRect(x*70,y*70,(x+1)*70,(y+1)*70);
sur_UfoN->DrawFast(NULL,SUR_BACK,m_UFO[i].x,m_UFO[i].y,true);
if(++m_UFO[i].frame>49)
m_UFO[i].frame=0;
if(rand()%ShootChance==0)
{
UFOShoot(m_UFO[i].x,m_UFO[i].y);
snd_ufire.Play();
}
if(m_UFO[i].type==_UFO_3_ && rand()%15==0 && (m_UFO[i].x>iShipPos-70 && m_UFO[i].x<iShipPos+140))
{
UFOShoot(m_UFO[i].x,m_UFO[i].y);
snd_ufire.Play();
}
switch(m_UFO[i].move)
{
case 0:
{
for(int j=i+1;j<MAX_UFO;j++) // 敌方UFO之间的碰撞检测
UFO_Hit_UFO(i,j);
m_UFO[i].x+=m_UFO[i].inc;
if(m_UFO[i].x>=570 || m_UFO[i].x<=0)
m_UFO[i].inc=-m_UFO[i].inc;
}
break;
case 1:
m_UFO[i].x+=m_UFO[i].inc;
if(m_UFO[i].x>=567 || m_UFO[i].x<=3)
{
m_UFO[i].move=2;
m_UFO[i].qdown=0;
m_UFO[i].inc=-m_UFO[i].inc;
}
break;
case 2:
m_UFO[i].y+=abs(m_UFO[i].inc);
m_UFO[i].qdown+=abs(m_UFO[i].inc);
if(m_UFO[i].qdown>=80)
m_UFO[i].move=1;
break;
case 3:
m_UFO[i].x-=m_UFO[i].inc;
m_UFO[i].y-=m_UFO[i].inc;
if(m_UFO[i].x<=0)
{
m_UFO[i].x=0;
m_UFO[i].move=4;
}
if(m_UFO[i].y<=0)
{
m_UFO[i].y=0;
m_UFO[i].move=5;
}
break;
case 4:
m_UFO[i].x+=m_UFO[i].inc;
m_UFO[i].y-=m_UFO[i].inc;
if(m_UFO[i].x>=560)
{
m_UFO[i].x=560;
m_UFO[i].move=3;
}
if(m_UFO[i].y<=0)
{
m_UFO[i].y=0;
m_UFO[i].move=6;
}
break;
case 5:
m_UFO[i].x-=m_UFO[i].inc;
m_UFO[i].y+=m_UFO[i].inc;
if(m_UFO[i].x<=0)
{
m_UFO[i].x=0;
m_UFO[i].move=6;
}
if(m_UFO[i].y>=400)
{
m_UFO[i].y=400;
m_UFO[i].move=3;
}
break;
case 6:
m_UFO[i].x+=m_UFO[i].inc;
m_UFO[i].y+=m_UFO[i].inc;
if(m_UFO[i].x>=560)
{
m_UFO[i].x=560;
m_UFO[i].move=5;
}
if(m_UFO[i].y>=400)
{
m_UFO[i].y=400;
m_UFO[i].move=4;
}
break;
case 7:
m_UFO[i].x+=m_UFO[i].inc;
if(m_UFO[i].x<=-70)
m_UFO[i].x=640;
else if(m_UFO[i].x>=640)
m_UFO[i].x=-70;
break;
}
if(m_UFO[i].move!=0 && m_UFO[i].move!=7)
UFO_Hit_Ship();
}
else if(m_UFO[i].type == _UFO_EXPLODE_)
{
bNone=false;
int x=m_UFO[i].frame%5;
int y=m_UFO[i].frame/5;
sur_UfoN->SetBltRect(x*70,(y+10)*70,(x+1)*70,(y+11)*70);
sur_UfoN->DrawFast(NULL,SUR_BACK,m_UFO[i].x,m_UFO[i].y,true);
if(++m_UFO[i].frame>19)
{
m_UFO[i].frame=0;
m_UFO[i].type=_UFO_DIED_;
}
}
}
return bNone;
}
void DrawBullet()
{
for(int i=0;i<MAX_BULLET;i++)
{
if(m_Bullet[i].type!=0)
{
if(m_Bullet[i].y<0 || m_Bullet[i].y>454)
m_Bullet[i].type=0;
else
{
switch(m_Bullet[i].type)
{
case _UFO_:
sur_Bullet.SetBltRect(m_Bullet[i].frame*20,0,(m_Bullet[i].frame+1)*20,20);
if(++m_Bullet[i].frame>19)
m_Bullet[i].frame=0;
m_Bullet[i].y+=6+iLevel/6;
break;
case _SHIP_S1_:
sur_Bullet.SetBltRect(220,20,240,40);
m_Bullet[i].y-=6;
break;
case _SHIP_S2_:
sur_Bullet.SetBltRect(m_Bullet[i].frame*20,20,(m_Bullet[i].frame+1)*20,40);
if(++m_Bullet[i].frame>10)
m_Bullet[i].frame=0;
m_Bullet[i].y-=10;
break;
}
sur_Bullet.DrawFast(NULL,SUR_BACK,m_Bullet[i].x,m_Bullet[i].y,true);
}
}
}
}
bool DrawExtra()
{
bool bNone=true;
for(int i=0;i<MAX_EXTRA;i++)
{
if(m_Extra[i].type!=0)
{
bNone=false;
sur_Extras.SetBltRect(25*m_Extra[i].frame,(m_Extra[i].type-11)*25,25*(m_Extra[i].frame+1),(m_Extra[i].type-11+1)*25);
sur_Extras.DrawFast(NULL,SUR_BACK,m_Extra[i].x,m_Extra[i].y,true);
if(++m_Extra[i].frame>18)
m_Extra[i].frame=0;
if((m_Extra[i].y+=m_Extra[i].inc)>455)
m_Extra[i].type=0;
}
}
return bNone;
}
void ProcessInput()
{
Input.Update();
if(Input.IsKeyPress(DIK_ESCAPE,KD_DOWN))
{
m_State=G_MENU;
Timer.Reset();
TimerTP.Reset();
}
if(iShield>0)
{
if(Input.IsKeyPress(DIK_LEFT,KD_DOWN))
iShipMov=-4;
else if(Input.IsKeyPress(DIK_RIGHT,KD_DOWN))
iShipMov=4;
else if(!iShipMov==0 && (Input.IsKeyPress(DIK_RETURN,KD_DOWN) || Input.IsKeyPress(DIK_DOWN,KD_DOWN)))
{
long pos=(long)((iShipPos-320)/320.0*(PAN_RIGHT/20));
snd_stop.SetPan(pos);
iShipMov=0;
snd_stop.Play();
}
else if(Input.IsKeyPress(DIK_LCONTROL,KD_DOWN) || Input.IsKeyPress(DIK_RCONTROL,KD_DOWN))
{
if(++iWeapon>iMaxWeapon)
iWeapon=0;
}
else if(Input.IsKeyPress(DIK_SPACE,KD_DOWN) && m_Bullet[0].type==0 && m_Bullet[1].type==0 && m_Bullet[2].type==0)
{
snd_sfire.Play();
switch(iWeapon)
{
case WP_LASER_1:
if(--iLaserAmmo<0)
{
iLaserAmmo=0;
iShield-=4;
}
m_Bullet[0].type=_SHIP_S1_;
m_Bullet[0].x=iShipPos+24;
m_Bullet[0].y=380;
break;
case WP_LASER_2:
if((iLaserAmmo-=2)<0)
{
iLaserAmmo=0;
iShield-=8;
}
m_Bullet[0].type=_SHIP_S1_;
m_Bullet[0].x=iShipPos+11;
m_Bullet[0].y=380;
m_Bullet[1].type=_SHIP_S1_;
m_Bullet[1].x=iShipPos+34;
m_Bullet[1].y=380;
break;
case WP_LASER_3:
if((iLaserAmmo-=3)<0)
{
iLaserAmmo=0;
iShield-=12;
}
m_Bullet[0].type=_SHIP_S1_;
m_Bullet[0].x=iShipPos+11;
m_Bullet[0].y=380;
m_Bullet[1].type=_SHIP_S1_;
m_Bullet[1].x=iShipPos+23;
m_Bullet[1].y=380;
m_Bullet[2].type=_SHIP_S1_;
m_Bullet[2].x=iShipPos+35;
m_Bullet[2].y=380;
break;
case WP_PHOTON_1:
if((--iPhotonAmmo)<0)
{
iPhotonAmmo=0;
iShield-=6;
}
m_Bullet[0].type=_SHIP_S2_;
m_Bullet[0].x=iShipPos+24;
m_Bullet[0].y=380;
break;
case WP_PHOTON_2:
if((iPhotonAmmo-=2)<0)
{
iPhotonAmmo=0;
iShield-=12;
}
m_Bullet[0].type=_SHIP_S2_;
m_Bullet[0].x=iShipPos+11;
m_Bullet[0].y=380;
m_Bullet[1].type=_SHIP_S2_;
m_Bullet[1].x=iShipPos+34;
m_Bullet[1].y=380;
break;
case WP_PHOTON_3:
if((iPhotonAmmo-=3)<0)
{
iPhotonAmmo=0;
iShield-=18;
}
m_Bullet[0].type=_SHIP_S2_;
m_Bullet[0].x=iShipPos+11;
m_Bullet[0].y=380;
m_Bullet[1].type=_SHIP_S2_;
m_Bullet[1].x=iShipPos+23;
m_Bullet[1].y=380;
m_Bullet[2].type=_SHIP_S2_;
m_Bullet[2].x=iShipPos+35;
m_Bullet[2].y=380;
break;
}
TestShipExplode();
}
}
}
void HitTest()
{
UFOS_Hit_Ship(); // 敌方子弹与玩家飞船碰撞检测
ShipS_Hit_UFO(); // 玩家子弹与敌方UFO碰撞检测
Ship_Hit_Extra(); // 玩家飞船与物品碰撞检测
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -