Decided to remake an old classic, Pong! I know this has been done a million times before but I thought I'd give it a shot and maybe learn a thing or two.
Move left and right and smack the ball to the other side to score!
Known Bugs:
-Ball sometimes passes through the paddle when hit at the corners of the screen
-Menu graphics glitching slightly when selecting options
Controls:
Player 1:
Arrow Keys
Player 2
E,S,D,F
Version 1.1:
Added in simple AI for playing single player and now the first player to 5 points wins!
Version 1.0:
Original version with only a 2 player endless mode and controls screen.
Good, but DEFINITELY want to consider making an option for a 1-player game. Not everyone has as many friends as some do. :)
Haha okay I'll see if I can add that in the future ;) I've just been playing using 2 hands whilst testing but it's really difficult -___-
function ai(p) if (p.x > ball.x) p.x -=1 elseif (p.x + p.width < ball.x) p.x +=1 end |
Something like that should work. The elseif considers the width of the paddle, so that it doesn't keep trying to slam the right side wall. I didn't really look how you have it set up, but I assume you have some kind of width property, and if not, just put in the number value. Then, in your menu, add an option for 1 or 2 players. If 1 player is selected, enable the ai obviously, and disable the second player controls (to prevent cheating). Should be relatively straightforward.
Bonus points if you also make it so the AI has a chance to bounce off walls similar to how players can (with the function I wrote, the AI would never bounce off a wall, since the furthest it would ever move would be exactly to the wall and then stopping, so you'd just have to make it use your acceleration/deceleration routines instead of my incrementing and decrementing the x coordinate).
Thanks for the reply Scathe! Yeah I see what you mean, I thought programming the AI for this would be way harder than just accelerating towards the balls x co-ord but I think this should do nicely!
I'll have a go at implementing it now...
PC, I noticed your ball does not have deflection with English. That is, if the ball hits the far left of your paddle, then the ball will be reflected with a much stronger tilt to the left.
Same on the opposite. If the ball hits a paddle, either player's or opponent's paddle. If the ball hits the right edge, then the ball will have an increased acceleration to the right.
Let's see if we can pick up PONG on the Youtube.
https://www.youtube.com/watch?v=it0sf4CMDeM
Now watch the player on the right as they do a high deflect.
dw817, I originally wanted to get the ball to be able to bounce off of the ends of the paddles but decided against it for the sake of simplicity ¯_(ツ)_/¯
You can, however, move the paddle fast in a certain direction to add some of the paddles velocity to the ball and send it flying off in any given direction.
[Please log in to post a comment]