我的广场不会改变大小! [英] My Square won't change size!

查看:73
本文介绍了我的广场不会改变大小!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望正方形在单击后多次收缩或增大,因此我为最小"和最大"尺寸创建了一个常量变量,并创建了一个布尔值以确定该正方形是否应缩小或增大.如果陈述对我来说最有意义;我想不出更好的方法.请帮忙!

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!

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);
		}

推荐答案

备份GParkings所说的话:
不要使用相等性测试:除非在特殊条件下,否则它们将不起作用:始终使用大于或等于和小于或等于.

不要使用幻数:将常量定义为:
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:
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.

if (currentSquare.Width == MaxWidth && currentSquare.Height == MaxHeight)

相反,您可能想要||.因此,如果其中任何一个填满该区域,请停止生长.这样,您的尖叫声应该几乎可以保持方形!

您无需将布尔值与true进行对照!您可以只使用bool变量的名称:

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)

与它相反

if (!SwitchGrowthDirection)

读为如果不是SwitchGrowthDirection"

不要用这样的名字来称呼布尔:如果您将其命名为growBigger,那么代码的读起来会更自然:

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.


这篇关于我的广场不会改变大小!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆