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/inverse_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
- attr_accessor :vel_x, :vel_y, :x, :y, :w, :h
- 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)
- puts "o"
- 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 Invaders"
- @background_image = Gosu::Image.new(self, "media/space.png", true)
- @player = Player.new(self, 300, 380, 0, 0, "media/triangle.bmp")
- @tiros_player = Array.new()
- @tiros_inimigos = Array.new()
- @inimigos = Array.new(3) {|i| Array.new(3) {|j| Inimigo.new(self, 50*i ,50*j, 5)}}
- @nave = Inimigo.new(self, 100, 100, 5)
- end
- def update
- @player.input(@tiros_player, self)
- @player.move
- @dir = 0
- @inimigos.flatten.each do |inimigo|
- if ( ((inimigo.x + inimigo.w) + (inimigo.vel_x)) > 640 or (inimigo.x) + (inimigo.vel_x) < 0)
- @dir = 1
- end
- end
- @inimigos.flatten.each { |inimigo| inimigo.vel_x *= -1} if @dir == 1
- @inimigos.flatten.each { |inimigo| inimigo.move }
- end
- def draw
- @background_image.draw(0, 0, 0)
- @player.draw
- @inimigos.flatten.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