Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this task, we are applying the knowledge we get from GUI control flow and Gosu cycle. Modify a simple Ruby program to move a

In this task, we are applying the knowledge we get from GUI control flow and Gosu cycle.

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

Please provide a detailed response with the full code as the final answer. Inadequate answers will be reported.

image text in transcribed

Provided code:

gosu_major_cycle.rb:

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 ")

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 ")

@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_text("Cycle count: #{@cycle}", 10, 10, z = ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)

puts("2. In draw ")

end

end

window = GameWindow.new

window.show

In this task, we are applying the knowledge we get from GUI control flow and Gosu cycle. Modify a simple Ruby program to move a shape across the screen by modifying the tasks in the Gosu cycle and methods. Enhance the code provided as follows: Add a variable in the method called with the initial value of zero. Add code into the update() method that will add 10 to shape_ x ( to move our shape by increasing the X value X coordinate ) 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 we can use draw_rect function from the Gosu library to dray our Rectangle (Check this docu)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions