Shortest Possible Pong Program in C++

Started by
6 comments, last by Zaoshi Kaba 9 years, 10 months ago

Hi everyone, me and my friend once again had a competition involving the classic Pong. Instead of a severe time restriction, we had to use the fewest lines of code possible with a max line length 80 characters.

This is possibly the biggest little mess I have ever coded, enjoy the GOTO


#include <SFML/Graphics.hpp>
#include <windows.h>
int scoreReg[4] = {0,0,0,0}; //score 1, score 2,
void main() {
  sf::RenderWindow window(sf::VideoMode(700, 700), "Small Pong Program");
  sf::Font font; //Set font
  font.loadFromFile("arial.ttf");
  sf::Text text; //Set text
  text.setFont(font);
  text.setColor(sf::Color::Green);
  text.setCharacterSize(24);
  text.setPosition(10,10);
  sf::CircleShape ballShape(5.0f,12); //Ball shape
  sf::RectangleShape paddleShape;     //Paddle shape
  sf::Vector2f size(100,15);  //BEGIN GOTO LABEL BELOW
  //Top paddle, bottom paddle, ball, ball vel, reg a, reg b
  begin: float positions[8] = {310,50,310,635,345,342.5,0.5,0.5};
  if(scoreReg[0] == 5){text.setString("Top player wins!"); //Top Player wins
    goto end;}
  if(scoreReg[1] == 5){text.setString("Bottom player wins!"); //Bottom wins
    goto end;}
  paddleShape.setSize(size);
  ballShape.setPosition(positions[4], positions[5]);
  ballShape.setFillColor(sf::Color::Red);
  while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) //Event loop for window close
        if (event.type == sf::Event::Closed) 
          window.close();
    //Top player controls
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) positions[0] -= 1;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))positions[0] += 1;
    //Bottom player controls
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))positions[2] -= 1;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))positions[2] += 1;
    if(positions[4] <=0 || positions[4] >=695) //Ball collision with sides
      positions[6] = positions[6]*-1;          //Flip ball x vel
    if(positions[5] < 5){ //Yes kill me goto
      scoreReg[1] += 1;
      goto begin;} //New ball and add 1 point to bottom player
    else if (positions[5] > 695){ //Yes kill me goto
      scoreReg[0] += 1;
      goto begin;} //New ball and add 1 point to top player
    positions[4] += positions[6]; //Update ball pos x
    positions[5] += positions[7]; //Update ball pos y
    ballShape.setPosition(positions[4], positions[5]);  //Set ball pos
    for(int i=0; i<3; i+=2){ //Brilliant idea offset using for loop i
      if(positions[4]+5 < positions[i]) continue; //x left
      else if (positions[4] >positions[i] + size.x) continue; //x right
      if(positions[5]+5 < positions[i+1]) continue; //x left
      else if (positions[5] >positions[i+1] + size.y) continue; //x right
      positions[7] = positions[7]*-1;}//Collision occured reverse ball
    std::string score = std::to_string(scoreReg[0]) + " : " +
    std::to_string(scoreReg[1]);
    text.setString(score);
    paddleShape.setPosition(positions[0], positions[1]);
    window.draw(paddleShape); //Draw top paddle
    paddleShape.setPosition(positions[2], positions[3]);
    window.draw(paddleShape); //Draw bottom paddle
    window.draw(ballShape);
    window.draw(text);
    window.display();
    window.clear(); // Clear and then draw everything
    Sleep(2);}
  end:;  //GOTO END LABEL    
  window.draw(text);
  window.display();
  Sleep(5000);}
Advertisement

You could probably shorten that further with the ? ternary operator. (And technically the whole code could all be one line, there is no rule in C++ that you can't do thingone;thingtwo;thingthree; But I'm assuming some sort of contest rule was involved)

Hi, I plan to just haven't got around to it yet. I also need to add the ball changing velocity when it hits a paddle, writing this felt so wrong.

I'm sure you could do it in about 10-15 lines of heavily obfuscated code.

The title reminded me of an article that was on the site a decade ago. I don't think it survived the transition to the new site, but the Internet Archive found the old article, a DirectX Tetris clone in 30 lines..

It used to be very easy to find, it was article #1 in the articles collection, titled "HO 2 MAEK TERTIS in 3213 sampel steps1!" If you look at the source, it is both enlightening and discouraging to what is actually needed for a simple game.

Was using SFML a requirement? If not, write your own library that has all the required functionality precompiled, and your program will only be a couple of lines. ;)

"Shortest programs" don't count if you use full frameworks to accomplish them. I would say OP's program doesn't count because he imported some library that did a lot.

If he did this correctly, just making a window is like 1000 lines of code.


If he did this correctly, just making a window is like 1000 lines of code.

A little bit of an exageration but I get your point.

I'm sure I read an article somewhere about a regular competition to fit programs on the back of buisness cards but can't remember. I think one of them was a complete version of breakout.

Multiplayer pong in 2600 symbols: http://www.ioccc.org/2001/kev.c

This topic is closed to new replies.

Advertisement