-
-
Save bil-bas/1491584 to your computer and use it in GitHub Desktop.
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 'ray' | |
require 'forwardable' | |
class Card | |
extend Forwardable | |
def_delegators :@sprite, :pos, :x, :y | |
attr_reader :rank, :suit, :sprite | |
def initialize(rank, suit) | |
@rank = rank | |
@suit = suit | |
@sprite = Ray::Sprite.new('card_deck.png') | |
@sprite.sheet_size = [13, 5] | |
@sprite.sheet_pos = [rank, suit] | |
end | |
def pick_up(mouse_pos) | |
@offset = @sprite.pos - mouse_pos | |
end | |
def drag(mouse_pos) | |
@sprite.pos = mouse_pos + @offset | |
end | |
def received_click?(mouse_pos) | |
@sprite.to_rect.contain?(mouse_pos) | |
end | |
end | |
class Deck | |
attr_reader :cards | |
def initialize | |
@cards = [] | |
suits = 0..13 | |
ranks = 0..4 | |
suits.each do |rank| | |
ranks.each { |suit| @cards << Card.new(rank, suit) } | |
end | |
end | |
def size | |
@cards.size | |
end | |
end | |
class PatienceScene < Ray::Scene | |
def setup | |
@background_color = Ray::Color.new(31, 95, 25) | |
@deck = Deck.new | |
@deck.cards.each { |c| c.sprite.pos = [0, 0] } | |
end | |
def register | |
add_hook :quit, method(:exit!) | |
add_hook :key_press, key(:q), method(:exit!) | |
always do | |
on :mouse_press do | |
@clicked_card = @deck.cards.find { |card| card.received_click?(mouse_pos) } | |
@clicked_card.pick_up(mouse_pos) if @clicked_card | |
end | |
on :mouse_release do | |
@clicked_card = nil | |
end | |
@clicked_card.drag(mouse_pos) if @clicked_card | |
end | |
end | |
def render(win) | |
win.clear(@background_color) | |
win.draw(@deck.cards[1].sprite) | |
end | |
end | |
class Game < Ray::Game | |
def initialize | |
super("Patience", :size => [800, 600]) | |
PatienceScene.bind(self) | |
scenes << :patience_scene | |
end | |
end | |
Game.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment