Accidental Pac-Man

Started by
4 comments, last by nobodynews 21 years, 5 months ago
I was screwing around with pixel plotting and made a program which would fill in a growing circle with random pixels. What I basically did was make a vector, gave it a random angle, then computed the x,y coordinates. But for some reason I decided to use degrees and I only used rand() % 360, which wasn't accurate, and made a lot of straight lines. Then I used rand() % 3600, converted to radians, then divided by 10.0, which was more accurate but there were still some holes left when I used solid color. So finally I did rand() % 36000, converted to radians, then divided by 100.0. And I got Pac-man. It didn't take me long realize I either had math overflow or rand() didn't go that high. I'm pretty sure it's cause rand doesn't go that high. Here's the loop I was using, modified a little for effect:
      
int Pac_Man() {
	// Just the main loop

	// #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
	if (KEYDOWN(VK_ESCAPE)) {

		SendMessage(main_window_handle, WM_CLOSE, 0, 0); }	

	memset(&ddsd,0,sizeof(ddsd)); 
	ddsd.dwSize = sizeof(ddsd);

	if (FAILED(lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL))) {
		return(0); }

	int mempitch          = static_cast<int>(ddsd.lPitch);
	UCHAR *video_buffer   = static_cast<UCHAR *>(ddsd.lpSurface);

	const int halfScreenx = SCREEN_WIDTH / 2;
	const int halfScreeny = SCREEN_HEIGHT / 2;
	const double PI = 3.14159;
	const double degToRad = PI / 180;
	static int tempVecLength = 1;
	int vecLength = rand() % tempVecLength;

	// plot pacman shape

	for (int index = 0; index < 100; index++) {
		// select random position and color

		UCHAR color = 1;
		double Angle = (rand() % 36000 * degToRad) / 100.0;
		int x = static_cast<int>(halfScreenx + vecLength * cos(Angle));
		int y = static_cast<int>(halfScreeny + vecLength * sin(Angle));

		// plot the pixels

		video_buffer[x+y*mempitch] = color; }

	if(tempVecLength < halfScreeny) { // make sure circle doesn't get larger than smallest part of the screen

		tempVecLength++; }

	if (FAILED(lpddsprimary->Unlock(NULL))) {
		return(0);	}
	return(0);
}
      
I'm sure there are dozens of ways I could have designed the code better, but, oh well, I was just goofing around. [edited by - nobodynews on November 7, 2002 11:49:28 PM]

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
Moved to a more appropriate forum.

-Ryan "Run_The_Shadows"
-Run_The_Shadows@excite.com
-The Navidson Record! The best film you''ll never see!
Hit us with a screenshot


MatrixCubed
http://MatrixCubed.cjb.net



[edited by - MatrixCubed on November 7, 2002 12:47:28 AM]
RAND_MAX is indeed 32767 (0x7FFF) in Microsoft''s implementation.

(and, yes, there are better ways to get better random numbers. )

-scott
Yes, Scaught is right. RAND_MAX is usually the same as an int. You can build a little program to check what the maximum value of your RAND_MAX is.

  //This will display value of RAND_MAX constant.#include <iostream>#include <cstdlib>using namespace std;int main(){	cout << "The value of RAND_MAX on my system is: " << RAND_MAX << endl;	return 0;}  


The RAND_MAX constant is stored in the cstdlib library. Hope this helps some of you. =)
~Rasidian."'Grat is not nice, indeed!"
Yeah, rand() is limited to a 32k integer.

What you could do is %16000 and /50.0, or 24000 and 75.0, etc.. (not too sure; my brain and math don''t mix when I haven''t had coffee for a while, but I''m sure you get the idea). It''d be more precise than %3600 /10, but much less understandable.

You could do...

MaxAngle = 16000;
DivideBy = 50.0;

MyValue = rand()%MaxAngle / DivideBy;

...which would be somewhat more understandable, though. Well, like I said, my mind is numbed due to a lack of caffeine in my blood, but I''m pretty sure you could get something more precise using that.

And yeah, rand() isn''t the best number generator you can get ;P

This topic is closed to new replies.

Advertisement