Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the square to shrink or grow after so many clicks, so I created constant variables for the Minimum and Maximum sizes and a boolean to determine whether the square should shrink or grow. If statements make the most sense to me; I can't think of any better methods. Please help!

C#
protected override void Update(GameTime gameTime)
		{
			// Allows the game to exit
			if (GamePad.GetState(PlanyerIndex.One).Buttons.Back == ButtonState.Pressed)
				this.Exit();

			// TODO: Add your update logic here
			if (timeRemaining == 0.0f)
			{
				currentSquare = new Rectangle(
					rand.Next(0, this.Window.ClientBounds.Width - 32),
					rand.Next(0, this.Window.ClientBounds.Height - 32),
					64, 64);  //The size of the square is here

				timeRemaining = TimePerSquare;
			}

			MouseState mouse = Mouse.GetState();

			if (mouse.LeftButton == ButtonState.Pressed &&
			  (currentSquare.Contains(mouse.X, mouse.Y)))
			{
				//Check to see if square is at its maximum size and set bool to false
				//This will start shrinking the square
				if (currentSquare.Width == MaxWidth && currentSquare.Height == MaxHeight)
				{
					SwitchGrowthDirection = false;
				}

				//Check to see if square is at its minimum size and set bool to true
				//This will start growing the square
				if (currentSquare.Width == MinWidth && currentSquare.Height == MinHeight)
				{
					SwitchGrowthDirection = true;
				}

				//Grow Square
				if (SwitchGrowthDirection == true)
				{
					currentSquare.Height += 16;
					currentSquare.Width += 16;
				}

				//Shrink square
				if (SwitchGrowthDirection == false)
				{
					currentSquare.Height -= 16;
					currentSquare.Width -= 16;
				}
				playerScore++;
				timeRemaining = 0.0f;
			}
			timeRemaining = MathHelper.Max(0, timeRemaining -
				(float)gameTime.ElapsedGameTime.TotalSeconds);

			this.Window.Title = "Score : " + playerScore.ToString();

			base.Update(gameTime);
		}
Posted
Comments
GParkings 6-Sep-11 10:05am    
I'm putting this in a comment as i don't think it will fix your stated problem but will probably bite you in the future, regardless:

1. Unless your minWidth, minHeight, MaxWidth and maxHeight values are of the form: initial value + n*16 then your if conditions will never resolve to true.

e.g. if your initial height is 10 and your maximum height is 40 the first increase will put your height to 26, the second to 42. At this point you have exceeded your maximum but your condition (height == 40) has not resolved to true (i suggest a >= on maxmiums and <= on minimums)

2. your first and second If statements are (or should be) mutually exclusive and can therefore be simplified to an 'if..else if' statement

1 solution

To back up what GParkings said:
Don't use equality tests: they will not work except under special conditions: always use greater-than-or-equal and less-than-or-equal instead.

Don't use Magic Numbers: Define a constant as:
C#
const int resizeDelta = 16;
This way, if you want to make it bigger or smaller in future, you only have to change it in one place - it is also easier to change it to a variable if you want it to start slow and speed up, say.

You probably don't want && anyway: You will keep growing even when one of them is too big.
C#
if (currentSquare.Width == MaxWidth && currentSquare.Height == MaxHeight)
Instead, you probably want || so that if either of them fills the area, stop growing. This way your squere should pretty much stay square!

You don't need to check a bool value against true! You can just use the name of the bool variable:
if (SwitchGrowthDirection)
And it's opposite
if (!SwitchGrowthDirection)
reads as "if Not SwitchGrowthDirection"

Don't call bools by names like that: if you call it growBigger instead, then the code reads more naturally:
if (growBigger)
Reads as "If grow bigger..." use names like Direction when you will compare them against values like Up and Down, or North and South instead.

I wouldn't do it quite that way anyway: You don't need the bool at all. If you replace it with an int, which holds a positive value for "Grow Bigger", a negative value for "Get Smaller" and zero "stay the same" then you would just add the current value to the existing every time, and you don't need the test.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900