如何在 Monogame 中逐渐改变背景颜色
How to gradually change background color in Monogame
我是编程新手和 c# 新手,但我正在尝试制作 2D 游戏。我创建了一个背景 class 和一个关闭 class,关闭 class 用于我要实现的退出按钮。我想由此实现的是,当我释放关闭按钮时,背景会逐渐降低,色调从白色变为更深的白色。问题是我不知道如何真正编码 this.Here 是对我的代码的看法。
关闭Class
private Texture2D texture;
private Vector2 position;
private Rectangle bounds;
private Color color;
MouseState oldstate;
public Close(Texture2D texture, Vector2 position){
this.texture = texture;
this.position = position;
bounds = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
color = new Color(40, 40, 40);
}
public void Update(GameTime gameTime){
MouseState state = Mouse.GetState();
Point point = new Point(state.X, state.Y);
if(bounds.Contains(point) && !(state.LeftButton == ButtonState.Pressed)){
color = new Color(235, 50, 50);
} else if((!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Pressed)) || (!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Released))){
color = new Color(40, 40, 40);
}
if((state.LeftButton == ButtonState.Pressed) && (oldstate.LeftButton == ButtonState.Released) && (bounds.Contains(point))){
color = new Color(172, 50, 50);
}
if((state.LeftButton == ButtonState.Released) && (oldstate.LeftButton == ButtonState.Pressed) && (bounds.Contains(point))){
}
oldstate = state;
}
public void Draw(SpriteBatch spriteBatch){
spriteBatch.Draw(texture, position, color);
}
背景Class
public Color color;
public Background(Color color){
this.color = color;
}
public void Update(GameTime gameTime){
}
更具体一点,我希望颜色在背景 class 中改变,并且能够通过关闭 class 调用它。另外,请记住,背景颜色是在 Game1 class 中指定的,并且也会从中调用 Update 方法。
无论如何,我们将不胜感激。
我会做的是这样的事情,虽然我没有太多使用表单的经验,所以它可能会或可能不会按预期工作。
您可以根据需要通过 Close
class 呼叫 SyncFadeOut()/AsycFadeOut()
。
同步(阻塞)版本:
public void SyncFadeOut()
{
// define how many fade-steps you want
for (int i = 0; i < 1000; i ++)
{
System.Threading.Thread.Sleep(10); // pause thread for 10 ms
// ----
// do the incremental fade step here
// ----
}
}
异步(非阻塞)版本:
System.Timers.Timer timer = null;
public void FadeOut(object sender, EventArgs e)
{
// ----
// do the incremental fade step here
// ----
// end conditions
if ([current_color] <= [end_color])
{
timer.Stop();
// trigger any additional things you want, like close window
}
}
public void AsyncFadeOut()
{
System.Timers.Timer timer = new System.Timers.Timer(10); // triggers every 10ms, change this if you want a faster/slower fade
timer.Elapsed += new System.Timers.ElapsedEventHandler(FadeOut);
timer.AutoReset = true;
timer.Start();
}
我是编程新手和 c# 新手,但我正在尝试制作 2D 游戏。我创建了一个背景 class 和一个关闭 class,关闭 class 用于我要实现的退出按钮。我想由此实现的是,当我释放关闭按钮时,背景会逐渐降低,色调从白色变为更深的白色。问题是我不知道如何真正编码 this.Here 是对我的代码的看法。
关闭Class
private Texture2D texture;
private Vector2 position;
private Rectangle bounds;
private Color color;
MouseState oldstate;
public Close(Texture2D texture, Vector2 position){
this.texture = texture;
this.position = position;
bounds = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
color = new Color(40, 40, 40);
}
public void Update(GameTime gameTime){
MouseState state = Mouse.GetState();
Point point = new Point(state.X, state.Y);
if(bounds.Contains(point) && !(state.LeftButton == ButtonState.Pressed)){
color = new Color(235, 50, 50);
} else if((!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Pressed)) || (!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Released))){
color = new Color(40, 40, 40);
}
if((state.LeftButton == ButtonState.Pressed) && (oldstate.LeftButton == ButtonState.Released) && (bounds.Contains(point))){
color = new Color(172, 50, 50);
}
if((state.LeftButton == ButtonState.Released) && (oldstate.LeftButton == ButtonState.Pressed) && (bounds.Contains(point))){
}
oldstate = state;
}
public void Draw(SpriteBatch spriteBatch){
spriteBatch.Draw(texture, position, color);
}
背景Class
public Color color;
public Background(Color color){
this.color = color;
}
public void Update(GameTime gameTime){
}
更具体一点,我希望颜色在背景 class 中改变,并且能够通过关闭 class 调用它。另外,请记住,背景颜色是在 Game1 class 中指定的,并且也会从中调用 Update 方法。
无论如何,我们将不胜感激。
我会做的是这样的事情,虽然我没有太多使用表单的经验,所以它可能会或可能不会按预期工作。
您可以根据需要通过 Close
class 呼叫 SyncFadeOut()/AsycFadeOut()
。
同步(阻塞)版本:
public void SyncFadeOut()
{
// define how many fade-steps you want
for (int i = 0; i < 1000; i ++)
{
System.Threading.Thread.Sleep(10); // pause thread for 10 ms
// ----
// do the incremental fade step here
// ----
}
}
异步(非阻塞)版本:
System.Timers.Timer timer = null;
public void FadeOut(object sender, EventArgs e)
{
// ----
// do the incremental fade step here
// ----
// end conditions
if ([current_color] <= [end_color])
{
timer.Stop();
// trigger any additional things you want, like close window
}
}
public void AsyncFadeOut()
{
System.Timers.Timer timer = new System.Timers.Timer(10); // triggers every 10ms, change this if you want a faster/slower fade
timer.Elapsed += new System.Timers.ElapsedEventHandler(FadeOut);
timer.AutoReset = true;
timer.Start();
}