Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
砲台ゲーム
require 'gnuplot'
class GameField
def initialize
@first = Random.rand(100...400)
@last = @first + Random.rand(50..100)
end
def test(u, t)
show(u, t)
end
def parabora(u, t)
t = t / 180.0 * Math::PI
g = 9.8
t_flight = 2 * u * Math.sin(t) / g
intervals = (0.0..t_flight).step(0.001)
return [
intervals.map {|x| u * Math.cos(t) * x},
intervals.map {|y| u * Math.sin(t) * y - 0.5*g*y*y }
]
end
def parabora_only(u, t)
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
ball = Gnuplot::DataSet.new(parabora(u, t)) do |ds|
ds.with = "lines lt rgb\"blue\""
ds.linewidth = 1
end
plot.data << ball
IRuby.display(plot)
end
end
end
def show
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.title 'Game'
# gnuplotの場合、配列の要素がxになるので、挟まないといけない
fix = Gnuplot::DataSet.new([600]) { |ds| ds.with = "lines lt rgb \"#ffffff\"" }
field = Gnuplot::DataSet.new([*0..@first].map {|x| 0 }) do |ds|
ds.with = "lines lt rgb\"green\""
ds.linewidth = 10
end
hole = Gnuplot::DataSet.new([*0..@last].map {|x| 0 }) do |ds|
ds.with = "lines lt rgb\"red\""
ds.linewidth = 5
end
plot.data << fix
plot.data << hole
plot.data << field
IRuby.display(plot)
end
end
end
def test(u, t)
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
# gnuplotの場合、配列の要素がxになるので、挟まないといけない
fix = Gnuplot::DataSet.new([600]) { |ds| ds.with = "lines lt rgb \"#ffffff\"" }
field = Gnuplot::DataSet.new([*0..@first].map {|x| 0 }) do |ds|
ds.with = "lines lt rgb\"green\""
ds.linewidth = 10
end
hole = Gnuplot::DataSet.new([*0..@last].map {|x| 0 }) do |ds|
ds.with = "lines lt rgb\"red\""
ds.linewidth = 5
end
ball = Gnuplot::DataSet.new(parabora(u, t)) do |ds|
ds.with = "lines lt rgb\"blue\""
ds.linewidth = 1
end
plot.data << fix
plot.data << hole
plot.data << field
plot.data << ball
t2 = t / 180.0 * Math::PI
last = u * Math.cos(t2) * (2 * u * Math.sin(t2) / 9.8)
if last.abs.between?(@first, @last)
plot.title '成功'
else
plot.title '残念'
end
IRuby.display(plot)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment