• Create Account

## Code Logic Help - Pong

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1mistervirtue  Members

611
Like
0Likes
Like

Posted 10 October 2012 - 09:57 AM

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;
}
}
{
if (position.X > screenBounds.Width || position.X  < 0)
{
return true;
}
return false;
}
{
Rectangle ballLocation = new Rectangle(
(int)position.X, (int)position.Y, texture.Width, texture.Height);
{
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.

### #2menyo  Members

896
Like
1Likes
Like

Posted 10 October 2012 - 11:48 AM

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:

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

Latest finished project Project

### #3mistervirtue  Members

611
Like
0Likes
Like

Posted 10 October 2012 - 01:52 PM

Could you give me an example of subclassing or maybe a good link.

### #4caldiar  Members

715
Like
2Likes
Like

Posted 10 October 2012 - 02:32 PM

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.

### #5mistervirtue  Members

611
Like
0Likes
Like

Posted 11 October 2012 - 10:58 AM

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, 11 October 2012 - 11:07 AM.

### #63Ddreamer  Members

3822
Like
0Likes
Like

Posted 11 October 2012 - 11:11 AM

Hi,

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

Clinton

Edited by 3Ddreamer, 11 October 2012 - 11:12 AM.

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software.  The better the workflow pipeline, then the greater the potential output for a quality game.  Completing projects is the last but finest order.

by Clinton, 3Ddreamer

### #7mistervirtue  Members

611
Like
0Likes
Like

Posted 11 October 2012 - 11:18 AM

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

### #83Ddreamer  Members

3822
Like
0Likes
Like

Posted 11 October 2012 - 11:30 AM

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!

Clinton

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software.  The better the workflow pipeline, then the greater the potential output for a quality game.  Completing projects is the last but finest order.

by Clinton, 3Ddreamer

### #9mistervirtue  Members

611
Like
0Likes
Like

Posted 11 October 2012 - 12:29 PM

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.

### #10BeerNutts  Members

4287
Like
0Likes
Like

Posted 11 October 2012 - 01:35 PM

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #11mistervirtue  Members

611
Like
0Likes
Like

Posted 11 October 2012 - 08:47 PM

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?

### #12superman3275  Members

2061
Like
0Likes
Like

Posted 11 October 2012 - 09:50 PM

Private = No Inheritece. If you want inheritence use protected or public in your class.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

or Personal-Message me on here !

### #13mistervirtue  Members

611
Like
0Likes
Like

Posted 11 October 2012 - 11:39 PM

Superman3275, Thanks man. I getting the hang of this thing now. I thanks everyone for the help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.