I am trying to finish my Pong Clone by Monday to start my personal journey in to the game programming. I have been slacking off but I realized I am only cheating myself if continue. My resolve to finish this project is strong and nothing is going to get in my way.
I have come to a design problem though. I have a Paddle Class for the player but I don't know how to make a automated paddle for the computer. Seeing as they will have very similar attributes but I thought of making the computer paddle a subclass of the player paddle (or inherit from the PlayerPaddle Class). I don't fully understand how to do that though. If I could get some help that would be very much appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace JONG_PongClone_
{
class Ball
{
//The postition of the ball
Vector2 position;
//Motion of the ball
Vector2 motion;
//speed of the ball
float ballSpeed = 2;
//Texture For the ball
Texture2D texture;
//Collision rectangle for the ball
Rectangle screenBounds;
//Ball Constructor
public Ball(Texture2D texture, Rectangle screenBounds)
{
this.texture = texture;
this.screenBounds = screenBounds;
}
//Update Method for the ball
public void Update()
{
position += ballSpeed * motion;
CheckForCollisonWithTop();
}
public void CheckForCollisonWithTop()
{
if (position.Y < 0)
{
position.Y = 0;
motion.Y *= -1;
}
if (position.Y + texture.Height > screenBounds.Height)
{
position.Y = screenBounds.Height - texture.Height;
motion.Y *= -1;
}
}
public bool BehindPaddle()
{
if (position.X > screenBounds.Width || position.X < 0)
{
return true;
}
return false;
}
public void CheckForCollisionWithPaddle(Rectangle paddleLocation)
{
Rectangle ballLocation = new Rectangle(
(int)position.X, (int)position.Y, texture.Width, texture.Height);
if (paddleLocation.Intersects(ballLocation))
{
position.X = paddleLocation.X - texture.Width;
motion.X *= -1;
}
}
public void SetInStartPosition()
{
motion = new Vector2(1, -1);
position.X = (screenBounds.Width - texture.Width) / 2;
position.Y = (screenBounds.Height - texture.Height) / 2;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
}
I feel like the smart way of doing this would have the computer paddle be a subclass of paddle. I just don't know exactly how to do that. I am still a little shaky on inheritance and such.
Edited by mistervirtue, 10 October 2012 - 10:02 AM.






