Space Invaders Enemy Code Question?1

BlitzMax Forums/BlitzMax Beginners Area/Space Invaders Enemy Code Question?1

TomShep(Posted 2011) [#1]
Hi all,

As my learning programming and blitzmax continues I thought a little space invaders mock up would be a good idea. Before I start smashing away at my keyboard for hours, I just want to make sure I am going at it (lol) in the right direction.

1) What is the best way to code the enemies? Is it to use an array, then use the 'listfromarray' command to get them all into a list?

Any help would be great!


CS_TBL(Posted 2011) [#2]
Every coder has his own style 'n solutions. I'm myself not much a fan of lists (I typically don't need them in the first place), and if your enemy count is typical for for Space Invaders - say 32 - then who cares.

(untested, made in this forum!)
Type TEnemy
  Field state:int ' 0 dead, 1 alive
  Field x:int,y:int,sprite:int,points:int

  Method Init(x:int,y:int)
    self.x=x
    self.y=y
  End Method

  Method Calc()
    If state=0 Return
  End Method

  Method Draw()
    If state=0 Return
  End Method
End Type

Type TGame
  Field Enemy[]:TEnemy
  
  Method MakeEnemies()
    Enemy=new TEnemy[32]
    For local t:int=0 to 31
      Enemy[t]=New TEnemy
    Next
  End Method

  Method CalcEnemies()
    For local t:int=0 to 31
      Enemy[t].Calc
    Next
  End Method
End Type


Last edited 2011

Last edited 2011


TomShep(Posted 2011) [#3]
Hmm thanks. Im very confused but will try anyway!

Would this also be the best way to code the barracades!


Jesse(Posted 2011) [#4]
He never said it was the best way. He also said that every coder has his own way of doing things.

if you have an idea, try it if it does what what you envision than in that case it should be good enough.

some programmers, specially seasoned ones, just take for granted that other programmers are skilled in certain techniques, say OOP. I guess I am at fault of that too surely.

I am not sure you know OOP but if you don't, you can ignore all of his code except that you can do it in array format as opposed to list.

I personally like to use lists and as such I can manipulate without the need to convert back and forth between arrays and list.

I don't think you should either as the conversion is time consuming and everything can be done with out the need to convert But as I said if your idea does exactly what you want than it's fine.

The thing to note is that the limiting factor in most game productions is speed. If with all that goes on in your game, it still works good than don't worry, you will have plenty of time learn to make your code better.

tips:
arrays are faster than list but take a bit more skill to learn to manipulate them.
conversion from list to array and visa-versa is not a good idea for intensive paced games.

note, those are just tips not rules for coding.


Czar Flavius(Posted 2011) [#5]
You can do it in either an array or a list, but avoid converting between arrays and list as this is slow. It wouldn't make a difference for a small game, but it's good to learn the pros and cons of each.

Arrays are useful if you ever need to grab a particular instance by id number. For example, your player could have 3 weapon slots, and pick one using the number key. I would have this as an array, you just select the right number.

If I have a collection of something where I want to update each one with the same rules, I prefer to use a list. This is because lists aren't limited to a fixed number of items. You can add or remove items from the list over time. This would be useful if you improve your game to spawn more enemies over time with no pre-set limit. It also means you wouldn't waste time checking if enemies in the array have been destroyed (so don't update them), just remove them from the list.


TomShep(Posted 2011) [#6]
Thanks chaps I will have bash at this - I had to take a break (through frustration mainly) but ill get onto it now and see if i can get anywhere!