Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- begin
- # In case you use Gosu via RubyGems.
- require 'rubygems'
- rescue LoadError
- # In case you don't.
- end
- require 'gosu'
- class Objeto
- def initialize(window, x, y, vel_x = 0, vel_y = 0, imagem = "media/triangle.bmp")
- @vel_x = vel_x
- @vel_y = vel_y
- @imagem = Gosu::Image.new(window, imagem)
- @x = x
- @y = y
- @w = @imagem.width
- @h = @imagem.height
- end
- def colisao(objeto)
- return true if (@x + @w > objeto.x and @x < objeto.x + objeto.w and @y + @h > objeto.y and @y < objeto.y + objeto.h)
- return false
- end
- def move
- @x += @vel_x
- @y += @vel_y
- @x = 0 if (@x < 0)
- @x = 640 - @w if (@x + @w > 640)
- @y = 0 if (@y < 0)
- @y = 480 - @h if (@y + @h > 480)
- end
- def draw
- @imagem.draw(@x, @y, 1)
- end
- end
- class Player < Objeto
- def input(tiros, window)
- if (window.button_down? Gosu::Button::KbLeft or window.button_down? Gosu::Button::GpLeft)
- @vel_x = -5
- elsif (window.button_down? Gosu::Button::KbRight or window.button_down? Gosu::Button::GpRight)
- @vel_x = 5
- else
- @vel_x = 0
- end
- if (window.button_down? Gosu::Button::KbUp or window.button_down? Gosu::Button::GpUp)
- self.atira(tiros, window)
- end
- end
- def atira(tiros, window)
- tiros.push(Objeto.new(window, @x, @y , 0, - 10, "media/tiro.bmp"))
- end
- end
- class Inimigo < Objeto
- def atira(tiros, window)
- tiros.push(Objeto.new(window, @x, @y , 0, 10, "media/tiro.bmp"))
- end
- end
- class GameWindow < Gosu::Window
- def initialize
- super(640, 480, false)
- self.caption = "Space Invades"
- @background_image = Gosu::Image.new(self, "media/space.png", true)
- @player = Player.new(self, 300, 380, "media/inverse_triangle.bmp")
- @tiros_player = Array.new()
- @tiros_inimigos = Array.new()
- @inimigos = Array.new(10) {|i| Array.new(10) {|j| Inimigo.new(self, 50*i ,50*j, 5)}}
- @nave = Inimigo.new(self, 100, 100, 5, 0, "media/triangle.bmp")
- end
- def update
- @player.input(@tiros_player, self)
- @player.move
- @direcao = 1 if (@inimigos[0][0].vel_x > 0)
- @direcao = -1 if (@inimigos[0][0].vel_x < 0)
- @inimigos.each do |inimigo|
- if (inimigo.x + (inimigo.vel_x * @direcao) > 640 or inimigo.x +(inimigo.vel_x * @direcao) < 0)
- @direcao *= -1
- inimigo.y += 50
- end
- end
- @inimigos.each { |inimigo| inimigo.move }
- end
- def draw
- @background_image.draw(0, 0, 0)
- @player.draw
- @inimigos.each { |inimigo| inimigo.draw }
- end
- def button_down(id)
- if (id == Gosu::Button::KbEscape)
- close
- end
- end
- end
- window = GameWindow.new
- window.show
Add Comment
Please, Sign In to add comment