5
\$\begingroup\$

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.

asked Oct 29, 2020 at 14:45
\$\endgroup\$
2
  • \$\begingroup\$ It works properly, but I have to manually set new height and new width with Inflate function, which I found very ineffective and time-consuming, hence I'm looking for a better way as I don't think my code is good enough and it needs some more improvements! \$\endgroup\$ Commented Oct 29, 2020 at 14:55
  • \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$ Commented Oct 29, 2020 at 15:12

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.