Interval for spawn of car2 (every 2 seconds)

// Beginning Game Programming, 2nd Edition
// Chapter 8
// Tiled_Sprite program source code file

#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <d3d9.h>
#include "timer.h"

#define car2num 5

LPDIRECT3DTEXTURE9 maincar_image, car2_image;
SPRITE maincar;

SPRITE car2[car2num];
LPDIRECT3DSURFACE9 back, intro;
LPD3DXSPRITE sprite_handler;
LPD3DXSPRITE car2_handler;


#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")

HRESULT result, result2;
void introduction();
void startGame();
void car2start();


bool StartGame;
bool spawn;
//timing variable
long start = GetTickCount();
Timer timer;


//initializes the game
int Game_Init(HWND hwnd)
{
//set random number seed
srand(time(NULL));
int first, second, third,r;



first = 180;
second = 355;
third = 535;
StartGame = false;
spawn = false;

//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if (result != D3D_OK)
return 0;

//load texture with "pink/magenta" as the transparent color
maincar_image = LoadTexture("maincar.png", D3DCOLOR_XRGB(255,0,255));
if (maincar_image == NULL)
return 0;


//load the background image
intro = LoadSurface("intro.png", NULL);
if (intro == NULL)
return 0;
back = LoadSurface("flatbackground.bmp", NULL);
if (back == NULL)
return 0;

//initialize the sprite's properties
maincar.x = (SCREEN_WIDTH / 2 - 50 );
maincar.y = SCREEN_HEIGHT-150;
maincar.width = 100; //location in the image file
maincar.height = 150; //location in the image file
maincar.curframe = 0;
maincar.lastframe = 1; //12
maincar.animdelay = 3;
maincar.animcount = 0;
maincar.movex = 3; //change this to any value and see the effect

//car2 sprite's properties
car2_image = LoadTexture("car2.png", D3DCOLOR_XRGB(255,0,255));
if (car2_image == NULL)
return 0;
D3DXCreateSprite(d3ddev, &car2_handler);

for (int n = 0; n<car2num; n++)

{

srand((unsigned int)time(NULL));
r = rand() % 3 + 1;
if (r == 1)
{
car2[n].x = first;
}

if (r == 2)
{
car2[n].x = second;
}

if (r == 3)
{
car2[n].x = third;
}


car2[n].y = 0;
car2[n].width = 84; //location in the image file
car2[n].height = 148; //location in the image file
car2[n].curframe = 0;
car2[n].lastframe = 1; //12
car2[n].animdelay = 3;
car2[n].animcount = 0;
car2[n].movey = 1; //change this to any value and see the effect
}


//return okay
return 1;
}

//the main game loop
void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if (d3ddev == NULL)
return;


//after short delay, ready for next frame?
//this keeps the game running at a steady frame rate
if (GetTickCount() - start >= 30) //big value slow animation effect
{
//reset timing
start = GetTickCount();




//has animation delay reached threshold?
if (++maincar.animcount > maincar.animdelay)
{
//reset counter
maincar.animcount = 0;

//animate the sprite

}


//start of logics about movement



if (spawn == true)
{
car2[0].y += 2;
car2[1].y += 4;
car2[2].y += 5;
car2[3].y += 6;
car2[4].y += 7;

}


//end of logics
}

//start rendering
if (d3ddev->BeginScene())
{
introduction();
if (StartGame == true)
{
startGame();

car2start();



}








//erase the entire background


//stop rendering
d3ddev->EndScene();
}

//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);

//check for escape key (to exit program)
if (KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);


if(maincar.x > 150)
{
if(KEY_DOWN(VK_LEFT))
{
maincar.x -= maincar.movex;

}
}

//Moves the truck to the right
if(maincar.x < (650 - maincar.width))
{
if(KEY_DOWN(VK_RIGHT))
{
maincar.x += maincar.movex;

}
}







}

//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{

if (maincar_image != NULL)
maincar_image->Release();

if (back != NULL)
back->Release();

if (intro != NULL)
intro->Release();

if (sprite_handler != NULL)
sprite_handler->Release();

if (car2_handler != NULL)
car2_handler->Release();
}
void introduction()
{

d3ddev->StretchRect(intro, NULL, backbuffer, NULL, D3DTEXF_NONE);


if (KEY_DOWN(VK_SPACE))

{
StartGame = true;


}






}

void startGame()
{


d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
//start sprite handler


sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
//create vector to update sprite position
D3DXVECTOR3 position((float)maincar.x, (float)maincar.y, 0);

//configure the rect for the source tile
RECT srcRect;
int columns = 8; // next frame 50, 0 and 100 64
srcRect.left = (maincar.curframe % columns) * maincar.width; //left = 1 % 8 * 50 = 50
srcRect.top = (maincar.curframe / columns) * maincar.height; //top = 1 / 8 * 64 = 0
srcRect.right = srcRect.left + maincar.width; //right = 50 + 50 = 100
srcRect.bottom = srcRect.top + maincar.height; //bottom = 0 + 64

//draw the sprite maincar
sprite_handler->Draw(
maincar_image,
&srcRect,
NULL,
&position,
//D3DCOLOR_XRGB(0,0,0));
D3DCOLOR_ARGB(255, 255, 255, 255)); //you can adjust the alpha coordinate

//stop drawing
sprite_handler->End();









}

void car2start()
{
spawn = true;

D3DXVECTOR3 car2position(0,0,0); //create vector to update sprite position


srand((unsigned int)time(NULL));

car2_handler->Begin(D3DXSPRITE_ALPHABLEND);




//configure the rect for the source tile

for (int n = 0; n <car2num; n++)
{
RECT car2rect;
int carcolumn = 8; // next frame 50, 0 and 100 64
car2rect.left = (car2[n].curframe % carcolumn) * car2[n].width; //left = 1 % 8 * 50 = 50
car2rect.top = (car2[n].curframe / carcolumn) * car2[n].height; //top = 1 / 8 * 64 = 0
car2rect.right = car2rect.left + maincar.width; //right = 50 + 50 = 100
car2rect.bottom = car2rect.top + maincar.height; //bottom = 0 + 64
}






timer.run();
if (timer.seconds % 2 == 1)
{
for (int i = 0; i < car2num; i++)

//draw the sprite
car2position.x = (float)car2[1].x;
car2position.y = (float)car2[1].y;
car2_handler->Draw(
car2_image,
NULL,
NULL,
&car2position,
//D3DCOLOR_XRGB(0,0,0));
D3DCOLOR_ARGB(255, 255, 255, 255)); //you can adjust the alpha coordinate
}


//stop drawing
car2_handler->End();
}
Please, do not double post. It clutters forums and spreads attempts to help you. Another thread: http://www.cplusplus.com/forum/general/159362/
Topic archived. No new replies allowed.