I have a problem with my code. I am trying to make 2000 blocks bounce on the screen. I have made it work with one block but when I add more the x and y-axis update on all of them as soon as one of them hit the borders at the edge of the screen. So I want all objects x and y axises to update independently and not move together as they're doing right now. Here is my code:
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);
}
}
}
and then 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);
}
}
}
Would be thankful if you were to help me in my project.
I would recommend putting them in a personal Update() class. They'll need to make use of their local
speed1andspeed2variables, and the globalspeed1andspeed2in the game1.cs should be removed to avoid confusion.Something like this:
in game1.cs:
In Block.cs: