Guest User

Untitled

a guest
Jun 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. begin
  2. # In case you use Gosu via RubyGems.
  3. require 'rubygems'
  4. rescue LoadError
  5. # In case you don't.
  6. end
  7.  
  8. require 'gosu'
  9.  
  10. class Objeto
  11. def initialize(window, x, y, vel_x = 0, vel_y = 0, imagem = "media/inverse_triangle.bmp")
  12. @vel_x = vel_x
  13. @vel_y = vel_y
  14. @imagem = Gosu::Image.new(window, imagem)
  15. @x = x
  16. @y = y
  17. @w = @imagem.width
  18. @h = @imagem.height
  19. end
  20.  
  21. def colisao(objeto)
  22. return true if (@x + @w > objeto.x and @x < objeto.x + objeto.w and @y + @h > objeto.y and @y < objeto.y + objeto.h)
  23. return false
  24. end
  25.  
  26. def move
  27. @x += @vel_x
  28. @y += @vel_y
  29. @x = 0 if (@x < 0)
  30. @x = 640 - @w if (@x + @w > 640)
  31. @y = 0 if (@y < 0)
  32. @y = 480 - @h if (@y + @h > 480)
  33. end
  34.  
  35. def draw
  36. @imagem.draw(@x, @y, 1)
  37. end
  38. attr_accessor :vel_x, :vel_y, :x, :y, :w, :h
  39. end
  40.  
  41. class Player < Objeto
  42. def input(tiros, window)
  43. if (window.button_down? Gosu::Button::KbLeft or window.button_down? Gosu::Button::GpLeft)
  44. @vel_x = -5
  45. elsif (window.button_down? Gosu::Button::KbRight or window.button_down? Gosu::Button::GpRight)
  46. @vel_x = 5
  47. else
  48. @vel_x = 0
  49. end
  50.  
  51. if (window.button_down? Gosu::Button::KbUp or window.button_down? Gosu::Button::GpUp)
  52. puts "o"
  53. self.atira(tiros, window)
  54. end
  55. end
  56.  
  57. def atira(tiros, window)
  58. tiros.push(Objeto.new(window, @x, @y , 0, - 10, "media/tiro.bmp"))
  59. end
  60. end
  61.  
  62. class Inimigo < Objeto
  63. def atira(tiros, window)
  64. tiros.push(Objeto.new(window, @x, @y , 0, 10, "media/tiro.bmp"))
  65. end
  66. end
  67.  
  68. class GameWindow < Gosu::Window
  69. def initialize
  70. super(640, 480, false)
  71. self.caption = "Space Invaders"
  72. @background_image = Gosu::Image.new(self, "media/space.png", true)
  73.  
  74. @player = Player.new(self, 300, 380, 0, 0, "media/triangle.bmp")
  75. @tiros_player = Array.new()
  76. @tiros_inimigos = Array.new()
  77. @inimigos = Array.new(3) {|i| Array.new(3) {|j| Inimigo.new(self, 50*i ,50*j, 5)}}
  78. @nave = Inimigo.new(self, 100, 100, 5)
  79. end
  80.  
  81. def update
  82. @player.input(@tiros_player, self)
  83. @player.move
  84. @dir = 0
  85.  
  86. @inimigos.flatten.each do |inimigo|
  87. if ( ((inimigo.x + inimigo.w) + (inimigo.vel_x)) > 640 or (inimigo.x) + (inimigo.vel_x) < 0)
  88. @dir = 1
  89. end
  90. end
  91. @inimigos.flatten.each { |inimigo| inimigo.vel_x *= -1} if @dir == 1
  92.  
  93. @inimigos.flatten.each { |inimigo| inimigo.move }
  94.  
  95. end
  96.  
  97. def draw
  98. @background_image.draw(0, 0, 0)
  99. @player.draw
  100. @inimigos.flatten.each { |inimigo| inimigo.draw }
  101. end
  102.  
  103. def button_down(id)
  104. if (id == Gosu::Button::KbEscape)
  105. close
  106. end
  107. end
  108. end
  109.  
  110. window = GameWindow.new
  111. window.show
Add Comment
Please, Sign In to add comment