I am right now creating a snake game in ruby 2d and need a way for the computer to detect when the snake touches the apple.
I have found nothing on this topic as the ruby2d community is fairly small.
I am right now creating a snake game in ruby 2d and need a way for the computer to detect when the snake touches the apple.
I have found nothing on this topic as the ruby2d community is fairly small.
You are gonna want to write a function that returns true whenever snake has the same xy-coordinates as the apple.
Let's suppose your apple a
has member variables x
and y
. The snakes head s
also has its current x and y coordinates saved in its member variables x
and y
.
def hit(a, s)
return a.x == s.x && a.y == s.y
end
So in your gameloop always call hit(a, s)
and do what you need to do based on the boolean outcome of the function.