Skip to content

Instantly share code, notes, and snippets.

@ntijoh-daniel-berg
Last active December 15, 2015 05:49
Show Gist options
  • Save ntijoh-daniel-berg/5211811 to your computer and use it in GitHub Desktop.
Save ntijoh-daniel-berg/5211811 to your computer and use it in GitHub Desktop.
require 'chingu'
class Game < Chingu::Window
def initialize
super
self.input = {esc: :exit}
Player1.create
Player2.create
end
def update
super
Laser.each_bounding_circle_collision(Player1, Player2) do |laser, player|
laser.destroy
player.destroy
end
end
end
class Player < Chingu::GameObject
has_traits :velocity, :collision_detection, :bounding_circle
def setup
@velocity_x = 0
@velocity_y = 0
@image = Gosu::Image["ship.png"]
end
def fire
Laser.create(x: @x + Gosu::offset_x(@angle, 50),
y: @y + Gosu::offset_y(@angle, 50),
angle: @angle,
velocity_x: @velocity_x + Gosu::offset_x(@angle, 5),
velocity_y: @velocity_y + Gosu::offset_y(@angle, 5))
end
def update
@velocity_x *= 0.95
@velocity_y *= 0.95
end
def accelerate
@velocity_x += Gosu::offset_x(@angle, 0.5)
@velocity_y += Gosu::offset_y(@angle, 0.5)
end
def left
@angle -= 5
end
def right
@angle += 5
end
end
class Player1 < Player
def setup
@x = 200
@y = 300
@angle = 0
self.input = {holding_a: :left, holding_d: :right, holding_w: :accelerate, s: :fire}
super
end
end
class Player2 < Player
def setup
@x = 400
@y = 500
@angle = 0
self.input = {holding_left: :left, holding_right: :right, holding_up: :accelerate, space: :fire}
super
end
end
class Laser < Chingu::GameObject
has_traits :velocity, :timer, :collision_detection, :bounding_circle
def setup
@image = Gosu::Image["laser.png"]
Gosu::Sound["pew-pew.wav"].play
after(1000) { self.destroy}
end
end
Game.new.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment