Ascii Pong
The challenge is to recreate the classic game "pong" in ASCII characters in the shortest code possible.
Requirements/specifications
- The "screen" should be 45x25 characters.
- White space should actually be white space.
- The paddles should be 9 equals signs:
=========
and should be on the top-most and bottom-most rows (I know the original is played side-to-side instead of top-to-bottom, but I think this works better for ascii pong). - The ball can be a lower or upper case
o
or a zero. - Two input buttons of any kind (it is fine if the user presses a key that makes a letter show up in the input, that is fine, too) to move the player's paddle left and right one or two character at a time.
- The ball needs to ricochet at the appropriate angle upon hitting a paddle or a wall (hint: negate the x or y difference value).
- The score should be displayed somewhere in the output. The score is how many times the player successfully hits the ball.
- If the ball misses the player's paddle, terminate the program.
- There should be some sort of AI (even if the AI's paddle just matches the ball's x value).
- The ball can't travel in a straight line vertically or horizontally.
The start screen/first frame should look something like this:
=========
o
=========
score: 0
Scoring This is code-golf, so the shortest code wins ... however, there are some bonuses to decrease your character count:
- -30 characters: Change trajectory of ball depending on where it hits on the paddle
- -10 characters: Make the game speed up over time
- -20 characters: Make the AI beatable
- -20 characters: Avoid any situation where the player stays still and the game goes on forever without winning or losing (caused by certain trajectories and AI patterns)
- -20 characters: Make the ball start moving at a (semi-)random trajectory
- -25 characters: Add a reset option
Here is an un-golfed example with no bonuses in JavaScript:
//init
var x = 22,
y = 12,
xd = Math.random() < 0.5 ? -1 : 1,
yd = 1,
player = opponent = 18,
score = 0,
//interval that runs 10x per second (minus the runtimeof one cycle)
interval = setInterval(function() {
//move ball
x += xd;
y += yd;
//move opponent
opponent = x - 4;
//collision with walls
if(x <= 0 || x >= 44) xd = -xd;
//collision with paddles
if(y == 1) {
if(x >= opponent && x < opponent + 9) yd = -yd;
else {
//you win
clearInterval(interval);
document.body.innerHTML = "You win!<br>Your score was: " + score;
return;
}
}
else if(y == 23) {
if(x >= player && x < player + 9) {
yd = -yd;
score++;
}
else {
//you lose
clearInterval(interval);
document.body.innerHTML = "You lose!<br>Your score was: " + score;
return;
}
}
draw();
}, 100);
function draw() {
var body = document.body;
body.innerHTML = "";
for(var i = 0; i < 25; i++) {
for(var j = 0; j < 45; j++) {
//white space is default
var char = " ";
//ball
if(j == x && i == y) char = "o";
//paddles
if(i === 0) {
if(j >= opponent && j < opponent + 9) char = "=";
}
else if(i == 24) {
if(j >= player && j < player + 9) char = "=";
}
body.innerHTML += char;
}
body.innerHTML += "<br>";
}
body.innerHTML += "score: " + score;
}
//key press listener for left and right movement
window.onkeydown = function() {
if (window.event.keyCode == 37) player -= 2;
else if(window.event.keyCode == 39) player += 2;
};
<body style="font-family: monospace; white-space: pre">
</body>
Let the games begin!