Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'gosu'
- class Player
- attr_reader :x
- attr_reader :y
- def initialize
- @sprite = Gosu::Image.new("cowboy.png")
- @x = 128
- @y = 128
- end
- def x=(new_x)
- @x =[[0, new_x].max,640-@sprite.width].min #stops player going off edge of screen
- end
- def y=(new_y)
- @y =[[0, new_y].max,480-@sprite.height].min #stops player going off edge of screen
- end
- def draw
- @sprite.draw(@x,@y,3)
- end
- end
- class Bullet
- attr_accessor :x
- attr_accessor :y
- def initialize(x, y)
- @x = x
- @y = y
- @sprite = Gosu::Image.new("bullet.png")
- end
- def draw
- @sprite.draw(@x, @y, 2)
- end
- def update
- @x += 4
- end
- end
- class GameWindow < Gosu::Window
- def initialize
- super 640,480
- self.caption = 'Cowboys 5'
- @bg_image = Gosu::Image.new("grassy.png", tileable: true)
- @player = Player.new
- @bullets = []
- @gun_ready = true
- end
- def update
- if Gosu::button_down? Gosu::KbLeft then
- @player.x -= 2
- end
- if Gosu::button_down? Gosu::KbRight then
- @player.x += 2
- end
- if Gosu::button_down? Gosu::KbUp then
- @player.y -= 2
- end
- if Gosu::button_down? Gosu::KbDown then
- @player.y += 2
- end
- if Gosu::button_down?(Gosu::KbSpace) then
- if @gun_ready
- @bullets.push Bullet.new(@player.x+50, @player.y+26)
- @gun_ready = false
- end
- else
- @gun_ready = true
- end
- @bullets.delete_if do |bullet|
- bullet.update
- if bullet.x > 640
- true
- end
- end
- end
- def draw
- @bullets.each do |bullet|
- bullet.draw
- end
- @player.draw
- @bg_image.draw(0,0,0)
- self.caption = @bullets.size
- end
- end
- window = GameWindow.new
- window.show
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement