Skip to content

Instantly share code, notes, and snippets.

@esad
Created May 23, 2009 01:35
Show Gist options
  • Save esad/116462 to your computer and use it in GitHub Desktop.
Save esad/116462 to your computer and use it in GitHub Desktop.
Sketch for a game of pong in kyoto
require 'kyoto'
use 'actors/velocity'
use 'actors/bound_to_world'
use 'game/pausable'
use 'game/score'
use 'color'
use 'collision_detection'
class Paddle < Actor
acts_as_bound_to_world
width 50.px
height 20.px
def draw
rect(bounds,:color => Color.white)
end
end
class AIPaddle < Paddle
def update
# here be AI
end
end
class Ball < Actor
R = 5.px
acts_as_bound_to_world :reflect => true
def on_collision_with(o)
if o.is_a?(Paddle)
# reflect
elsif o.is_a?(World)
# score
game.restart
end
end
def draw
circle(bounds.midpoint,R)
end
end
class Pong < Game
def setup
@player = Paddle.new(Screen.center,Screen.bottom-Paddle.height)
@ai = AIPaddle.new(Screen.center,Screen.top+Paddle.height)
@ball = Ball.new(Screen.midpoint)
end
def restart
@ball.bounds.midpoint = Screen.midpoint
@ball.velocity.y = [-1,1].rand
@ball.velocity.x = randomly_between(-2..2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment