Jump to content
  • Advertisement
Sign in to follow this  
ChristianFrantz

C# array question

This topic is 1869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a space invaders clone and before I get into any of the hard coding I want to figure something out. For my aliens I'm going to be using a sprite sheet that has only two images of the alien. One with its arms up and one with them down. I know how to animate the image from the sprite sheet but the issue is getting many of these aliens onto the screen. I was thinking about creating an array with all of them in the array but would that make sense to do it that way? or is there a more simple way to this?

Share this post


Link to post
Share on other sites
Advertisement

That could work too. But now I had another thought: would it be even easier to make a whole separate class for the alien? And then just put whatever number of aliens I want on the screen in that list? I think I'm confusing myself tho...

Share this post


Link to post
Share on other sites

Personally I would use a List. It would be a lot easier to add and remove the aliens. Plus you won't be limited in the size like an array.

A c# list<> is a dynamically sized array class, much like c++'s vector.  This is probably what you want.  

 

However if you know a maximum number of aliens then an array would be fine, you would need a flag to know which ones are active, so you can selectively draw and update only the ones you want.

Share this post


Link to post
Share on other sites
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
 
namespace WindowsGame3
{
    public class Alien
    {
        public Texture2D Texture { get; set; }
        public int Rows { get; set; }
        public int Columns { get; set; }
        private int currentFrame;
        private int totalFrames;
        
        public Alien(Texture2D texture, int rows, int columns)
        {
            Texture = texture;
            Rows = rows;
            Columns = columns;
            currentFrame = 0;
            totalFrames = Rows * Columns;
        }
 
        public void Update()
        {
            currentFrame++;
            if (currentFrame == totalFrames)
                currentFrame = 0;
        }
 
        public void Draw(SpriteBatch spriteBatch, Vector2 location)
        {
            int width = Texture.Width / Columns;
            int height = Texture.Height / Rows;
            int row = (int)((float)currentFrame / (float)Columns);
            int column = currentFrame % Columns;
 
            Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
            Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
 
            spriteBatch.Begin();
            spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
            spriteBatch.End();
        }
    }
}
 
Thats my alien class so far. The only issue I'm having is that its switching between the two frames way too fast. How would I slow this down?

Share this post


Link to post
Share on other sites

Also I think I'm going to use an array instead of a list. I figured there to be 24 aliens on the screen and I think that an array would be easier to work with since I'm only destroying the aliens, not adding any

 

Alien[] alienGroup = new Alien[24];

 

 

            for (int x = 0; x < alienGroup.Length; x++)
            {
                alienGroup[x] = new Alien(alienSprite, 1, 2);
            }
 
this will work i hope? unless it would be better to make a two dimensional ray. Im thinking 4 rows of 6 aliens, but im not sure which would be easier to draw
Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
An array is probably more complex than a list. This is because with an array you have to manually track two things, the array itself and a counter of the number of active objects. You also may need to implement logic to "shuffle" the aliens in the array when one in the middle dies. A list handles this all for you.

Alternatively, you could use "null" to represent dead aliens. However, now your code must carefully check for null before accessing any element of the array.

A two dimensional array might make sense if you were writing a game like Space Invaders, where the aliens have some kind of pattern or formation. In particular, it allows you to avoid storing the positions of individual aliens, their position on the screen or for collision can be inferred from the indices of their entry in the matrix. If your game just spawns aliens in random locations, then I would not recommend a multi-dimensional array. However, if your game has some kind of limited, grid based levels, then storing all the objects (including the player, any obstacles, pickups etc) in a multi-dimensional array would make sense, though you may wish to store the aliens in a separate container anyway.

As for the problem of your sprites animating too fast, you need to incorporate real world time into your update cycle. Otherwise your animations will play at different speeds on different machines, and they will slow down if the game starts to get "busy" - such as levels with more enemies, or if the enemies shoot lots of bullets or there are explosions, etc.

Share this post


Link to post
Share on other sites

With the way the aliens are layed out, an array would make more sense i think. There a 4 rows of six, and when one dies the dont squish together. They stay in the same pattern but with just one missing from the area that was hit

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!