Hey gamedev
Straight to the point, I'm using rand() to randomise where my enemies spawn each time you play, but for some reason every time I run the game they are always in the same position as they were last time I played. It seems like rand() is using the same seed every time, but I pass time to srand. Any ideas?
CODE! :D
Heres the seeding of srand
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLinw, int nShowCmd)
{
srand(time(0));
HWND hWnd;
WNDCLASSEX wc;
RECT client = {5, 30, CAMERASIZE.right, CAMERASIZE.bottom};
void InitializeD3D(HWND hWnd);
void Pulse();
void KillD3D();
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof WNDCLASSEX;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpszClassName = L"WINDOW";
wc.lpfnWndProc = WinProc;
wc.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wc);
AdjustWindowRectEx(&client, WS_OVERLAPPEDWINDOW, FALSE, NULL);
hWnd = CreateWindowEx(
NULL, L"WINDOW", L"DEAD ROAD",
WS_OVERLAPPEDWINDOW,
client.left, client.top, client.right, client.bottom,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nShowCmd);
MSG msg = {0};
InitializeD3D(hWnd);
while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
}
GetCursorPos(&mousePosition);
ScreenToClient(hWnd, &mousePosition);
Pulse();
srand(rand());
}
KillD3D();
return msg.wParam;
}
And here's where I initialize enemy positions, in Zombies constructor
Zombie()
{
animBox.left = 0;
animBox.top = 0;
animBox.right = 23;
animBox.bottom = 42;
position = D3DXVECTOR2((rand() % 4000), (rand() % 4000));
health = 100;
isAlive = true;
width = 23;
height = 42;
animX = 0;
animY = 0;
timePassed = 0;
};
Thanks :)