Created
March 12, 2012 22:38
-
-
Save farnoy/2025147 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'gosu' | |
| module GoUp | |
| def self.update(obj, window) | |
| obj.move(0, -1) | |
| end | |
| end | |
| module GoDown | |
| def self.update(obj, window) | |
| obj.move(0, 1) | |
| end | |
| end | |
| class GameWindow < Gosu::Window | |
| def initialize | |
| super 640, 480, false | |
| self.caption = "Gosu tutorial game" | |
| @dots = [] | |
| @dots << Dot.new(self, GoUp) | |
| @dots << Dot.new(self, GoDown) | |
| @dots.each do |dot| | |
| i = @dots.index(dot) + 1 | |
| dot.warp(i * 160, i * 120) | |
| end | |
| end | |
| def update | |
| @dots.each do |dot| | |
| dot.update(self) | |
| end | |
| #if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft then | |
| #@dot.move(-1) | |
| #end | |
| #if button_down? Gosu::KbRight or button_down? Gosu::GpRight then | |
| #@dot.move(1) | |
| #end | |
| #if button_down? Gosu::KbUp or button_down? Gosu::GpButton0 then | |
| #@dot.move(0, -1) | |
| #end | |
| #if button_down? Gosu::KbDown or button_down? Gosu::GpButton1 then | |
| #@dot.move(0, 1) | |
| #end | |
| end | |
| def draw | |
| @dots.each {|dot| dot.draw } | |
| end | |
| def button_down(id) | |
| if id == Gosu::KbEscape | |
| close | |
| end | |
| end | |
| end | |
| class Dot | |
| def initialize(window, solver) | |
| @image = Gosu::Image.new(window, "anchor.png", false) | |
| @solver = solver | |
| @x = @y = 0.0 | |
| end | |
| def warp(x, y) | |
| @x, @y = x, y | |
| end | |
| def move(offsetX = 0, offsetY = 0) | |
| @x += offsetX | |
| @y += offsetY | |
| @x %= 640 | |
| @y %= 480 | |
| end | |
| def update(window) | |
| @solver.update(self, window) | |
| end | |
| def draw | |
| @image.draw_rot(@x, @y, 1, 0) | |
| end | |
| end | |
| GameWindow.new.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment