CODECUBE VENTURES

C# PONG Demo part 2

Since my last article, I've implemented some new features to my pong game.

The first feature is the paddles ... they are simple Rectangle objects because of the simple simple graphics inherent in PONG.

The second feature is some basic collision detection. Using the IntersectsWith() method of the rectangle object, I simply reverse the horizontal direction if the method returns true.

//Collision Detection
if (lPaddle.IntersectsWith(rect))
{
if (xdir == Direction.Right)
xdir = Direction.Left;
else
xdir = Direction.Right;
}
if (rPaddle.IntersectsWith(rect))
{
if (xdir == Direction.Right)
xdir = Direction.Left;
else
xdir = Direction.Right;
}

Of course, this code can be improved. First of all by encapsulating all of this into a Paddle class, and having a method called ReverseDirection(), but for the sake of simplicity, I'm keeping it simple for now.

Also, I will create my own collision detection in the future because this has introduced a bug. If the "ball" collides with the paddle on the top or bottom surface, the ball will "float" through the paddle ... an effect you'll have to see to understand.

Finally, I captured some keyboard input so that you can move the right paddle up and down. The code is simple for now:

protected override void OnKeyDown(KeyEventArgs kev)
{
switch (kev.KeyCode)
{
case Keys.Up:
lPaddle.Y-=5;
break;
case Keys.Down:
lPaddle.Y+=5;
break;
}
}

At any rate, I think the project is moving along nicely ... you can download the latest code below (along with the SharpDevelop project files)

pong2.zip - 8k

Latest post: Digging Up the First Version of CodeCube

See more in the archives