Cześć tworzę sobie snake-a w konsolii, ale mam problem z stworzeniem obiektu klasy "Position" Chodzi o to, że w parametrze przekazuje obiekt "Position", ale mimo to wywala mi wyjątek mówiący, że przekazałem nulla. Wiem, że nikt nie lubi analizować kodu, ale bardzo proszę o pomoc.
Bład pojawia się w linii 59.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace Snake
{
class SnakeControl
{
public SnakeControl(Board board)
{
Vector2D vector2D = new Vector2D(){X = 0, Y=1};
Random ranomizer = new Random();
int timeTicks = 0;
int counter = 0;
board.GenerateCherry();
while (true)
{
while (!Console.KeyAvailable)
{
Position earlierHeadPosition = new Position(board.Snake.HeadPosition);
for (int i = 0; i < 10; i++)
{
if (board.Snake.Ogon[i] != null)
{
if(i==0)
board.Snake.Ogon[0].PreviouslyPosition = new Position(earlierHeadPosition);
else
board.Snake.Ogon[i].PreviouslyPosition = new Position(board.Snake.Ogon[i - 1]);
}
}
board.Snake.HeadPosition.X = board.Snake.HeadPosition.X + vector2D.X;
board.Snake.HeadPosition.Y = board.Snake.HeadPosition.Y + vector2D.Y;
for (int i = 0; i < 10; i++)
{
if (board.Snake.Ogon[i] != null)
{
if (i == 0)
board.Snake.Ogon[0] = earlierHeadPosition;
else
board.Snake.Ogon[i] = board.Snake.Ogon[i - 1].PreviouslyPosition;
}
}
//Zebranie wisienki
if (board.Cherry != null && board.Snake.HeadPosition.X == board.Cherry.X && board.Snake.HeadPosition.Y == board.Cherry.Y)
{
if (counter == 0)
board.Snake.Ogon[counter] = new Position(earlierHeadPosition);
else
board.Snake.Ogon[counter] = new Position(board.Snake.Ogon[counter - 1].PreviouslyPosition);
counter++;
board.Cherry = null;
}
#region GenerateCherry
timeTicks++;
if (timeTicks == 25)
{
board.GenerateCherry();
timeTicks = 0;
}
#endregion
board.RefreshBoard();
Thread.Sleep(100);
}
ConsoleKey key = Console.ReadKey().Key;
if (key == ConsoleKey.DownArrow)
{
vector2D.Y = 1;
vector2D.X = 0;
}
else if (key == ConsoleKey.UpArrow)
{
vector2D.Y = -1;
vector2D.X = 0;
}
else if (key == ConsoleKey.LeftArrow)
{
vector2D.X = -1;
vector2D.Y = 0;
}
else if (key == ConsoleKey.RightArrow)
{
vector2D.X = 1;
vector2D.Y = 0;
}
}
}
}
}
Klasa "Position":
using System;
using System.Collections.Generic;
using System.Text;
namespace Snake
{
class Position
{
public int X { get; set; }
public int Y { get; set; }
public Position PreviouslyPosition { get; set; }
public Position(int x, int y)
{
X = x;
Y = y;
}
public Position(Position position)
{
X = position.X;
Y = position.Y;
}
public Position() { }
}
}
Klasa "Snake":
using System;
using System.Collections.Generic;
using System.Text;
namespace Snake
{
class Snake
{
public Position HeadPosition { get; set; }
public Position[] Ogon { get; set; }
public Snake(Position headPosition)
{
HeadPosition = headPosition;
Ogon = new Position[10];
}
}
}
Edit: Zapomniałem dodać błędu:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Snake
Ślad stosu:
at Snake.Position..ctor(Position position) in C:\Users\krysw\source\repos\Snake\Position.cs:line 21
at Snake.SnakeControl..ctor(Board board) in C:\Users\krysw\source\repos\Snake\SnakeControl.cs:line 59
at Snake.Program.Main(String[] args) in C:\Users\krysw\source\repos\Snake\Program.cs:line 23