参考书籍:
源代码:
// Includes, namespace and prototypes
#include "template.h"
#include<iostream>
#include<stdio.h>
using namespace AGK;
app App;
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SPACE_INDEX = 1;
const int PLANE_INDEX = 2;
const int BOOM_INDEX = 7;
const int STONE_INDEX = 3;
const int STONE_INDEX2 = 4;
const int STONE_INDEX3 = 5;
const int PlANE_IMAGE_INDEX = 1;
const int STONE_IMAGE_INDEX = 2;
const int BOOM_IMAGE_INDEX = 3;
const float BOOM_START_X = 0;
const float BOOM_START_Y = 0;
const float PLANE_X =412 ;
const float PLANE_Y =612;
const float STONE_START_X = 510;
const float STONE_START_Y = 20;
const float STONE2_START_X = 300;
const float STONE2_START_Y = 20;
const float STONE3_START_X = 100;
const float STONE3_START_Y = 20;
float g_mouseX,g_mouseY;
const int INCREMENT = 10;
const float ROTATION = 10;
const float STONE_END_Y = 768;
int DEFEN=0;
int stone=0;
bool game_stage = false;
// Begin app, called once at the start
void app::Begin( void )
{
agk::SetWindowTitle("Fly War");
agk::SetVirtualResolution(SCREEN_WIDTH,SCREEN_HEIGHT);
//把飞机的黑色背景变成透明 只有png格式的图片可以透明化 第三个参数 设为1就透明了
agk::LoadImage(PlANE_IMAGE_INDEX,"plane.png",1);
agk::LoadImage(STONE_IMAGE_INDEX,"stone.png",1);
agk::LoadImage(BOOM_IMAGE_INDEX,"boom.png",1);
agk::CreateSprite(SPACE_INDEX,"space.png");
agk::CreateSprite(PLANE_INDEX,PlANE_IMAGE_INDEX);
agk::CreateSprite(STONE_INDEX,STONE_IMAGE_INDEX);
agk::CreateSprite(STONE_INDEX2,"stone.png");
agk::CreateSprite(STONE_INDEX3,"stone.png");
agk::CreateSprite(BOOM_INDEX,BOOM_IMAGE_INDEX);
//设置深度 防止图片交叉覆盖
agk::SetSpriteDepth(SPACE_INDEX,1000);
agk::SetSpriteDepth(PLANE_INDEX,0);
agk::SetSpriteDepth(STONE_INDEX,0);
agk::SetSpriteDepth(STONE_INDEX2,0);
agk::SetSpriteDepth(STONE_INDEX3,0);
agk::SetSpriteDepth(BOOM_INDEX,0);
//飞机的位置
agk::SetSpritePosition(PLANE_INDEX,PLANE_X,PLANE_Y);
agk::SetSpritePosition(STONE_INDEX,STONE_START_X,STONE_START_Y);
agk::SetSpritePosition(STONE_INDEX2,STONE2_START_X,STONE2_START_Y);
agk::SetSpritePosition(STONE_INDEX3,STONE3_START_X,STONE3_START_Y);
agk::SetSpritePosition(BOOM_INDEX,BOOM_START_X,BOOM_START_Y);
agk::SetRawMouseVisible(0);//隐藏鼠标
agk::SetSyncRate(40,1);//调节图形帧数 也可以调石头掉落速度
}
// Main loop, called every frame
void app::Loop ( void )
{
//-------------------------------------1
float stone1 = agk::GetSpriteY(STONE_INDEX);
float stoneAngle1 = agk::GetSpriteAngle(STONE_INDEX);
if(stone1<STONE_END_Y)
{
agk::SetSpriteY(STONE_INDEX,stone1+INCREMENT);
agk::SetSpriteAngle(STONE_INDEX,stoneAngle1+ROTATION);
}
else
{
int stone1_start_x = agk::Random(0,1024);
agk::SetSpriteX(STONE_INDEX,stone1_start_x);
agk::SetSpriteY(STONE_INDEX,STONE_START_Y);
DEFEN = DEFEN+1;
}
//-------------------------------------2
float stone2 = agk::GetSpriteY(STONE_INDEX2);
float stoneAngle2 = agk::GetSpriteAngle(STONE_INDEX2);
if(stone2<STONE_END_Y)
{
agk::SetSpriteY(STONE_INDEX2,stone2+INCREMENT+6);
agk::SetSpriteAngle(STONE_INDEX2,stoneAngle2+ROTATION+10);
}
else
{
int stone2_start_x = agk::Random(0,1024);
agk::SetSpriteX(STONE_INDEX2,stone2_start_x);
agk::SetSpriteY(STONE_INDEX2,STONE2_START_Y);
DEFEN = DEFEN+1;
}
//-------------------------------------3
float stone3 = agk::GetSpriteY(STONE_INDEX3);
float stoneAngle3 = agk::GetSpriteAngle(STONE_INDEX3);
if(stone3<STONE_END_Y)
{
agk::SetSpriteY(STONE_INDEX3,stone3+INCREMENT+10);
agk::SetSpriteAngle(STONE_INDEX3,stoneAngle3+ROTATION+6);
}
else
{
int stone3_start_x = agk::Random(0,1024);
agk::SetSpriteX(STONE_INDEX3,stone3_start_x);
agk::SetSpriteY(STONE_INDEX3,STONE3_START_Y);
DEFEN = DEFEN+1;
}
g_mouseX = agk::GetRawMouseX();
g_mouseY = agk::GetRawMouseY();
agk::SetSpritePosition(PLANE_INDEX,g_mouseX-60,g_mouseY-25);
float plane_x = agk::GetSpriteX(PLANE_INDEX);
float plane_y = agk::GetSpriteY(PLANE_INDEX);
if(agk::GetSpriteHit(g_mouseX,g_mouseY)==STONE_INDEX||agk::GetSpriteHit(g_mouseX,g_mouseY)==STONE_INDEX2||agk::GetSpriteHit(g_mouseX,g_mouseY)==STONE_INDEX3)
{
app::End();
}
//-------------------------------------飞弹
float boom1Y = agk::GetSpriteY(BOOM_INDEX);
float boom1X = agk::GetSpriteX(BOOM_INDEX);
if(agk::GetRawMouseLeftPressed())
{
boom1X = g_mouseX;
boom1Y = g_mouseY;
}
if(agk::GetSpriteHit(boom1X,boom1Y)!=STONE_INDEX&&agk::GetSpriteHit(boom1X,boom1Y)!=STONE_INDEX2&&agk::GetSpriteHit(boom1X,boom1Y)!=STONE_INDEX3)
{
agk::SetSpriteX(BOOM_INDEX,boom1X);
agk::SetSpriteY(BOOM_INDEX,boom1Y-INCREMENT);
}
if(agk::GetSpriteHit(boom1X,boom1Y)==STONE_INDEX)
{
agk::SetSpriteX(BOOM_INDEX,BOOM_START_X);
agk::SetSpriteY(BOOM_INDEX,BOOM_START_Y);
int stone_start_x = agk::Random(0,1024);
agk::SetSpriteX(STONE_INDEX,stone_start_x);
agk::SetSpriteY(STONE_INDEX,STONE_START_Y);
stone++;
}
if(agk::GetSpriteHit(boom1X,boom1Y)==STONE_INDEX2)
{
agk::SetSpriteX(BOOM_INDEX,BOOM_START_X);
agk::SetSpriteY(BOOM_INDEX,BOOM_START_Y);
int stone2_start_x = agk::Random(0,1024);
agk::SetSpriteX(STONE_INDEX2,stone2_start_x);
agk::SetSpriteY(STONE_INDEX2,STONE2_START_Y);
stone++;
}
if(agk::GetSpriteHit(boom1X,boom1Y)==STONE_INDEX3)
{
agk::SetSpriteX(BOOM_INDEX,BOOM_START_X);
agk::SetSpriteY(BOOM_INDEX,BOOM_START_Y);
int stone3_start_x = agk::Random(0,1024);
agk::SetSpriteX(STONE_INDEX3,stone3_start_x);
agk::SetSpriteY(STONE_INDEX3,STONE3_START_Y);
stone++;
}
//--------------------------------
agk::PrintC("Your gorl is ");
agk::Print(DEFEN);
agk::PrintC("You shoot ");
agk::PrintC(stone);
agk::PrintC("stone");
agk::Sync();
}
// Called when the app ends
void app::End ( void )
{
system("pause");
exit(1);
}