Forum: Ruby My car is "falling" off the screen green shoes

64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (rubystudent66)
on 2013-06-05 18:09
(Received via mailing list)
Team,

I am having so much fun with shoes!!!!!!!!!!!!!!!!

But, I have a problem. I finally made my car image moves from left to
right. But it reaches the end of the screen on the right and it
disappears.
Can anyone help me keep my car return back to the starting pint, which
is
the left bottom of the screen? I want for that to happen continuously.
The
following is the actual code I'm using:


    @carim = image  "/home/rstudent/MyData/Programs/shoes/car1.png"
    every 6 do
       animate do |i|
          @carim.left += (-20..50).rand
       end
    end

Any help will be appreciated.
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (martin_d)
on 2013-06-06 00:08
(Received via mailing list)
Hint: Check out the mod function (%). For instance, if you have a
screen of width 20, a car at position 10, and it moves 15 units in
either direction:

irb(main):001:0> a = 10
=> 10
irb(main):002:0> b = a + 15
=> 25  # <--- gone off the right edge
irb(main):003:0> c = (a + 15) % 20
=> 5  # <--- wrapped around
irb(main):004:0> d = a - 15
=> -5  # <--- gone off the left edge
irb(main):005:0> e = (a - 15) % 20
=> 15  # <--- wrapped around

martin
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (rubystudent66)
on 2013-06-06 15:15
(Received via mailing list)
Hi Martin,

I defined my screen as "fullscreen" and I don't know how to tell the
actual
size in order to control the car movements. I have the following:
*Shoes.app fullscreen: true, :title => "VRMQMon", :resizable => true  do
*

   1. How does one determine the size of the screen while in full
screen?
   2. How can I tell when the car reached the edges?


Thank you
689dae2a2a14404ed27048633f472147?d=identicon&s=25 ashbb shoeser (satoshi_a)
on 2013-06-06 15:56
(Received via mailing list)
Hi Ruby Student,

> I am having so much fun with shoes!!!!!!!!!!!!!!!!
Fantastic! Glad to hear that. :-D

Okay, try out the following. ;-)

require 'green_shoes'
Shoes.app do
  @carim = image "/home/rstudent/MyData/Programs/shoes/car1.png"
  animate do
    @carim.move (@carim.left + 5) % width, @carim.top
  end
end

ashbb
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2013-06-06 20:51
and then, a little physics


==========================================================
require 'green_shoes'

def inside(a,m,b) m<a ? a : (m> b ? b : m) end
def inside?(a,m,b)  inside(a,m,b)==m end

Shoes.app :width => 600,:height=> 100 do
  info= para ""
  @carim = image "car.png" #!!!!!

  speed=10
  acceleration=0.0
  pos=300

  h={"Left" => -0.3 , "Right" => +0.4 }

  keypress do |k|
    acceleration = inside(-10, (acceleration+(h[k] || 0)) ,+10)
  end

  animate do
    speed+=acceleration
    speed*=0.97
    acceleration=0
    speed *= -1 unless inside?(0,pos+speed,width-@carim.width)
    pos=pos+speed
    speed=inside(-10,speed,+10)

    @carim.move pos, 100-@carim.height
    info.replace "  Speed=%5.2f " %[speed]
  end
end
==========================================================
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (rubystudent66)
on 2013-06-06 21:10
(Received via mailing list)
This also works very nicely!

Thank you very much for taking the time to answer my post.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.