Monogame 使 class 中的对象独立移动
Monogame make objects in class move independently
我的代码有问题。我正在尝试让 2000 个方块在屏幕上弹跳。我已经使它与一个块一起工作,但是当我添加更多时,只要其中一个碰到屏幕边缘的边界,就会在所有块上更新 x 和 y 轴。所以我希望所有对象的 x 轴和 y 轴独立更新,而不是像现在这样一起移动。这是我的代码:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Project_blob
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
Texture2D pixelTexture;
List<Block> blocks = new List<Block>();
Block block;
int speed1 = 1;
int speed2 = -1;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
Random rnd = new Random();
// TODO: Add your initialization logic here
block = new Block();
for (int i = 0; i < 2000; i++)
{
var block = new Block();
block.X = rnd.Next(0, 780);
block.Y = rnd.Next(0, 500);
block.Color = new Color(rnd.Next(256), rnd.Next(256), rnd.Next(256));
blocks.Add(block);
}
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
pixelTexture = Content.Load<Texture2D>("pixel");
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
foreach (Block block in blocks)
{
if (block.X < 0) speed2 += 1;
if (block.X > 770) speed2 += -1;
if (block.Y > 450) speed1 += -1;
if (block.Y < 0) speed1 += 1;
if (speed1 < -1) speed1 = -1;
if (speed1 > 1) speed1 = 1;
if (speed2 < -1) speed2 = -1;
if (speed2 > 1) speed2 = 1;
block.X += speed2;
block.Y += speed1;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
foreach (Block block in blocks)
{
block.Draw(_spriteBatch, pixelTexture);
}
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
然后是Block.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Project_blob
{
public class Block
{
public int X { get; set; } = 100;
public int Y { get; set; } = 100;
public Color Color { get; set; } = Color.Red;
public int speed1 = 1;
public int speed2 = -1;
public void Draw(SpriteBatch spriteBatch, Texture2D texture)
{
spriteBatch.Draw(texture, new Rectangle(X, Y, 30, 30), Color);
}
}
}
如果你能在我的项目中帮助我,我将不胜感激。
您的游戏中有 speed1
和 speed2
的全局变量,我认为您正在尝试为每个单独的块计算这些变量。只需将它们置于循环中并在每次迭代时重置它们
foreach (Block block in blocks)
{
var speed1 = 1;
var speed2 = -1;
if (block.X < 0) speed2 += 1;
if (block.X > 770) speed2 += -1;
if (block.Y > 450) speed1 += -1;
if (block.Y < 0) speed1 += 1;
if (speed1 < -1) speed1 = -1;
if (speed1 > 1) speed1 = 1;
if (speed2 < -1) speed2 = -1;
if (speed2 > 1) speed2 = 1;
block.X += speed2;
block.Y += speed1;
}
我建议将它们放入个人 Update() class。他们需要使用本地 speed1
和 speed2
变量,并且应该删除 game1.cs 中的全局 speed1
和 speed2
以避免混淆.
像这样:
在game1.cs中:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
foreach (Block block in blocks)
{
block.Update();
}
base.Update(gameTime);
}
在Block.cs中:
public void Update()
{
if (X < 0) speed2 += 1;
if (X > 770) speed2 += -1;
if (Y > 450) speed1 += -1;
if (Y < 0) speed1 += 1;
if (speed1 < -1) speed1 = -1;
if (speed1 > 1) speed1 = 1;
if (speed2 < -1) speed2 = -1;
if (speed2 > 1) speed2 = 1;
X += speed2;
Y += speed1;
}
我的代码有问题。我正在尝试让 2000 个方块在屏幕上弹跳。我已经使它与一个块一起工作,但是当我添加更多时,只要其中一个碰到屏幕边缘的边界,就会在所有块上更新 x 和 y 轴。所以我希望所有对象的 x 轴和 y 轴独立更新,而不是像现在这样一起移动。这是我的代码:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Project_blob
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
Texture2D pixelTexture;
List<Block> blocks = new List<Block>();
Block block;
int speed1 = 1;
int speed2 = -1;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
Random rnd = new Random();
// TODO: Add your initialization logic here
block = new Block();
for (int i = 0; i < 2000; i++)
{
var block = new Block();
block.X = rnd.Next(0, 780);
block.Y = rnd.Next(0, 500);
block.Color = new Color(rnd.Next(256), rnd.Next(256), rnd.Next(256));
blocks.Add(block);
}
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
pixelTexture = Content.Load<Texture2D>("pixel");
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
foreach (Block block in blocks)
{
if (block.X < 0) speed2 += 1;
if (block.X > 770) speed2 += -1;
if (block.Y > 450) speed1 += -1;
if (block.Y < 0) speed1 += 1;
if (speed1 < -1) speed1 = -1;
if (speed1 > 1) speed1 = 1;
if (speed2 < -1) speed2 = -1;
if (speed2 > 1) speed2 = 1;
block.X += speed2;
block.Y += speed1;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
foreach (Block block in blocks)
{
block.Draw(_spriteBatch, pixelTexture);
}
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
然后是Block.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Project_blob
{
public class Block
{
public int X { get; set; } = 100;
public int Y { get; set; } = 100;
public Color Color { get; set; } = Color.Red;
public int speed1 = 1;
public int speed2 = -1;
public void Draw(SpriteBatch spriteBatch, Texture2D texture)
{
spriteBatch.Draw(texture, new Rectangle(X, Y, 30, 30), Color);
}
}
}
如果你能在我的项目中帮助我,我将不胜感激。
您的游戏中有 speed1
和 speed2
的全局变量,我认为您正在尝试为每个单独的块计算这些变量。只需将它们置于循环中并在每次迭代时重置它们
foreach (Block block in blocks)
{
var speed1 = 1;
var speed2 = -1;
if (block.X < 0) speed2 += 1;
if (block.X > 770) speed2 += -1;
if (block.Y > 450) speed1 += -1;
if (block.Y < 0) speed1 += 1;
if (speed1 < -1) speed1 = -1;
if (speed1 > 1) speed1 = 1;
if (speed2 < -1) speed2 = -1;
if (speed2 > 1) speed2 = 1;
block.X += speed2;
block.Y += speed1;
}
我建议将它们放入个人 Update() class。他们需要使用本地 speed1
和 speed2
变量,并且应该删除 game1.cs 中的全局 speed1
和 speed2
以避免混淆.
像这样:
在game1.cs中:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
foreach (Block block in blocks)
{
block.Update();
}
base.Update(gameTime);
}
在Block.cs中:
public void Update()
{
if (X < 0) speed2 += 1;
if (X > 770) speed2 += -1;
if (Y > 450) speed1 += -1;
if (Y < 0) speed1 += 1;
if (speed1 < -1) speed1 = -1;
if (speed1 > 1) speed1 = 1;
if (speed2 < -1) speed2 = -1;
if (speed2 > 1) speed2 = 1;
X += speed2;
Y += speed1;
}