Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions