Skip to content

Instantly share code, notes, and snippets.

@zspencer
Created February 4, 2012 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zspencer/1741184 to your computer and use it in GitHub Desktop.
Save zspencer/1741184 to your computer and use it in GitHub Desktop.
Possible implementation of Pong with an imaginary API I need to just build
Shoes.app width: 800, height: 600 do
tick 60 do
stop_ticking if @right_player.won? or @left_player.won?
alert "Right Player Wins!" if @right_player.won?
alert "Left Player Wins!" if @left_player.won?
end
moving_element :ball do
location top: 400, left: 300
velocity horizontal: 50, vertical: 50
shape :circle, radius: 50
on_tick do
move next_position
end
on_collision do |collided_element|
velocity horizontal: :reverse
end
on_exiting_canvas do |side|
if side == :top or side == :bottom
velocity vertical: :reverse
else
move starting_position
score_point side
end
end
end.draw
element :paddle do
shape :rectangle, height: 100, width: 25
end
player :pong_player do
attr_accessor :paddle
winning_score 10
speed 10
on_join do |options|
score 0
self.paddle = options[:paddle]
end
on_up { paddle.move_relative(top: speed*-1) }
on_down { paddle.move_relative(top: speed) }
end
@left_paddle = element(:paddle) { location top: 400, left: 0 }.draw
@left_player = player(:pong_player).join(paddle: @left_paddle, up_key: "k", down_key: "j")
@right_paddle = element(:paddle) { location top: 400, left: 800 }.draw
@right_player = player(:pong_player).join(paddle: @right_paddle, up_key: "d", down_key: "s")
def score_point side
@right_player.score += 1 if side == :left
@left_player.score += 1 if side == :right
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment