【游戏】基于 matlab GUI lanchester作战模拟设计【Matlab 304期】

一、简介

1 Lanchester战争模型
第一次世界大战期间,英国工程师Lanchester针对战争问题建模,以预测战争的结果。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、源代码

function varargout = lcst(varargin)
% LCST M-file for lcst.fig
%      LCST, by itself, creates a new LCST or raises the existing
%      singleton*.
%
%      H = LCST returns the handle to a new LCST or the handle to
%      the existing singleton*.
%
%      LCST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LCST.M with the given input arguments.
%
%      LCST('Property','Value',...) creates a new LCST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before lcst_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to lcst_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help lcst

% Last Modified by GUIDE v2.5 26-Oct-2008 20:07:52

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @lcst_OpeningFcn, ...
                   'gui_OutputFcn',  @lcst_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{
    
    1})
    gui_State.gui_Callback = str2func(varargin{
    
    1});
end

if nargout
    [varargout{
    
    1:nargout}] = gui_mainfcn(gui_State, varargin{
    
    :});
else
    gui_mainfcn(gui_State, varargin{
    
    :});
end
% End initialization code - DO NOT EDIT

% --- Executes just before lcst is made visible.
function lcst_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to lcst (see VARARGIN)
axes(handles.axes2);
[x1,map]=imread('1.jpg');
[x2,map]=imread('2.jpg');
x3=imread('3.jpg');
 x4=imread('4.jpg');
x5=imread('5.jpg');
x6=imread('6.jpg');
x7=imread('7.jpg');
x8=imread('8.jpg');x9=imread('9.jpg');x10=imread('10.jpg');x11=imread('11.jpg');
M(1)=im2frame(x1,map);
M(2)=im2frame(x2,map);M(3)=im2frame(x3,map);
M(4)=im2frame(x4,map);M(5)=im2frame(x5,map);
M(6)=im2frame(x6,map);M(7)=im2frame(x7,map);
M(8)=im2frame(x8,map);M(9)=im2frame(x9,map);
M(10)=im2frame(x10,map);
M(11)=im2frame(x11,map);
movie(M,1000,3)
% Choose default command line output for lcst
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so window can get raised using lcst.


% UIWAIT makes lcst wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = lcst_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{
    
    1} = handles.output;

% --- Executes on button press in simulation.
function simulation_Callback(hObject, eventdata, handles)
% hObject    handle to simulation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
cla;
x0=str2double(get(handles.red1,'String'));
 a=str2double(get(handles.red2,'String'));
 y0=str2double(get(handles.blue1,'String'));
 b=str2double(get(handles.blue2,'String'));
 
%  for k=1:100
%      t(k)=et/100*k
%         x(k)=x0-a*t(k);
%         y(k)=y0-b*t(k);
%  end
popup_sel_index = get(handles.popupmenu1, 'Value');%根据不同的定律进行计算

switch popup_sel_index
    case 1
        if a*y0>b*x0
    et=x0/a;
    Rb=(a*y0-b*x0)/a;
    Rr=0;
    w='蓝方';
end
if a*y0<b*x0
    et=y0/b;
    Rr=(b*x0-a*y0)/b;
    Rb=0;
    w='红方';
end
if a*y0==b*x0
    et=x0/a;
    Rr=0;
    Rb=0;
    w='同归于尽';
end
        set(handles.winp,'string',w);
        set(handles.endt,'string',num2str(et));
        set(handles.redr,'string',num2str(Rr));
        set(handles.bluer,'string',num2str(Rb));
%         if x(end)==0
         plot([0,et],[x0,Rr],'r-');
         hold on
         plot([0,et],[y0,Rb],'b-');
         hold off
         grid on
         grid minor
    case 2
      if a*y0^2>b*x0^2
          w='蓝方';
          Rb=sqrt(y0^2-(b/a)*x0^2)
          Rr=0;
          et=log((sqrt(b)*x0+sqrt(a)*y0)/(-sqrt(b)*x0+sqrt(a)*y0))/2/sqrt(a*b);
          
      end
       if a*y0^2<b*x0^2
          w='红方';
          Rr=sqrt(x0^2-(a/b)*y0^2)
          Rb=0;
          et=log((sqrt(b)*x0+sqrt(a)*y0)/(sqrt(b)*x0-sqrt(a)*y0))/2/sqrt(a*b);
       end
       set(handles.winp,'string',w);%显示计算结果
        set(handles.endt,'string',num2str(et));
        set(handles.redr,'string',num2str(Rr));
        set(handles.bluer,'string',num2str(Rb));
        %fh = @(x,y) a.*x.^2 - b.*y.^2 - a.*y0.^2+b.*x0.^2;
        %ezplot(@(x,y)myfun(x,y,x0,a,y0,b),[0,x0]);
        t=[0:0.1:et];
        ab=sqrt(a*b);
        x=x0*cosh(ab*t)-(sqrt(a/b)*y0)*sinh(ab*t);
        y=y0*cosh(ab*t)-(sqrt(b/a)*x0)*sinh(ab*t);
        plot(t,x,'r-');
        hold on 
        plot(t,y,'b-');
        hold off
        grid minor
%         bar(1:.5:10);
%     case 4
%         plot(membrane);
%     case 5
%         surf(peaks);
end


% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if (double(get(hObject,'Value')==2))
     set(handles.text6,'ForegroundColor','yellow');
end

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{
    
    get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
     set(hObject,'BackgroundColor','blue');
end

set(hObject, 'String', {
    
    'lanchester线性律', 'lanchester平方律', });%选择菜单的设计



function red1_Callback(hObject, eventdata, handles)
% hObject    handle to red1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of red1 as text
%        str2double(get(hObject,'String')) returns contents of red1 as a double


% --- Executes during object creation, after setting all properties.
function red1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to red1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

三、运行结果

在这里插入图片描述

四、备注

完整代码或者代写添加QQ 912100926
往期回顾>>>>>>
【游戏】基于GUI四子棋【Matlab 302期】
【游戏】基于GUI座位抽奖【Matlab 303期】

猜你喜欢

转载自blog.csdn.net/m0_54742769/article/details/114382326