• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ChristianFrantz

C# array question

15 posts in this topic

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?

0

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.

0

Share this post


Link to post
Share on other sites

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...

0

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.

0

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?
0

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
0

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.
0

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

0

Share this post


Link to post
Share on other sites

Thats not what rip-off was trying to suggest. He was saying if you use an array you have to keep track of whether the sprite should be visible (i.e has been hit or not) in a list when its hit you just remove it from the list and no longer need to check whether it needs to be rendered, updated or have collisions for hits against it  etc anymore. 

Edited by Azalrion
0

Share this post


Link to post
Share on other sites

My 2-cents: Array is going to cause more problems that it would solve.

 

So, you use an array Alien[24] and delete Alien[16] well now your list has NULL. you now have to check for this or risk having an exception thrown, if you use a List<Alien> when you remove element 16, there is no null, just 13 elements in the list.

 

The only advantage a Array will give you over a list of perhaps with drawing

 

(for int x; x < 6; x++) 
    DrawAt(x * 64)
 

but again, this can be done using a List Too.

1

Share this post


Link to post
Share on other sites

OK so this is what I have so far

 

int alienCount = 6; 

public List<Vector2> alienPos = new List<Vector2>();

 

in the start of my class

 

            for (int x = 0; x < alienPos.Count; x++)
            {
                alienPos[x] = new Vector2 (20*x + 50, 50);
            }

 

this is my initialize method

 


            for (int x = 0; x < alienPos.Count; x++)
            {
                alien.Draw(spriteBatch, alienPos[x]);
            }

 

It compiles and runs but the aliens arent being drawn on the screen

 

Edit: figured out that I wasn't ever adding anything to the list -.- but heres the code for that

 

alienPos[x].Add(new Vector2(x + 20 + 50, 20));

 

And it gives me the error that Add doesnt take 1 argument

Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites

Hey burnt_casadilla, 

 

Just a few things:

 

You shouldn't need alienCount or alienPos. Rather, you'll want to have a List<Alien> that stores its Position, so that it can Draw itself with only spriteBatch passed in as a parameter.

 

The reason for the error, is that you're actually calling Vector2.Add (which adds together 2 Vector2), since you're referring to the item in the List itself by specifying the index.

0

Share this post


Link to post
Share on other sites

OK so this is what I have so far

 

int alienCount = 6; 

public List<Vector2> alienPos = new List<Vector2>();

 

in the start of my class

 

            for (int x = 0; x < alienPos.Count; x++)
            {
                alienPos[x] = new Vector2 (20*x + 50, 50);
            }

            for (int x = 0; x < alienPos.Count; x++)
            {
                alien.Draw(spriteBatch, alienPos[x]);
            }

 

It compiles and runs but the aliens arent being drawn on the screen

 

Edit: figured out that I wasn't ever adding anything to the list -.- but heres the code for that

 

alienPos[x].Add(new Vector2(x + 20 + 50, 20));

 

And it gives me the error that Add doesnt take 1 argument

 

because alienPos is a List<Vector2> and the indexer alienPos[x] returns a Vector2 so you are doing Vector2.Add(..) like ndssia said.

 

alienpos.Add(new Vector2(x + 20 + 50, 20)); //Why 20 + 50? why not just 70 :P seems a little silly
0

Share this post


Link to post
Share on other sites

I actually figured it out at 3 am when my brain was shutting off :P very strange. But thank you everyone for the help!! One last request...I'm going to need a timer or something along those lines. I want the aliens to move a certain x position every second, and then in a time span of 5 seconds I'm going to have them shoot their bullets randomly so not every alien is shooting at once

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0