Effect:

I didn’t find the right background, so I drew it myself. If you have a better one, you can change it.

steps

1 Create Axes and image import

Window creation:

Mainfig=figure('units','pixels','position',[50 100 760 400],...
                       'Numbertitle','off','menubar','none','resize','off',...
                       'name','dragonBoat');
axes('parent',Mainfig,'position',[0 0 1 1],...
   'XLim', [0 760],...
   'YLim', [0 400],...
   'NextPlot','add',...
   'layer','bottom',...
   'Visible','on',...
   'YDir','reverse',...
   'XTick',[], ...
   'YTick',[]);

Copy the code

Image import:

[bkg_C,~,~]=imread('river.png');
[boat_C,~,boat_Alp]=imread('boat.png');
[stone_C,~,stone_Alp]=imread('stone.png');

Copy the code

2 Create timer function to move background

DrawBkgHdl=image([0 760],[0 400],bkg_C);

t=0;
tempBkg_C=[bkg_C,bkg_C];
fps = 20;
game = timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @dragongame);
start(game)

	function dragongame(~,~)
        t=t+6;
        modt=mod(t,720);
        
        newBkg_C=tempBkg_C(:,1+modt:684+modt,:);
        set(DrawBkgHdl,'CData',newBkg_C) 
    end

Copy the code

3 Draw a block and move it

In fact, five blocks move back and forth, increasing the value and redrawing as one of the blocks decreases to a negative number

stonePos=[600;870;1140;1410]; StonePos = [stonePos, randi ([90330], [4, 1])]; for i=1:size(stonePos,1) drawStoneHdl(i)=image([stonePos(i,1)-39 stonePos(i,1)+39],[stonePos(i,2)-20 stonePos(i,2)+20],stone_C,'AlphaData',stone_Alp); End function dragongame(~,~) % %... stonePos(:,1)=stonePos(:,1)-20/3; StonePos (stonePos (:, 1) < = 0, 2) randi ([90330], [1, 1]). StonePos (stonePos (:, 1) < 0, 1) = stonePos (stonePos (:, 1) < 0, 1) + 1080. for ii=1:size(stonePos,1) set(drawStoneHdl(ii),'XData',[stonePos(ii,1)-39 stonePos(ii,1)+39],... 'YData',[stonePos(ii,2)-20 stonePos(ii,2)+20]); end endCopy the code

Draw the ship and create the mouse callback

BoatPos = [380200]; DrawBoatHdl=image([boatPos(1)-75 boatPos(1)+75],[boatPos(2)-50 boatPos(2)+50],boat_C,'AlphaData',boat_Alp); set(gcf,'WindowButtonMotionFcn',@moveBoat,'tag','mov') function moveBoat(~,~) xy=get(gca,'CurrentPoint'); Temp_y = y (1, 2); temp_y(temp_y<100)=90; temp_y(temp_y>340)=330; boatPos=[380,temp_y]; set(DrawBoatHdl,'YData',[temp_y-50 temp_y+50]); endCopy the code

5 collision judgment function

	function flag=judge(Bpos,Spos)
        flag1=abs(Bpos(1)-Spos(:,1))<80;
        flag2=abs((Bpos(2)+35)-Spos(:,2))<30;
        flag3=flag1&flag2;
        flag=any(flag3);
    end

Copy the code

The dragongame function is rewritten as follows

Function dragongame(~,~) % %... %... if judge(boatPos,stonePos) stop(game) set(gcf,'WindowButtonMotionFcn',[]); The text (50200, 'game over', 'FontSize, 54,' Color ', 'w', 'tag', 'TXT) end to endCopy the code

6 Complete Code

function dragonBoat Mainfig=figure('units','pixels','position',[50 100 760 400],... 'Numbertitle','off','menubar','none','resize','off',... 'name','dragonBoat'); axes('parent',Mainfig,'position',[0 0 1 1],... 'XLim', [0 760],... 'YLim', [0 400],... 'NextPlot','add',... 'layer','bottom',... 'Visible','on',... 'YDir','reverse',... 'XTick',[], ... 'YTick',[]); [bkg_C,~,~]=imread('river.png'); [boat_C,~,boat_Alp]=imread('boat.png'); [stone_C,~,stone_Alp]=imread('stone.png'); DrawBkgHdl=image([0 760],[0 400],bkg_C); stonePos=[600;870;1140;1410]; StonePos = [stonePos, randi ([90330], [4, 1])]; for i=1:size(stonePos,1) drawStoneHdl(i)=image([stonePos(i,1)-39 stonePos(i,1)+39],[stonePos(i,2)-20 stonePos(i,2)+20],stone_C,'AlphaData',stone_Alp); End boatPos = [380200]; DrawBoatHdl=image([boatPos(1)-75 boatPos(1)+75],[boatPos(2)-50 boatPos(2)+50],boat_C,'AlphaData',boat_Alp); t=0; tempBkg_C=[bkg_C,bkg_C]; fps = 20; game = timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @dragongame); Start (game) text (10, 20, [' have advanced ', num2str (t), 'm'], 'FontSize, 14,' Color ', 'w', 'tag', 'TXT'); set(gcf,'WindowButtonMotionFcn',@moveBoat,'tag','mov') function dragongame(~,~) t=t+6; modt=mod(t,720); newBkg_C=tempBkg_C(:,1+modt:684+modt,:); set(DrawBkgHdl,'CData',newBkg_C) stonePos(:,1)=stonePos(:,1)-20/3; StonePos (stonePos (:, 1) < = 0, 2) randi ([90330], [1, 1]). StonePos (stonePos (:, 1) < 0, 1) = stonePos (stonePos (:, 1) < 0, 1) + 1080. for ii=1:size(stonePos,1) set(drawStoneHdl(ii),'XData',[stonePos(ii,1)-39 stonePos(ii,1)+39],... 'YData',[stonePos(ii,2)-20 stonePos(ii,2)+20]); End set (findobj (' tag ', 'TXT'), 'String' [' have advanced ', num2str (t), 'm']); if judge(boatPos,stonePos) stop(game) set(gcf,'WindowButtonMotionFcn',[]); End function moveBoat(~,~) xy=get(gca,'CurrentPoint') end function moveBoat(~,~) xy=get(gca,'CurrentPoint'); Temp_y = y (1, 2); temp_y(temp_y<100)=90; temp_y(temp_y>340)=330; boatPos=[380,temp_y]; set(DrawBoatHdl,'YData',[temp_y-50 temp_y+50]); end function flag=judge(Bpos,Spos) flag1=abs(Bpos(1)-Spos(:,1))<80; flag2=abs((Bpos(2)+35)-Spos(:,2))<30; flag3=flag1&flag2; flag=any(flag3); end endCopy the code

Copyright Notice: This article is the original article of CSDN blogger “Slandarer”, in accordance with CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement. The original link: blog.csdn.net/slandarer/a…