Created
August 1, 2012 02:35
-
-
Save lrlucena/3223071 to your computer and use it in GitHub Desktop.
Jogo do Avião
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bomba | |
attr_reader :x, :y, :ativa | |
def initialize(janela) | |
@janela = janela | |
@icon = Gosu::Image.new(@janela, 'bomba.png', true) | |
@y = -rand(200) | |
@x = rand(@janela.width-60) | |
@ativa = true | |
end | |
def update(laser) | |
@y = @y + 10 | |
if @y > @janela.width then | |
@y = -rand(200) | |
@x = rand(@janela.width) | |
end | |
atingida_por?(laser) | |
end | |
def draw | |
@icon.draw(@x, @y, 3) if @ativa | |
end | |
def atingida_por?(laser) | |
if Gosu::distance(laser.x, laser.y, @x, @y) < 20 then | |
@ativa= false | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Janela < Gosu::Window | |
def initialize | |
super(600,300,false) | |
@jogador = Jogador.new(self) | |
@bombas = 3.times.map{Bomba.new(self)} | |
@rodando = true | |
@font = Gosu::Font.new(self, Gosu::default_font_name, 20) | |
end | |
def draw | |
@jogador.draw | |
@bombas.each {|bomba| bomba.draw} | |
@font.draw("Vidas: #{@jogador.vidas}", 10, 10, 3.0, 1.0, 1.0, 0xffffffff) | |
end | |
def update | |
@rodando = false if @jogador.atingido_por? @bombas | |
if not @rodando and button_down? Gosu::Button::KbR and @jogador.vidas > 0 then | |
@rodando = true | |
@jogador.reiniciar_posicao | |
end | |
rode_jogo if @rodando | |
end | |
def rode_jogo | |
@bombas | |
.select {|bomba| bomba.ativa} | |
.each {|bomba| bomba.update(@jogador.laser)} | |
@jogador.update | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Jogador | |
attr_reader :vidas, :x, :y, :laser | |
def initialize(janela) | |
@janela = janela | |
@icon = Gosu::Image.new(@janela, "jogador.png", true) | |
@x, @y = 0, @janela.height - 80 | |
@explosao = Gosu::Image.new(@janela, "explosao.png", true) | |
@explodido = false | |
@vidas = 5 | |
@laser = Laser.new(self, @janela) | |
end | |
def mova_esquerda | |
@x = (if @x>10 then @x - 10 else 0 end) | |
end | |
def mova_direita | |
@x = (if @x < @janela.width-60 then @x+10 | |
else @janela.width-50 end) | |
end | |
def draw | |
if @explodido then @explosao.draw(@x, @y, 4) | |
else @icon.draw(@x,@y,1); @laser.draw end | |
end | |
def atingido_por?(bombas) | |
exp = @explodido | |
@explodido = bombas.any? do |bomba| | |
Gosu::distance(bomba.x, bomba.y, @x, @y) < 20 | |
end | |
if @explodido and not exp then | |
@vidas = @vidas - 1 | |
end | |
@explodido | |
end | |
def reiniciar_posicao | |
@x = rand(@janela.width) | |
end | |
def update | |
mova_esquerda if @janela.button_down? Gosu::Button::KbLeft | |
mova_direita if @janela.button_down? Gosu::Button::KbRight | |
@laser.atirar if @janela.button_down? Gosu::Button::KbSpace | |
@laser.update | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Laser | |
attr_reader :x, :y | |
def initialize(jogador, janela) | |
@jogador, @janela = jogador, janela | |
@atirando = false | |
@x, @y = @jogador.x, @jogador.y | |
@icon = Gosu::Image.new(@janela, "laser.png", true) | |
end | |
def atirar | |
@atirando = true | |
end | |
def update | |
if @atirando then | |
@y = @y - 10 | |
@atirando = false if @y < 0 | |
else | |
@x, @y = @jogador.x, @jogador.y | |
end | |
end | |
def draw | |
if @atirando then | |
@icon.draw(@x, @y, 4) | |
else | |
@icon.draw(@jogador.x,@jogador.y,4) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gosu' | |
require_relative 'janela' | |
require_relative 'jogador' | |
require_relative 'bomba' | |
require_relative 'laser' | |
janela = Janela.new | |
janela.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment