rubydraw 0.2.0 → 0.2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,147 @@
1
+ require 'rubygems'
2
+ require 'rubydraw'
3
+
4
+ class GameWindow < Rubydraw::Window
5
+ def initialize
6
+ super(500, 500, Rubydraw::Color::White)
7
+ @score = 0
8
+ @score_text = Rubydraw::Text.new("", Rubydraw::Color::Black)
9
+ @game_objects = []
10
+ # Add a new bucket.
11
+ @game_objects << Bucket.new(self)
12
+ # Spawn a ball.
13
+ spawn_ball
14
+ whenever Rubydraw::Events::QuitRequest do
15
+ close
16
+ end
17
+ end
18
+
19
+ # Delete any balls that are at the bottom of the screen, then
20
+ # tick all the game objects.
21
+ def tick
22
+ @game_objects.each {|obj|
23
+ if obj.is_a?(Ball) and obj.at_bottom?
24
+ @game_objects.delete(obj)
25
+ end}
26
+ @score_text.contents = @score.to_s
27
+ @score_text.draw(self, Point[0, 0])
28
+ if @game_objects.size == 1
29
+ spawn_ball
30
+ end
31
+ @game_objects.each {|obj| obj.tick}
32
+ end
33
+
34
+ # Spawn a new ball, then let it fall!
35
+ def spawn_ball
36
+ @game_objects << Ball.new(self, Point[rand(@width), -50])
37
+ end
38
+
39
+ def bucket
40
+ @game_objects[0]
41
+ end
42
+
43
+ # This is called when the user gets a ball into the bucket.
44
+ def scored_with(ball)
45
+ delete(ball)
46
+ spawn_ball
47
+ @score += 1
48
+ end
49
+
50
+ # Remove the given object from @game_objects.
51
+ def delete(obj)
52
+ @game_objects.delete(obj)
53
+ end
54
+ end
55
+
56
+ class Bucket
57
+ def initialize(window)
58
+ whenever Rubydraw::Events::KeyPressed, window do |event|
59
+ case event.key
60
+ when Rubydraw::Key::RightArrow
61
+ @x += 5
62
+ when Rubydraw::Key::LeftArrow
63
+ @x -= 5
64
+ end
65
+ end
66
+ whenever Rubydraw::Events::MousePressed, window do
67
+ puts ball_entry_zone.to_s
68
+ end
69
+ @window = window
70
+ @image = Rubydraw::Image.new("media/bucket.png")
71
+ @x = 0
72
+ end
73
+
74
+ def height
75
+ @image.height
76
+ end
77
+
78
+ def width
79
+ @image.width
80
+ end
81
+
82
+ def tick
83
+ @position = Point[@x, @window.height - height]
84
+ @image.draw(@window, @position)
85
+ end
86
+
87
+ # Where balls can enter the bucket.
88
+ def ball_entry_zone
89
+ Rectangle[@position, Point[width, 20]]
90
+ end
91
+ end
92
+
93
+ class Ball
94
+ def initialize(window, positon)
95
+ @window, @position, = window, positon
96
+ @x, @y = @position.x, @position.y
97
+ @image = Rubydraw::Image.new("media/ball.png")
98
+ end
99
+
100
+ def tick
101
+ @y += 5
102
+ @position = Point[@x, @y]
103
+ @image.draw(@window, @position)
104
+ if touching_bucket?
105
+ @window.scored_with(self)
106
+ end
107
+ end
108
+
109
+ def width
110
+ @image.width
111
+ end
112
+
113
+ def height
114
+ @image.height
115
+ end
116
+
117
+ def at_bottom?
118
+ @y >= @window.height
119
+ end
120
+
121
+ # Returns a point representing the bottom left corner.
122
+ def bottom_left
123
+ Point[@x, @y + height]
124
+ end
125
+
126
+ # Returns a point at the bottom right corner.
127
+ def bottom_right
128
+ Point[@x + width, @y + height]
129
+ end
130
+
131
+ # Returns true if the bottom of this ball is touching the
132
+ # bucket.
133
+ def touching_bucket?
134
+ offset = Point[0, 20]
135
+ [bottom_left - offset, bottom_right - offset].each {|p|
136
+ if p.inside?(bucket.ball_entry_zone)
137
+ return true
138
+ end}
139
+ false
140
+ end
141
+
142
+ def bucket
143
+ @window.bucket
144
+ end
145
+ end
146
+
147
+ GameWindow.new.show
Binary file
Binary file
@@ -81,10 +81,6 @@ module Rubydraw
81
81
  attr_reader(:key)
82
82
 
83
83
  def initialize(key)
84
- unless key.is_a?(Numeric)
85
- raise SDLError "Failed to create new event because key is not a number."
86
- end
87
-
88
84
  @key = key
89
85
  end
90
86
  end
@@ -25,9 +25,6 @@ module Rubydraw
25
25
  # Notice that you don't blit surfaces to other surfaces when using
26
26
  # Rubygame, but instead you draw things.
27
27
  def draw(window, position)
28
- unless position.respond_to?(:x) and position.respond_to?(:y)
29
- raise ArgumentError, "Expected a Point, got #{position}"
30
- end
31
28
  source_rect = Rectangle[Point[0, 0], Point[width, height]]
32
29
  blit_rect = Rectangle[position, Point[window.width, window.height]]
33
30
  SDL::BlitSurface(@sdl_image, source_rect.to_sdl, window.sdl_surface, blit_rect.to_sdl)
@@ -13,12 +13,6 @@ module Rubydraw
13
13
 
14
14
  # Create a new point with the given +x+ and +y+ positions.
15
15
  def initialize(x, y)
16
- unless x.respond_to?(:+)
17
- "Expected an integer for x, got #{x}"
18
- end
19
- unless y.respond_to?(:+)
20
- "Expected an integer for y, got #{y}"
21
- end
22
16
  @x, @y = x, y
23
17
  end
24
18
 
@@ -11,15 +11,7 @@ module Rubydraw
11
11
  end
12
12
 
13
13
  # Create a new rectangle with the given dimensions and position.
14
- def initialize(position, dimensions)
15
- #raise ArgumentError, "Rectangle dimensions must be a Point" unless dimensions.is_a?(Rubydraw::Point)
16
- #raise ArgumentError, "Rectangle position must be a Point" unless position.is_a?(Rubydraw::Point)
17
- unless position.respond_to?(:x) and position.respond_to?(:y)
18
- raise ArgumentError, "Expected a Point, got #{position}"
19
- end
20
- unless dimensions.respond_to?(:x) and dimensions.respond_to?(:y)
21
- raise ArgumentError, "Expected a Point, got #{position}"
22
- end
14
+ def initialize(position, dimensions)``
23
15
  @position, @dimensions = position, dimensions
24
16
  end
25
17
 
@@ -8,12 +8,7 @@ module Rubydraw
8
8
  attr_reader(:width, :height)
9
9
 
10
10
  # Create a new window.
11
- def initialize(width, height, bkg_color=Color::Black)
12
- unless height.is_a?(Numeric) and width.is_a?(Numeric)
13
- # Raise an error
14
- raise SDLError "Window width and height have to be a number."
15
- end
16
-
11
+ def initialize(width, height, bkg_color=Color::Black)\
17
12
  @width = width
18
13
  @height = height
19
14
  @open = false
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydraw
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 89
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
9
  - 0
10
- version: 0.2.0
10
+ - 3
11
+ version: 0.2.0.3
11
12
  platform: ruby
12
13
  authors:
13
14
  - J. Wostenberg
@@ -64,9 +65,10 @@ files:
64
65
  - examples/window_ex.rb
65
66
  - examples/image_ex.rb
66
67
  - examples/sound_ex.rb
68
+ - examples/ball_catch_game.rb
67
69
  - examples/media/bug.png
68
- - examples/media/hooray.ogg
69
- - examples/media/noise.ogg
70
+ - examples/media/ball.png
71
+ - examples/media/bucket.png
70
72
  - lib/ext/string.rb
71
73
  - lib/ext/object.rb
72
74
  - lib/ext/aliases.rb
Binary file
Binary file