space_invaders 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +38 -0
  6. data/Rakefile +1 -0
  7. data/assets/fonts/unifont.ttf +0 -0
  8. data/assets/images/Bullet.png +0 -0
  9. data/assets/images/FullBlock.png +0 -0
  10. data/assets/images/InvaderA1.png +0 -0
  11. data/assets/images/InvaderA2.png +0 -0
  12. data/assets/images/InvaderB1.png +0 -0
  13. data/assets/images/InvaderB2.png +0 -0
  14. data/assets/images/InvaderC1.png +0 -0
  15. data/assets/images/InvaderC2.png +0 -0
  16. data/assets/images/OkBlock.png +0 -0
  17. data/assets/images/RedInvader.png +0 -0
  18. data/assets/images/Ship.png +0 -0
  19. data/assets/images/ShipCrushedLeft.png +0 -0
  20. data/assets/images/ShipCrushedRight.png +0 -0
  21. data/assets/images/WeakBlock.png +0 -0
  22. data/assets/sounds/InvaderBullet.wav +0 -0
  23. data/assets/sounds/InvaderHit.wav +0 -0
  24. data/assets/sounds/ShipBullet.wav +0 -0
  25. data/assets/sounds/ShipHit.wav +0 -0
  26. data/bin/space-invaders +9 -0
  27. data/go.rb +5 -0
  28. data/lib/space_invaders.rb +4 -0
  29. data/lib/space_invaders/app.rb +99 -0
  30. data/lib/space_invaders/base.rb +14 -0
  31. data/lib/space_invaders/behaviors/centerable.rb +13 -0
  32. data/lib/space_invaders/behaviors/collideable.rb +18 -0
  33. data/lib/space_invaders/behaviors/fireable.rb +35 -0
  34. data/lib/space_invaders/behaviors/mesurable.rb +17 -0
  35. data/lib/space_invaders/blocks/block.rb +62 -0
  36. data/lib/space_invaders/blocks/u_block.rb +37 -0
  37. data/lib/space_invaders/blocks/u_block_container.rb +46 -0
  38. data/lib/space_invaders/bullets/bullet.rb +46 -0
  39. data/lib/space_invaders/bullets/bullet_collection.rb +23 -0
  40. data/lib/space_invaders/invaders/invader.rb +38 -0
  41. data/lib/space_invaders/invaders/invader_abc.rb +27 -0
  42. data/lib/space_invaders/invaders/invader_row.rb +52 -0
  43. data/lib/space_invaders/invaders/invaders_container.rb +146 -0
  44. data/lib/space_invaders/invaders/red_invader.rb +66 -0
  45. data/lib/space_invaders/screens/game_over_screen.rb +42 -0
  46. data/lib/space_invaders/screens/next_level_screen.rb +39 -0
  47. data/lib/space_invaders/screens/welcome_screen.rb +46 -0
  48. data/lib/space_invaders/ship/drowned_ship_animator.rb +46 -0
  49. data/lib/space_invaders/ship/ship.rb +99 -0
  50. data/lib/space_invaders/statics/button_controller.rb +33 -0
  51. data/lib/space_invaders/statics/game_status.rb +52 -0
  52. data/lib/space_invaders/statics/images.rb +39 -0
  53. data/lib/space_invaders/statics/sounds.rb +37 -0
  54. data/lib/space_invaders/trackers/lives_tracker.rb +27 -0
  55. data/lib/space_invaders/trackers/score_tracker.rb +27 -0
  56. data/lib/space_invaders/utils.rb +19 -0
  57. data/space_invaders.gemspec +23 -0
  58. metadata +143 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef1994e571b0ba658a9cf60b3dae336c4b0339ee
4
+ data.tar.gz: d62eee014f87581690779525441e4b483f9e693c
5
+ SHA512:
6
+ metadata.gz: 485ada3b384472ab8ce9bba409362b21698f9e1d95d8c0d3e9bbf00f1b0d2390ec3bd5c69c239a20312c02e6bdef443f46fab9c40045168421fdc2d97cb83d7f
7
+ data.tar.gz: 4622246205ed25368e63f4a92fdd43f7093b5e446655fa8b1d01842753eb4d4837e3c990f6a9c71e7d5bfe8e63ae77bd55974950273eea517010ee3f4b9e440e
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in space_invaders.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Damir Svrtan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # Space Invaders
2
+
3
+ Classic arcade game Space Invaders written in Ruby w/ [Gosu](https://github.com/jlnr/gosu).
4
+
5
+ ![Screenshot](http://i.imgur.com/Xfewqhn.png)
6
+ ## Getting Started
7
+
8
+ The game is packed as a gem, so you can simply run:
9
+
10
+ $ gem install space_invaders
11
+ $ space-invaders
12
+
13
+ Or you can clone the repo and simply run the main file:
14
+
15
+ $ git clone git@github.com:DamirSvrtan/space-invaders.rb.git
16
+ $ ruby go.rb
17
+
18
+ ## Troubles during installation:
19
+
20
+ If you have problems installing the game, you're probably missing some OS specific libraries that Gosu uses. For further information checkout:
21
+
22
+ * [Getting started with Gosu on Linux](https://github.com/jlnr/gosu/wiki/Getting-Started-on-Linux)
23
+ * [Getting started with Gosu on OS-X](https://github.com/jlnr/gosu/wiki/Getting-Started-on-OS-X)
24
+ * [Getting started with Gosu on Windows](https://github.com/jlnr/gosu/wiki/Getting-Started-on-Windows)
25
+
26
+ ## Thank you note
27
+
28
+ Thnx to people at [RubyWeekly](http://rubyweekly.com/) for publishing awesome stuff every week, such as [Mike Moores talk on LA Ruby Conference about creating games with Gosu](http://www.confreaks.com/videos/3049-larubyconf2014-writing-games-with-ruby?utm_source=rubyweekly&utm_medium=email).
29
+
30
+ Graphics partially taken from [How to make games in iOS](http://www.raywenderlich.com/51068/how-to-make-a-game-like-space-invaders-with-sprite-kit-tutorial-part-1).
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( http://github.com/<my-github-username>/space_invaders/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'space_invaders'
3
+ require "space_invaders/app"
4
+ # require "space_invaders/version"
5
+ # require 'space_invaders/app'
6
+
7
+ SpaceInvaders::App.new.show
8
+
9
+ # puts "BU"
data/go.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'pry'
2
+ require './lib/space_invaders'
3
+ require './lib/space_invaders/app.rb'
4
+
5
+ SpaceInvaders::App.new.show
@@ -0,0 +1,4 @@
1
+ require 'gosu'
2
+
3
+ module SpaceInvaders
4
+ end
@@ -0,0 +1,99 @@
1
+ require "gosu"
2
+ require 'pry'
3
+
4
+ require 'space_invaders/screens/welcome_screen'
5
+ require 'space_invaders/screens/game_over_screen'
6
+ require 'space_invaders/screens/next_level_screen'
7
+
8
+ require 'space_invaders/trackers/score_tracker'
9
+ require 'space_invaders/trackers/lives_tracker'
10
+
11
+ require 'space_invaders/statics/game_status'
12
+ require 'space_invaders/statics/button_controller'
13
+ require 'space_invaders/statics/images'
14
+ require 'space_invaders/statics/sounds'
15
+
16
+ require 'space_invaders/invaders/invaders_container'
17
+ require 'space_invaders/invaders/red_invader'
18
+
19
+ require 'space_invaders/blocks/u_block'
20
+ require 'space_invaders/blocks/u_block_container'
21
+
22
+ require 'space_invaders/ship/ship'
23
+ require 'space_invaders/base'
24
+ require 'space_invaders/utils'
25
+
26
+ module SpaceInvaders
27
+ class App < Gosu::Window
28
+
29
+ DEFAULT_FONT = "assets/fonts/unifont.ttf"
30
+
31
+ STATICS = :game_status, :button_controller, :images, :sounds,
32
+ :welcome_screen, :game_over_screen, :next_level_screen
33
+ TRACKERS = :lives_tracker, :score_tracker
34
+ DYNAMICS = :ship, :invaders_container, :u_block_container, :red_invader
35
+
36
+ attr_reader *STATICS, *TRACKERS, *DYNAMICS
37
+
38
+ def initialize width=800, height=600, fullscreen=false
39
+ super
40
+ self.caption = "Sprite Demonstration"
41
+ initialize_statics
42
+ initialize_dynamics_and_trackers
43
+ end
44
+
45
+ def button_down id
46
+ button_controller.button_down id
47
+ end
48
+
49
+ def update
50
+ if game_status.drowned_ship?
51
+ ship.update
52
+ elsif game_status.being_played?
53
+ update_dynamics
54
+ end
55
+ end
56
+
57
+ def draw
58
+ if game_status.hasnt_started?
59
+ welcome_screen.draw
60
+ elsif game_status.next_level?
61
+ next_level_screen.draw
62
+ elsif game_status.drowned_ship? or game_status.being_played?
63
+ draw_dynamics
64
+ draw_trackers
65
+ elsif game_status.finished?
66
+ game_over_screen.draw
67
+ end
68
+ end
69
+
70
+ def initialize_statics
71
+ define_properties *STATICS
72
+ end
73
+
74
+ def initialize_dynamics_and_trackers
75
+ define_properties *DYNAMICS, *TRACKERS
76
+ end
77
+
78
+ def draw_dynamics
79
+ DYNAMICS.each {|dynamic_element| self.send(dynamic_element).draw}
80
+ end
81
+
82
+ def draw_trackers
83
+ TRACKERS.each {|tracker_element| self.send(tracker_element).draw}
84
+ end
85
+
86
+ def update_dynamics
87
+ DYNAMICS.each {|dynamic_element| self.send(dynamic_element).update}
88
+ end
89
+
90
+ private
91
+
92
+ def define_properties(*properties)
93
+ properties.each do |property|
94
+ instance_variable_set "@#{property}", Utils.to_klass(property).new(self)
95
+ end
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,14 @@
1
+ module SpaceInvaders
2
+ class Base
3
+ attr_reader :app
4
+
5
+ def initialize app
6
+ @app = app
7
+ end
8
+
9
+ def game_status
10
+ app.game_status
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module SpaceInvaders
2
+ module Centerable
3
+ def horizontal_center_draw(image, y)
4
+ x = app.width/2 - image.width/2
5
+ image.draw(x, y, 1)
6
+ end
7
+
8
+ def vertical_center_draw(image, x)
9
+ y = app.height/2 - image.height/2
10
+ image.draw(x, y, 1)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module SpaceInvaders
2
+ module Collideable
3
+ def collides_with?(bullets)
4
+ bullets.each do |bullet|
5
+ if got_hit_by? bullet
6
+ bullet.delete
7
+ return true
8
+ end
9
+ end
10
+ return false
11
+ end
12
+
13
+ def got_hit_by?(bullet)
14
+ bullet.x_position.between?(x_position, x_position + width) and
15
+ bullet.y_position.between?(y_position, y_position + height)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ require 'space_invaders/bullets/bullet_collection'
2
+ require 'space_invaders/bullets/bullet'
3
+
4
+ module SpaceInvaders
5
+ module Fireable
6
+
7
+ def bullet_collection
8
+ @bullet_collection ||= BulletCollection.new
9
+ end
10
+
11
+ alias_method :bullets, :bullet_collection
12
+
13
+ def fire!
14
+ bullet = Bullet.new(shooter, bullets_going_down?, bullet_collection, bullet_offset_speed)
15
+ sound.play
16
+ end
17
+
18
+ def shooter
19
+ raise NotImplementedError.new("You must implement the inherited shooter method")
20
+ end
21
+
22
+ def sound
23
+ raise NotImplementedError.new("You must implement the inherited sound method")
24
+ end
25
+
26
+ def bullets_going_down?
27
+ true
28
+ end
29
+
30
+ def bullet_offset_speed
31
+ 5
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ module SpaceInvaders
2
+ module Mesurable
3
+ attr_accessor :x_position, :y_position
4
+
5
+ def x_middle
6
+ @x_position + @image.width/2
7
+ end
8
+
9
+ def width
10
+ @image.width
11
+ end
12
+
13
+ def height
14
+ @image.height
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ require 'space_invaders/base'
2
+ require 'space_invaders/behaviors/collideable'
3
+
4
+ module SpaceInvaders
5
+ class Block < Base
6
+ include Collideable
7
+
8
+ attr_accessor :health, :x_position, :y_position, :u_block
9
+
10
+ def initialize u_block, x_position, y_position
11
+ super(u_block.app)
12
+
13
+ @u_block = u_block
14
+ @x_position = x_position
15
+ @y_position = y_position
16
+ @health = 3
17
+ set_image
18
+ end
19
+
20
+ def update
21
+ downgrade_image_or_die if collides_with? rival_bullets
22
+ end
23
+
24
+ def draw
25
+ @image.draw x_position, y_position, 1
26
+ end
27
+
28
+ def rival_bullets
29
+ app.ship.bullets.bullets + app.invaders_container.bullets.bullets
30
+ end
31
+
32
+ def width
33
+ @image.width
34
+ end
35
+
36
+ def height
37
+ @image.height
38
+ end
39
+
40
+ def dead?
41
+ health.zero?
42
+ end
43
+
44
+ def downgrade_image_or_die
45
+ self.health -= 1
46
+ if health > 0
47
+ set_image
48
+ else
49
+ u_block.delete(self)
50
+ end
51
+ end
52
+
53
+ def set_image
54
+ @image = case health
55
+ when 3 then app.images.full_block
56
+ when 2 then app.images.ok_block
57
+ when 1 then app.images.weak_block
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ require 'space_invaders/base'
2
+ require 'space_invaders/blocks/block'
3
+
4
+ module SpaceInvaders
5
+ class UBlock < Base
6
+
7
+ attr_reader :blocks, :x_position, :y_position
8
+
9
+ def initialize app, x_position, y_position
10
+ super app
11
+ @x_position = x_position
12
+ @y_position = y_position
13
+ @blocks = []
14
+ initialize_blocks
15
+ end
16
+
17
+ def update
18
+ blocks.each {|block| block.update }
19
+ end
20
+
21
+ def draw
22
+ blocks.each {|block| block.draw }
23
+ end
24
+
25
+ def initialize_blocks
26
+ x = [10, 10, 10, 40, 70, 70, 70]
27
+ y = [10, 30, 50, 10, 10, 30, 50]
28
+ 7.times do |i|
29
+ @blocks << Block.new(self, x[i] + x_position, y[i] + y_position)
30
+ end
31
+ end
32
+
33
+ def delete(block)
34
+ blocks.delete(block)
35
+ end
36
+ end
37
+ end