Skip to content

Instantly share code, notes, and snippets.

@chadjemmett
Created May 6, 2016 20:55
Show Gist options
  • Save chadjemmett/bb9e36f9755d0427a719285ba79d6f3e to your computer and use it in GitHub Desktop.
Save chadjemmett/bb9e36f9755d0427a719285ba79d6f3e 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"
@message = Gosu::Font.new(20)
coordinate_array = []
i = 0
11.times {coordinate_array << [i+=50, 140]}
i = 0
11.times {coordinate_array << [i+=50, 180]}
i = 0
11.times {coordinate_array << [i+=50, 220]}
i = 0
11.times {coordinate_array << [i+=50, 260]}
i = 0
11.times {coordinate_array << [i+=50, 300]}
@invader_phalanx = []
coordinate_array.each do |item|
@invader_phalanx << Invader.new(item[0], item[1])
end
@text_message = ""
end
def update
@invader_phalanx.each do |alien_ship|
#this is where all the invader logic goes.
alien_ship.move
end
close if Gosu::button_down?(Gosu::KbEscape)
end
def button_down(id)
end
def draw
@message.draw("#{@text_message}", 10, 30, FONT_COLOR)
@message.draw("W => 640 - H => 480", 425, 30, FONT_COLOR)
@invader_phalanx.each {|item| item.draw if item.alive == true}
end
end
class Invader
attr_accessor :x, :y, :alive, :rotate
def initialize(x, y)
@x = x
@y = y
@sprite = Gosu::Image.new("media/invader.png")
@alive = true
@rotate = 0
end
def draw
#invader animation will go here.
@sprite.draw_rot(@x, @y, 1, @rotate)
end
#this collision method is different than the player one. This is the one I wrote myself
def collision?(barrier_x, barrier_y)
(barrier_x.between?(@x - 10, @x) and barrier_y.between?(@y, @y + 10)) || (barrier_x.between?(@x, @x + 10) && barrier_y.between?(@y - 10, @y)) || (barrier_x.between?(@x - 10, @x) && barrier_y.between?(@y - 10, @y)) || (barrier_x.between?(@x, @x + 10) && barrier_y.between?(@y, @y + 10))
end
def move
#moves the invaders to the left
@x -= 1
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