ruby_game 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Binary file
Binary file
File without changes
Binary file
Binary file
Binary file
Binary file
data/bin/ruby_game CHANGED
@@ -1,19 +1,39 @@
1
1
  #!/usr/bin/env ruby
2
-
3
2
  require File.expand_path('../lib/ruby_game', File.dirname(__FILE__))
4
- =begin
5
- RubyGame::MonsterFactory.define :ghost do
6
- image 'Ghost'
7
- velocity 4
8
- actions do
9
- 15.times {move_up}
10
- 15.times {move_down}
11
- end
3
+
4
+ RubyGame::Monster.define :ghost1 do
5
+ image_name "ghost1"
6
+ velocity 3
7
+ action :move_up, velocity: 2, repeat: 80
8
+ action :move_down, velocity: 2, repeat: 80
12
9
  end
13
- =end
10
+
11
+ RubyGame::Monster.define :ghost2 do
12
+ image_name "ghost2"
13
+ action :move_left, velocity: 1, repeat: 160
14
+ action :move_right, velocity: 4, repeat: 40
15
+ end
16
+
17
+ RubyGame::Monster.define :ghost3 do
18
+ image_name "ghost3"
19
+ velocity 1
20
+ action :move_up, repeat: 80
21
+ action :move_left, repeat: 80
22
+ action :move_down, repeat: 80
23
+ action :move_right, repeat: 80
24
+ end
25
+
26
+ RubyGame::Monster.define :dark_knight do
27
+ image_name "dark_knight"
28
+ action :forward
29
+ end
30
+
14
31
  game = RubyGame::Game.new(640, 480)
15
- game.start! do |g|
16
- g.player RubyGame::Player.new(590, 420)
17
- g.ruby RubyGame::Ruby.new(65, 115)
18
- g.monster RubyGame::Monster.new(520, 300)# RubyGame::MonsterFactory.build(:ghost, 3)
32
+ game.start! do
33
+ player RubyGame::Player.new(590, 420)
34
+ ruby RubyGame::Ruby.new(65, 115)
35
+ monsters RubyGame::Monster.build(:ghost1, 2)
36
+ monsters RubyGame::Monster.build(:ghost2, 2)
37
+ monsters RubyGame::Monster.build(:ghost3)
38
+ monsters RubyGame::Monster.build(:dark_knight)
19
39
  end
data/lib/ruby_game.rb CHANGED
@@ -1,13 +1,12 @@
1
- require_relative 'ruby_game/static_object'
2
- require_relative 'ruby_game/moving_object'
3
- require_relative 'ruby_game/player'
4
- require_relative 'ruby_game/ruby'
5
- require_relative 'ruby_game/monster'
6
- #require_relative 'ruby_game/monster_factory'
7
- require_relative 'ruby_game/game'
8
- require_relative 'ruby_game/version'
1
+ require_relative "ruby_game/static_object"
2
+ require_relative "ruby_game/moving_object"
3
+ require_relative "ruby_game/ruby"
4
+ require_relative "ruby_game/player"
5
+ require_relative "ruby_game/monster"
6
+ require_relative "ruby_game/game"
7
+ require_relative "ruby_game/version"
9
8
 
10
9
  module RubyGame
11
10
  ROOT_PATH = File.expand_path('../', File.dirname(__FILE__))
12
11
  IMAGES_PATH = File.join(ROOT_PATH, '/app/assets/images')
13
- end
12
+ end
@@ -16,16 +16,20 @@ module RubyGame
16
16
  @player.move_up if button_down? Gosu::Button::KbUp
17
17
  @player.move_down if button_down? Gosu::Button::KbDown
18
18
 
19
- self.win! if @player.touch?(@ruby)
20
- #self.gameover! if @monster.touch?(@player)
19
+ @monsters.each do |monster|
20
+ monster.execute(@player)
21
+ self.gameover! if monster.touch?(@player)
22
+ end
23
+
24
+ self.won! if @player.touch?(@ruby)
21
25
  end
22
26
  end
23
27
 
24
28
  def draw
25
29
  @background_image.draw(0, 0, 0)
26
- @font.draw("Victoire !", 210, 240, 2, 1.0, 1.0, 0xffffff00) if self.won?
30
+ @font.draw("You won!", 200, 240, 2, 1.0, 1.0, 0xffffff00) if self.won?
27
31
  @font.draw("Game Over", 175, 240, 2, 1.0, 1.0, 0xffffff00) if self.gameover?
28
- @objects.each {|object| object.draw}
32
+ ([@ruby, @player] + @monsters).each {|object| object.draw}
29
33
  end
30
34
 
31
35
  def button_down(id)
@@ -33,26 +37,28 @@ module RubyGame
33
37
  self.restart! if id == Gosu::Button::KbR
34
38
  end
35
39
 
40
+ def monsters(monsters)
41
+ @monsters += monsters
42
+ end
36
43
 
37
- %w(player ruby monster).each do |object|
44
+ %w(player ruby).each do |object|
38
45
  define_method object do |value|
39
46
  instance_variable_set("@#{object}", value)
40
- @objects << value
41
47
  end
42
48
  end
43
49
 
44
50
  def start!(&block)
45
- @objects = []
46
- @init = block if block
47
- @init.call(self)
48
- @objects.each {|object| object.init_image(self)}
49
- [@player, @monster].compact.each {|moving_object| moving_object.init_limits(width, height, 15, 40)}
51
+ @monsters = []
52
+ @init = block if block_given?
53
+ self.instance_eval(&@init)
54
+ ([@ruby, @player] + @monsters).each {|object| object.init_image(self)}
55
+ ([@player] + @monsters).each {|moving_object| moving_object.init_limits(width, height, 15, 40)}
50
56
  @state = :run
51
- self.show if block
57
+ self.show if block_given?
52
58
  end
53
59
  alias_method :restart!, :start!
54
60
 
55
- def win!
61
+ def won!
56
62
  @state = :won
57
63
  end
58
64
 
@@ -1,8 +1,62 @@
1
1
  module RubyGame
2
2
  class Monster < MovingObject
3
- def initialize(x, y, image_name = 'ghost')
3
+
4
+ @@monsters = {}
5
+ Action = Struct.new(:direction, :velocity)
6
+
7
+ def initialize(x = 0, y = 0, image_name = 'ghost')
4
8
  super
5
9
  @velocity = 3
10
+ @actions = []
11
+ @action_index = 0
12
+ end
13
+
14
+ def self.build(name, number = 1)
15
+ Array.new(number) {@@monsters[name].clone}
16
+ end
17
+
18
+ def init_limits(max_width, max_height, border_with, border_top_with)
19
+ super
20
+ @x = rand border_top_with..(max_width - 2*border_top_with)
21
+ @y = rand border_top_with..(max_height - 2*border_top_with)
22
+ end
23
+
24
+ def forward(player)
25
+ @x += player.x <=> @x
26
+ @y += player.y <=> @y
27
+ end
28
+
29
+ def self.define(name, &block)
30
+ monster = Monster.new
31
+ monster.instance_eval(&block)
32
+ @@monsters[name] = monster
33
+ end
34
+
35
+ def image_name(image_name)
36
+ @image_name = image_name
37
+ end
38
+
39
+ def velocity(velocity)
40
+ @velocity = velocity
41
+ end
42
+
43
+ def action(direction, options = {})
44
+ opts = {velocity: @velocity, repeat: 1}.merge options
45
+ @actions += Array.new(opts[:repeat]) {Action.new(direction, opts[:velocity])}
46
+ end
47
+
48
+ def execute(player)
49
+ next_index = @action_index % @actions.length
50
+ action = @actions[next_index]
51
+ default_velocity = @velocity
52
+ @velocity = action.velocity
53
+ if action.direction == :forward
54
+ self.send(action.direction, player)
55
+ else
56
+ self.send(action.direction)
57
+ end
58
+ @velocity = default_velocity
59
+ @action_index = next_index + 1
6
60
  end
7
61
  end
8
62
  end
@@ -1,19 +1,19 @@
1
1
  module RubyGame
2
2
  class MovingObject < StaticObject
3
- def initialize(x, y, image_name = 'player')
3
+ def initialize(x, y, image_name)
4
4
  super
5
5
  @velocity = 2
6
6
  end
7
7
 
8
- def touch?(object)
9
- Math.hypot(object.x - @x, object.y - @y) <= object.width/2
10
- end
11
-
12
8
  def init_limits(max_width, max_height, border_with, border_top_with)
13
- @max_width, @max_height, @border_with = max_width, max_height
9
+ @max_width, @max_height = max_width, max_height
14
10
  @border_with, @border_top_with = border_with, border_top_with
15
11
  end
16
12
 
13
+ def touch?(object)
14
+ Math.hypot(object.x - @x, object.y - @y) <= object.width/2
15
+ end
16
+
17
17
  def move_left
18
18
  @x -= @velocity if @x > @border_with + (@image.width/2)
19
19
  end
@@ -1,5 +1,4 @@
1
1
  require 'forwardable'
2
-
3
2
  module RubyGame
4
3
  class StaticObject
5
4
  extend Forwardable
@@ -1,3 +1,3 @@
1
1
  module RubyGame
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/ruby_game.gemspec CHANGED
@@ -3,7 +3,7 @@ require File.expand_path('../lib/ruby_game/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Matthieu Segret"]
6
- gem.email = ["matthieu.segret@gmail.com"]
6
+ gem.email = ["matthieu@humancoders.com"]
7
7
  gem.description = %q{RubyGame est un jeu pour apprendre à développer avec Ruby}
8
8
  gem.summary = %q{RubyGame est un jeu pour apprendre à développer avec Ruby}
9
9
  gem.homepage = "http://formations.humancoders.com/formations/ruby"
@@ -14,7 +14,4 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "ruby_game"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = RubyGame::VERSION
17
- gem.platform = Gem::Platform::RUBY
18
-
19
- gem.add_dependency "gosu"
20
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_game
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-13 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: gosu
16
- requirement: &70260638433380 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *70260638433380
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
+ dependencies: []
25
14
  description: RubyGame est un jeu pour apprendre à développer avec Ruby
26
15
  email:
27
- - matthieu.segret@gmail.com
16
+ - matthieu@humancoders.com
28
17
  executables:
29
18
  - ruby_game
30
19
  extensions: []
@@ -36,9 +25,15 @@ files:
36
25
  - README.md
37
26
  - Rakefile
38
27
  - app/assets/images/background.png
39
- - app/assets/images/ghost.png
28
+ - app/assets/images/cactuar.png
29
+ - app/assets/images/dark_knight.png
30
+ - app/assets/images/ghost1.png
31
+ - app/assets/images/ghost2.png
32
+ - app/assets/images/ghost3.png
33
+ - app/assets/images/hornet.png
40
34
  - app/assets/images/player.png
41
35
  - app/assets/images/ruby.png
36
+ - app/assets/images/undead.png
42
37
  - bin/ruby_game
43
38
  - lib/ruby_game.rb
44
39
  - lib/ruby_game/game.rb