Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 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/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.  
  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. self.atira(tiros, window)
  53. end
  54. end
  55.  
  56. def atira(tiros, window)
  57. tiros.push(Objeto.new(window, @x, @y , 0, - 10, "media/tiro.bmp"))
  58. end
  59. end
  60.  
  61. class Inimigo < Objeto
  62. def atira(tiros, window)
  63. tiros.push(Objeto.new(window, @x, @y , 0, 10, "media/tiro.bmp"))
  64. end
  65. end
  66.  
  67. class GameWindow < Gosu::Window
  68. def initialize
  69. super(640, 480, false)
  70. self.caption = "Space Invades"
  71. @background_image = Gosu::Image.new(self, "media/space.png", true)
  72.  
  73. @player = Player.new(self, 300, 380, "media/inverse_triangle.bmp")
  74. @tiros_player = Array.new()
  75. @tiros_inimigos = Array.new()
  76. @inimigos = Array.new(10) {|i| Array.new(10) {|j| Inimigo.new(self, 50*i ,50*j, 5)}}
  77. @nave = Inimigo.new(self, 100, 100, 5, 0, "media/triangle.bmp")
  78. end
  79.  
  80. def update
  81. @player.input(@tiros_player, self)
  82. @player.move
  83.  
  84. @direcao = 1 if (@inimigos[0][0].vel_x > 0)
  85. @direcao = -1 if (@inimigos[0][0].vel_x < 0)
  86. @inimigos.each do |inimigo|
  87. if (inimigo.x + (inimigo.vel_x * @direcao) > 640 or inimigo.x +(inimigo.vel_x * @direcao) < 0)
  88. @direcao *= -1
  89. inimigo.y += 50
  90. end
  91. end
  92. @inimigos.each { |inimigo| inimigo.move }
  93. end
  94.  
  95. def draw
  96. @background_image.draw(0, 0, 0)
  97. @player.draw
  98. @inimigos.each { |inimigo| inimigo.draw }
  99. end
  100.  
  101. def button_down(id)
  102. if (id == Gosu::Button::KbEscape)
  103. close
  104. end
  105. end
  106. end
  107.  
  108. window = GameWindow.new
  109. window.show
Add Comment
Please, Sign In to add comment