Created
April 19, 2012 09:26
-
-
Save JamesZoft/2419934 to your computer and use it in GitHub Desktop.
2d shooter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'gosu' | |
require 'pp' | |
class GameWindow < Gosu::Window | |
def initialize | |
super 640, 480, false | |
self.caption = "Gosu Tutorial Game" | |
@background_image = Gosu::Image.new(self, "spaceship_right_closed.gif", true) | |
@player = Player.new(self) | |
@player.warp(320, 240) | |
@star_anim = Gosu::Image::load_tiles(self, "bullet.jpg", 25, 25, false) | |
@stars = Array.new | |
@font = Gosu::Font.new(self, Gosu::default_font_name, 20) | |
@bullets = [] | |
end | |
def update | |
if button_down? Gosu::KbLeft or button_down? Gosu::KbA then | |
@player.turn_left | |
end | |
if button_down? Gosu::KbRight or button_down? Gosu::KbD then | |
@player.turn_right | |
end | |
if button_down? Gosu::KbUp or button_down? Gosu::KbW then | |
@player.accelerate_forwards | |
end | |
if button_down? Gosu::KbDown or button_down? Gosu::KbS then | |
@player.accelerate_backwards | |
end | |
if button_down? Gosu::KbSpace then | |
@bullets << @player.shoot(self) | |
end | |
@bullets.each do |bullet| | |
bullet.update | |
bullet.draw | |
end | |
@player.move | |
@player.collect_stars(@stars) | |
if rand(100) < 4 and @stars.size < 25 then | |
@stars.push(Star.new(@star_anim)) | |
end | |
end | |
def draw | |
@player.draw | |
@stars.each { |star| star.draw } | |
@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00) | |
end | |
def button_down(id) | |
if id == Gosu::KbEscape | |
close | |
end | |
end | |
end | |
class Bullet | |
def initialize(window) | |
@image = Gosu::Image.new(window, "bullet.gif", false) | |
@x = @y = @vel_x = @vel_y = 0.0 | |
@beep = Gosu::Sample.new(window, "macossounds/macossounds/WAV/Boing.wav") | |
@beep.play | |
end | |
def warp(x, y) | |
@x, @y = x, y | |
end | |
def accelerate_forwards | |
@vel_x += Gosu::offset_x(0.0, 0.25) | |
@vel_y += Gosu::offset_y(0.0, 0.25) | |
end | |
def move | |
@x += @vel_x | |
@y += @vel_y | |
@x %= 640 | |
@y %= 480 | |
@vel_x *= 0.95 | |
@vel_y *= 0.95 | |
end | |
def update | |
accelerate_forwards | |
end | |
def draw | |
@image.draw_rot(@x, @y, 1, 0.0) | |
end | |
end | |
class Player | |
attr_reader :score | |
def initialize(window) | |
@image = Gosu::Image.new(window, "spaceship_right_closed.gif", false) | |
@beep = Gosu::Sample.new(window, "macossounds/macossounds/WAV/Logjam.wav") | |
@x = @y = @vel_x = @vel_y = @angle = 0.0 | |
@score = 0 | |
end | |
def warp(x, y) | |
@x, @y = x, y | |
end | |
def turn_left | |
@angle -= 4.5 | |
end | |
def turn_right | |
@angle += 4.5 | |
end | |
def shoot(window) | |
bullet = Bullet.new(window) | |
bullet.warp(@x, @y) | |
bullet | |
end | |
def accelerate_forwards | |
@vel_x += Gosu::offset_x(@angle + 90, 0.25) | |
@vel_y += Gosu::offset_y(@angle + 90, 0.25) | |
end | |
def accelerate_backwards | |
@vel_x -= Gosu::offset_x(@angle + 90, 0.25) | |
@vel_y -= Gosu::offset_y(@angle + 90, 0.25) | |
end | |
def move | |
@x += @vel_x | |
@y += @vel_y | |
@x %= 640 | |
@y %= 480 | |
@vel_x *= 0.95 | |
@vel_y *= 0.95 | |
end | |
def draw | |
@image.draw_rot(@x, @y, 1, @angle) | |
end | |
def collect_stars(stars) | |
stars.reject! do |star| | |
if Gosu::distance(@x, @y, star.x, star.y) < 35 then | |
@score += 10 | |
@beep.play | |
true | |
else | |
false | |
end | |
end | |
end | |
end | |
module ZOrder | |
Background, Stars, Player, UI = *0..3 | |
end | |
class Star | |
attr_reader :x, :y | |
def initialize(animation) | |
@animation = animation | |
@color = Gosu::Color.new(0xff000000) | |
@color.red = rand(256 - 40) + 40 | |
@color.green = rand(256 - 40) + 40 | |
@color.blue = rand(256 - 40) + 40 | |
@x = rand * 640 | |
@y = rand * 480 | |
end | |
def draw | |
img = @animation[Gosu::milliseconds / 100 % @animation.size]; | |
img.draw(@x - img.width / 2.0, @y - img.height / 2.0, | |
ZOrder::Stars, 1, 1, @color, :add) | |
end | |
end | |
window = GameWindow.new | |
window.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment