📄 g03-01.cpp
字号:
// ============================================================================
// g03-01.cpp
// Demonstrating the use of arrays in games.
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Array.h"
#include <stdlib.h>
#include <time.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "MONSTERS! - Game Demo 3-01";
const int WIDTH = 640;
const int HEIGHT = 480;
// ============================================================================
// The Monster Class.
// ============================================================================
class Monster
{
public:
int m_x;
int m_y;
int m_hitpoints;
};
// ============================================================================
// Global Variables
// ============================================================================
// this is the main window for the framework
SDL_Surface* g_window = 0;
// The monster array, and the number of monsters in the game.
Array<Monster> g_monsterarray( 32 );
int g_monsters = 0;
// the monster bitmaps
SDL_Surface* g_monsterbmp = 0;
// ============================================================================
// Game Functions
// ============================================================================
bool AddMonster()
{
// resize the moster array if needed.
if( g_monsters == g_monsterarray.Size() )
g_monsterarray.Resize( g_monsterarray.Size() + 32 );
// create the new monster.
g_monsterarray[g_monsters].m_x = rand() % 640;
g_monsterarray[g_monsters].m_y = rand() % 480;
g_monsterarray[g_monsters].m_hitpoints = 11 + (rand() % 10);
g_monsters++;
// return success!
return true;
}
void RemoveMonster( int p_index )
{
// use the fast-remove algorithm to move the
// last monster into the deleted monsters index.
g_monsters--;
g_monsterarray[p_index] = g_monsterarray[g_monsters];
}
void CheckMonsters()
{
// check to see if any monsters are dead.
// if so, remove it.
int index = 0;
while( index < g_monsters )
{
if( g_monsterarray[index].m_hitpoints <= 0 )
RemoveMonster( index );
else
index++;
}
}
int main( int argc, char* argv[] )
{
srand( time(0) );
// declare coordinates.
int x, y;
int index;
// declare event holder
SDL_Event event;
// initialize the video system.
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
// set our at exit function
atexit( SDL_Quit );
// set the video mode.
g_window = SDL_SetVideoMode( WIDTH, HEIGHT, 0, SDL_ANYFORMAT );
// load the monster:
g_monsterbmp = SDL_LoadBMP( "monster1.bmp" );
SDL_SetColorKey( g_monsterbmp, SDL_SRCCOLORKEY,
SDL_MapRGB( g_monsterbmp->format, 255, 255, 255 ) );
while( 1 )
{
//look for an event
if( SDL_PollEvent( &event ) )
{
//an event was found
if( event.type == SDL_QUIT )
break;
if( event.type == SDL_MOUSEBUTTONDOWN )
{
// get the mouse state.
SDL_GetMouseState( &x, &y );
// loop through the monsters backwards until we find one
// that the user clicked on.
// we're looping backwards because the last monsters are
// drawn on top, and will be hit first.
index = g_monsters - 1;
while( index >= 0 )
{
if( x >= g_monsterarray[index].m_x &&
x < g_monsterarray[index].m_x + g_monsterbmp->w &&
y >= g_monsterarray[index].m_y &&
y < g_monsterarray[index].m_y + g_monsterbmp->h )
{
// hit the monster!
// 10-19 damage.
g_monsterarray[index].m_hitpoints -= rand() % 10 + 10;
// set index to -1, so the loop will exit.
// remember, we only want to remove one monster.
index = -1;
}
else
{
index--;
}
}
}
if( event.type == SDL_KEYDOWN )
{
// a key was pressed.
if( event.key.keysym.sym == SDLK_ESCAPE )
{
// if ESC was pressed, quit the program.
SDL_Event quit;
quit.type = SDL_QUIT;
SDL_PushEvent( &quit );
}
else
{
// if any other key was pressed, spawn a new monster.
AddMonster();
}
}
} // end event loop.
// check to see if any monsters need removing.
CheckMonsters();
// clear the screen to white.
SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 255, 255, 255 ) );
// loop through the array and draw all the monsters.
for( index = 0; index < g_monsters; index++ )
{
SDLBlit( g_monsterbmp, g_window,
g_monsterarray[index].m_x,
g_monsterarray[index].m_y );
}
// update the entire window.
SDL_UpdateRect( g_window, 0, 0, 0, 0 );
}
// done
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -