0 Game Effects

When clicking a brand namely, this brand and surrounding 4 brands also can produce change accordingly, think method lets all brands be in the same side namelyThe gameThe victory.



1 FIG interface and background panel

This section is relatively simple, mainly on the interface and the background board property setting, we use programmatic way to call the App Designer control:

ddooggFig=uifigure('units','pixels',... 'position',[320 120 360 400],... 'Numbertitle','off',... 'menubar','none',... 'resize','off',... 'name','ddoogg',... The 'color', 0.98 [0.98 0.98]); bkgLabel=uilabel(ddooggFig); bkgLabel.Position=[10 10 340 340]; bkgLabel.Text=''; bkgLabel.BackgroundColor=[193 214 232]./255;

We used 1 to represent one type of dog and 2 to represent another type of dog. DOGMAT starts with all 1 to indicate the first type of dog on all brands, IMGSource represents the position of the two types of dog pictures, and BKGColor represents the background color of the dog card

DogMat = ones (5, 5); % data matrix imgSource={'images\doga. PNG ','images\ dogg.png '}; BkgColor =[[252 251 238]./255;[222 248 252]./255]; Dog image background color

Use a two-layer for loop to figure out where to build the control:

For I =1:5 for j=1:5 DogMathDL (I,j)= UIImage (ddooggFig); DogMatHdl (I, j). The Position = [20 + 65 * (j - 1), 280-65 * (I - 1), 60, 60]. dogMatHdl(i,j).ImageSource=imgSource{1}; dogMatHdl(i,j).BackgroundColor=bkgColor(1,:); dogMatHdl(i,j).UserData=[i,j]; end end

Notice that we set a userData property for each image, which represents the position of the image so that we can identify which image to click on later.

!

2.2 Game victory TAB

Draw a TAB to show the game win:

% win tag win=false; WinLabel = UILabel (ddooggFig); winLabel.Position=[15 150 330 60]; Winlabel. Text=' Congratulations on solving the puzzle, please click restart '; winLabel.BackgroundColor=[238 236 225]./255; winLabel.FontSize=19; winLabel.FontWeight='bold'; winLabel.HorizontalAlignment='center'; winLabel.FontColor=[113 106 63]./255;



Tags are hidden at the start of the game and won’t show up until you win, so we’ll hide them first:

winLabel.Visible='off';

2.3 Click the mouse brand callback

Set (dogMatHdl,' imageClickedFCN ',@clickDog) function clickDog(~,event) if ~win % objNum=event.Source.UserData; CrossList =[-1 0;0 1;1 0; 0-1;0 0]; crossList=[-1 0;0 1;0 0]; crossList=[-1 0;0 1;0 0]; crossList=[-1 0;0 1;0 0]; crossList=[-1 0;0 1;0 0]; CrossList (ii,:)+objNum; crossList(ii,:)+objNum; If all(changePos>=1&changePos<=5)% if all(changePos>=1&changePos<=5)% ChangePOS (1), changePOS (2))=mod(changePOS (1), changePOS (2)),2)+1; dogMatHdl(changePos(1),changePos(2)).ImageSource=imgSource{dogMat(changePos(1),changePos(2))}; dogMatHdl(changePos(1),changePos(2)).BackgroundColor=bkgColor(dogMat(changePos(1),changePos(2)),:); End end % if all the CARDS, game over if all (all) (dogMat = = 1) | | all (all) (dogMat = = 2) win = true; winLabel.Visible='on'; end end end



3. Game difficulty button group

3.1 Button drawing

Draw three buttons. The initial [Beginner] button is selected, that is, the difficulty level is 1. We set GameLevel to 1, and distinguish the color of [Beginner] button from other buttons to indicate the selected state:

gameLevel=1; Levelbtn (1)= UIButton (ddooggFig); LevelBtn (1). The Position =,75,30 [10360]; = 'primary' levelBtn (1). The Text; levelBtn(1).FontWeight='bold'; levelBtn(1).FontSize=14; levelBtn(1).BackgroundColor=[13 141 209]./255; levelBtn(1).FontColor=[1 1 1]; levelBtn(1).UserData=1; Levelbtn (2)= UIButton (ddooggFig); LevelBtn (2). The Position =,75,30 [95360]; = 'intermediate' levelBtn (2). The Text; levelBtn(2).FontWeight='bold'; levelBtn(2).FontSize=14; levelBtn(2).BackgroundColor=[2 164 173]./255; levelBtn(2).FontColor=[1 1 1]; levelBtn(2).UserData=2; Levelbtn (3)= UIButton (ddooggFig); LevelBtn (3). The Position =,75,30 [180360]; = 'senior' levelBtn (3). The Text; levelBtn(3).FontWeight='bold'; levelBtn(3).FontSize=14; levelBtn(3).BackgroundColor=[2 164 173]./255; levelBtn(3).FontColor=[1 1 1]; levelBtn(3).UserData=3;



3.2 Difficulty selection callback

Change the value of GameLevel, and then change the button color

Set (levelBtn,'ButtonPushedFcn',@changeLevel) function changeLevel(~,event) levelBtn(gameLevel).BackgroundColor=[2 164 173]./255; objNum=event.Source.UserData; gameLevel=objNum; levelBtn(gameLevel).BackgroundColor=[13 141 209]./255; end



4. Game refresh module

4.1 Refresh game button drawing

restartBtn=uibutton(ddooggFig); RestartBtn. Position =,85,30 [265360]; RestartBtn.text =' Start again '; restartBtn.FontWeight='bold'; restartBtn.FontSize=14; restartBtn.BackgroundColor=[2 164 173]./255; restartBtn.FontColor=[1 1 1];

4.2 Simulate mouse click At the beginning of our game and click restart, the simplest way to generate the game situation that is not completed is to simulate mouse click on a random number of cards. The more times you click, the more difficult it will be. We already have the callback function of mouse click: Function clickDog(~,event) We find that we need to simulate mainly the second parameter event, and we find that we only need to use the UserData under the Source attribute of the event, i.e. the card position information: objNum=event.Source.UserData; We can construct a structure containing UserData information, for example: SimEvent.source.UserData =[1,2]; ClickDog ([],simEvent) is called as follows: ClickDog ([],simEvent) simulates clicking on the brand in the first row, second column, and simulates clicking on other brands.

4.2 Refresh Game Callback A Refresh Game Callback needs to be called at the end of the program to ensure that the puzzle starts at one point.

Set (restartBtn,'ButtonPushedFcn',@restart) function restart(~,~) % winLabel.Visible='off'; DogMat = ones (5, 5); For ii=1:5 for jj=1:5 for jj=1:5 for jj.ImageSource=imgSource{1}; dogMatHdl(ii,jj).BackgroundColor=bkgColor(1,:); Switch gameLevel case 1,changeTimes=3; case 2,changeTimes=5; case 3,changeTimes=11; For ii=1:changeTimes changePos=randi([1,5],[1,2]); simEvent.Source.UserData=changePos; clickDog([],simEvent) end end restart()



5 Complete code

function ddoogg ddooggFig=uifigure('units','pixels',... 'position',[320 120 360 400],... 'Numbertitle','off',... 'menubar','none',... 'resize','off',... 'name','ddoogg',... The 'color', 0.98 [0.98 0.98]); bkgLabel=uilabel(ddooggFig); bkgLabel.Position=[10 10 340 340]; bkgLabel.Text=''; bkgLabel.BackgroundColor=[193 214 232]./255; % draw a dog and win label = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = dogMat = ones (5, 5); % data matrix imgSource={'images\doga. PNG ','images\ dogg.png '}; BkgColor =[[252 251 238]./255;[222 248 252]./255]; For I =1:5 for j=1:5 DogMathDL (I,j)= UIImage (ddooggFig); DogMatHdl (I, j). The Position = [20 + 65 * (j - 1), 280-65 * (I - 1), 60, 60]. dogMatHdl(i,j).ImageSource=imgSource{1}; dogMatHdl(i,j).BackgroundColor=bkgColor(1,:); dogMatHdl(i,j).UserData=[i,j]; End end % win=false; WinLabel = UILabel (ddooggFig); winLabel.Position=[15 150 330 60]; Winlabel. Text=' Congratulations on solving the puzzle, please click restart '; winLabel.BackgroundColor=[238 236 225]./255; winLabel.FontSize=19; winLabel.FontWeight='bold'; winLabel.HorizontalAlignment='center'; winLabel.FontColor=[113 106 63]./255; winLabel.Visible='off'; Set (dogMatHdl,' imageClickedFCN ',@clickDog) function clickDog(~,event) if ~win objNum= event.source.UserData;  crossList=[-1 0;0 1;1 0;0 -1;0 0]; for ii=1:5 changePos=crossList(ii,:)+objNum; if all(changePos>=1&changePos<=5) dogMat(changePos(1),changePos(2))=mod(dogMat(changePos(1),changePos(2)),2)+1; dogMatHdl(changePos(1),changePos(2)).ImageSource=imgSource{dogMat(changePos(1),changePos(2))}; dogMatHdl(changePos(1),changePos(2)).BackgroundColor=bkgColor(dogMat(changePos(1),changePos(2)),:); end end if all(all(dogMat==1))||all(all(dogMat==2)) win=true; winLabel.Visible='on'; End game level button end end % = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = gameLevel = 1; Levelbtn (1)= UIButton (ddooggFig); LevelBtn (1). The Position =,75,30 [10360]; = 'primary' levelBtn (1). The Text; levelBtn(1).FontWeight='bold'; levelBtn(1).FontSize=14; levelBtn(1).BackgroundColor=[13 141 209]./255; levelBtn(1).FontColor=[1 1 1]; levelBtn(1).UserData=1; Levelbtn (2)= UIButton (ddooggFig); LevelBtn (2). The Position =,75,30 [95360]; = 'intermediate' levelBtn (2). The Text; levelBtn(2).FontWeight='bold'; levelBtn(2).FontSize=14; levelBtn(2).BackgroundColor=[2 164 173]./255; levelBtn(2).FontColor=[1 1 1]; levelBtn(2).UserData=2; Levelbtn (3)= UIButton (ddooggFig); LevelBtn (3). The Position =,75,30 [180360]; = 'senior' levelBtn (3). The Text; levelBtn(3).FontWeight='bold'; levelBtn(3).FontSize=14; levelBtn(3).BackgroundColor=[2 164 173]./255; levelBtn(3).FontColor=[1 1 1]; levelBtn(3).UserData=3; Set (levelBtn,'ButtonPushedFcn',@changeLevel) function changeLevel(~,event) levelBtn(gameLevel).BackgroundColor=[2 164 173]./255; objNum=event.Source.UserData; gameLevel=objNum; levelBtn(gameLevel).BackgroundColor=[13 141 209]./255; End game % refresh button = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = restartBtn = uibutton (ddooggFig); RestartBtn. Position =,85,30 [265360]; RestartBtn.text =' Start again '; restartBtn.FontWeight='bold'; restartBtn.FontSize=14; restartBtn.BackgroundColor=[2 164 173]./255; restartBtn.FontColor=[1 1 1]; Set (restartBtn,'ButtonPushedFcn',@restart) function restart(~,~) win=false; winLabel.Visible='off'; DogMat = ones (5, 5); for ii=1:5 for jj=1:5 dogMatHdl(ii,jj).ImageSource=imgSource{1}; dogMatHdl(ii,jj).BackgroundColor=bkgColor(1,:); end end switch gameLevel case 1,changeTimes=3; case 2,changeTimes=5; case 3,changeTimes=11; The end for ii = 1: changeTimes changePos = randi ([1, 5], [1, 2]). simEvent.Source.UserData=changePos; clickDog([],simEvent) end end restart() end