/*
Beginning Game Programming, Third Edition
Anti-Virus Game
MyGame.cpp
*/
#include "MyDirectX.h"
#include <sstream>
using namespace std;
const string APPTITLE = "Anti-Virus Game";
const int SCREENW = 1024;
const int SCREENH = 768;
/
/font variables
LPD3DXFONT font;
LPD3DXFONT debugfont;
//timing variables
bool paused = false;
DWORD refresh = 0;
DWORD screentime = 0;
double screenfps = 0.0;
double screencount = 0.0;
DWORD coretime = 0;
double corefps = 0.0;
double corecount = 0.0;
DWORD currenttime;
//background scrolling variables
const int BUFFERW = SCREENW * 2;
const int BUFFERH = SCREENH;
double scrollx=0;
double scrolly=0;
LPDIRECT3DSURFACE9 background = NULL;
const double virtual_level_size = BUFFERW * 5;
double virtual_scrollx = 0;
//player variables
LPDIRECT3DTEXTURE9 player_ship;
SPRITE player;
enum PLAYER_STATES
{
NORMAL = 0,
PHASING = 1,
CHARGING = 2,
OVERLOADING = 3
};
PLAYER_STATES player_state = NORMAL;
PLAYER_STATES player_state_previous = NORMAL;
D3DXVECTOR2 position_history[8];
int position_history_index = 0;
DWORD position_history_timer = 0;
double charge_angle = 0.0;
double charge_tweak = 0.0;
double charge_tweak_dir = 1.0;
double energy = 100.0;
double health = 100.0;
//enemy virus objects
const int VIRUSES = 100;
LPDIRECT3DTEXTURE9 virus1_image;
SPRITE viruses[VIRUSES];
const int FRAGMENTS = 30;
LPDIRECT3DTEXTURE9 fragment_image;
SPRITE fragments[FRAGMENTS];
//bullet variables
LPDIRECT3DTEXTURE9 purple_fire;
const int BULLETS = 300;
SPRITE bullets[BULLETS];
int player_shoot_timer = 0;
int firepower = 1;
int bulletcount = 0;
/
/sound effects
CSound *tisk=NULL;
CSound *foom=NULL;
CSound *charging=NULL;
//GUI elements
LPDIRECT3DTEXTURE9 energy_slice;
LPDIRECT3DTEXTURE9 health_slice;
//controller vibration
int vibrating = 0;
int vibration = 100;
/**
**
**/
bool Game_Init(HWND window)
{
srand( ( int )time( NULL ) );
Direct3D_Init(window, SCREENW, SCREENH, false);
DirectInput_Init(window);
DirectSound_Init(window);
//create a font
font = MakeFont("Arial Bold", 24);
debugfont = MakeFont("Arial", 14);
/
/load background
LPDIRECT3DSURFACE9 image = NULL;
image = LoadSurface("binary.bmp");
if (!image) return false;
//load player sprite
player_ship = LoadTexture("ufo.bmp", D3DCOLOR_XRGB( 0, 0, 0 ));
player.x = 100;
player.y = 350;
player.width = player.height = 64;
for (int n=0; n<4; n++)
position_history[n] = D3DXVECTOR2(100000,0);
/
/load bullets
purple_fire = LoadTexture("a");
for (int n=0; n<BULLETS; n++)
{
bullets[n].alive = false;
bullets[n].x = 0;
bullets[n].y = 0;
bullets[n].width = 55;
bullets[n].height = 16;
}
//load enemy viruses
virus1_image = LoadTexture("a");
for (int n=0; n<VIR
USES; n++)
{
viruses[n].alive = true;
viruses[n].width = 128;
viruses[n].height = 128;
viruses[n].x = (float)( 1000 + rand() % BUFFERW );
viruses[n].y = (float)( rand() % SCREENH );
viruses[n].velx = (float)((rand() % 12 + 2) * -1);
viruses[n].vely = (float)(rand() % 2 - 1);
}
//load gui elements
energy_slice = LoadTexture("a");
health_slice = LoadTexture("a");
//load audio files
tisk = LoadSound("clip.wav");
foom = LoadSound("longfoom.wav");
charging = LoadSound("charging.wav");
//load memory fragments (energy)
fragment_image = LoadTexture("a");
for (int n=0; n<FRAGMENTS; n++)
{
fragments[n].alive = true;
D3DCOLOR fragmentcolor = D3DCOLOR_ARGB(
200 + rand() % 55,
150 + rand() % 100,
150 + rand() % 100,
150 + rand() % 100);
fragments[n].color = fragmentcolor;
fragments[n].width = 96;
fragments[n].height = 96;
fragments[n].scaling = (float)(rand() % 20 + 10) / 100.0f;
fragments[n].rotation = (float)( rand() % 360 );
fragments[n].velx = (float)(rand() % 4 + 2) * -1.0f;
fragments[n].vely = (float)(rand() % 10 - 5) / 10.0f;
fragments[n].x = (float)( rand() % BUFFERW );
fragments[n].y = (float)( rand() % SCREENH );
}
//create background
HRESULT result =
d3ddev->CreateOffscreenPlainSurface(
BUFFERW,
BUFFERH,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&background,
NULL);
if (result != D3D_OK) return false;
//copy image to upper left corner of background
RECT source_rect = {0, 0, SCREENW, SCREENH };
RECT dest_ul = { 0, 0, SCREENW, SCREENH };
d3ddev->StretchRect(
image,
&source_rect,
background,
&dest_ul,
D3DTEXF_NONE);
//copy image to upper right corner of background
RECT dest_ur = { SCREENW, 0, SCREENW*2, SCREENH };
d3ddev->StretchRect(
image,
&source_rect,
background,
&dest_ur,
D3DTEXF_NONE);
/
/get pointer to the back buffer
d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO,
&backbuffer);
//remove image
image->Release();
return true;
}
/**
**
**/
void Game_End()
{
background->Release();
font->Release();
debugfont->Release();
fragment_image->Release();
delete charging;
delete foom;
delete tisk;
energy_slice->Release();
health_slice->Release();
virus1_image->Release();
purple_fire->Release();
player_ship->Release();
DirectSound_Shutdown();
DirectInput_Shutdown();
Direct3D_Shutdown();
}
/**
**
**/
template <class T>
std::string static ToString(const T & t, int places = 2
)
{
ostringstream oss;
oss.precision(places);
oss.setf(ios_base::fixed);
oss << t;
return oss.str();
}
/**
**
**/
void move_player(float movex, float movey)
{
//cannot move while overloading!
if (player_state == OVERLOADING
|| player_state_previous == OVERLOADING)
return;
float multi = 4.0f;
player.x += movex * multi;
player.y += movey * multi;
}
/**
**
**/
const double PI = 3.1415926535;
const double PI_under_180 = 180.0f / PI;
const double PI_over_180 = PI / 180.0f;
double toRadians(double degrees)
{
return degrees * PI_over_180;
}
double toDegrees(double radians)
{
return radians * PI_under_180;
}
double wrap(double value, double bounds)
{
double result = fmod(value, bounds);
if (result < 0) result += bounds;
return result;
}
double wrapAngleDegs(double degs)
{
return wrap(degs, 360.0);
}
double LinearVelocityX(double angle)
{
//angle -= 90;
if (angle < 0) angle = 360 + angle;   
return cos( angle * PI_over_180);
}
double LinearVelocityY(double angle)
{
//angle -= 90;
if (angle < 0) angle = 360 + angle;   
return sin( angle * PI_over_180);
}
/**
**
**/
void add_energy(double value)
{
energy += value;
if (energy < 0.0) energy = 0.0;
if (energy > 100.0) energy = 100.0;
}
/**
**
**/
void Vibrate(int contnum, int amount, int length)
{
vibrating = 1;
vibration = length;
XInput_Vibrate(contnum, amount);
}
/**
**
**/
int find_bullet()
{
int bullet = -1;
for (int n=0; n<BULLETS; n++)
{
if (!bullets[n].alive)
{
bullet = n;
break;
}
}
return bullet;
}
/**
**
**/
bool player_overload()
{
//disallow overoad unless energy is at 100%
if (energy < 50.0) return false;
//reduce energy for this shot
add_energy(-0.5);
if (energy < 0.0)
{
energy = 0.0;
return false;
}
//play charging sound
PlaySound(charging);
//vibrate controller
Vibrate(0, 20000, 20);
int b1 = find_bullet();
if (b1 == -1) return true;
bullets[b1].alive = true;
bullets[b1].velx = 0.0f;
bullets[b1].vely = 0.0f;
bullets[b1].rotation = (float)(rand() % 360);
bullets[b1].x = player.x + player.width;
bullets[b1].y = player.y + player.height/2
- bullets[b1].height/2;
bullets[b1].y += (float)(rand() % 200 - 100);
return true;
}
/**
**
**/
void player_shoot()
{
//limit firing rate
if ((int)timeGetTime() < player_shoot_timer + 100) return;
player_shoot_timer = timeGetTime();
//reduce energy for this shot
add_energy(-0.20);
/*if (energy < 1.0)
{
energy = 0.0;
return;
}*/
//play firing sound
PlaySound(tisk);
Vibrate(0, 25000, 10);
//lau
nch bullets based on firepower level
switch(firepower)
{
case 1:
{
//create a bullet
int b1 = find_bullet();
if (b1 == -1) return;
bullets[b1].alive = true;
bullets[b1].rotation = 0.0;
bullets[b1].velx = 12.0f;
bullets[b1].vely = 0.0f;
bullets[b1].x = player.x + player.width/2;
bullets[b1].y = player.y + player.height/2
- bullets[b1].height/2;
}
break;
case 2:
{
//create bullet 1
int b1 = find_bullet();
if (b1 == -1) return;
bullets[b1].alive = true;
bullets[b1].rotation = 0.0;
bullets[b1].velx = 12.0f;
bullets[b1].vely = 0.0f;
bullets[b1].x = player.x + player.width/2;
bullets[b1].y = player.y + player.height/2
- bullets[b1].height/2;
bullets[b1].y -= 10;
//create bullet 2
int b2 = find_bullet();
if (b2 == -1) return;
bullets[b2].alive = true;
bullets[b2].rotation = 0.0;
bullets[b2].velx = 12.0f;
bullets[b2].vely = 0.0f;
bullets[b2].x = player.x + player.width/2;
bullets[b2].y = player.y + player.height/2
- bullets[b2].height/2;
bullets[b2].y += 10;
}
break;
case 3:
{
//create bullet 1
int b1 = find_bullet();
if (b1 == -1) return;
bullets[b1].alive = true;
bullets[b1].rotation = 0.0;
bullets[b1].velx = 12.0f;
bullets[b1].vely = 0.0f;
bullets[b1].x = player.x + player.width/2;
bullets[b1].y = player.y + player.height/2
- bullets[b1].height/2;
//create bullet 2
int b2 = find_bullet();
if (b2 == -1) return;
bullets[b2].alive = true;
bullets[b2].rotation = 0.0;
bullets[b2].velx = 12.0f;
bullets[b2].vely = 0.0f;
bullets[b2].x = player.x + player.width/2;
bullets[b2].y = player.y + player.height/2
- bullets[b2].height/2;
bullets[b2].y -= 16;
//create bullet 3
int b3 = find_bullet();
if (b3 == -1) return;
bullets[b3].alive = true;
bullets[b3].rotation = 0.0;
bullets[b3].velx = 12.0f;
bullets[b3].vely = 0.0f;
bullets[b3].x = player.x + player.width/2;
bullets[b3].y = player.y + player.height/2
- bullets[b3].height/2;
bullets[b3].y += 16;
}
break;
case 4:
{
/
/create bullet 1
int b1 = find_bullet();
if (b1 == -1) return;
bullets[b1].alive = true;
bullets[b1].rotation = 0.0;
bullets[b1].velx = 12.0f;
bullets[b1].vely = 0.0f;
bullets[b1].x = pl
ayer.x + player.width/2;
bullets[b1].x += 8;
bullets[b1].y = player.y + player.height/2
-
bullets[b1].height/2;
bullets[b1].y -= 12;
//create bullet 2
int b2 = find_bullet();
if (b2 == -1) return;
bullets[b2].alive = true;
bullets[b2].rotation = 0.0;
bullets[b2].velx = 12.0f;
bullets[b2].vely = 0.0f;
bullets[b2].x = player.x + player.width/2;
bullets[b2].x += 8;
bullets[b2].y = player.y + player.height/2
- bullets[b2].height/2;
bullets[b2].y += 12;
//create bullet 3
int b3 = find_bullet();
if (b3 == -1) return;
bullets[b3].alive = true;
bullets[b3].rotation = 0.0;
bullets[b3].velx = 12.0f;
bullets[b3].vely = 0.0f;
bullets[b3].x = player.x + player.width/2;
bullets[b3].y = player.y + player.height/2
- bullets[b3].height/2;
bullets[b3].y -= 32;
//create bullet 4
int b4 = find_bullet();
if (b4 == -1) return;
bullets[b4].alive = true;
bullets[b4].rotation = 0.0;
bullets[b4].velx = 12.0f;
bullets[b4].vely = 0.0f;
bullets[b4].x = player.x + player.width/2;
bullets[b4].y = player.y + player.height/2
- bullets[b4].height/2;
bullets[b4].y += 32;
}
break;
case 5:
{
//create bullet 1
int b1 = find_bullet();
if (b1 == -1) return;
bullets[b1].alive = true;免费游戏代码大全
bullets[b1].rotation = 0.0;
bullets[b1].velx = 12.0f;
bullets[b1].vely = 0.0f;
bullets[b1].x = player.x + player.width/2;
bullets[b1].y = player.y + player.height/2
- bullets[b1].height/2;
bullets[b1].y -= 12;
//create bullet 2
int b2 = find_bullet();
if (b2 == -1) return;
bullets[b2].alive = true;
bullets[b2].rotation = 0.0;
bullets[b2].velx = 12.0f;
bullets[b2].vely = 0.0f;
bullets[b2].x = player.x + player.width/2;
bullets[b2].y = player.y + player.height/2
- bullets[b2].height/2;
bullets[b2].y += 12;
//create bullet 3
int b3 = find_bullet();
if (b3 == -1) return;
bullets[b3].alive = true;
bullets[b3].rotation = -4.0;// 86.0;
bullets[b3].velx = (float) (12.0 *
LinearVelocityX( bullets[b3].rotation ));
bullets[b3].vely = (float) (12.0 *
LinearVelocityY( bullets[b3].rotation ));
bullets[b3].x = player.x + player.width/2;
bullets[b3].y = player.y + player.height/2
- bullets[b3].height/2;
bullets[b3].y -= 20;
//create bullet 4
int b4 = find_bullet();
if (b4 == -1) return;
bullets[b4].alive = true;
bullets[b4].rotation = 4.0;// 94.0;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。