Guest User

Untitled

a guest
Mar 13th, 2011
543
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'rubygems'
  2. require 'gosu'
  3. WIDTH = 1366
  4. HEIGHT = 768
  5. class Point
  6.   attr_accessor :x, :y, :dx, :dy
  7.   def initialize(x, y, vx, vy, win)
  8.     @x = x
  9.     @y = y
  10.     @ox = x - vx
  11.     @oy = y - vy
  12.   end
  13.   def update
  14.     @dx = @x - @ox
  15.     @dy = @y - @oy + 0.1
  16.     @ox = @x
  17.     @oy = @y
  18.     @x = @x + @dx
  19.     @y = @y + @dy
  20.     if @y > HEIGHT - 10
  21.       @y = HEIGHT - 10
  22.       @dx = @x-@ox
  23.             @dx *= 0.5
  24.             @ox = @ox + @dx
  25.     end
  26.     if @y < 10
  27.       @y = 10
  28.       @dx = @x-@ox
  29.             @dx *= 0.5
  30.             @ox = @ox + @dx
  31.     end
  32.     if @x > WIDTH - 10
  33.       @x = WIDTH - 10
  34.       @dy = @y-@oy
  35.             @dy *= 0.5
  36.             @oy = @oy + @dy
  37.     end
  38.     if @x < 10
  39.       @x = 10
  40.       @dy = @y-@oy
  41.             @dy *= 0.5
  42.             @oy = @oy + @dy
  43.     end
  44.   end
  45. end
  46.  
  47. class Contact
  48.   def initialize(p1, p2,win)
  49.     @p1 = p1
  50.     @p2 = p2
  51.     @win = win
  52.     @length = Math.sqrt((@p1.x-@p2.x)**2 + (@p1.y-@p2.y)**2)
  53.   end
  54.   def update
  55.     3.times{
  56.     dist = Math.sqrt((@p1.x-@p2.x)**2 + (@p1.y-@p2.y)**2)
  57.         diff = dist - @length
  58.         dx = @p1.x-@p2.x
  59.         dy = @p1.y-@p2.y
  60.         if @length > 0
  61.             diff = diff / @length
  62.     else
  63.             diff = 0
  64.     end
  65.  
  66.         dx *= 0.15
  67.         dy *= 0.15
  68.  
  69.         @p1.x -= diff*dx
  70.         @p1.y -= diff*dy
  71.  
  72.         @p2.x += diff*dx
  73.         @p2.y += diff*dy
  74.     }
  75.   end
  76.   def draw
  77.     @win.draw_line(@p1.x,@p1.y,Gosu::white,@p2.x,@p2.y,Gosu::white)
  78.   end
  79. end
  80.  
  81. class GameWindow < Gosu::Window
  82.   def initialize
  83.     super(1366, 768, true)
  84.     self.caption = "Physics"
  85.     game = 'snake'
  86.     if game == 'snake'
  87.     @points, @contacts = [] ,[]
  88.     10.times{|i|
  89.     @points.push(Point.new(200 - 14*i,50,0,0,self))
  90.     @contacts.push(Contact.new(@points[i-1],@points[i],self))
  91.     }
  92.     elsif game == 'man'
  93.       @points = [
  94.         Point.new(200,80,0,0,self),
  95.         Point.new(180,100,0,0,self),
  96.         Point.new(220,100,0,0,self),
  97.         Point.new(200,120,0,0,self),
  98.         Point.new(200,140,0,0,self),
  99.         Point.new(165,170,0,0,self),
  100.         Point.new(235,170,0,0,self),
  101.         Point.new(165,240,0,0,self),
  102.         Point.new(230,240,0,0,self),
  103.         Point.new(165,285,0,0,self),
  104.         Point.new(165,320,0,0,self),
  105.         Point.new(230,285,0,0,self),
  106.         Point.new(230,320,0,0,self),
  107.         Point.new(140,180,0,0,self),
  108.         Point.new(120,210,0,0,self),
  109.         Point.new(260,180,0,0,self),
  110.         Point.new(280,210,0,0,self)
  111.         ]
  112.       @contacts = [
  113.         Contact.new(@points[0],@points[1],self),
  114.         Contact.new(@points[2],@points[1],self),
  115.         Contact.new(@points[0],@points[3],self),
  116.         Contact.new(@points[0],@points[2],self),
  117.         Contact.new(@points[3],@points[1],self),
  118.         Contact.new(@points[3],@points[2],self),
  119.         Contact.new(@points[4],@points[3],self),
  120.         Contact.new(@points[5],@points[4],self),
  121.         Contact.new(@points[5],@points[6],self),
  122.         Contact.new(@points[6],@points[4],self),
  123.         Contact.new(@points[5],@points[7],self),
  124.         Contact.new(@points[7],@points[8],self),
  125.         Contact.new(@points[8],@points[6],self),
  126.         Contact.new(@points[6],@points[7],self),
  127.         Contact.new(@points[5],@points[8],self),
  128.         Contact.new(@points[4],@points[7],self),
  129.         Contact.new(@points[4],@points[8],self),
  130.         Contact.new(@points[9],@points[7],self),
  131.         Contact.new(@points[10],@points[9],self),
  132.         Contact.new(@points[11],@points[8],self),
  133.         Contact.new(@points[12],@points[11],self),
  134.         Contact.new(@points[13],@points[5],self),
  135.         Contact.new(@points[14],@points[13],self),
  136.         Contact.new(@points[15],@points[6],self),
  137.         Contact.new(@points[16],@points[15],self)
  138.         ]
  139.     end
  140.   end
  141.  
  142.   def update
  143.     @points.each{|p| p.update}
  144.     @contacts.each{|c| c.update}
  145.     if button_down? Gosu::Button::KbUp then
  146.       @points[0].y -= 1.4
  147.     end
  148.     if button_down? Gosu::Button::KbLeft then
  149.       @points[0].x -= 0.4
  150.     end
  151.     if button_down? Gosu::Button::KbRight then
  152.       @points[0].x += 0.4
  153.     end
  154.   end
  155.  
  156.   def draw
  157.     @contacts.each { |c|
  158.      c.draw
  159.     }
  160.   end
  161. end
  162.  
  163. window = GameWindow.new
  164. window.show
RAW Paste Data