Skip to content

Instantly share code, notes, and snippets.

@chadjemmett
Last active May 16, 2016 00:51
Show Gist options
  • Save chadjemmett/35aaf8f7c2b57e842956b7511a5dd2a9 to your computer and use it in GitHub Desktop.
Save chadjemmett/35aaf8f7c2b57e842956b7511a5dd2a9 to your computer and use it in GitHub Desktop.
require 'gosu'
FONT_COLOR = 0xff_ffff00
class SpaceInvader < Gosu::Window
def initialize
super 640, 480
self.caption = "Space Invaders"
@text_message = "Hello world"
@message = Gosu::Font.new(20)
@x = 300
@y = 300
@x_target = 500
@yellow_square = Gosu::Image.new("invaders/media/yellow_square.png")
end
def update
self.move
close if Gosu::button_down?(Gosu::KbEscape)
end
def move
@x += 3 unless self.collision?(640, @y)
end
def collision?(barrier_x, barrier_y)
(barrier_x.between?(@x - 50, @x) and barrier_y.between?(@y, @y + 50)) || (barrier_x.between?(@x, @x + 50) && barrier_y.between?(@y - 50, @y)) || (barrier_x.between?(@x - 50, @x) && barrier_y.between?(@y - 50, @y)) || (barrier_x.between?(@x, @x + 50) && barrier_y.between?(@y, @y + 50))
end
def needs_cursor?
true
end
def draw
@yellow_square.draw_rot(@x, @y, 0, 0)
@message.draw("X => #{@x} Y => #{@y}", 10, 30, FONT_COLOR)
@message.draw("W => 640 - H => 480", 425, 30, FONT_COLOR)
@message.draw("#{@x_target}, #{@x}", 10, 60, FONT_COLOR)
end
end
window = SpaceInvader.new
window.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment