the only thing i could think of is creating an array for storing bullets, lets say there can be 10 on the screen at a time. and whenever spacebar is pressed, create a bullet at shipPos.X and shipPos.Y, and send the bullet straight up by subracting its y value.
i guess my problem is that i really only know one side to doing things. I have a hard time thinking of different ways to program if my first option doesnt work because I get stuck in a loop trying to fix something that might not even work at all
different way of learning
the only thing i could think of is creating an array for storing bullets, lets say there can be 10 on the screen at a time. and whenever spacebar is pressed, create a bullet at shipPos.X and shipPos.Y, and send the bullet straight up by subracting its y value.
i guess my problem is that i really only know one side to doing things. I have a hard time thinking of different ways to program if my first option doesnt work because I get stuck in a loop trying to fix something that might not even work at all
That'll work just fine. Just keep checking it for collisions and when it goes outside of the playing area (screen) remove it from the array (This could be done using null). Making it possible to shoot another bullet.
I don't even know where to start with the code for this. So far I have
[source lang="csharp"]Texture2D ship, bullet, alien;
Texture2D[] aliens = new Texture2D[20];
Texture2D[] bullets = new Texture2D[10];
Vector2 shipPos = new Vector2(200, 540);
Vector2 alienPos = new Vector2(200, 400);
Vector2 bulletPos = new Vector2(200, 450);
Vector2[] aliensPos = new Vector2[20];
Vector2[] bulletsPos = new Vector2[10];
int alienCount = 0;
int bulletCount = 0;
KeyboardState state = Keyboard.GetState();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 400;
graphics.PreferredBackBufferHeight = 600;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
alien = Content.Load<Texture2D>("alien");
bullet = Content.Load<Texture2D>("bullet");
ship = Content.Load<Texture2D>("ship");
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 4; y++)
{
aliens[alienCount] = Content.Load<Texture2D>("alien");
aliensPos[alienCount].X = 100 + (x * 40);
aliensPos[alienCount].Y = 20 + (y * 40);
alienCount++;
}
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (state.IsKeyDown(Keys.Right) && shipPos.X < 360)
{
shipPos.X += 5;
}
if (state.IsKeyDown(Keys.Left) && shipPos.X > 0)
{
shipPos.X -= 5;
}
if (state.IsKeyDown(Keys.Space))
{
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ship, shipPos, Color.White);
spriteBatch.Draw(bullet, bulletPos, Color.White);
spriteBatch.Draw(alien, alienPos, Color.White);
for (int alienCount = 0; alienCount < 20; alienCount++)
{
spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/source]
I know that the update method runs 60 times a second, so that would create more bullets than I need per each press of the spacebar. And I have no idea how to get the position of the ship and how to create a bullet there
[source lang="csharp"]Texture2D ship, bullet, alien;
Texture2D[] aliens = new Texture2D[20];
Texture2D[] bullets = new Texture2D[10];
Vector2 shipPos = new Vector2(200, 540);
Vector2 alienPos = new Vector2(200, 400);
Vector2 bulletPos = new Vector2(200, 450);
Vector2[] aliensPos = new Vector2[20];
Vector2[] bulletsPos = new Vector2[10];
int alienCount = 0;
int bulletCount = 0;
KeyboardState state = Keyboard.GetState();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 400;
graphics.PreferredBackBufferHeight = 600;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
alien = Content.Load<Texture2D>("alien");
bullet = Content.Load<Texture2D>("bullet");
ship = Content.Load<Texture2D>("ship");
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 4; y++)
{
aliens[alienCount] = Content.Load<Texture2D>("alien");
aliensPos[alienCount].X = 100 + (x * 40);
aliensPos[alienCount].Y = 20 + (y * 40);
alienCount++;
}
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (state.IsKeyDown(Keys.Right) && shipPos.X < 360)
{
shipPos.X += 5;
}
if (state.IsKeyDown(Keys.Left) && shipPos.X > 0)
{
shipPos.X -= 5;
}
if (state.IsKeyDown(Keys.Space))
{
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ship, shipPos, Color.White);
spriteBatch.Draw(bullet, bulletPos, Color.White);
spriteBatch.Draw(alien, alienPos, Color.White);
for (int alienCount = 0; alienCount < 20; alienCount++)
{
spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/source]
I know that the update method runs 60 times a second, so that would create more bullets than I need per each press of the spacebar. And I have no idea how to get the position of the ship and how to create a bullet there
You can use a variable in conjunction with gameTime.ElapsedTime to make a cooldown on how often the bullet actually gets fired.
ShipPos.X and ShipPos.Y are keeping track of your ship's position. When you make a new bullet, relate it to those coordinates.
ShipPos.X and ShipPos.Y are keeping track of your ship's position. When you make a new bullet, relate it to those coordinates.
so something like this?
[source lang="csharp"] if (state.IsKeyDown(Keys.Space))
{
for (int i = 0; i < 10; i++)
{
bullets[bulletCount] = Content.Load<Texture2D>("bullet");
bulletsPos[bulletCount].X = shipPos.X;
bulletsPos[bulletCount].Y = shipPos.Y;
bulletCount++;
}
}[/source]
[source lang="java"] for (int bulletCount = 0; bulletCount < 10; bulletCount++)
{
spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White);
}
[/source]
i try this and it says texture does not expect null
[source lang="csharp"] if (state.IsKeyDown(Keys.Space))
{
for (int i = 0; i < 10; i++)
{
bullets[bulletCount] = Content.Load<Texture2D>("bullet");
bulletsPos[bulletCount].X = shipPos.X;
bulletsPos[bulletCount].Y = shipPos.Y;
bulletCount++;
}
}[/source]
[source lang="java"] for (int bulletCount = 0; bulletCount < 10; bulletCount++)
{
spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White);
}
[/source]
i try this and it says texture does not expect null
You don't need to create array of textures for bullets. They look all the same, so you just need one texture.
so in drawing you do just:
[source lang="csharp"]
for(int i = 0; i < bulletCount; i++)
{
spriteBatch.Draw(bullet, bulletPos, Color.White);
}
[/source]
For bullets I suggest you creating a class for this. It would contain a List of Vector2, time delay constant(to not spam bullets), bullet velocity, method to handle shooting bullets, method to update each bullets position, and one to destroy bullets (when they are out of screen or when they colide). Then, instead of using for loop with a fixed number of bullets, you can use foreach and thus have 'limitless' number of them. It's the easiest way i believe, in more advanced scenario you would also create a class for bullets, but leave that for later
so in drawing you do just:
[source lang="csharp"]
for(int i = 0; i < bulletCount; i++)
{
spriteBatch.Draw(bullet, bulletPos, Color.White);
}
[/source]
For bullets I suggest you creating a class for this. It would contain a List of Vector2, time delay constant(to not spam bullets), bullet velocity, method to handle shooting bullets, method to update each bullets position, and one to destroy bullets (when they are out of screen or when they colide). Then, instead of using for loop with a fixed number of bullets, you can use foreach and thus have 'limitless' number of them. It's the easiest way i believe, in more advanced scenario you would also create a class for bullets, but leave that for later
oh crap. im completely hopeless when it comes to creating classes. I know how they work and how to call methods from in them but the bulk of the code confuses me
oh crap. im completely hopeless when it comes to creating classes. I know how they work and how to call methods from in them but the bulk of the code confuses me
But that's the essence of programming in languages like C# or Java. I suggest you to make some simple classes in windows console application. Create classes like calculator. Just to befriend OOP.
oh crap. im completely hopeless when it comes to creating classes. I know how they work and how to call methods from in them but the bulk of the code confuses me
Then I'd recommend doing alot of testing and trying different approaches. Simply messing around with classes, before you go and start making games. Classes are an essential part of games/programming overall. No point in rushing making games when you have to learn the basics first, I know it sucks. But sometimes life does.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement