Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write [] = swars(player). player must be 1. Draw a ship wherever the mouse is positioned inside the window, such that the ship will move

Write [] = swars(player). player must be 1. Draw a ship wherever the mouse is positioned inside the window, such that the ship will move with the mouse. Next, modify swars such that the ship will no longer follow the mouse. Instead, when the right mouse button is clicked, the ship will move in a straight line from the ships present position to the location the mouse was when the right mouse button was clicked. The ship will halt when it reaches the spot where the right mouse button was clicked. The speed of movement is up to you, but should be slow enough that we can watch it move bit by bit across the screen. It shouldnt be so slow as to be boring, though, and this speed can be adjusted in the subsequent steps to make game play more interesting. Typing q while the mouse is in the window will cause the program to exit. You dont need to delete the window, but the ship should no longer respond to mouse clicks or motion after typing q.

attached is the code need to use while loop. and arrange the codes and add the transition

function [] = draw_object(mainAxis, object, mousePos)

%draw_object draws a graphics object on a set of axes

%Input arguments

% mainAxis - handle to axes object

% object - structure to the object to be drawn with the following fields

% outline - outline of the object

% patch - patch object for the object

% w - width of the object

% h - height of the object

% mousePos - 2 element vector containing [x y] of the mouse position

objectOutlinePos = [mousePos(1) - (object.w * 0.5), mousePos(2) - (object.h * 0.5)];

curobjectOutline = [object.outline(1,:) + objectOutlinePos(1); object.outline(2,:) + objectOutlinePos(2)];

set(object.patch, 'XData', curobjectOutline(1,:), 'YData', curobjectOutline(2,:));

function [mousePos] = get_mouse_position(mainAxis)

%get_mouse_position gets the current mouse position

%Input arguments:

% mainAxis - axes object

%Output arguments

% mousePos - 2 element vector containing: [x y]

tempMousePos = get(mainAxis,'CurrentPoint');

mousePos = [tempMousePos(1), tempMousePos(3)];

function [mainAxis, ship, axisTitle] = initialize_graphics()

%initialize_graphics creates the graphic environment

%Input arguments

% none

%Output arguments

% mainAxis - handle to the axes object

% ship - structure containing the following fields

% w - width of ship

% h - height of ship

% outline - outline of ship

% patch - patch object of ship

% handle to the axis title

SHIP_START_Y = 20; %hero start position

ship.w = 15; %hero width

ship.h = 20; %hero height

SHIP_SHAPE = [1 0 1 2 3 4 3 1; ... %x values

0 1 2 4 2 1 0 0]; %y values

xScale = ship.w / max(SHIP_SHAPE(1,:));

yScale = ship.h / max(SHIP_SHAPE(2,:));

%coordinats for drawing hero at 0,0.

%scale hero so that he's HERO_W wide and HERO_H tall

ship.outline = [SHIP_SHAPE(1,:) .* xScale; SHIP_SHAPE(2,:) .* yScale];

heroPos = [100 150];

fig = figure;

set(fig,'color','black');

set(fig,'Resize','off');

pointer=NaN(16,16);

pointer(4,1:7) = 2;

pointer(1:7,4)=2;

pointer(4,4)=1;

set(fig,'Pointer','Custom');

set(fig,'PointerShapeHotSpot',[4 4]);

set(fig,'PointerShapeCData',pointer);

set(fig,'KeyPressFcn',@keyDownListener);

set(fig,'WindowButtonDownFcn', @mouseDownListener);

set(fig,'WindowButtonUpFcn', @mouseUpListener);

set(fig,'WindowButtonMotionFcn', @mouseMoveListener);

mainAxis = axes();

%set color for the court, hide axis ticks.

AXIS_COLOR = [0, 0, 0]; %the sky

set(mainAxis, 'color', AXIS_COLOR, 'YTick', [], 'XTick', []);

%handle for displaying the score

axisTitle = title('');

font = 'Courier';

large_text = 20;

green = [.1, .7, .1];

title_color = green;

set(axisTitle,'fontsize', large_text)

%set(axisTitle, 'FontName', font,'fontsize', large_text);

set(axisTitle, 'Color', title_color);

%set size of the graphics window

axis([0 200 0 324]);

axis off;

ship.patch = patch(NaN,NaN,'b');

set(ship.patch,'LineWidth', 2);

set(ship.patch,'EdgeColor', 'red');

function keyDownListener(src,event)

global quitGame;

switch event.Key

case 'q'

quitGame = true;

end

end

function mouseDownListener(src, event)

switch event

case 1

fprintf('left mouse button pressed ');

case 2

fprintf('middle mouse button pressed ');

case 3

fprintf('right mouse button pressed ');

end

function mouseMoveListener(src, event)

%no body.

%this handler must be registerd for axis 'CurrentPoint' to be

%updated when mouse moves.

end

function print_title(axisTitle, titlestring)

%titlestring prints a string as an axis title

%Input arguments:

% axisTitle - handle to the axis title

% titlestring - the string to print

%Output arguments:

% none

set(axisTitle, 'String', titlestring);

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

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions