Creating Pong With Ruby: Finishing Touches

Where we last left off we had just finished creating a class to represent all of the various game objects and now we’re going to use this new class to create the games pong paddles. Once you’ve created your “Paddle” class you’ll need to define variables for the width and height of the object. Ten pixels by sixty pixels is a fairly accurate representation of the original games paddles but feel free to change the size to whatever you’d like.

class Paddle < GameObject
  WIDTH = 10
  HEIGHT = 60

Next we need to initialize the paddles in a similar fashion to how we initialized our game window in the previous article. You’ll need to use the super “method”, passing in both the x and y variables from the game object class as well as the width and height variables you just created.

def initialize(x, y)
  super(x, y, WIDTH, HEIGHT)
end

After this you’ll need to actually draw in your paddle, we’ll do this by using “Gosu.draw_rect” which is a Gosu method that allows the drawing of rectangles. Now you’re going to want to pass in the x, y, w, and h game object variables into “draw_rect” so it knows where to draw the paddle and how big to make it. Gosu also comes with a series of different variables used to represent colour in your games and you’ll need to pass one of these into the “draw_rect” method. In the original game the paddles were white but if you feel like mixing it up here’s a full list of colours available in Gosu.

def draw
  Gosu.draw_rect x, y, w, h, Gosu::Color::WHITE
end

Lastly we need to give the player control over their paddle. This is surprisingly simple to do, you just need to set the paddle’s y value to equal itself plus/minus a certain number of pixels every time update is called. I’ve personally found that 6 is a good speed but experimenting with different speeds on your own and see what you like.

def move_down
  @y = @y+6
  if @y < 0
    @y = 0
  end
end
def move_up
  @y = @y-6
  if @y < 0
    @y = 0
  end
end

Creating the Ball Class

The start of the ball class is pretty similar to that of the paddles but with its width and height set to 10×10 instead of 10×60 and with an additional variable that’ll be used to change the position of the ball later on.

class Ball < GameObject
  WIDTH = 10
  HEIGHT = 10
  attr_reader :v
def initialize(x, y, v)
  super(x, y, WIDTH, HEIGHT)
  @v = v
end

Now you need to create an update method for the ball that will constantly check and update its position. To do this simply check the ball’s current x and y position and then set them equal to themselves times 2.

def update
  self.x += v[:x]
  self.y += v[:y]
end

This next method is what will actually draw the ball and is in essence just an exact copy of the draw method used for the paddles the only difference being the variables have different values now.

def draw
  Gosu.draw_rect x, y, WIDTH, HEIGHT, Gosu::Color::WHITE
end

Congrats! You’ve finished your pong game and can try it out now. To get the game to display you need to set a variable equal to “GameWindow.new” and then use the “.show” method to display that variable, which contains your game window within it.

window = GameWindow.new
window.show

Further Suggestions

For some extra challenge try creating multiple levels of difficulty for the enemy AI. You could also add in functionality for the player to decide which side they want to play on in the single player. Really though I would say beyond what I just mentioned there isn’t too much to expand upon with this program and I’d encourage you to move on to making more complex games once you feel satisfied with this one.

Good Luck!

Download the source code here.

If you have any questions or comments email me at nick@crumbsofcode.com