Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

This is a ruby programming.

anyone help me with giving proper explanation step by step for this coding. I need explanations for each lines of coding. I am a beginner for programming.

Here is the question
Gosu Major 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.

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.


What I have tried:

My coding in text format

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 < Gosu::Window
# 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"
@shape_x = 0
# 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)
@shape_x +=10
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"
Gosu.draw_rect(@shape_x, 30, 100, 50, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
end
end

window = GameWindow.new
window.show
Posted
Updated 20-Sep-22 22:00pm

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Comments
Member 15773088 21-Sep-22 5:18am    
If you cant answer try to help by providing useful resources, don't answer in unprofessional manner
Richard MacCutchan 21-Sep-22 7:01am    
OriginalGriff is one of the people here who certainly could answer. But as he explains, it would take far too long. Your question is impossible to answer in a Quick Answers forum. So he is not being unprofessional, just realistic.
 
Share this answer
 
Comments
Member 15773088 21-Sep-22 5:16am    
Thank you for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900