Question: Modify a simple Ruby program to move a shape across the screen by modifying the tasks in the Gosu cycle update ( ) and draw

Modify a simple Ruby program to move a shape across the screen by modifying the tasks in the Gosu cycle update() and draw() methods.
Enhance the code provided as follows:
Add a variable in the initialize() method called shape_x with the initial value of zero.
Add code into the update() method that will add 10 to shape_x.
Add code into the draw() method that will draw a shape (square or circle of any visible colour) at the y coordinate of 30 and x coordinate of shape_x.
please fix the following code:
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP =*0..2
end
class GameWindow Gosu::Window
def initialize
super(200,135, false)
self.caption = "Gosu Cycle Example"
@background_image = Gosu::Image.new("media/earth.png")
@font = Gosu::Font.new(20)
@cycle =0
@shape_x =0 # Add variable to track shape x-coordinate
end
def update
@cycle +=1
@shape_x +=10 # Add code to increment shape x-coordinate
end
def draw
@background_image.draw(0,0, ZOrder::BACKGROUND)
@font.draw_text("Cycle count: #{@cycle}",10,10, ZOrder::TOP, 1.0,1.0, Gosu::Color::BLACK)
# Add code to draw shape at y=30 and x=shape_x
shape_image = load_shape_image("media/square.png")
shape_image.draw(@shape_x,30, ZOrder::TOP, 1,1, Gosu::Color::GREEN) if shape_image
end
def load_shape_image(image_path)
begin
Gosu::Image.new(image_path)
rescue => exception
puts "Error loading image: #{exception.message}"
return nil
end
end
end
window = GameWindow.new
window.show The GOSU cycle can also be
represented as:
Window Class
initialize()
update()
Thece run until the game stops
draw()
 Modify a simple Ruby program to move a shape across the

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!