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

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

Provide the full Ruby code as the final answer. Any deviation or change from the request (the code has to work), will result in a report!

Existing code:

Shape_moving.rb:

require 'gosu'

module ZOrder

BACKGROUND, MIDDLE, TOP = *0..2

end

WIDTH = 400

HEIGHT = 500

SHAPE_DIM = 50

# Instructions:

# Fix the following code so that:

# 1. The shape also can be moved up and down

# 2. the shape does not move out of the window area

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(WIDTH, HEIGHT, false)

self.caption = "Shape Moving"

@shape_y = HEIGHT / 2

@shape_x = WIDTH / 2

end

# Put any work you want done in update

# This is a procedure i.e the return value is 'undefined'

def update

if button_down?(Gosu::KbRight)

if @shape_x != (WIDTH - SHAPE_DIM)

@shape_x += 3

end

end

if button_down?(Gosu::KbLeft) && (@shape_x != 0)

@shape_x -= 3

end

end

# Draw (or Redraw) the window

# This is procedure i.e the return value is 'undefined'

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

Modify a simple Ruby program to move a shape across the screen by changing the provided code in the Gosu cycle method. For more information about drawing the shape Method: Gosu.draw rect You must enhance the code provided as follows: Step 1: The shape also can be moved up and down. Step 2: The shape does not move out of the window area. Upload a screenshot of your program running to the workspace

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!