My first pong game

Started by
7 comments, last by evillive2 17 years, 12 months ago
I just finished my first pong game that took me about 5-6 hours to make. I can't admit that i've done it 100% myself because the time class and some other ideas are something i've learned that someone else used in tutorials. The rest is all my own ideas that I found out while coding. I don't know how a pong game should be coded, but this is how I did it, have never read a tutorial about how to write a pong game, just learned SDL in general. There are some bugs that i'm aware of myself, for example. The computer is probably unbeatable but i'm happy I managed to make it move after the ball because I wasn't sure how to make it and it only took 20 minutes to make. The ball can also sometimes get stuck in the paddle, it's not that common while playing but can happen so don't be surprised if that happens. Another thing is that the computer paddle can if it wants, move out of the screen that's just how I wanted it :) I would be happy if you could post your comments and ways to improve the game itself. Anything you could come with would help me, like ways to improve the "pong physic" i've used, or improving the computers thinking. I know there is much to work on because this is my first time, but i've learned a lot while coding it. The source with executable is located here for those who are interested: http://www.pbe.se/testpong.RAR You win when you get 10 points, don't know if it's even possible to win against the computer, but anyways, that's how it is. If you haven't sdl_image, there may be some problems with missing dll files and other stuff, but I have included the dll files i've used.
Advertisement
I havent downloaded it as Im at work. But are you using intersectRect() for the collision detection, because that could be why the ball gets stuck, that function is about as useful as burnt toast to a heart surgeon. Also, if you think the computer is unbeatable, thats because you have him moving as fast as the ball, make him move in the direction of the ball, but at 3/4 or 4/5 the speed. Ill download it later and give you more feedback.
We have youth, how about a fountain of smart.e4 e5 f4 d5
The ball getting stuck in the paddle is due to a collision at the top or bottom of the paddle. The ball goes inside from the top or bottom and then fits the criteria for a collision so it just keeps reversing the x velocity (keeping it locked inside the paddle), until it comes out again in the top or bottom. You can fix it by testing for a top or bottom paddle collision or allowing the ball to pass through the top or bottom without reversing x velocity. There's two easy ways to implement a beatable AI. Either switch the paddle speed to slower than the ball, or get a random number and only move the paddle a certain % of the time (50-80% based on difficulty). Congrats on a first game! Don't worry about using 100% original code, no need to reinvent the wheel, and you'll find as you go on that most of the code you see is copied from somewhere else.
I would make the ball go the right if it touches the right half of the paddle, and even more to right when just at the right edge (like breakout).
Quote:Original post by Tang of the Mountain
I havent downloaded it as Im at work. But are you using intersectRect() for the collision detection, because that could be why the ball gets stuck, that function is about as useful as burnt toast to a heart surgeon. Also, if you think the computer is unbeatable, thats because you have him moving as fast as the ball, make him move in the direction of the ball, but at 3/4 or 4/5 the speed. Ill download it later and give you more feedback.


I'm using SDL_Rect for collision detection, didn't know about intersectRect(). The computer wasn't unbeatable after all, it's just hard to get a point.

Quote:ChurchSkiz
The ball getting stuck in the paddle is due to a collision at the top or bottom of the paddle. The ball goes inside from the top or bottom and then fits the criteria for a collision so it just keeps reversing the x velocity (keeping it locked inside the paddle), until it comes out again in the top or bottom. You can fix it by testing for a top or bottom paddle collision or allowing the ball to pass through the top or bottom without reversing x velocity. There's two easy ways to implement a beatable AI. Either switch the paddle speed to slower than the ball, or get a random number and only move the paddle a certain % of the time (50-80% based on difficulty). Congrats on a first game! Don't worry about using 100% original code, no need to reinvent the wheel, and you'll find as you go on that most of the code you see is copied from somewhere else.


Thank you.
Now when you say it, it makes sense. It always happens when the paddle goes against the ball while it's moving towards it. Maybe increasing the y position and x position at the same time when it hits the side will do it, that may prevent the ball from going inside the paddle. I will fix that problem somehow.

Quote:Marmin
I would make the ball go the right if it touches the right half of the paddle, and even more to right when just at the right edge (like breakout).


I added a basic one like that first, but removed it later because it made some things buggy. I think I will add it again later to make it more functional.

[Edited by - password on April 18, 2006 2:45:10 PM]
I'm completely lost, can someone help me with the idea how to make the ball not get stuck in the paddle. I've tried this and I thought it worked in the beginning, it did work until it stopped working :)

if (ballBox.x <= playerClass->playerBox.x + BOARD_WIDTH-12) {
ballBox.y+=ySpeed;
} else if (ballBox.x <= playerClass->playerBox.x + 12) {
ballBox.y+=ySpeed;
}

if (ballBox.x <= playerClass2->playerBox.x + BOARD_WIDTH-12) {
ballBox.y-=ySpeed;
yDirection=0;
} else if (ballBox.x <= playerClass2->playerBox.x + 12) {
ballBox.y-=ySpeed;
yDirection=0;
}

I put that code in the show function in the collision if clause. Would appreciate if anyone could help me with this problem. It's the last thing I will add before i'll continue experimenting with new stuff.
nice game for basically your first one. where did you find tutorials on graphics?
I read these tutorials by Lazy foo, if you mean SDL:
http://lazyfooproductions.com/SDL_tutorials/index.php

I had this problem as well when I first did pong. Using intersecting rectangles is really fast for collision detection and pixel perfect collision detection can be overkill for something like pong where your ball is moving quick enough to make the rough collision detection almost unnoticeable.

At any rate, my solution was to check for collision before actually moving the ball.

My initial collision detection used a simple bounding box check where I would calculate where I wanted to move the ball, then pass in the resulting bounding box to my own IntersectBox function which compared 2 boxes to see if they intersected. If they did, I didn't move the ball there and processed a ball collision from the previous position. This handled the ball getting stuck to the paddle thing pretty well and didn't need to go into pixel perfect collision detection.

The trick then becomes figuring out where and which face the ball is colliding on. With pong this is relatively simple because at any time the ball in motion is only capable of a collision with 2 faces of any 1 paddle and many times this is easy to throw 1 or the other out. Then, throwing out perfect corner collisions (they will make you crazy) you can expand on that by saying there are only 4 points on the ball that can make contact with either paddle, 2 if you use the same logic as with the paddle (the top center, right center, bottom center and left center). From that you can go so far as to use line segment collision to find the exact point of collision (do this even if you don't need to... it does help in other games) or you can best guess it based on which point is colliding with the paddle.

That sounds confusing I think but move a penny or something round on something rectangular and you will kind of see what I mean about the 4 points and stuff.
Evillive2

This topic is closed to new replies.

Advertisement