def check_collisions(player, object)
left1 = player.x
left2 = object.x
right1 = player.x + player.width
right2 = object.x + object.width
top1 = player.y
top2 = object.y
bottom1 = player.y + player.height
bottom2 = object.y + object.height
if bottom1 < top2
false
return
end
if top1 > bottom2
false
return
end
if right1 < left2
false
return
end
if left1 > right2
false
return
end
true
end
end
def update
move_x = move_y = 0
move_x += 3 if button_down? Gosu::KbRight
move_x -= 3 if button_down? Gosu::KbLeft
move_y += 3 if button_down? Gosu::KbDown
move_y -= 3 if button_down? Gosu::KbUp
if check_collisions(@player, @pot_image)
move_x = move_y = 0
end
@player.move(move_x, move_y)
end
if bottom1 < top2
return false
end
if top1 > bottom2
return false
end
if right1 < left2
return false
end
if left1 > right2
return false
end
return true
x
or y
; rather, your objects have x
and y
methods and you are drawing an image relative to that. Because of how draw_rot
works, x
and y
are at the centre of the image of each object.move_x
and move_y
into check_collision
, which are the new values, but instead check_collision
uses the current position values.
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill