###Shape Moving Modify the provided code in the Gosu cycle update() method. ####Instruction
Use the code provided (from this task’s resources in Doubtfire) to get started.
You must enhance the code provided as follows:
1.The shape also can be moved up and down
2.The shape does not move out of the window area
A.
◈Fix the following code so that: The shape also can be moved up and down the shape does not move out of the window area.
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP = *0..2
end
WIDTH = 400
HEIGHT = 500
SHAPE_DIM = 50
class GameWindow < Gosu::Window
def initialize
end
def update
end
def draw
end
end
window = GameWindow.new
window.show
B.
◈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"
@HEIGHT = 30
@WIDTH = 80
# Create and load a font for drawing text on the screen
@font = Gosu::Font.new(20)
@cycle = 0
@shape_y = HEIGHT / 2
@shape_x = WIDTH / 2
end
C.
◈Put any work you want done in update. ◈His is a procedure i.e the return value is ‘undefined’.
def update
@cycle += 1
sleep(0.1) # give a break
if button_down?(Gosu::KbRight)
if @shape_x != (WIDTH - SHAPE_DIM)
@shape_x += 10
puts "Right button has pressed"
end
end
if button_down?(Gosu::KbLeft) && (@shape_x != 0)
@shape_x -= 10
puts "Left button has pressed"
end
if button_down?(Gosu::KbUp) && (@shape_y != 0 )
@shape_y -= 10
puts "Up button has pressed"
end
if button_down?(Gosu::KbDown)
if @shape_y != (HEIGHT - SHAPE_DIM)
@shape_y += 10
puts "Down button has pressed."
end
end
end
D.
◈Draw (or Redraw) the window. ◈This is procedure i.e the return value is ‘undefined’.
def draw
@font.draw("Cycle count: #{@cycle}", 10, 10, z = ZOrder::TOP, 1.0, 1.0, Gosu::Color::WHITE)
Gosu.draw_rect(@shape_x, @shape_y, SHAPE_DIM, SHAPE_DIM, Gosu::Color::RED, ZOrder::TOP, mode=:default)
end
E.
◈Entire code ::
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP = *0..2
end
WIDTH = 400
HEIGHT = 500
SHAPE_DIM = 50
class GameWindow < Gosu::Window
def initialize
super WIDTH, HEIGHT, false
self.caption = "Shape Moving"
@HEIGHT = 30
@WIDTH = 80
@font = Gosu::Font.new(20)
@cycle = 0
@shape_y = HEIGHT / 2
@shape_x = WIDTH / 2
end
def update
@cycle += 1
sleep(0.1)
if button_down?(Gosu::KbRight)
if @shape_x != (WIDTH - SHAPE_DIM)
@shape_x += 10
puts "Right button has pressed"
end
end
if button_down?(Gosu::KbLeft) && (@shape_x != 0)
@shape_x -= 10
puts "Left button has pressed"
end
if button_down?(Gosu::KbUp) && (@shape_y != 0 )
@shape_y -= 10
puts "Up button has pressed"
end
if button_down?(Gosu::KbDown)
if @shape_y != (HEIGHT - SHAPE_DIM)
@shape_y += 10
puts "Down button has pressed."
end
end
end
def draw
@font.draw("Cycle count: #{@cycle}", 10, 10, z = ZOrder::TOP, 1.0, 1.0, Gosu::Color::WHITE)
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