Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

In this task, you are provided with the Ruby source code for a version of the Food Hunter program. You are required to extend that

In this task, you are provided with the Ruby source code for a version of the Food Hunter program. You are required to extend that program to implement the changes specified below.

The following steps will guide you to complete this task.

image text in transcribed

image text in transcribed

Foodhunter.rb :

# Encoding: UTF-8

require 'rubygems'

require 'gosu'

# Create some constants for the screen width and height

# The following determines which layers things are placed on on the screen

# background is the lowest layer (drawn over by other layers), user interface objects are highest.

module ZOrder

BACKGROUND, FOOD, PLAYER, UI = *0..3

end

# Note: There is one class for each record in the Pascal Food Hunter Game

class Hunter

attr_accessor :score, :image, :yuk, :yum, :hunted, :hunted_image, :vel_x, :vel_y, :angle, :x, :y, :score

def initialize(hunted)

@image = Gosu::Image.new("media/Hunter.png")

@yuk = Gosu::Sample.new("media/Yuk.wav")

@yum = Gosu::Sample.new("media/Yum.wav")

@hunted = hunted # default

@hunted_image = Gosu::Image.new("media/SmallIcecream.png")

@vel_x = @vel_y = 3.0

@x = @y = @angle = 0.0

@score = 0

end

end

def set_hunted(hunter, hunted)

hunter.hunted = hunted

case hunted

when :chips

hunted_string = "media/" + "SmallChips.png"

when :icecream

hunted_string = "media/" + "SmallIcecream.png"

when :burger

hunted_string = "media/" + "SmallBurger.png"

when :pizza

hunted_string = "media/" + "SmallPizza.png"

end

hunter.hunted_image = Gosu::Image.new(hunted_string)

end

def warp(hunter, x, y)

hunter.x, hunter.y = x, y

end

def move_left hunter

hunter.x -= hunter.vel_x

hunter.x %= 640

end

def move_right hunter

hunter.x += hunter.vel_x

hunter.x %= 640

end

def move_up hunter

hunter.y -= hunter.vel_y

hunter.y %= 480

end

def move_down hunter

hunter.y += hunter.vel_y

hunter.y %= 480

end

def draw_hunter hunter

hunter.image.draw_rot(hunter.x, hunter.y, ZOrder::PLAYER, hunter.angle)

hunter.hunted_image.draw_rot(hunter.x, hunter.y, ZOrder::PLAYER, hunter.angle)

end

def collect_food(all_food, hunter)

all_food.reject! do |food|

if Gosu.distance(hunter.x, hunter.y, food.x, food.y)

if (food.type == hunter.hunted)

hunter.score += 1

hunter.yum.play

else

hunter.score += -1

hunter.yuk.play

end

true

else

false

end

end

end

class Food

attr_accessor :x, :y, :type, :image, :vel_x, :vel_y, :angle, :x, :y, :score

def initialize(image, type)

@type = type;

@image = Gosu::Image.new(image);

@vel_x = rand(-2 .. 2) # rand(1.2 .. 2.0)

@vel_y = rand(-2 .. 2)

@angle = 0.0

# replace hard coded values with global constants:

@x = rand * 640

@y = rand * 480

@score = 0

end

end

def move food

food.x += food.vel_x

food.x %= 640

food.y += food.vel_y

food.y %=480

end

def draw_food food

food.image.draw_rot(food.x, food.y, ZOrder::FOOD, food.angle)

end

class FoodHunterGame

def initialize

# replace hard coded values with global constants:

super 640, 480

self.caption = "Food Hunter Game"

@background_image = Gosu::Image.new("media/space.png", :tileable => true)

@all_food = Array.new

# Food is created later in generate-food

@player = Hunter.new(:icecream)

warp(@player, 320, 240)

@font = Gosu::Font.new(20)

end

def update

# For key mappings see https://www.libgosu.org/cppamespace_gosu.html#enum-members

if Gosu.button_down? Gosu::KB_LEFT or Gosu.button_down? Gosu::GP_LEFT

move_left @player

end

if Gosu.button_down? Gosu::KB_RIGHT or Gosu.button_down? Gosu::GP_RIGHT

move_right @player

end

if Gosu.button_down? Gosu::KB_UP or Gosu.button_down? Gosu::GP_BUTTON_0

move_up @player

end

if Gosu.button_down? Gosu::KB_DOWN or Gosu.button_down? Gosu::GP_BUTTON_9

move_down @player

end

@all_food.each { |food| move food }

self.remove_food

collect_food(@all_food, @player)

# the following will generate new food randomly as update is called each timestep

if rand(100)

@all_food.push(generate_food)

end

# change the hunted food randomly:

if rand(400) == 0

change = rand(4)

case change

when 0

set_hunted(@player, :icecream)

when 1

set_hunted(@player, :chips)

when 2

set_hunted(@player, :burger)

when 3

set_hunted(@player, :pizza)

end

end

end

def draw

@background_image.draw(0, 0, ZOrder::BACKGROUND)

draw_hunter @player

@all_food.each { |food| draw_food food }

@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, Gosu::Color::YELLOW)

end

def generate_food

case rand(4)

when 0

Food.new("media/Chips.png", :chips)

when 1

Food.new("media/Burger.png", :burger)

when 2

Food.new("media/Icecream.png", :icecream)

when 3

Food.new("media/Pizza.png", :pizza)

end

end

def remove_food

@all_food.reject! do |food|

# Replace the following hard coded values with global constants:

if food.x > 640 || food.y > 480 || food.x

true

else

false

end

end

end

def button_down(id)

if id == Gosu::KB_ESCAPE

close

end

end

end

FoodHunterGame.new.show if __FILE__ == $0

Build upon the previous task with this and the following images

image text in transcribed

image text in transcribed

Please code these tasks in Ruby and provide an adequate response. You will be reported if the response is not satisfactory.

In this task, you are provided with the Ruby source code for a version of the Food Hunter program. You are required to extend that program to implement the changes specified below. The following steps will guide you to complete this task. Step 1: Download the Ruby Food Hunter starter code (attached below). Step 2: Make the changes as specified below to the provided code. Specification Changes: 1. Food objects that appears on the screen should randomly be selected to change direction. When selected for changing direction the image for the food item should momentary displaylflash the 'smoke.png' icon (see the media folder in the Resources) long enough for the user to briefly see it before the food item/object changes its movement to a random selected direction. As the programmer you will need to experiment to determine what is an appropriate frequency of selection in relation to good game play. Note: You will probably need to use the modulus operator (\%) and the method: Gosu.milliseconds. 2. Add global constants SCREEN_HElGHT and SCREEN_WIDTH, change the screen dimensions to 800600. Make sure everything works as it should. Watch the video (Mitchell 2021) for more details. Step 3: Save your code using the name: Resources See the following resources to help with this task. Depending on what you'd like your custom program to do, select the relevant sections of the Pine (2009) and Sobkowicz (2015) books to inform your design: - Learn to program (Pine 2009) - Learn game programming with Ruby: Bring your ideas to life with Gosu (Sobkowicz 2015). - Ruby tutorial (Barbalho 2019) - Comprehensive Ruby_programming_(edutechional 2016). - This video gives you an introduction to a free Ruby programming course from DevCamp (DevCamp2020). Step 4: Submit the following to the workspace: 1. Your code. 2. Your screenshot of your code running. It should look something like the following: You are now close to completing tasks related to all of the unit learning outcomes and can work toward demonstrating these in your own program. Aim to create something of at least the complexity of the original Food Hunter program for the lower distinction grade or more complex for higher grades. Specifically, it should: Demonstrate the use of functional decomposition - implement the program with a number of functions and procedures. (Maybe even modular decomposition with separate units if you can identify some reusable artefacts - optional but nice) Demonstrate the use of arrays and records Demonstrate the use of structured programming (sequence, selection, and repetition) Demonstrate appropriate use coding conventions - case, indentation It must not use global variables or goto. Make sure you can explain your code in an interview! Use the checklist on the next page to make sure you have everything you need to submit! You are now close to completing tasks related to all of the unit learning outcomes, and can work toward demonstrating these in your own program. Aim to create something of at least the complexity of the original Food Hunter program for the lower distinction grade or more complex for higher grades. Specifically, it should: MarkingRubricSummary(4)-1.pdf Demonstrate the use of functional decomposition - implement the program with a number of functions and procedures. (Maybe even modular decomposition with separate units if you can identify some reusable artefacts - optional but nice) Demonstrate the use of arrays and records Demonstrate the use of structured programming (sequence, selection, and repetition) Demonstrate appropriate use coding conventions - case, indentation It must not use global variables or goto. Make sure you can explain your code in an interview! Use the checklist on the next page to make sure you have everything you need to submit! Note: Your program should be different from the food hunter program and the lecture demonstration programs. You want to demonstrate that you have learnt from these tasks and can apply what you have learnt to some other program design. If you are aiming for a High Distinction, review the related High Distinction Project document and check the marking rubrics for detalls on how you can ensure this program meets the HD requirements

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions