我试图将一些东西从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;
}
我得到的错误实际上表明索引超出了范围,而且在这之后,我得到了一个循环的异常,说循环下面的每一行都有不可访问的代码
您在C#代码中犯了一个拼写错误。您编写了0 < N
而不是i < N
。