I'm making a game. There is a turtle and lots of platforms it can walk on. It walks on its own and you can change the direction it is walking. I implemented a working logic, but I think it can be improved.
I made 2 classes (one for the turtle and one for the platforms).
class Turtle
{
public Rect HitBox;
public bool IsMoving;
public bool IsDirectionForward;
public double DistanceWalked;
public double MetersFalled;
}
class Ground
{
public Rect HitBox;
}
Then I created a virtual Rect that would simulate the right width and height of the ground and the turtle only after using Inflate function.
public MainWindow()
{
InitializeComponent();
ground1 = new Ground();
ground1.HitBox = new Rect(groundImg1.Margin.Left, groundImg1.Margin.Top, groundImg1.Width, groundImg1.Height);
ground1.HitBox.Inflate(0, -35);
zelvicka = new Turtle();
zelvicka.IsMoving = true;
zelvicka.IsDirectionForward = true;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(20);
timer.Tick += Update;
timer.Start();
}
I've done the same with the turtle, whose virtual Rect is updating every time it moves in Update function generated by DispatcherTimer so the virtual Rect is always up to date.
private void Update(object sender, EventArgs e)
{
zelvicka.HitBox = new Rect(turtleImg.Margin.Left, turtleImg.Margin.Top, turtleImg.Width, turtleImg.Height);
zelvicka.HitBox.Inflate(-110, -30);
// ZELVICKA IS GOING FORWARD AND IS TOUCHING THE GROUND
// ZELVICKA IS GOING BACKWARDS AND IS TOUCHING THE GROUND
if (zelvicka.IsMoving && zelvicka.IsDirectionForward && zelvicka.HitBox.IntersectsWith(ground1.HitBox))
{
turtleImg.Margin = new Thickness(zelvicka.DistanceWalked += 3, zelvicka.MetersFalled, 0, 0);
}
else if (zelvicka.IsMoving && !zelvicka.IsDirectionForward && zelvicka.HitBox.IntersectsWith(ground1.HitBox))
{
turtleImg.Margin = new Thickness(zelvicka.DistanceWalked -= 3, zelvicka.MetersFalled, 0, 0);
}
}
Rate this code, please, and suggest improvements, if any.