-
-
Save jschoolcraft/1e000fc3473b9918242c8aaa4166da24 to your computer and use it in GitHub Desktop.
Coin Toss
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
$gtk.reset | |
class Button | |
attr_accessor :label | |
def initialize(x, y, h, w, label) | |
@x = x | |
@y = y | |
@h = h | |
@w = w | |
@label = label | |
@rect = [@x, @y, @w, @h] | |
end | |
def clicked?(point) | |
point.inside_rect?(@rect) | |
end | |
def render(args) | |
args.outputs.borders << [*@rect, 0, 0, 0, 255] | |
args.outputs.solids << [*@rect, 255, 0, 0, 255] | |
args.outputs.labels << [@x + 25, @y + @h, @label, 13] | |
end | |
end | |
class Coin | |
attr_accessor :face_up | |
def initialize(x, y, size) | |
@faces ||= ["Heads", "Tails"] | |
@face_up ||= @faces[rand(2)] | |
@x = x | |
@y = y | |
@size = size | |
end | |
def flip | |
@face_up = @faces[rand(2)] | |
end | |
def render(args, alpha) | |
sprite_size = 150 | |
args.outputs.sprites << [@x - sprite_size/2, @y, sprite_size, sprite_size, "sprites/#{@face_up}.png", 0, alpha] | |
args.outputs.labels << [@x, @y, @face_up, 25, 1, 0, 0, 0, alpha] | |
end | |
end | |
class CoinToss | |
def initialize | |
@coin = Coin.new(1280/2, 300, 50) | |
@bet = nil | |
@won = 0 | |
@loss = 0 | |
@coin_alpha = 0 | |
end | |
def render_input | |
button_h = 50 | |
button_w = 150 | |
button_x = 300 | |
button_y = 150 | |
@heads ||= Button.new(button_x + 100, button_y, button_h, button_w, "Heads") | |
@tails ||= Button.new(button_x + 400, button_y, button_h, button_w, "Tails") | |
@buttons ||= [ | |
@heads, | |
@tails | |
] | |
@buttons.map { |button| button.render(@args) } | |
end | |
def render_coin | |
return unless @coin | |
@coin_alpha += 7 | |
@coin.render(@args, @coin_alpha) | |
end | |
def render_results | |
@args.outputs.labels << [1280 / 4 - 50, 600, "Won: #{@won} Loss: #{@loss}", 50] | |
end | |
def render_instructions | |
@args.outputs.labels << [1280 / 2 - 200, 50 + 25, "Click HEADS or TAILS to bet and flip coin"] | |
@args.outputs.labels << [1280 / 2 - 100, 50, "Press SPACE to restart"] | |
end | |
def render | |
render_input | |
render_coin | |
render_results | |
render_instructions | |
end | |
def toss_coin | |
@coin_alpha = 0 | |
@coin.flip | |
if @bet == @coin.face_up | |
@won += 1 | |
else | |
@loss += 1 | |
end | |
@bet = nil | |
end | |
def handle_input | |
if @args.inputs.keyboard.key_down.space | |
$gtk.reset | |
end | |
return unless @args.inputs.mouse.click | |
return if @bet | |
return if @coin_alpha <= 255 | |
@bet = nil | |
@buttons.each do |button| | |
next unless button.clicked?(@args.inputs.mouse.click.point) | |
@bet = button.label | |
end | |
toss_coin if @bet | |
end | |
def tick(args) | |
@args = args | |
render | |
handle_input | |
end | |
end | |
def tick args | |
args.state.game ||= CoinToss.new | |
args.state.game.tick(args) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment