• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mistervirtue

Code Logic Help - Pong

12 posts in this topic

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.
[CODE]
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);
}
}
}
[/CODE]

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
0

Share this post


Link to post
Share on other sites
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
1

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 [b]is a paddle[/b] 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.
2

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
0

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][img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] [/sup]

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

Share this post


Link to post
Share on other sites
[quote name='mistervirtue' timestamp='1349975891' post='4989166']
I actually Just solved my own problem. I feel like a big man right now. Thanks for your help everyone.
[/quote]

It would be even [i]bigger[/i] of you to let the other newbies in the community know what pit fall to avoid and how you solved it! [img]http://public.gamedev.net//public/style_emoticons/default/ohmy.png[/img]

[img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

Clinton
0

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.
0

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.
0

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?
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0