示例#1
0
        private void MoveTank(Tank tank, int x)
        {
            switch (tank.Orient)
            {
            case 1:
            {
                Matrix[tank.CoordX, tank.CoordY] = 0;
                tank.CoordY -= x;
                if (Impact(tank) == false)
                {
                    tank.CoordY += x;
                    break;
                }
                if (Matrix[tank.CoordX, tank.CoordY] != 0)
                {
                    switch (Matrix[tank.CoordX, tank.CoordY])
                    {
                    case 2:
                    {
                        tank.HealthPickUP();

                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 3:
                    {
                        tank.MoneyPickUP();

                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 4:
                    {
                        tank.ArmorPickUP();

                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }
                    }
                }

                Matrix[tank.CoordX, tank.CoordY] = 1;


                Grid.SetColumn(tank.Img, tank.CoordY);
                break;
            }

            case 2:
            {
                Matrix[tank.CoordX, tank.CoordY] = 0;
                tank.CoordX -= x;
                if (Impact(tank) == false)
                {
                    tank.CoordX += x;
                    break;
                }

                if (Matrix[tank.CoordX, tank.CoordY] != 0)
                {
                    switch (Matrix[tank.CoordX, tank.CoordY])
                    {
                    case 2:
                    {
                        tank.HealthPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 3:
                    {
                        tank.MoneyPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 4:
                    {
                        tank.ArmorPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }
                    }
                }

                Matrix[tank.CoordX, tank.CoordY] = 1;
                Grid.SetRow(tank.Img, tank.CoordX);
                break;
            }

            case 3:
            {
                Matrix[tank.CoordX, tank.CoordY] = 0;
                tank.CoordY += x;
                if (Impact(tank) == false)
                {
                    tank.CoordY -= x;
                    break;
                }

                if (Matrix[tank.CoordX, tank.CoordY] != 0)
                {
                    switch (Matrix[tank.CoordX, tank.CoordY])
                    {
                    case 2:
                    {
                        tank.HealthPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 3:
                    {
                        tank.MoneyPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 4:
                    {
                        tank.ArmorPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }
                    }
                }

                Matrix[tank.CoordX, tank.CoordY] = 1;
                Grid.SetColumn(tank.Img, tank.CoordY);
                break;
            }

            case 4:
            {
                Matrix[tank.CoordX, tank.CoordY] = 0;
                tank.CoordX += x;
                if (Impact(tank) == false)
                {
                    tank.CoordX -= x;
                    break;
                }

                if (Matrix[tank.CoordX, tank.CoordY] != 0)
                {
                    switch (Matrix[tank.CoordX, tank.CoordY])
                    {
                    case 2:
                    {
                        tank.HealthPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 3:
                    {
                        tank.MoneyPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }

                    case 4:
                    {
                        tank.ArmorPickUP();
                        foreach (PowerUp pow in PowerUps)
                        {
                            if (pow.CoordX == tank.CoordX && pow.CoordY == tank.CoordY)
                            {
                                Field.Children.Remove(pow.Img);
                                PowerUps.Remove(pow);
                                break;
                            }
                        }
                        break;
                    }
                    }
                }

                Matrix[tank.CoordX, tank.CoordY] = 1;
                Grid.SetRow(tank.Img, tank.CoordX);
                break;
            }
            }
        }
示例#2
0
        private void InitializeMatrix()
        {
            for (int i = 0; i < noTanks; i++)
            {
                int    orient    = Rand(5, 1);
                string uriString = @"Img/Tank" + orient.ToString() + ".png";

                Image img = new Image
                {
                    Source     = new BitmapImage(new Uri(uriString, UriKind.Relative)),
                    Stretch    = Stretch.Fill,
                    Opacity    = 1,
                    Visibility = Visibility.Visible
                };
                int xM, yM, xm, ym;

                switch (i)
                {
                case 0:
                {
                    xM = 3;
                    xm = 0;
                    yM = 4;
                    ym = 0;
                    break;
                }

                case 1:
                {
                    xM = 7;
                    xm = 4;
                    yM = 4;
                    ym = 0;
                    break;
                }

                case 2:
                {
                    xM = 3;
                    xm = 0;
                    yM = 12;
                    ym = 7;
                    break;
                }

                default:
                {
                    xM = 7;
                    xm = 4;
                    yM = 12;
                    ym = 7;
                    break;
                }
                }  // in cele 4 cadrane

                Tank tank = new Tank(Rand(xM, xm), Rand(yM, ym), orient, "Tank" + (1 + i).ToString(), img);
                tanks.Add(tank);

                Field.Children.Add(tank.Img);
                Grid.SetRow(tank.Img, tank.CoordX);
                Grid.SetColumn(tank.Img, tank.CoordY);

                Matrix[tank.CoordX, tank.CoordY] = 1;
            }  // Init Tanks

            for (int i = 0; i < noHealthPickUps; i++)
            {
                Image img = new Image
                {
                    Source     = new BitmapImage(new Uri(@"Img/Cross.png", UriKind.Relative)),
                    Stretch    = Stretch.Fill,
                    Opacity    = 1,
                    Visibility = Visibility.Visible
                };

                int x = Rand(7, 0);
                int y = Rand(12, 0);
                while (Matrix[x, y] != 0)
                {
                    x = Rand(7, 0);
                    y = Rand(12, 0);
                }

                PowerUp power = new PowerUp(x, y, img);
                PowerUps.Add(power);

                Field.Children.Add(img);
                Grid.SetRow(img, x);
                Grid.SetColumn(img, y);

                Matrix[x, y] = 2;
            } // Init Health

            for (int i = 0; i < noMoneyPickUps; i++)
            {
                Image img = new Image
                {
                    Source     = new BitmapImage(new Uri(@"Img/Cash.png", UriKind.Relative)),
                    Stretch    = Stretch.Fill,
                    Opacity    = 1,
                    Visibility = Visibility.Visible
                };

                int x = Rand(7, 0);
                int y = Rand(12, 0);
                while (Matrix[x, y] != 0)
                {
                    x = Rand(7, 0);
                    y = Rand(12, 0);
                }

                PowerUp power = new PowerUp(x, y, img);
                PowerUps.Add(power);

                Field.Children.Add(img);
                Grid.SetRow(img, x);
                Grid.SetColumn(img, y);

                Matrix[x, y] = 3;
            } // Init Money

            for (int i = 0; i < noArmorPickUps; i++)
            {
                Image img = new Image
                {
                    Source     = new BitmapImage(new Uri(@"Img/ArmU.png", UriKind.Relative)),
                    Stretch    = Stretch.Fill,
                    Opacity    = 1,
                    Visibility = Visibility.Visible
                };

                int x = Rand(7, 0);
                int y = Rand(12, 0);
                while (Matrix[x, y] != 0)
                {
                    x = Rand(7, 0);
                    y = Rand(12, 0);
                }

                PowerUp power = new PowerUp(x, y, img);
                PowerUps.Add(power);

                Field.Children.Add(img);
                Grid.SetRow(img, x);
                Grid.SetColumn(img, y);

                Matrix[x, y] = 4;
            } // Init Armor

            for (int i = 0; i < noStops; i++)
            {
                Image img = new Image
                {
                    Source     = new BitmapImage(new Uri(@"Img/Tree.png", UriKind.Relative)),
                    Stretch    = Stretch.Fill,
                    Opacity    = 1,
                    Visibility = Visibility.Visible
                };

                Field.Children.Add(img);

                int x = Rand(7, 0);
                int y = Rand(12, 0);
                while (Matrix[x, y] != 0)
                {
                    x = Rand(7, 0);
                    y = Rand(12, 0);
                }

                Grid.SetRow(img, x);
                Grid.SetColumn(img, y);

                Matrix[x, y] = 5;
            } // Init Stops

            {
                int c = 0;
                for (int i = 0; i < 7; i++)
                {
                    for (int j = 0; j < 12; j++)
                    {
                        if (Matrix[i, j] == 5)
                        {
                            Stops[0, c] = i;
                            Stops[1, c] = j;
                            c++;
                        }
                    }
                }
            } // Init Stops Array

            // while (Matrix[x, y] != 0 && Matrix[x - 1, y] != 0 && Matrix[x - 1, y - 1] != 0 && Matrix[x - 1, y + 1] != 0 && Matrix[x, y - 1] != 0 && Matrix[x, y + 1] != 0 && Matrix[x + 1, y - 1] != 0 && Matrix[x + 1, y + 1] != 0 && Matrix[x + 1, y] != 0)
            // nothing around
        }