Reputation: 63
I've been exercising my skills by making a Space Invaders clone. I asked a question 2 days ago, that was solved and I've made lots of progress. I've managed to get everything working, collisons, sound, gamestate management and movement etc
Everything works fine except the invaders only shoot from index 0 in my array(left most invader) in each row. Code for this is in the regions starting from line 344 in game1.cs.
The invaders move fine too. They hit the edges of the screen and move down pixels, and continue in the opposite direction. However, if I destroy the outermost invaders, the game still recognises the dead invaders.
So the game suddenly tells an invader in the centre of the screen to go down, and in the opposite direction. It makes the game a bit more dynamic, but this is not my desired effect. The code for movement is in the same place, starting from line 344 in game1.cs
The source can be downloaded here: http://db.tt/1AodUfdS
Upvotes: 0
Views: 275
Reputation: 32627
The reason why in most cases the first invader shoots is that you create a new Random object each time. This will be initialized with the system time. So in a loop, most random numbers will be equal (because system time is nearly equal). Instead create a Random object at the beginning of your game.
For your second problem: You always use the first (index 0) and last (index 10) invader to determine if the row should change directions. Instead, use the first / last invader that is visible:
var LeftMost = Type1Invaders.Where(i => i.invaderVis).FirstOrDefault();
var RightMost = Type1Invaders.Where(i => i.invaderVis).LastOrDefault();
With these you can continue your test. If the result is null, then the row is empty.
Upvotes: 1