C# 我是否正确声明了此数组? 我试图把C++中的一些东西翻译成C,现在翻译完成了,但是当编译数组的索引超出界限时,我会出错,所以我想知道我是否正确地声明了数组。

C# 我是否正确声明了此数组? 我试图把C++中的一些东西翻译成C,现在翻译完成了,但是当编译数组的索引超出界限时,我会出错,所以我想知道我是否正确地声明了数组。,c#,c++,sfml,sfml.net,C#,C++,Sfml,Sfml.net,[整段代码] 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},

[整段代码]

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

使用系统;
使用SFML.Graphics;
使用SFML.Window;
使用SFML.Audio;
使用SFML.System;
名称空间Pruebas_算法
{
班级计划
{
常量int num=8;
静态int[,]点=新int[num,2]{{300610},
{ 1270, 430},
{1380,2380},
{1900,2460},
{1970,1700},
{2550,1680},
{2560,3150},
{500, 3300}};
公共结构车
{
公共浮动x;
公众浮躁;
公众浮标速度;
公共浮动角度;
公共int n;
公共车辆(浮动速度、浮动角度、整数n)
:此()
{   
速度=2;
角度=0;
n=0;
}
公开作废动议()
{
x+=数学正弦(角度)*速度;
y-=数学坐标(角度)*速度;
}
公共无效findTarget()
{
浮动tx=点[n,0];
浮点ty=点[n,1];
浮动β=角度-MathF.Atan2(tx-x,-ty+y);
if(数学Sin(beta)<0)
{
角度+=0.005f*速度;
}
其他的
{
角度-=0.005f*速度;
}
如果((x-tx)*(x-tx)+(y-ty)*(y-ty)<25*25)
{
n=(n+1)%num;
}
}
};
静态void OnClose(对象发送方、事件参数)
{
RenderWindow=(RenderWindow)发送方;
window.Close();
}
静态void Main(字符串[]参数)
{
RenderWindow应用程序=新RenderWindow(新视频模式(640480),“赛车游戏”,Styles.Default);
应用程序设置帧速率限制(60);
app.SetVerticalSyncEnabled(真);
app.DispatchEvents();
app.Clear();
纹理t1=新纹理(“c://images/background.png”);
纹理t2=新纹理(“c://images/car.png”);
t1.平滑=真;
t2.平滑=真实;
雪碧背景=新雪碧(t1);
雪碧疤痕=新雪碧(t2);
sBackground.Scale=新矢量2f(2,2);
sCar.Origin=新向量2f(22,22);
浮子R=22f;
常数int N=5;
汽车[]汽车=新车[N];
对于(int i=0;0-最大速度)
{
如果(速度>0)
{
速度-=12月;
}
其他的
{
速度-=acc;
}
}
如果(!向上&&!向下)
{
如果(速度-dec>0)
{
速度-=12月;
}
否则如果(速度+dec<0)
{
速度+=dec;
}
其他的
{
速度=0;
}
}
如果(右和速度!=0)
{
角度+=转速*转速/最大转速;
}
如果(左和速度!=0)
{
角度-=转速*转速/最大转速;
}
汽车[0]。速度=速度;
轿厢[0]。角度=角度;
对于(int i=0;i320)偏移量x=车[0].x-320;
如果(车[0].y>240)关闭
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;
            }