✅ 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
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
}
}
Yeah, sounds like a good idea
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?
ye
Well, uh, add that functionality?
With each move, first check if the resulting position would be valid
If not, don't move
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.
For example, something like
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...]
rangeRight
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”
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
Well thank you then. You’ve helped clean up some of my uncertainty
:LarvaSmile:
:Ok:
Can I ask how do I close this?
Like so: $close
If you have no further questions, please use /close to mark the forum thread as answered