Question: Modify a simple Ruby program to move a shape across the screen by changing the provided code in the Gosu cycle update ( ) method.

Modify a simple Ruby program to move a shape across the screen by changing the provided code in the Gosu cycle update() method.
Use the code provided to get started.
You must enhance the code provided as follows:
1. The shape also can be moved up and down
2. The shape does not move out of the window area
Upload a screenshot of your program running to the workspace.
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP =*0..2
end
WIDTH =400
HEIGHT =500
SHAPE_DIM =50
class GameWindow < Gosu::Window
def initialize
super(WIDTH, HEIGHT, false)
self.caption = "Shape Moving"
@shape_y = HEIGHT /2
@shape_x = WIDTH /2
end
def update
if button_down?(Gosu::KbRight) && @shape_x <(WIDTH - SHAPE_DIM)
@shape_x +=3
end
if button_down?(Gosu::KbLeft) && @shape_x >0
@shape_x -=3
end
if button_down?(Gosu::KbUp) && @shape_y >0
@shape_y -=3
end
if button_down?(Gosu::KbDown) && @shape_y <(HEIGHT - SHAPE_DIM)
@shape_y +=3
end
end
def draw
Gosu.draw_rect(@shape_x, @shape_y, SHAPE_DIM, SHAPE_DIM, Gosu::Color::RED, ZOrder::TOP, mode = :default)
end
end
window = GameWindow.new
window.show

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!