Using LINQ to isolate a particular obect from a list of objects

Posted by dezkev on Stack Overflow See other posts from Stack Overflow or by dezkev
Published on 2010-05-18T09:00:45Z Indexed on 2010/05/18 9:10 UTC
Read the original article Hit count: 193

Filed under:
|
|
|

Hello All,

alt text

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));

        }

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ