Skip to content

Instantly share code, notes, and snippets.

@silasmontgomery
Created January 23, 2012 03:25
Show Gist options
  • Save silasmontgomery/1660278 to your computer and use it in GitHub Desktop.
Save silasmontgomery/1660278 to your computer and use it in GitHub Desktop.
Pong Clone - Ruby, Shoes, Hackety Hack!
# Pong Forever! (Pong Clone)
# Let the critiquing begin!
# Set element attributes
screen_width = 640
screen_height = 480
paddle_height = 80
paddle_width = 10
ball_diameter = 20
ball_speed = 2
paddle_speed = 2
# Create canvas and make resizable
Shoes.app :width => screen_width, :height => screen_height, :resizable => true do
background black
# Center ball and paddles vertically
ball_center = (self.height / 2) - (ball_diameter / 2)
paddle_center = (self.height / 2) - (paddle_height / 2)
# Create ball and paddles
@ball = oval(0,ball_center,20, :fill => white)
@left_paddle = rect(0,paddle_center,paddle_width,paddle_height, :fill => green)
@right_paddle = rect(self.width-paddle_width,paddle_center,paddle_width, paddle_height, :fill => green)
# Start with paddles in stopped position
@moving = 0
# Set x and y ball speed
y_dir = x_dir = ball_speed
# Animate at 200 fps
@anim = animate 200 do |i|
# Set top and bottom boundaries, reverse y direction on hit
if @ball.top >= (self.height - @ball.height) or @ball.top < 0
y_dir *= -1
end
# If ball hits left or right boundary, stop animation remove ball and paddles
if @ball.left >= (self.width - @ball.width) or @ball.left < 0
@anim.toggle
@ball.remove
@left_paddle.remove
@right_paddle.remove
# Ask to play again. If yes, recreate ball and paddles and restart animation. If no, close.
if confirm("Play again?")
@ball = oval(0,ball_height,20, :fill => white)
@left_paddle = rect(0,screen_height / 2,paddle_width,paddle_height, :fill => green)
@right_paddle = rect(self.width-paddle_width,screen_height / 2, paddle_width,paddle_height, :fill => green)
@moving = 0
@anim.toggle
else
close()
end
end
# If ball hits left or right paddle within vertical bounaries, reverse x direction. (this one is ugly)
if @ball.left <= (@left_paddle.left + @left_paddle.width) && x_dir < 0 &&
@ball.top + @ball.height > @left_paddle.top &&
@ball.top < @left_paddle.top + @left_paddle.height ||
@ball.left + @ball.width >= @right_paddle.left &&
@ball.top + @ball.height > @right_paddle.top &&
@ball.top < @right_paddle.top + @right_paddle.height
x_dir *= -1
end
# Move ball and paddles
@ball.move(@ball.left + x_dir, @ball.top + y_dir)
@left_paddle.move @left_paddle.left, @left_paddle.top + @moving
@right_paddle.move @right_paddle.left, @right_paddle.top + @moving
# Stop paddles if they reach the top or bottom boundary
if @left_paddle.top <= 0 || @left_paddle.top + @left_paddle.height >= self.height
@moving = 0
end
end
# Set paddle direction based on up or down key
keypress do |key|
if key == :up
@moving = paddle_speed * -1
elsif key == :down
@moving = paddle_speed
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment