/pong

Pong assignment from CS50

Primary LanguageLuaOtherNOASSERTION

Pong Clone

Good ol' Pong. Thanks Atari!

The original code in this project is from Project 0 from HarvardX's CS50 Introduction to Game Development. The course includes an assignment to implement an "AI player". The code uses the LÖVE framework.

Goal

Writing a version where different computer players (I think calling them "AI" is setting the AI bar way too low) can play against each other.

Usage

  • Download and install LÖVE (Windows, Mac, or Linux)
  • From the source directory, run love .
  • To compare computer player classes, the Player implementation must be in the competitors/ sub-directory. Then pass the name of the .lua source file(s)

Examples:

love .                    # Play with default Player
love . PlayerV2           # Play default against "v2" implementation
love . PlayerV1 PlayerV2  # Play "v1" vs "v2" (no humans)

Changes to CS50

The Pong implementation from the CS50 course allowed a ball to "pass through" a paddle once the ball exceeded a certain speed. The collision detector never had a chance to kick in, because the ball went from "just in front of" to "just behind" the paddle in a single step. I "fixed" that by "clipping" the ball to the x coordinate of a paddle if it passed through too quickly.

As a consequence stalemates occured when my computer player played itself. Very boring.

The problem was that the ball's velocity was dominated by the dx movement. The horizontal movement was increased 3% after each hit, however the vertical movement was a random value between -50 and 50. As the ball accelerated at each "hit" of the paddle it eventually moved very fast back and forth with relatively little vertical movement. In the end the paddles hardly needed to move at all.

Prior to my changes to the collision detector there would be no stalemate because one side would "win" simply because the ball was moving too fast for any player to intercept. This seemed a bit unfair and random so I wanted to end stalemates in a way that players felt they could control (either manually or by coding a better computer player).

The original game of Pong allowed the player some control over the angle of deflection when the ball bounced off the paddle. If the ball hit the paddle's edge, it would bounce off at an acute angle. If it hit the middle of the paddle, it bounced off at a right angle. This behaviour could be useful.

Current Status

  • Fixed the Ball class so that vertical movement is retained, even at high speeds
  • Ball deflection angle is no longer random, but is determined by where it hits the paddle. More acute angles are achieved by hitting the ball towards an edge of the paddle
  • You can pass different Player class implementations to the game from the command line. The game will print the winner on stdout when done

Next:

  • Can we implement a "cheating" Player class?

Later:

  • Use Lua's method of making member variables "private" (see Programming in Lua, s16.4) to protect Paddle instances from direct manipulation by Players

Rules (Mechanics)

Implemented:

  • Like the original, the paddle is divided into 8 segments, and the angle that the ball is deflected off the paddle is determined by which segment it hits
  • Vertical movement of the ball is now scaled with horizontal movement. It's now possible for a ball to move so fast a paddle can't intercept it (paddles are limited by a max PADDLE_SPEED), which means stalemates no longer happen

TODO:

  • The original also had a limitation that made the very top and very bottom of the screen inaccessible to paddles. This could be exploited by skilled players to score points - otherwise the game could go on too long
  • Hitting the ball with a paddle that is moving could impart some momentum to the ball, consistent with the direction the paddle was moving at time of collision
  • Hitting the ball with a paddle that is still could slightly decrease the velocity of the ball

Sources and Credits