View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Code Help - Pong Game

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.

2 replies to this topic

### #1mistervirtue  Members

Posted 16 October 2012 - 08:59 AM

I know what I want to do, but I am unsure how to do it. I want to have my ComputerPaddle Sprite Update it's location based on the position of my Ball Sprite. Code-wise I am not sure on how to allow my ComputerPaddle class to acess the postion of the ball class.
NOTE: Please don't tell me excatly how to do it, please drop hints/tips. I learn better if I have but together myself, but a point in the right direction is always nice.
---------------------------
[source lang="csharp"]class ComputerPaddle : BasePaddle { //Speed of the paddle float paddleSpeed = 15; public ComputerPaddle(Texture2D texture, Rectangle screenBounds, Vector2 direction, Vector2 position) : base(texture, screenBounds, direction, position) { this.texture = texture; this.screenBounds = screenBounds; } public override void Update() { //Use the location of the ball to move the paddle up and down // in the Y direction, toward Y value of the ball } }[/source]
------------------------------------------
My Ball Class
[source lang="csharp"] class Ball { //The position 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(); } //Any time ball collides with the bounds of the window increase the speed of the ball 1 point. //Any time ball collides with the bounds of the window change the directoin of the motion in the Y direction public void CheckForCollisonWithTop() { if (position.Y < 0) { position.Y = 0; motion.Y *= -1; ballSpeed += 1; } if (position.Y + texture.Height > screenBounds.Height) { position.Y = screenBounds.Height - texture.Height; motion.Y *= -1; ballSpeed += 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; RestartBallSpeed(); } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, position, Color.White); } public void RestartBallSpeed() { ballSpeed = 2; } }[/source]
-------------------------------------------

Like I said I think there is a way for me to acess the postion of the ball but I don't know how. Any help would be very much appreciated. I think I can do something similar to my CheckForCollisionWithPaddle() Method. I just need a little but of a push start.

Edited by mistervirtue, 16 October 2012 - 09:37 AM.

### #2dynasty1121  Members

Posted 16 October 2012 - 11:22 AM

Without laying out the exact code, implement a getter.

### #3mistervirtue  Members

Posted 16 October 2012 - 01:08 PM

Sorry I posted this in the wrong fourm

Edited by mistervirtue, 16 October 2012 - 01:19 PM.

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.