✅ Working with abstract classes in a homework problem

So for this bit of homework I'm doing, im required to make it so that a player, a class that inherits abstract class gameobject, cannot go further than the reaches of the canvas. I'm not very good at abstract classes, but I had the idea to override the move methods as given in the abstract class so I could use them in the player class according to movement specification
13 Replies
Rev E. Lations
Rev E. LationsOP2w ago
using DodgeEm.View.Sprites; namespace DodgeEm.Model { /// <summary> /// Manages the player. /// </summary> /// <seealso cref="GameObject" /> public class Player : GameObject { #region Data members private const int SpeedXDirection = 3; private const int SpeedYDirection = 3; #endregion #region Constructors /// <summary> /// Initializes a new instance of the <see cref="Player" /> class. /// </summary> public Player() { Sprite = new PlayerSprite(); SetSpeed(SpeedXDirection, SpeedYDirection); } #endregion } }
Angius
Angius2w ago
Yeah, sounds like a good idea
Rev E. Lations
Rev E. LationsOP2w ago
So the abstract class’s version of the methods don’t start off as abstract I think The code I’ve posted is the base code but I tried setting the methods in the abstract class to be virtual Then in the player class I had them be override And then I added the precondition But do you know the best way I could then have the actual functionality?
Angius
Angius2w ago
ye Well, uh, add that functionality? With each move, first check if the resulting position would be valid If not, don't move
Rev E. Lations
Rev E. LationsOP2w ago
I tried doing Precondition This.moveleft But it crashes the program So I was wondering if that didn’t work in general One of my concerns is not violating D.R.Y.
Angius
Angius2w ago
For example, something like
void MoveLeft()
{
var newPositionX = currentPosition.X - 1;
if (newPositionX < 0) return;
currentPosition.X = newPositionX;
}
void MoveLeft()
{
var newPositionX = currentPosition.X - 1;
if (newPositionX < 0) return;
currentPosition.X = newPositionX;
}
This would prevent the player from moving left, if the move would result in the position on the X axis being negative So it constrains the valid X positions to [0...] range
Rev E. Lations
Rev E. LationsOP2w ago
Right Maybe I’ll just have to email my professor to see if that would be valid by his specifications A big part of this focus is on writing the code “cleanly” Like in Robert Martin’s “Clean Code”
Angius
Angius2w ago
I guess one thing to make it more clean would be declaring constants for the position constraints Other than that, can't see what would be unclean about it
Rev E. Lations
Rev E. LationsOP2w ago
Well thank you then. You’ve helped clean up some of my uncertainty :LarvaSmile:
Angius
Angius2w ago
:Ok:
Rev E. Lations
Rev E. LationsOP2w ago
Can I ask how do I close this?
Angius
Angius2w ago
Like so: $close
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?