The challenge is simple. Give the shortest code possible to reproduce the classic 2-player game of pong http://en.wikipedia.org/wiki/Pong . The level of graphics and functionality should be as close as possible to this javascript demonstration http://codeincomplete.com/posts/2011/5/14/javascript_pong/demo.html (but without the extra options you can click on on the left or the frame, fps etc. information in the bottom right).

As always the code must be written in a free language (in both senses) and should be runnable on linux. Any libraries used must also be free, easily available and not be written for the purposes of this competition (and also not already contain a working version of Pong!).


Javascript, 883 (+ 70 HTML)

c.font="60px monospace"
c.fillText(a+" "+b,266,60)

/* Variable index:
a -> left player score
b -> right player score
c -> context
e -> event
i -> counter for dashed line
k -> keycode
m -> left paddle y
n -> right paddle y
p -> left paddle y velocity
q -> right paddle y velocity
s -> is start of game
u -> ball x velocity
v -> ball y velocity
w -> game is waiting (paused)
x -> ball x
y -> ball y

The script can be placed at the end of <body> or called onLoad. It needs the following canvas element:

<canvas id="c"width="640"height="480"style="background:#000"></canvas>

Player 1 uses the q and a keys, and player 2 uses the p and l keys. Press the esc key to pause and any key to start/continue.

You can play it in your browser here.

I wasn't sure of the physics to use, so I started off with a simple reflection method and then added some variety and experimented with it a bit. The ball's velocity in the y direction is affected by where on the paddle you hit the ball, so you have some control over where the ball goes. The ball's velocity in the x direction slowly increases with each hit in the rally.

I suspect that it will be beaten quite easily by solutions using libraries, but I had fun making it in plain javascript.

| improve this answer | |
  • 2
    \$\begingroup\$ That's a really nice implementation. \$\endgroup\$ – felipa Mar 12 '13 at 14:15
  • 1
    \$\begingroup\$ Could be golfed a bit more, I know I'm about two years late here. but you could assign 20 to a variable named g and save one measly byte. \$\endgroup\$ – Zacharý Dec 9 '16 at 0:25
  • \$\begingroup\$ Damn, it's hard to play on an azerty keyboard... \$\endgroup\$ – dim Jan 5 '18 at 16:05

sed, 35

Raising the bar a bit with a postage-stamp sed meditation.

s/> / >/
s/ </< /

The meditation is enjoyed on stdin/stdout on two computers, not necessarily connected by a network. The meditation begins in the state

0         <       1

with guru zero on the left and one on the right. The angle bracket moves left and right, and if a guru maneuvers their number to contact the cursor as it comes to their side, their score is increased by one, and they become elated with joy.

The meditation is initiated by typing the above state into sed -f medi.sed, and the computer responds with the next state. The dutiful guru types that state into the meditation, reading aloud the next key they will press, with both gurus pressing the holy key to enter the future at the same time. The dutiful computer replies with the next state. This, in turn, is read aloud while typed in unison as with the last. Continue advancing into the future until infinite bliss is achieved.

Gurus desiring a challenge may play 'turbo' mode, wherein the gurus attempt to collaboratively predict the computer's next state, and typing it into the prompt instead of the current state. Gurus will have the wisdom to verify agreement between their predictions before entering the future.

| improve this answer | |
  • 24
    \$\begingroup\$ Dude... what did I just read? \$\endgroup\$ – MrZander Mar 8 '13 at 23:43
  • 10
    \$\begingroup\$ @MrZander Only through extensive contemplation may the wisdom of the meditation be revealed. This contemplation is best aided through execution of the program. \$\endgroup\$ – boothby Mar 9 '13 at 18:00

Python (with pygame) 650 bytes


  • 1 and 2 player modes - When the game begins, press 1 for 1 player, or 2 for 2 players. The game will not begin until one of these keys is pressed.
  • Increasing ball speed - Each volley, the ball speed is increased so that after 10 volleys it has increased by approximately 50%, after 20 it will be 50% faster than that, etc.
  • Variable ball deflection - Ball deflection is based on two factors: what portion of the paddle it strikes, and whether or not the paddle is moving upon impact. If the ball strikes the paddle near one of the ends, it will be deflected more strongly than if it strikes near the middle (almost as if it were a curved surface). Additionally, if the paddle is in motion, the motion of the paddle is added to the deflection. In order to obtain the strongest deflection, the ball must strike near the end of the paddle, and the paddle must be in motion towards that same end. This is very similar to the original Pong for Atari 2600.
  • Pause - The game may be paused at any time by pressing the Space bar. Play will resume upon pressing the space bar a second time.
  • Controls - As with the example, player 1 moves with the Q and A keys, and player 2 moves with P and L.

As a final note, I would like to request that this solution not be selected as the accepted answer, for various reasons.

from pygame import*
while s.fill(time.wait(3)):
 event.get();k=key.get_pressed();t^=o*k[32];o=1-k[32];z=z or-k[49]-k[50]*2;e=k[113]-k[97];f=[k[112]-k[108],(h>0)*cmp(y,q-32)][z];a=p<g;b=q-[y,x][a]
 if p%608<g:m,n,p,h,v=[m+1-a,m,n+a,n,g+a*592,p,1-a*2,h/-.96,1,b/32.+~[f,e][a]][-g<b<j::2]
 for r in[(0,x,g,j),(624,y,g,j),(p,q,g,g)]+[(316,i*31,8,15)for i in range(g)]:draw.rect(s,w,r)
 if z*t:v*=(0<q<464)*2-1;x-=(0<x-e<400)*e/.6;y-=(0<y-f<400)*f/.6;p+=h;q+=v
 c=font.SysFont('monospace',j,1).render('%3d %%-3d'%m%n,1,w);s.blit(c,(320-c.get_width()/2,0));d.flip()

Sample screen capture:

Note: the font used for the score may vary from system to system.

| improve this answer | |
  • \$\begingroup\$ The score shows in the left half for me, and I think the deflection is too strong, but it's pretty cool anyway :) \$\endgroup\$ – aditsu quit because SE is EVIL Mar 14 '13 at 8:28
  • \$\begingroup\$ @aditsu are you operating under linux? I'll see if I can find an alternative that works regardless of the selected font face. The idea behind the deflection is that it would be possible to deflect the ball at a faster speed (maximum 2.5 on my scale) than the paddle can move (1.67). This is only possible if you give it some 'spin'. If the paddle isn't moving when the ball strikes, the maximum is velocity will be 1.5. \$\endgroup\$ – primo Mar 14 '13 at 8:38
  • \$\begingroup\$ Yes I'm using Linux. I find it strange that the ball can actually go "back" (up or down) after hitting the paddle, instead of continuing in the same direction (perhaps at a different angle). But maybe the Atari version did that, I don't know. \$\endgroup\$ – aditsu quit because SE is EVIL Mar 14 '13 at 9:07
  • \$\begingroup\$ @aditsu It cost a few bytes, but I believe the score should be centered regardless of the font chosen (so long as it is monospace). Let me know if it works for you. And yes, the deflection is very similar (although a smoother distribution) to the Atari version I was familiar with. If you imagine the paddle as a (convexly) curved surface (like an air hockey paddle), I think it should be more intuitive. \$\endgroup\$ – primo Mar 14 '13 at 11:22
  • \$\begingroup\$ Yes it's centered now \$\endgroup\$ – aditsu quit because SE is EVIL Mar 14 '13 at 16:53

HTML & JavaScript (take 2) - 525

Since the OP didn't seem to care much about the "as close as possible" part, here's an alternative solution that I mercilessly simplified, stripped and golfed. Q/A and P/L to play, but every other key also has an effect. Again, the code is fully self-contained and I tested it in Chromium 25 on Linux. I can golf it even further if you can accept small bugs or greater degradation of graphics quality/gameplay.

<canvas id=c><script>C=c.getContext('2d');f=C.fillRect.bind(C)
function g(n){++S[n];N=Math.random(M=n*613+9)*471}D=document
C.fillStyle='tan';C.font='4em x';C.fillText(S,280,60)

Thanks Shmiddty

| improve this answer | |
  • \$\begingroup\$ I don't understand the objection. As far as I can recall, the 715 byte version (edited 3 hours before this post) is nearly identical to the original version of Pong I had for my Atari 2600. \$\endgroup\$ – primo Mar 13 '13 at 1:50
  • \$\begingroup\$ @primo I'm assuming you're talking about my implied claim that your solution does not meet the "as close as possible" criterion. Well, the OP did not mention the Atari 2600 version, but a specific javascript implementation that he linked to. And at first sight there are 2 glaring differences in your code: no computer player and no "7-segment" score display. There are probably a lot more differences in the "mechanics". If these things are not important, then I think this solution should be valid too. \$\endgroup\$ – aditsu quit because SE is EVIL Mar 13 '13 at 2:09
  • \$\begingroup\$ This is my mistake. I did really mean "as close as possible" so your first solution is the best so far. \$\endgroup\$ – felipa Mar 13 '13 at 8:01
  • \$\begingroup\$ you can remove the c=window.c. You're assigning a global variable to itself. \$\endgroup\$ – Shmiddty Mar 13 '13 at 22:17
  • \$\begingroup\$ You can save 1 more character by moving ++S[n] inside of .random(++S[n]) \$\endgroup\$ – Shmiddty Mar 13 '13 at 22:35

HTML & JavaScript - 1663

Against my better judgment, I took the crazy approach of golfing the actual code from the demo. I removed some features and interface elements, but generally it works exactly the same - 0, 1 or 2 to choose the number of human players, Q/A and P/L to move.

Unless I made some mistakes, gameplay should be identical, pixel for pixel and millisecond for millisecond, to the original at 640*480 (hint: resizing the browser window changes the game size in the demo). It just doesn't give instructions, doesn't announce the winner and doesn't handle esc.

The code is fully self-contained and I tested it in Chromium 25 on Linux. Firefox doesn't like it very much.

<body bgcolor=0><canvas id=c height=480><script>R=Math.random
function T(z,t,u,v){P=0;if(e=v*E-u*F){a=(u*t-v*z)/e;b=(E*t-F*z)/e
function g(n){if(++S[n]>8)G=A.a=B.a=0
setInterval(function(){t=new Date()/1000-l;l+=t;U(A);U(B)

Some credits to Shmiddty for improvements

| improve this answer | |

Processing, 487 characters

int a=320,b=240,c=2,d=2,e=0,f=0,g=0,h=0,i=15,j=80,k=640,
l=160,m;void setup(){size(k,b*2);}void draw(){background

Sample screenshot:

enter image description here

This code was made with shortness in mind, so it is pretty buggy (the ball sometimes goes through the paddle, or wraps around it). Controls are Q/A for Player 1 and O/L for Player 2.

| improve this answer | |
  • 1
    \$\begingroup\$ As a game your Pong version also seems quite unfair, as only one player's keys are registered per frame :P \$\endgroup\$ – Jonathan Frech Jul 8 '18 at 21:09

C# - 1283 characters

This can be golfed quite a bit more but here it is.

using System;using System.Drawing;using System.Runtime.InteropServices;using System.Windows.Forms;using r=System.Drawing.RectangleF;namespace f{public partial class f:Form{public f(){InitializeComponent();}private void f_Load(object sender,EventArgs e){var t=this;var s=new r(0,0,300,300);var q=new r(0,0,15,50);var o=new r(0,0,15,50);var x=new PointF(150,150);var v=.06F;var h=v;var b=new r(x.X,x.Y,15,15);var p1=0;var p2=0;var p=new PictureBox{Size=t.Size,Location=new Point(0,0)};t.Controls.Add(p);p.Paint+=(wh,n)=>{var g=n.Graphics;Action<Brush,r>f=g.FillRectangle;var k=new SolidBrush(Color.Black);var w=new SolidBrush(Color.White);var d=new byte[256];GetKeyboardState(d);var l=q.Location;var _1=.1F;q.Location=new PointF(0,d[90]>1?l.Y+_1:d[81]>1?l.Y-_1:l.Y);l=o.Location;o.Location=new PointF(269,d[77]>1?l.Y+_1:d[79]>1?l.Y-_1:l.Y);f(k,s);f(w,q);f(w,o);Func<r,bool>i=b.IntersectsWith;h=i(q)||i(o)?-h:h;v=b.Top<1||b.Bottom>t.Height-30?-v:v;b.Offset(h,v);if(b.Left<0){p2++;b.Location=x;}if(b.Right>290){p1++;b.Location=x;}f(w,b);for(int j=0;j<19;)f(w,new r(140,(j+(j++%2))*15,10,10));var a=new Font("Arial",20);g.DrawString(p1.ToString(),a,w,100,12);g.DrawString(p2.ToString(),a,w,170,12);p.Invalidate();};}[DllImport("user32.dll")]static extern bool GetKeyboardState(byte[]s);}}

Edit: Didn't see the requirement for a free, linux-runnable language...

| improve this answer | |

Tcl/Tk, 932 bytes

Must be run in the interactive shell

gri [can .c -w 1024 -he 768 -bg #000]
proc A {} {set ::v [expr (int(rand()*36)+1)*20]}
proc R {C t\ X} {.c cr r $C -f #aaa -t $t}
proc I {} {incr ::v 20}
time {R "504 [I] 520 [I]"} 18
R "0 0 1024 20"
R "0 748 1024 768"
R "0 340 20 440" b
R "1004 340 1024 440" B
R "40 [A] 60 [I]" P
lmap n 4\ 5 T F\ S {.c cr t ${n}62 60 -ta $T -te 0 -fi #aaa -font {"" 56}}
proc C t\ n {lindex [.c coo $t] $n}
lmap {a b c d e} {q b 1 >20 -20 a b 3 <740 20 p B 1 >20 -20 l B 3 <740 20} {bind . $a "if \[C $b $c]$d {.c move $b 0 $e}"}
lassign {0 0 20 20} F S y x
proc M {} {lmap {_ a b c d e f} {0 <40 b 20 S 960 980 2 >984 B -20 F 80 100} {if [C P $_]$a {if [C P 1]>=[C $b 1]&&[C P 3]<=[C $b 3] {bell
set ::x $c} {.c itemco $d -te [incr ::$d]
if \$::$d>8 {tk_messageBox -message WINNER!
lmap T F\ S {.c itemco $T -te [set ::$T 0]}}
.c coo P $e [A] $f [I]}}}
.c move P $::x [set ::y [expr [C P 1]<40?20:[C P 3]>727?-20:$::y]]
after 99 M}
focus -f .


 #There is an Enter at the end

Just a very minimal version of Pong, where the ball only runs in diagonal angles and always has same velocity.

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.