Advertisement
heroofhyla

Cowboys 5

Nov 11th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.75 KB | None | 0 0
  1. require 'gosu'
  2.  
  3. class Player
  4.   attr_reader :x
  5.   attr_reader :y
  6.   def initialize
  7.     @sprite = Gosu::Image.new("cowboy.png")
  8.     @x = 128
  9.     @y = 128
  10.   end
  11.  
  12.   def x=(new_x)
  13.     @x =[[0, new_x].max,640-@sprite.width].min #stops player going off edge of screen
  14.   end
  15.  
  16.   def y=(new_y)
  17.     @y =[[0, new_y].max,480-@sprite.height].min #stops player going off edge of screen
  18.   end
  19.   def draw
  20.     @sprite.draw(@x,@y,3)
  21.   end
  22. end
  23.  
  24. class Bullet
  25.   attr_accessor :x
  26.   attr_accessor :y
  27.   def initialize(x, y)
  28.     @x = x
  29.     @y = y
  30.     @sprite = Gosu::Image.new("bullet.png")
  31.   end
  32.   def draw
  33.     @sprite.draw(@x, @y, 2)
  34.   end
  35.   def update
  36.     @x += 4
  37.   end
  38. end
  39.  
  40. class GameWindow < Gosu::Window
  41.   def initialize
  42.     super 640,480
  43.     self.caption = 'Cowboys 5'
  44.    
  45.     @bg_image = Gosu::Image.new("grassy.png", tileable: true)
  46.     @player = Player.new
  47.     @bullets = []
  48.     @gun_ready = true
  49.   end
  50.  
  51.   def update
  52.     if Gosu::button_down? Gosu::KbLeft then
  53.       @player.x -= 2
  54.     end
  55.     if Gosu::button_down? Gosu::KbRight then
  56.       @player.x += 2
  57.     end
  58.     if Gosu::button_down? Gosu::KbUp then
  59.       @player.y -= 2
  60.     end
  61.     if Gosu::button_down? Gosu::KbDown then
  62.       @player.y += 2
  63.     end
  64.     if Gosu::button_down?(Gosu::KbSpace) then
  65.       if @gun_ready
  66.         @bullets.push Bullet.new(@player.x+50, @player.y+26)
  67.         @gun_ready = false
  68.       end
  69.     else
  70.       @gun_ready = true
  71.     end
  72.     @bullets.delete_if do |bullet|
  73.       bullet.update
  74.       if bullet.x > 640
  75.         true
  76.       end
  77.     end
  78.   end
  79.  
  80.   def draw
  81.     @bullets.each do |bullet|
  82.       bullet.draw
  83.     end
  84.     @player.draw
  85.     @bg_image.draw(0,0,0)
  86.     self.caption = @bullets.size
  87.   end
  88. end
  89.  
  90. window = GameWindow.new
  91. window.show
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement