• Create Account

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

11 replies to this topic

### #1mistervirtue  Members

611
Like
0Likes
Like

Posted 16 October 2012 - 01:19 PM

(I misposted this is in general, this is should have been posted in For Beginners)

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.

### #2DpakoH  Members

1198
Like
0Likes
Like

Posted 16 October 2012 - 01:54 PM

why don't you make something like GameManager class that has both ball and paddle as members and it will handle the collision ?! there are other ways to do it of course

### #3stitchs  Members

1361
Like
0Likes
Like

Posted 16 October 2012 - 01:57 PM

Not an answer, but that much desired hint: search for 'Get and Set values in coding' in your desired search engine.

Regards,

Stitchs.

### #4Black-Rook  Members

1563
Like
0Likes
Like

Posted 16 October 2012 - 05:48 PM

Why not just keep two classes, one for Paddle and one for the Ball.

The above assumes bounding box collision.

You could also just have a function for collision in your logic code, and pass both the Paddle and Ball into that function to find out if a collision is true. If so, then you have the ball, and game do whatever it's supposed to do after.

There are many ways to do this, but don't try to complicate something that is simple.
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

### #5mistervirtue  Members

611
Like
0Likes
Like

Posted 16 October 2012 - 11:04 PM

I am not getting get and sets. I Have a Get BallPosition in my ball class but I don't know how to have my ComputerPaddle access it.
------------ MY Get for Ball Position in the ball Class --------------
[source lang="csharp"] public Vector2 GetPosition { get { return position; } }[/source]

Now how do I get my ComputerPaddle Access this value?

### #6Black-Rook  Members

1563
Like
1Likes
Like

Posted 16 October 2012 - 11:30 PM

Not sure why you're doing it that way... This is very simple thing to program.

Make a function that returns the value of your Ball's Position.

int Ball::Ball_X_Position()
{
return x;
}

Just have whatever you need done occur after positions have been read.

You don't need to use Get and Set.
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

### #7mistervirtue  Members

611
Like
0Likes
Like

Posted 16 October 2012 - 11:33 PM

I am using C#, so I don't know exactly what a function is.

### #8Black-Rook  Members

1563
Like
0Likes
Like

Posted 16 October 2012 - 11:41 PM

C# uses functions like C++ does. Since C# uses classes, you have functions in those classes.

For example:

using System;

class Ball
{
private int x, y;

// Constructor
public Ball(int Temp_X, int Temp_Y)
{
x = Temp_X;
y = Temp_Y;
}

// Destructor
~Ball(){}

// Get X
public int Get_X()
{
return x;
}
}

Just a basic example.

Edited by Black-Rook, 16 October 2012 - 11:44 PM.

GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

### #9mistervirtue  Members

611
Like
0Likes
Like

Posted 16 October 2012 - 11:44 PM

By functions do you mean Methods?

### #10Black-Rook  Members

1563
Like
0Likes
Like

Posted 16 October 2012 - 11:46 PM

Yes they're called methods. I shouldn't be using the word "functions", old habit!
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

### #11mistervirtue  Members

611
Like
0Likes
Like

Posted 16 October 2012 - 11:48 PM

Okay so can you try again and explain your methodology for solving this problem I have?

### #12Black-Rook  Members

1563
Like
0Likes
Like

Posted 16 October 2012 - 11:52 PM

You make a method which returns the position of object a, then object b has a method which reads the positions of object a and compares itself with those positions, then follows through with the appropriate actions.

I explained how to do this six posts above.
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

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.