public
Description: Very-high-level game programming framework, built on Rubygame & Ruby-OpenGL
Homepage: http://blog.rubygame.org/rebirth
Clone URL: git://github.com/jacius/rebirth.git
rebirth / games / bouncy_ball.rb
100644 144 lines (97 sloc) 2.624 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
=begin
 
THE GAME:
 
A ball falls and bounces inside a room. Point and click the ball to
gain a point and send it up into the air. But if it touches the
floor, you lose a point!
 
Get 15 points to win!
 
 
DEMONSTRATES:
 
* GameObject class inheritance.
* Visible shapes (Circle, Line).
* Loose physical shapes which fall and bounce.
* Static physical shapes which don't move.
* The Game class.
* The World class.
* The when_clicked event hook.
* The when_collided event hook.
 
CURRENT STATUS:
 
* Nonfuncitonal (library requirements not implemented.)
 
 
AUTHOR:
 
John Croisant, 2008-05-27 - 2008-05-29
 
=end
 
 
 
# Represents a bouncing ball.
class Ball < GameObject
  # Set up the physical and visual properties of the ball.
  def initialize( params = {} )
    base_params = {
      :center => v(0,0),
      :radius => 15.0,
      :density => 5.0,
      :elast => 0.9,
      :color => :red
    }
    
    add_shape Circle.new( base_params.merge(params) )
  end
end
 
 
class Wall < GameObject
  def initialize( p1, p2 )
    base_params = {
      :color => :white,
      :thickness => 3.0,
      :static => true,
      :elast => 1.0
    }
  
    add_shape Line.new( base_params.merge(:points => [p1,p2]) )
  end
end
 
 
 
def make_game
  game = Game.new
  
  game[:score] = 0
  
  game.when_key_pressed(:q) do
    puts "Bye!"
    exit
  end
  
  game.when_true( proc{ game[:score] >= 15 } ) do
    puts "You got 15 points! You win! \m/"
    exit
  end
  
  return game
end
 
 
def make_view
  return View.new(:size => [240,480],
                  :center => v(0,200),
                  :background => :black)
end
 
 
def make_world
  return World.new( :gravity => v(0,-9.8) )
end
 
 
def make_ball
  return Ball.new
end
 
 
def make_walls
  points = [ v( 100, 0), v( 100, 400), v(-100, 400), v(-100, 0) ]
  # Make a wall between each pair of points.
  return points.enum_cons(2).collect { |points| Wall.new( *points )}
end
 
 
def make_floor
  return Wall.new( v(-100, 5), v(100, 5) )
end
 
 
 
def main
  $game = make_game
  $game.view = make_view
 
  $game[:walls] = make_walls
  $game[:floor] = make_floor
  $game[:ball] = make_ball
  
  $game.world = make_world
  $game.world.add( $game[:floor], $game[:ball], $game[:walls] )
 
  $game[:ball].when_clicked do |click|
    $game[:score] += 1
    $game[:ball].shove( :from => click.pos, :strength => 30.0 )
  end
  
  $game[:floor].when_collided( :with => $game[:ball] ) do
    $game[:score] -= 1 unless $game[:score] - 1 < 0
  end
  
  $game.after_update do
    $game.title = "Bounce the ball! (%d pts)"%$game[:score]
end
$game.go
end