我是否正确声明了此数组?

问题描述 投票:0回答:1

我正在尝试将某些内容从C ++转换为C#,现在翻译已完全完成,但是在编译时出现错误,提示数组索引超出范围,所以我想知道是否正确声明了数组。

[整个代码]

using System;
using SFML.Graphics;
using SFML.Window;
using SFML.Audio;
using SFML.System;

namespace Pruebas_Algoritmia
{
   class Program
   {
       const int num = 8;
       static int[,] points = new int[num, 2] {{300, 610},
                     { 1270, 430},
                     {1380,2380},
                     {1900,2460},
                     {1970,1700},
                     {2550,1680},
                     {2560,3150},
                     {500, 3300}};

       public struct Car
       {
           public float x;
           public float y;
           public float speed;
           public float angle;
           public int n;

           public Car(float speed,float angle, int n)
               : this()
           {   
               speed = 2;
               angle = 0;
               n = 0;
           }

           public void move()
           {
               x += MathF.Sin(angle) * speed;
               y -= MathF.Cos(angle) * speed;
           }

           public void findTarget()
           {
               float tx = points[n, 0];
               float ty = points[n, 1];
               float beta = angle - MathF.Atan2(tx - x, -ty + y);
               if (Math.Sin(beta) < 0)
               {
                   angle += 0.005f * speed;
               }
               else
               {
                   angle -= 0.005f * speed;
               }
               if ((x - tx) * (x - tx) + (y - ty) * (y - ty) < 25 * 25)
               {
                   n = (n + 1) % num;
               }
           }


       };
       static void OnClose(object sender, EventArgs e)
       {
           RenderWindow window = (RenderWindow)sender;
           window.Close();
       }
       static void Main(string[] args)
       {
           RenderWindow app = new RenderWindow(new VideoMode(640, 480), "Car Racing Game", Styles.Default);
           app.SetFramerateLimit(60);
           app.SetVerticalSyncEnabled(true);
           app.DispatchEvents();
           app.Clear();
           Texture t1 = new Texture("c://images/background.png");
           Texture t2 = new Texture("c://images/car.png");
           t1.Smooth = true;
           t2.Smooth = true;

           Sprite sBackground = new Sprite(t1);
           Sprite sCar = new Sprite(t2);
           sBackground.Scale = new Vector2f(2, 2);

           sCar.Origin = new Vector2f(22, 22);
           float R = 22f;

           const int N = 5;
           Car[] car = new Car[N];

           for (int i = 0; 0 < N; i++)
           {
               car[i].x = 300 + i * 50;
               car[i].y = 1700 + i * 80;
               car[i].speed = 7 + i;
           }

           float speed = 0f;
           float angle = 0f;
           float maxSpeed = 12.0f;
           float acc = 0.2f;
           float dec = 0.03f;
           float turnSpeed = 0.08f;

          float offsetX = 0;
          float offsetY = 0;

           while (app.IsOpen)
           {
               Event e;
               app.Closed += new EventHandler(OnClose);

               app.Display();
           }

           bool up = false;
           bool down = false;
           bool right = false;
           bool left = false;

           if (Keyboard.IsKeyPressed(Keyboard.Key.Up))
           {
               up = true;
           }
           if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
           {
               right = true;
           }
           if (Keyboard.IsKeyPressed(Keyboard.Key.Down))
           {
               down = true;
           }
           if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
           {
               left = true;
           }

           //movement

           if (up && speed < maxSpeed)
           {
               if (speed < 0)
               {
                   speed += dec;
               }
               else
               {
                   speed -= acc;
               }
           }
           if (down && speed >- maxSpeed)
           {
               if (speed > 0)  
               {
                   speed -= dec;
               }
               else
               {
                   speed -= acc;
               }
           }
           if (!up && !down)
           {
               if (speed - dec > 0)
               {
                   speed -= dec;
               }
               else if(speed + dec < 0)
               {
                   speed += dec;
               }
               else
               {
                   speed = 0;
               }
           }
           if (right && speed != 0)
           {
               angle += turnSpeed * speed / maxSpeed;
           }
           if (left && speed != 0)
           {
               angle -= turnSpeed * speed / maxSpeed;
           }

           car[0].speed = speed;
           car[0].angle = angle;

           for (int i = 0; i < N; ++i)
           {
               car[i].move();
           }
           for (int i = 0; i < N; ++i)
           {
               car[i].findTarget();
           }

           for (int i = 0; i < N; ++i)
           {
               for(int j = 0; i < N; ++j)
               {
                   float dx = 0;
                   float dy = 0;
                   while (dx * dx + dy * dy < 4 * R * R)
                   {
                       car[i].x += dx / 10.0f;
                       car[i].x += dy / 10.0f;
                       car[j].x += dx / 10.0f;
                       car[j].x += dy / 10.0f;
                       dx = car[i].x - car[j].x;
                       dy = car[i].y - car[j].y;
                   }
               }
           }

           app.Clear(Color.White);

           if (car[0].x > 320) offsetX = car[0].x - 320;
           if (car[0].y > 240) offsetY = car[0].y - 240;

           //sBackground
           sBackground.Position = new Vector2f(-offsetX, -offsetY);
           app.Draw(sBackground);

           Color[] colors = new Color[] { Color.Red, Color.Green, Color.Magenta, Color.Blue, Color.White};

           for (int i = 0; i < N; ++i)
           {
               sCar.Position = new Vector2f(car[i].x - offsetX, car[i].y - offsetY);
               sCar.Rotation = car[i].angle * 180 / 3.141593f;
               sCar.Color = colors[i];
               app.Draw(sCar);
           }

           app.Display();
       }
   }   
}

我在第88行有此错误。

const int N = 5;
            Car[] car = new Car[N];

            for (int i = 0; 0 < N; i++)
            {
                car[i].x = 300 + i * 50;
                car[i].y = 1700 + i * 80;
                car[i].speed = 7 + i;
            }

最初在C ++中看起来像这样

const int N=5;
    Car car[N];
    for(int i=0;i<N;i++)
    {
      car[i].x=300+i*50;
      car[i].y=1700+i*80;
      car[i].speed=7+i;
    }

我从字面上得到的错误是说索引超出范围,在for循环之后我也得到了一个异常,说该循环下面的每一行都有不可达的代码

c# c++ sfml sfml.net
1个回答
0
投票

您在C#代码中输入了错误。您写的是0 < N而不是i < N

© www.soinside.com 2019 - 2024. All rights reserved.