Question: function archery _ game ( ) % Constants g = 9 . 8 1 ; % acceleration due to gravity ( m / s ^

function archery_game()
% Constants
g =9.81; % acceleration due to gravity (m/s^2)
% Set up figure
fig = figure('Position',[200,200,800,600]);
% Initialize archer position
archer_x =0;
archer_y =1; % Updated archer starting position
% Create sliders for angle and power
angle_slider = uicontrol('Style', 'slider', 'Min', 0, 'Max', 90, 'Value', 45,...
'Position', [100,50,300,20], 'Callback', @update_plot);
power_slider = uicontrol('Style', 'slider', 'Min', 1, 'Max', 20, 'Value', 10,...
'Position', [100,20,300,20], 'Callback', @update_plot);
% Create fire button
fire_button = uicontrol('Style', 'pushbutton', 'String', 'Fire', ...
'Position', [450,20,100,50], 'Callback', @fire_arrow);
% Set up axes
ax = axes('Parent', fig, 'Position', [0.1,0.2,0.8,0.7]);
axis(ax,[050020]); % Adjusted y-axis to accommodate the archer and target
xlabel(ax, 'Distance (m)');
ylabel(ax, 'Height (m)');
hold(ax,'on');
% Plot archer
archer_plot = plot(ax, archer_x, archer_y,'b.', 'MarkerSize', 20);
% Initialize arrow trajectory
arrow_plot = plot(ax, NaN, NaN, 'r-', 'LineWidth', 2);
% Play three rounds
for round =1:3
% Generate random target position
target_x = randi([10,40]);
target_y =1; % Place target on ground level
target_radius =0.5;
% Plot target
target_plot = viscircles(ax,[target_x, target_y], target_radius, 'EdgeColor', 'r');
% Update plot based on slider values
update_plot();
% Wait for user input
uiwait(fig);
% Check if the archer hits the target
if check_hit(archer_x, archer_y, target_x, target_y, target_radius)
disp('You hit the target!');
else
disp('You missed the target.');
end
% Remove target plot
delete(target_plot);
end
% Close figure
close(fig);
% Update plot based on slider values
function update_plot(~, ~)
% Get angle and power from sliders
angle = get(angle_slider, 'Value');
power = get(power_slider, 'Value');
% Calculate trajectory
time = linspace(0,2*power*sind(angle)/g,100); % Assuming g =9.81 m/s^2
xTrajectory = archer_x + power * cosd(angle)* time;
yTrajectory = archer_y + power * sind(angle)* time -0.5* g * time.^2;
% Update arrow trajectory
set(arrow_plot, 'XData', xTrajectory, 'YData', yTrajectory);
end
% Fire the arrow
function fire_arrow(~, ~)
% Get angle and power from sliders
angle = get(angle_slider, 'Value');
power = get(power_slider, 'Value');
% Calculate trajectory
time = linspace(0,2*power*sind(angle)/g,100); % Assuming g =9.81 m/s^2
xTrajectory = archer_x + power * cosd(angle)* time;
yTrajectory = archer_y + power * sind(angle)* time -0.5* g * time.^2;
% Update arrow trajectory
set(arrow_plot, 'XData', xTrajectory, 'YData', yTrajectory);
% Enable next round
uiresume(fig);
end
% Check if the archer hits the target
function hit = check_hit(archer_x, archer_y, target_x, target_y, target_radius)
% Calculate distance between archer and target
distance = sqrt((archer_x - target_x)^2+(archer_y - target_y)^2);
% Check if the distance is less than or equal to the target radius
hit = distance <= target_radius;
end
end
(Above is a code for an archery game. The problem is when I shoot the arrow at the target with the trajectory lined up, it doesnt count as a hit. No matter how close or even on the target the arrow trajectory is, it misses and I need it to properly tell the difference between a hit and a miss.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!