Class with Ruby/Shoes. please help!

Hello,

I´m working on a rect wie Ruby/Shoes, the rect should move right, left,
top and down. The methods should be in the Class Quadrat. This is what I
got so far, but I don´t know how to move on. I´m working on it since
hours, but nothing changes.

This is the methode I should involve also, but I don´t know where and
how:

Shoes.app do
@info = para “NO KEY is PRESSED.”
keypress do |k|
@info.replace “#{k.inspect} was PRESSED.”
end
end

And this is my code so far:

class Quadrat < Shoes::Widget
@x
@y
@a
@colour
@objekt

def initialize(x,y,a,colour)
@x=x
@y=y
@a =a
@colour=colour
@objekt=rect(x,y,a,a) #gibt Größe und Standpunkt des Quadrates an
end

def top ()
@y=@y-10 #verschiebt das Quadrat um 10 auf der Y-Achse nach oben
@objekt.move(@x,@y)
end

def down()
@y=@y+10 # verschiebt das Quadrat um 10 auf der y-Achse nach unten
@object.move(@x,@y)
end

def left()
@x=@x-10 # verschiebt das Quadrat um 10 auf der x-Achse nach links
@object.move(@x,@y)
end

def right()
@x=@x+10 #verschiebt das Quadrat um 10 auf der y-Achse nach rechts
@object.move(@x,@y)
end

end

Shoes.app width: 200, height: 200 do
Shoes.show_log
background black
a=Quadrat.new(100,150,30,green)

end

Johanna B. wrote in post #1133872:

Hello,

@objekt=rect(x,y,a,a) #gibt Größe und Standpunkt des Quadrates an
@objekt.move(@x,@y)

@object.move(@x,@y)
@x=@x-10 # verschiebt das Quadrat um 10 auf der x-Achse nach links
@object.move(@x,@y)

Objekt or object ? :slight_smile:

This one, perhaps :

Shoes.app width: 200, height: 200 do

def move() @object.move(@x,@y) end
def left() @x=[0,200,@x-10].sort[1] ; move end

keypress do |k| send(k) end
end

Thank you.

Now we are so far:

class Quadrat < Shoes::Widget
@x
@y
@a
@b
@farbe
@objekt

def initialize( x, y, a, b, farbe)
@x = x
@y = y
@a = a
@b = b
@farbe = farbe
fill farbe # Füllfarbe setzen
@objekt = rect( x, y, a, b)

end

def nach_oben ()
@y=@y-10
@objekt.move(@x,@y)
end
def nach_unten()
@y=@y+10
@objekt.move(@x,@y)
end
def nach_links()
@x=@x-10
@objekt.move(@x,@y)
end
def nach_rechts()
@x=@x+10
@objekt.move(@x,@y)
end
end

Shoes.app width: 200, height: 200 do
Shoes.show_log
background red

x = quadrat( 10, 10, 70, 20, green)

keypress do |k|
if (k == :right)
x.nach_rechts
end

keypress do |k|
if (k == :left)
x.nach_links
end

end
end

end

But if we press left or right, it only happens once. And if the rect is
out of the window it don´t come back.

What to do that it happens more times and i can always move the rect
from left to right and so on?

@x
@y

This don’t do anything…
initialize() is enough for create the Window instance

Shoes.app width: 200, height: 200 do
keypress do |k|
if (k == :right)
x.nach_rechts
end

keypress do |k|
if (k == :left)
x.nach_links
end
end
end
end

here you make mistake with do/end and if/end : you should indent
your block code.
You should use editor like notepad++ / sublime : they detect
bloc error., netbeans ruby editor do auto-indent.

Second keypress is a mistake : it’s seem that shoes memorize
only the last one. so this form should be ok :
keypress do |k|
if (k == :right)
x.nach_rechts
elsif (k == :left)
x.nach_links
end
end

But if we press left or right, it only happens once.

if error is raise on a shoes bloc, I think that shoes ‘desactive’
the handler so next left press do nothing…

Try green_shoes : it is compatible with red-shoes and is more ‘rubyist’

And if the rect is out of the window it don´t come back.
see my exemple for shrink a value : [min, (value+dx) ,max].sort[1]

Courage… Ruby is hard for beginning programming !

Thank you for you example. But I don´t understand it. Can you make it
with a real circumstance? I don´t also understand what dx means. And why
do I need the sort? So can you please give me an example for the left
key. Our Window has width: 500 height: 500

And if the rect is out of the window it don´t come back.
see my exemple for shrink a value : [min, (value+dx) ,max].sort[1]

This one, perhaps :

Shoes.app width: 200, height: 200 do

def move() @object.move(@x,@y) end
def left() @x=[0,200,@x-10].sort[1] ; move end

keypress do |k| send(k) end
end

ok, that work:

Shoes.app width: 200, height: 200 do
@x,@y,@object=100,100,rect(100,100,10,10)
def move(dx,dy)
@x=[0,180,@x+dx].sort[1]
@y=[0,180,@y+dy].sort[1]
@object.move(@x,@y)
end
def left() move(-10,0) end
def right() move(10,0) end
def up() move(0,-10) end
def down() move(0,10) end
keypress do |k| send(k) rescue alert($!.to_s) end
end

Ready for rrobots ? http://4r2r.github.io/opal-robots/#8499891vs8499895

Johanna B. wrote in post #1133999:

Thank you for you example. But I don´t understand it. Can you make it
with a real circumstance?

? it’s work…

I don´t also understand what dx means.
dx and dy are the displacement of the rectangle, projected on x and y
axes

And why do I need the sort?

you need limit the displacement for that x value are limited in 0…200
mini/maxi values.
the sort[1] is a joke for doing that

[0, 50,200].sort >> [0, 50,200] [1] >>> 50
[0,-50,200].sort >> [-50,0,200] [1] >>> 0
[0,250,200].sort >> [0,200,250] [1] >>> 200

or you can use :
x= a > ? 200 : a < 0 ? 0 : a

I use 180 in place of 200 because x,y are the top/left
corner of the rectangle

So can you please give me
an example for the left key. Our Window has width: 500 height: 500

[0,500,x].sort…

(sorry for my English, it’s difficult for me, I prefer ruby… :slight_smile: