我正在使用 subplot() 和 pcolor(zeros(7,7)); 為戰艦游戲生成下圖(請參見下面的鏈接)。我正在嘗試更改特定網格的顏色來表示船只,但我無法弄清楚,所以我可以請幫助嗎,例如,我需要將“您的董事會”中的網格 22 和 23 更改為紅色并具有更新現有數字,而不是獲得新數字。

uj5u.com熱心網友回復:
如果您設定一個函式來創建/著色特定方塊是最簡單的,那么這是微不足道的,并且可以在整個游戲中回圈使用。下面我制作了區域函式drawSquare(bs,N,player,color),它接受棋盤大小 ( bs)、平方數 ( N)、玩家 ( 'top'or 'bottom') 和顏色,可以是任何有效的 MATLAB 
figure(100);
bs = 6; % board size
% Initialise the board
setup( bs );
% Example usage colouring in specific squares
drawSquare( bs, 3, 'top', 'r' )
drawSquare( bs, 4, 'top', [0.6,0.5,0.5] );
function setup( bs )
clf; % clear the board
for ii = 1:(bs^2)
% Loop over all squares, make the base boards
drawSquare( bs, ii, 'top', 'w' );
drawSquare( bs, ii, 'bottom', 'w' );
end
% Format the plots without ticks and with titles
subplot( 2, 1, 1 );
set( gca, 'XTickLabel', '', 'YTickLabel', '', 'BoxStyle', 'full' );
title( 'Computer Board' );
subplot( 2, 1, 2 );
set( gca, 'XTickLabel', '', 'YTickLabel', '', 'BoxStyle', 'full' );
title( 'Your Board' );
end
function drawSquare( bs, N, player, color )
% sq = square size, N = square number, player = 'top' or 'bottom'
% color = valid plot colour for different square types
x = mod(N-1,bs); % x axis in grid
y = floor( (N-1)/bs ); % y axis in grid
if strcmpi( player, 'top' )
subplot( 2, 1, 1 ); % top player is subplot 1
x = bs - x; % top player square 1 is bottom-right
else
subplot( 2, 1, 2 ); % bottom player is subplot 2
y = bs - y; % bottom player square 1 is top-left
end
xp = [x, x 1, x 1, x]; % x coordinates of grid square
yp = [y, y, y 1, y 1]; % y coordinates of grid square
patch( xp, yp, color ); % use patch to make coloured square
text( x (1/2), y (1/2), num2str(N), 'HorizontalAlignment', 'center' );
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398946.html
下一篇:y'=y^2 1的數值解題
