Please help with my program!

I'm having a problem getting my program to work. What's supposed to happen is that you start with a single pixel and it randomly moves, leaving a trail of pixels behind. If you know this, please help me. Thanks! (By the way, you have to enlarge the window for it to work properly)



#include <iostream>
#include <ctime>
#include <string>
#include <Windows.h>
#include <conio.h>

using namespace std;
int main(){
int corx = 500;
int cory = 300;
int randomNum;
COLORREF color = RGB(255, 0, 0); // COLORREF to hold the color info`
HWND hwnd = FindWindowA("ConsoleWindowClass", NULL); // Get the HWND
HDC hdc = GetDC(hwnd); // Get the DC from that HWND

while (1)
{
srand(time(0));
randomNum = (rand() % 4) + 1;
cout << randomNum;
Sleep(1000);
if (randomNum = 1){
//down
cory++;
}
if(randomNum = 2){
//up
cory--;
}
if (randomNum = 3){
corx++;
}
if (randomNum = 4){
corx--;
}
SetPixel(hdc, corx, cory, color);
}
ReleaseDC(hwnd, hdc);
DeleteDC(hdc);
system("pause");
return 0;
}
1st, please use code tags: http://www.cplusplus.com/articles/jEywvCM9/

2nd, I believe you get the console HWND using GetConsoleWindow() https://msdn.microsoft.com/en-us/library/ms683175(VS.85).aspx

3rd, use C++11 <random> when possible:
1
2
3
4
5
6
7
8
9
10
11
#include <random> //C++11

int main()
{
  int min=/*MINIMUM_VALUE*/;
  int max=/*MAXIMUM_VALUE*/;
  std::default_random_engine gen;
  std::uniform_int_distribution<int> dist(min,max);
  int rand=dist(gen);
  return 0;
}

http://www.cplusplus.com/reference/random/

And 4th, the reason you have to resize is most likely because the point (corx,cory) may not be on the console window.
Topic archived. No new replies allowed.