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...

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. Use the code provided to get started. You also need to download the media folder with its contents. require 'gosu' module ZOrder BACKGROUND, MIDDLE, TOP = *0..2 end # Instructions: # Add a shape_x variable in the following (similar to the cycle one) # that is initialised to zero then incremented by 10 in update. # Change the draw method to draw a shape (circle or square) # (50 pixels in width and height) with a y coordinate of 30 # and an x coordinate of shape_x. class GameWindow # initialize creates a window with a width an a height # and a caption. It also sets up any variables to be used. # This is procedure i.e the return value is 'undefined' def initialize super(200, 135, false) self.caption = "Gosu Cycle Example" # Create and load an image to display @background_image = Gosu::Image.new("media/earth.png") # Create and load a font for drawing text on the screen @font = Gosu::Font.new(20) @cycle = 0 puts("0. In initialize\n") end # Put any work you want done in update # This is a procedure i.e the return value is 'undefined' def update puts("1. In update. Sleeping for one second\n") @cycle += 1 # add one to the current value of cycle sleep(1) end # the following method is called when you press a mouse # button or key def button_down(id) puts("In Button Down " + id.to_s) end # Draw (or Redraw) the window # This is procedure i.e the return value is 'undefined' def draw # Draws an image with an x, y and z #(z determines if it sits on or under other things that are drawn) @background_image.draw(0, 0, z = ZOrder::BACKGROUND) @font.draw("Cycle count: #{@cycle}", 10, 10, z = ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK) puts("2. In draw\n") end end window = GameWindow.new window.show
May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here