大富翁大家都玩过,走到建筑的位置可以买地,第二圈走到买过的地可以升级,别人经过后需要付过路费,每次经过起点都会获得一定资金,玩到最后还没破产的就是胜者,这里我用MATLAB制作了一款MATLAB版的大富翁小游戏:
点击方框内加号可换头像,点击文本区域可改昵称:
当然即使不换头像点击开始后,也会自动分配头像和名称:
第一次走过地可以买地:
B:收购价,T:过路费,最后一位数值是地产等级
再次走过地可以升级:
他人走过已被购买地产需要交过路费:
当有玩家负债时,会清空资产并显示负债退出游戏:
主要文件共10个m文件,其中 monopoly_main.m 是函数入口,运行该程序开始游戏!:
monopoly.mlappinstall 文件是APP安装包,点击如下安装APP按钮并选中该文件,可以将其安装在APP栏:
值得一提的是,本程序的头像预设也是用MATLAB绘制的:
部分代码展示:
function [monFig,monAx,diceAx,diceTool,selfTool,monMenuS]=gui_init monFig=uifigure; monFig.Position=[10 50 1000 620]; monFig.NumberTitle='off'; monFig.MenuBar='none'; monFig.Resize='off'; monFig.Name='monopoly'; monAx=uiaxes(monFig); monAx.PlotBoxAspectRatio=[1 1 1]; monAx.Position=[380 10 600 600]; %monAx.Box='on'; monAx.XLim=[0 100]; monAx.YLim=[0 100]; %monAx.Color=[0.9 0.9 0.9]; monAx.XColor=[0.95,0.95,0.95]; monAx.YColor=[0.95,0.95,0.95]; monAx.Toolbar.Visible='off'; selfPl=uipanel(monFig); selfPl.Title=''; selfPl.Position=[10 10 370 600]; diceAx=uiaxes(selfPl); diceAx.Position=[0 450 145 145]; diceAx.XLim=[0 10]; diceAx.YLim=[0 10]; diceAx.Color=[0.98 0.98 0.98]; diceAx.XColor=[0.95,0.95,0.95]; diceAx.YColor=[0.95,0.95,0.95]; diceAx.Toolbar.Visible='off'; hold(diceAx,'on') plot(diceAx,[2,8],[1,1],'k','LineWidth',2) plot(diceAx,[2,8],[9,9],'k','LineWidth',2) plot(diceAx,[1,1],[2,8],'k','LineWidth',2) plot(diceAx,[9,9],[2,8],'k','LineWidth',2) plot(diceAx,8+cos(0+(0:pi/(2*10):pi/2)),8+sin(0+(0:pi/(2*10):pi/2)),'k','LineWidth',2) plot(diceAx,2+cos(pi/2+(0:pi/(2*10):pi/2)),8+sin(pi/2+(0:pi/(2*10):pi/2)),'k','LineWidth',2) plot(diceAx,2+cos(pi+(0:pi/(2*10):pi/2)),2+sin(pi+(0:pi/(2*10):pi/2)),'k','LineWidth',2) plot(diceAx,8+cos(-pi/2+(0:pi/(2*10):pi/2)),2+sin(-pi/2+(0:pi/(2*10):pi/2)),'k','LineWidth',2) hold(diceAx,'off') diceTool.Btn=uibutton(monFig); diceTool.Btn.Text='摇 骰 子'; diceTool.Btn.BackgroundColor=[0.31 0.58 0.80]; diceTool.Btn.FontColor=[1 1 1]; diceTool.Btn.FontWeight='bold'; diceTool.Btn.Position=[180 480 180 40]; diceTool.Btn.FontSize=18; diceTool.Lbl1=uilabel(monFig); diceTool.Lbl1.Text='剩余玩家:'; diceTool.Lbl1.FontSize=18; diceTool.Lbl1.FontColor=[185 184 150]./500; diceTool.Lbl1.FontWeight='bold'; diceTool.Lbl1.Position=[180 560 180 40]; diceTool.Lbl2=uilabel(monFig); diceTool.Lbl2.Text='当前轮次:'; diceTool.Lbl2.FontSize=18; diceTool.Lbl2.FontColor=[185 184 150]./500; diceTool.Lbl2.FontWeight='bold'; diceTool.Lbl2.Position=[180 530 180 40]; for i=1:4 selfTool.(['player',num2str(i)]).Img=uiimage(monFig); selfTool.(['player',num2str(i)]).Img.Position=[30 400-(i-1)*110 60 60]; selfTool.(['player',num2str(i)]).Img.ImageSource='图片\player0.png'; selfTool.(['player',num2str(i)]).Img.UserData=i; selfTool.(['player',num2str(i)]).Ta=uitextarea(monFig); selfTool.(['player',num2str(i)]).Ta.Position=[100 402-(i-1)*110 70 23]; selfTool.(['player',num2str(i)]).Ta.Value='请输入昵称'; selfTool.(['player',num2str(i)]).Ta.UserData=i; selfTool.(['player',num2str(i)]).Ta2=uitextarea(monFig); selfTool.(['player',num2str(i)]).Ta2.Position=[180 433-(i-1)*110 180 25]; selfTool.(['player',num2str(i)]).Ta2.Value=''; selfTool.(['player',num2str(i)]).Ta2.Enable='off'; selfTool.(['player',num2str(i)]).Btn=uibutton(monFig); selfTool.(['player',num2str(i)]).Btn.Position=[100 435-(i-1)*110 70 23]; selfTool.(['player',num2str(i)]).Btn.Text=' 购买地产 '; selfTool.(['player',num2str(i)]).Btn.BackgroundColor=[0.31 0.58 0.80]; selfTool.(['player',num2str(i)]).Btn.FontColor=[1 1 1]; selfTool.(['player',num2str(i)]).Btn.FontWeight='bold'; selfTool.(['player',num2str(i)]).Btn.UserData=i; selfTool.(['player',num2str(i)]).Lb=uilistbox(monFig); selfTool.(['player',num2str(i)]).Lb.Position=[180 355-(i-1)*110 180 78]; selfTool.(['player',num2str(i)]).Lb.Items={'地产一(空)','地产二(空)','地产三(空)','... ...'}; selfTool.(['player',num2str(i)]).Txt=uilabel(monFig); selfTool.(['player',num2str(i)]).Txt.Position=[30 370-(i-1)*110 140 30]; selfTool.(['player',num2str(i)]).Txt.Text='资金 :0 '; selfTool.(['player',num2str(i)]).Txt.FontColor=[185 184 150]./500; selfTool.(['player',num2str(i)]).Txt.FontWeight='bold'; selfTool.(['player',num2str(i)]).Txt.FontSize=14; end monMenu=uimenu(monFig); monMenu.Text='开始游戏'; monMenuS.m2=uimenu(monMenu); monMenuS.m2.Text='二人游戏'; monMenuS.m3=uimenu(monMenu); monMenuS.m3.Text='三人游戏'; monMenuS.m4=uimenu(monMenu); monMenuS.m4.Text='四人游戏'; end
function self=self_init(n) for i=1:4 self.(['player',num2str(i)]).property=5000; self.(['player',num2str(i)]).pos=0; self.(['player',num2str(i)]).real_estate={}; self.(['player',num2str(i)]).estateNum=0; if i>n self.(['player',num2str(i)]).gameOver=1; else self.(['player',num2str(i)]).gameOver=0; end self.(['player',num2str(i)]).name=['玩家',num2str(i)]; end end
1.关于已购买多个地产升级某地产时左右矩阵大小不匹配问题,
解决方法:将monopoly_main函数113行附近 owenerPos=owenerList(:,2)==n;
更改为:owenerPos=owenerList(:,1)==self.([‘player’,num2str(n)]).pos;
2.由网友 [不知道叫什么] 指出若版本为2021a,则uiaxes会自动显示在uipanel下方导致骰子无法显示,所做更改为将该uiaxes的父类由uifigure更换为uipanel,并更改Position属性使其显示在相应的位置,详细更改如下:
将gui_init.m文件20-25行附近代码:
selfPl=uipanel(monFig); selfPl.Title=''; selfPl.Position=[10 10 370 600]; diceAx=uiaxes(monFig); diceAx.Position=[15 460 145 145];
改为:
selfPl=uipanel(monFig); selfPl.Title=''; selfPl.Position=[10 10 370 600]; diceAx=uiaxes(selfPl); diceAx.Position=[0 450 145 145];
3.由网友 [m0_57054690] 指出,打包后程序第一次生成随机数问题,为程序增添根据时间变化的随机数种子即可,该部分代码为:
tempTime=clock(); second=round(tempTime(6)+60*tempTime(5)+24*60*tempTime(4)); rng(second) diceNum=randi(6);