• Advertisement
Sign in to follow this  

Code Logic Help - Pong

This topic is 1927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Fellow Gamedev'ers,

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

Share this post


Link to post
Share on other sites
Advertisement
Good thinking,

What you could do is create a paddle class with all its attributes and needs.

then create a player class inhereting from the paddle class where you add movement and correct its position or even color/texture:

class player : paddle

Then for the ai you do the same and add some AI to its movement.

class AI : paddle

Share this post


Link to post
Share on other sites
While you're at this point, consider the following: A player isn't necessarily a paddle. Rather, a paddle is controlled by the player (or the AI).

Keeping that in mind, having the player inherit from the paddle class is probably not the most correct route to go as it indicates that your player is a paddle and not simply that your player can control a paddle. If you decide to still design your program in such a way, it's not going to explode in your face or anything and will definitely still work, but in more complicated projects it'll start making you scratch your head wondering what exactly is going on.

When thinking of the design in terms of 'the paddle can be controlled by either a player or the computer' it makes things a little simpler to work with.

You would have a single paddle class whose movement is determined by input received. You could then have a Player and NPC class which each can send input to the paddle they're associated with. The input for Player being created through keyboard button presses and the input for NPC being generated through some set of rules you've determined to affect their movement behavior.

Share this post


Link to post
Share on other sites
This is my BasePaddle Class. My Parent Class
----------------
[source lang="csharp"] abstract class BasePaddle
{
//Texture For the Block
Texture2D texture;
//Postion of the Block
Vector2 postion;
//Directoin of the Block
Vector2 direction;
//Collision Rectangle for the block
Rectangle screenBounds;

//Speed of Paddle Movement
float paddleSpeed = 20;

public BasePaddle(Texture2D texture, Rectangle screenBounds, Vector2 direction, Vector2 postion)
{
this.texture = texture;
this.screenBounds = screenBounds;
this.direction = direction;
this.postion = postion;
}
public virtual void Update()
{
direction = Vector2.Zero;
direction.Y *= paddleSpeed;
postion += direction;
}
public virtual Rectangle GetBounds()
{
return new Rectangle(
(int)postion.X, (int)postion.Y, texture.Width, texture.Height);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, postion, Color.White);
}
}[/source]

----------------
This is my Child Class PlayerPaddle
[source lang="csharp"] class Paddle : BasePaddle
{
//Texture For the Block
Texture2D texture;
//Postion of the Block
Vector2 postion;
//Directoin of the Block
Vector2 direction;
//Collision Rectangle for the block
Rectangle screenBounds;

//Speed of Paddle Movement
float paddleSpeed = 20;

//INPUT
KeyboardState keyboardState;

//Consturotor for Paddle
public Paddle(Texture2D texture, Rectangle screenBounds)
{
this.texture = texture;
this.screenBounds = screenBounds;
}

//Update for paddle
public void Update()
{
direction = Vector2.Zero;

keyboardState = Keyboard.GetState();

if (keyboardState.IsKeyDown(Keys.Up))
{
direction.Y = -1;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
direction.Y = 1;
}
direction.Y *= paddleSpeed;
postion += direction;
CheckScreenBounds();
}
//Check To see if player has left Screenbounds
private void CheckScreenBounds()
{
if (postion.Y < 0)
{
postion.Y = 0;
}
if (postion.Y + texture.Height > screenBounds.Height)
{
postion.Y = screenBounds.Height - texture.Height;
}
}
public void SetInStartPosition()
{
postion.Y = ((screenBounds.Height - texture.Height) / 2);
postion.X = (screenBounds.Width - texture.Width);
}
public Rectangle GetBounds()
{
return new Rectangle(
(int)postion.X, (int)postion.Y, texture.Width, texture.Height);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, postion, Color.White);
}


}[/source]

Can someone help me out and walk me through Subclassing and Inheritance, I am having a lot of trouble learning how to make this work. I am sorry If i sound like a needy four year old, But I can't figure out the whole subclassing thing. Edited by mistervirtue

Share this post


Link to post
Share on other sites
[sup]Hi, [/sup]

[sup]Correct the spelling of "Direction" in line 7 or elsewhere. See what happens. Also, you need to get into the habit of good commenting. [/sup]
[sup]Get back to us after you finished those tasks.[/sup]
[sup]smile.png [/sup]

[sup]Clinton[/sup] Edited by 3Ddreamer

Share this post


Link to post
Share on other sites

I actually Just solved my own problem. I feel like a big man right now. Thanks for your help everyone.


It would be even bigger of you to let the other newbies in the community know what pit fall to avoid and how you solved it! ohmy.png

wink.png

Clinton

Share this post


Link to post
Share on other sites
3Ddreamer I suppose you are right. My solution was create a constructor that in my Paddle class like so
[source lang="csharp"]
//Consturotor for Player Paddle
//it will have it's own texture and screenBounds so use This
public PlayerPaddle(Texture2D texture, Rectangle screenBounds, Vector2 direction, Vector2 position)
: base(texture, screenBounds, direction, position)
{
this.texture = texture;
this.screenBounds = screenBounds;
}
[/source]

That was my main problem was that I was trying to create an instance of the abstract BasePaddle class rather than create the instance of the Player Paddle. So I fixed my problem. Thanks again for all your help fellow gamedev's.

Share this post


Link to post
Share on other sites
mistervirtue,

Your player paddle class doesn't need to define texture, screenbounds, or any other variable the parent class has. The Player class will INHERIT those value.

So, basically, the PlayerPaddle already has those variables, and you don't need another copy of them.

Share this post


Link to post
Share on other sites
BeerNuts,

I deleted the variables but the game crashed, It said all of those variables are now inaccessible due to the protection level. What do I do now?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement