Using LINQ to isolate a particular obect from a list of objects
- by dezkev
Hello All,
I am trying my hand at making an invaders clone. there are 30 aliens arranged in a 5x 6 matrix on screen. I need to give the bottom most alien the ability to fire a shot.
I am using LINQ to group the aliens into 5 groups based on Location.X and then sort the groups descending.I then need to choose one of the groups ( that gives me 5 groups)
and select the First alien in the group and use it;s coordinate to fire a shot.
My code below ,well ,works , but aliens in ANY row are merrily firing shots- not just the bottom most. Pl look at my code below and tell me whats wrong.
(r = an instance of the Random class, all aliens are in a list called invaders).
{
var query = (from inv in invaders
group inv by inv.Location.X
into invgroup
orderby invgroup.Key descending
select invgroup).ToList();
var invfirst = query[r.Next(query.Count)].First();
invaderShots.Add(new Shot(
new Point(invfirst.Area.X + invfirst.Area.Width / 2, invfirst.Area.Y + invfirst.Area.Height + 5),
displayrect, Shot.Direction.Down));
}