Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nobodynews

Accidental Pac-Man

This topic is 5799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
RAND_MAX is indeed 32767 (0x7FFF) in Microsoft''s implementation.

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

-scott

Share this post


Link to post
Share on other sites
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. =)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!