当前位置:网站首页>Lantern Festival gift - plant vs zombie game (realized by Matlab)
Lantern Festival gift - plant vs zombie game (realized by Matlab)
2022-07-02 05:40:00 【It's a dream, it's you!】
Catalog
1 Spring breeze contains Yemei
2 Philosophies brought by the joy of games
3 The Dharma wheel turns in the sky —— Plants vs zombies show
1 Spring wind contain Yemei
In this lantern festival , It's boring at home , Never played King , And I prefer to stand in the perspective of a person who makes games , Play psychological warfare , Advance the code to attract players . Let's give you an example , in real life , It's hard to get real-time feedback for your hard work , But the king will kill five , Basically, the whole world is proud of you , This wave of psychological design is great ; also , When playing King , You can't see time in the game , Let you have no concept of time , In fact, the time setting is just a matter of a few lines of code . There are also many logical thinking in designing games , Not one by one .
I remember when I was a child, I was fascinated by playing a game of Plants vs. Zombies , I don't want to eat every day . For the sake of my boredom today , First read a poem by Emperor Yang of the Sui Dynasty , Calm down and get another Plants vs. zombies , Just use Matlab Achieve it. .
《 On New Year's Eve, lights are built on the thoroughfare and the south tower rises at night 》
——( Sui Dynasty ) Sui Yangdi
The Dharma wheel turns in the sky , Brahma came from heaven ;
A thousand lights from a lamp tree , Seven flowers bloom .
The shadow of the moon doubts the flowing water , Spring breeze contains night plum ;
Burn the golden ground , Clock sends glass platform .
2 Philosophies brought by the joy of games
What is meant by “ happy ”? What is the “ Happiness ”? This is a question that few people can answer . It's strange that , Everyone's daily behavior tendency is locked in this phantom goal without exception , As if it were right in front of you , As if it speaks for itself .
The ancients said :“ Contented people are always happy ”,—— This will probably become “ Happy ” A kind of self mockery of people who can't get up —— Modern economists have also compiled a formula that seems to be able to calculate accurately :

happy = earnings / desire , It means , The higher the income, the happier , If income is limited , That can only be reduced “ Denominator of desire ” To improve the “ Happiness value ” 了 , However , According to my observation , Those who squash their desires , Own happiness “ Probably as dry as boredom .
Strictly speaking , The psychological activity of happiness is nothing more than the sum of a series of low intelligence emotional activities , Its control center is located in the thalamic reticular system below the higher cortex of the brain . therefore , Everyone's normal psychology must be maintained in a painless and joyless “ Boring baseline ”.( Give the frog's sciatic nerve strong 、 The weak stimulation experiment is a powerful proof .)
3 The Dharma wheel turns in the sky —— Plants vs zombies show

4 Matlab Code implementation
function plants_vs_zombies
global map; % Display the plant species and planting time planted at various locations on the map
global bulletmap;
global list; % List of plant names
global cardlist; % Deck sort list
global cardbank; % Card group class
global bkg % Background class
global zeroplace; % The lower left corner of the planting area
global gridsize; % Plantable cell size
global stagenumber; % Map number : Day Black sky Day pool ...
global Data; % Plant vs zombie dataset
global energy; % The sun
global onhand; % Plant species in hand
global sun;
global sunControlTimer;
global sunTargetPos;
global sunPos;
global Mainfig;
global Mainaxes;
global DrawBkgHdl;
global DrawPlantsHdl;
global DrawCardBankHdl;
global DrawScoopBkgHdl;
global DrawCardHdl;
global DrawScoopHdl;
global TextEnergyHdl;
global DrawOnHandHdl;
global DrawSunHdl;
global DrawSunProducedHdl;
global DrawGetSunHdl;
global DrawGetSunProducedHdl;
%==========================================================================
function tempCDataNan=getCDataNan(temp_CData,temp_Alpha)
tempCData1=double(temp_CData(:,:,1))./255;
tempCData2=double(temp_CData(:,:,2))./255;
tempCData3=double(temp_CData(:,:,3))./255;
tempCData1(double(temp_Alpha)==0)=nan;
tempCData2(double(temp_Alpha)==0)=nan;
tempCData3(double(temp_Alpha)==0)=nan;
tempCDataNan(:,:,1)=tempCData1;
tempCDataNan(:,:,2)=tempCData2;
tempCDataNan(:,:,3)=tempCData3;
end
%==========================================================================
init()
function init(~,~)
Data=load('PlantsVsZombies.mat');
energy=2500;
sun=Data.sun;
sunControlTimer=0;
onhand=0;
cardlist=[2 1 3 4 5];%1 3];
list=Data.list;
bkg=Data.bkg;
cardbank=Data.cardbank;
stagenumber=5;
map=zeros(bkg.MapSize(stagenumber,1),bkg.MapSize(stagenumber,2),2);
bulletmap=zeros(bkg.MapSize(stagenumber,1),bkg.MapSize(stagenumber,2),3);
% Number of bullets generated at this position \ Whether to start generating bullets \ Generate bullet cycle \
temp_pos=[randi(bkg.MapSize(stagenumber,1)),randi(bkg.MapSize(stagenumber,2))];
zeroplace=bkg.ZeroPlace(stagenumber,:);
gridsize=bkg.GridSize(stagenumber,:);
sunTargetPos=[zeroplace(1)+(temp_pos(2)-1)*(gridsize(1))+min(temp_pos(2),6)*gridsize(4),...
zeroplace(2)+(temp_pos(1)-1)*(gridsize(3))+min(temp_pos(2),6)*gridsize(2)]+randi(50,[1 2]);
sunPos=sunTargetPos+[0 600];
%==================================================================
Mainfig=figure('units','pixels','position',[300 80 900 600],...
'Numbertitle','off','menubar','none','resize','off',...
'name','Plants Vs Zombies');
Mainaxes=axes('parent',Mainfig,'position',[0 0 1 1],...
'XLim', [0 900],...
'YLim', [0 600],...
'NextPlot','add',...
'layer','bottom',...
'Visible','on',...
'XTick',[], ...
'YTick',[]);
DrawBkgHdl=image([0 900],[0 600],flipud(bkg.Main(:,:,:,stagenumber)));
DrawCardBankHdl=image([0 446]+120,[-87 0]+600,flipud(cardbank.CardBkg(:,:,:)));
DrawScoopBkgHdl=image([0 70]+446+120,[-72 0]+600,flipud(cardbank.ScoopBkg(:,:,:)));
DrawOnHandHdl=image([0 Data.(list{onhand+1}).Size(1)]-Data.(list{onhand+1}).Size(1)/2,...
[0 Data.(list{onhand+1}).Size(2)]-Data.(list{onhand+1}).Size(2)/2,...
flipud(Data.(list{onhand+1}).Sprite(:,:,:,1)),...
'AlphaData',flipud(Data.(list{onhand+1}).Alpha(:,:,1)).*0.5);
TextEnergyHdl=text(158,527,num2str(energy),'HorizontalAlignment', 'center','fontsize',10);
[temp_x,temp_y]=meshgrid(1:sun.Size(1),1:sun.Size(2));
DrawSunHdl=surface(temp_x+sunPos(1),...
temp_y+sunPos(2),...
ones(sun.Size([1 2])),...
flipud(getCDataNan(sun.CData(:,:,:,1),sun.Alpha(:,:,1))),...
'CDataMapping','direct',...
'EdgeColor','none',...
'ButtonDownFcn',@onclicksun);
for i=1:length(cardlist)
temp_card=Data.(list{cardlist(i)+1}).Card;
DrawCardHdl(i)=image([0 Data.cardsize(2)].*1.02+198+(i-1)*(Data.cardsize(2)*1.02+0.075),...
[0 Data.cardsize(1)].*1.02+520,...
flipud(temp_card.CData),...
'alphaData',flipud(temp_card.Alpha),...
'tag',[num2str(i),num2str(cardlist(i))],...
'ButtonDownFcn',@selectplants);
end
DrawScoopHdl=image('XData',[0 Data.cardbank.Scoop.Size(1)].*0.5+570,...
'YData',[0 Data.cardbank.Scoop.Size(2)].*0.5+540,...
'CData',flipud(Data.cardbank.Scoop.CData),...
'alphaData',flipud(Data.cardbank.Scoop.Alpha),...
'tag',num2str('-1'),...
'ButtonDownFcn',@selectplants);
for i=1:bkg.MapSize(stagenumber,1)
for j=1:bkg.MapSize(stagenumber,2)
DrawPlantsHdl(i,j)=image([0 Data.(list{map(i,j,1)+1}).Size(1)]+zeroplace(1)+(j-1)*(gridsize(1))+min(j,6)*gridsize(4),...
[0 Data.(list{map(i,j,1)+1}).Size(2)]+zeroplace(2)+(i-1)*(gridsize(3))+min(j,6)*gridsize(2),...
flipud(Data.(list{map(i,j,1)+1}).Sprite(:,:,:,mod(map(i,j,2),Data.(list{map(i,j,1)+1}).Len)+1)),...
'alphaData',flipud(Data.(list{map(i,j,1)+1}).Alpha(:,:,mod(map(i,j,2),Data.(list{map(i,j,1)+1}).Len)+1)),...
'tag',[num2str(i),num2str(j),num2str(map(i,j,1))],...
'ButtonDownFcn',@growplants);
end
end
for i=1:bkg.MapSize(stagenumber,1)
for j=1:bkg.MapSize(stagenumber,2)
temp_pos=[zeroplace(1)+(j-1)*(gridsize(1))+min(j,6)*gridsize(4),...
zeroplace(2)+(i-1)*(gridsize(3))+min(j,6)*gridsize(2)];
[temp_x,temp_y]=meshgrid(1:sun.Size(1),1:sun.Size(2));
DrawSunProducedHdl(i,j)=surface(temp_x+temp_pos(1),...
temp_y+temp_pos(2),...
ones(sun.Size([1 2])),...
flipud(getCDataNan(sun.CData(:,:,:,1),sun.Alpha(:,:,1))),...
'CDataMapping','direct',...
'EdgeColor','none',...
'tag',num2str(i*10+j),...
'visible','off',...
'ButtonDownFcn',@onclicksun_produced);
end
end
fps = 10;
game = timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @PvZgame);
set(gcf,'tag','co','CloseRequestFcn',@clo);
function clo(~,~),stop(game),delete(findobj('tag','co'));clf,close,end
start(game)
set(gcf,'WindowButtonMotionFcn',@onhandfunc)
end
%==========================================================================
function onclicksun(~,~)
temp_pos=sunPos;
set(DrawSunHdl,'visible','off');
temp_distance=sqrt(sum(([160,563]-temp_pos).^2));
temp_vector=([160-35,563-35]-temp_pos)./floor(temp_distance/100);
[temp_x,temp_y]=meshgrid(1:sun.Size(1),1:sun.Size(2));
DrawGetSunHdl=surface(temp_x+temp_pos(1),...
temp_y+temp_pos(2),...
ones(sun.Size([1 2])),...
flipud(getCDataNan(sun.CData(:,:,:,1),sun.Alpha(:,:,1))),...
'CDataMapping','direct',...
'EdgeColor','none');
for i=1:floor(temp_distance/100)
pause(0.001)
set(DrawGetSunHdl,'XData',temp_x+temp_pos(1)+i*temp_vector(1),...
'YData',temp_y+temp_pos(2)+i*temp_vector(2),...
'CData',flipud(getCDataNan(sun.CData(:,:,:,mod(floor(i/2),sun.Len)+1),sun.Alpha(:,:,mod(floor(i/2),sun.Len)+1))));
end
delete(DrawGetSunHdl)
energy=energy+25;
set(TextEnergyHdl,'string',num2str(energy));
end
function onclicksun_produced(object,~)
temp_tag=object.Tag;
temp_id=[str2num(temp_tag(1)),str2num(temp_tag(2))];
set(DrawSunProducedHdl(temp_id(1),temp_id(2)),'visible','off');
temp_pos=[min(min(object.XData)),min(min(object.YData))];
temp_distance=sqrt(sum(([160,563]-temp_pos).^2));
temp_vector=([160-35,563-35]-temp_pos)./floor(temp_distance/100);
[temp_x,temp_y]=meshgrid(1:sun.Size(1),1:sun.Size(2));
DrawGetSunProducedHdl(temp_id(1),temp_id(2))=surface(temp_x+temp_pos(1),...
temp_y+temp_pos(2),...
ones(sun.Size([1 2])),...
flipud(getCDataNan(sun.CData(:,:,:,1),sun.Alpha(:,:,1))),...
'CDataMapping','direct',...
'EdgeColor','none');
for i=1:floor(temp_distance/100)
pause(0.002)
set(DrawGetSunProducedHdl(temp_id(1),temp_id(2)),'XData',temp_x+temp_pos(1)+i*temp_vector(1),...
'YData',temp_y+temp_pos(2)+i*temp_vector(2),...
'CData',flipud(getCDataNan(sun.CData(:,:,:,mod(floor(i/2),sun.Len)+1),sun.Alpha(:,:,mod(floor(i/2),sun.Len)+1))));
end
delete(DrawGetSunProducedHdl(temp_id(1),temp_id(2)))
energy=energy+25;
set(TextEnergyHdl,'string',num2str(energy));
end
function producesun(pos)
temp_x=get(DrawSunProducedHdl(pos(1),pos(2)),'XData');
temp_y=get(DrawSunProducedHdl(pos(1),pos(2)),'YData');
set(DrawSunProducedHdl(pos(1),pos(2)),'visible','on');
for i=pi/3:0.1:pi
pause(0.001)
set(DrawSunProducedHdl(pos(1),pos(2)),'XData',temp_x+i*10,'YData',temp_y+sin(i)*10);
set(DrawSunProducedHdl(pos(1),pos(2)),...
'CData',flipud(getCDataNan(sun.CData(:,:,:,mod(sunControlTimer,sun.Len)+1),sun.Alpha(:,:,mod(sunControlTimer,sun.Len)+1))));
end
end
%==========================================================================
function onhandfunc(~,~)
xy=get(gca,'CurrentPoint');
x=xy(1,1);y=xy(1,2);
if onhand~=-1
set(DrawOnHandHdl,'XData',[0 Data.(list{onhand+1}).Size(1)]-Data.(list{onhand+1}).Size(1)/2+x,...
'YData',[0 Data.(list{onhand+1}).Size(2)]-Data.(list{onhand+1}).Size(2)/2+y,...
'CData',flipud(Data.(list{onhand+1}).Sprite(:,:,:,1)),...
'AlphaData',flipud(Data.(list{onhand+1}).Alpha(:,:,1)).*0.5);
else
set(DrawOnHandHdl,'XData',[0 cardbank.Scoop.Size(1)].*0.5-cardbank.Scoop.Size(1)/4+x,...
'YData',[0 cardbank.Scoop.Size(2)].*0.5-cardbank.Scoop.Size(2)/4+y,...
'CData',flipud(cardbank.Scoop.CData),...
'AlphaData',flipud(cardbank.Scoop.Alpha));
end
end
function selectplants(object,~)
if str2num(object.Tag)~=-1
temp_id=str2num(object.Tag(1));
temp_type=str2num(object.Tag(2:end));
switch 1
case onhand==0,onhand=temp_type;
case onhand~=0,onhand=0;set(DrawScoopHdl,'alphaData',flipud(Data.cardbank.Scoop.Alpha));onhandfunc()
end
else
disp(1)
switch 1
case onhand==-1,onhand=0;set(DrawScoopHdl,'alphaData',flipud(Data.cardbank.Scoop.Alpha));onhandfunc()
case onhand~=-1,onhand=-1;set(DrawScoopHdl,'alphaData',flipud(Data.cardbank.Scoop.Alpha).*0);onhandfunc()
end
end
end
function growplants(object,~)
temp_id=[str2num(object.Tag(1)),str2num(object.Tag(2))];
temp_type=str2num(object.Tag(3:end));
if onhand~=0&&map(temp_id(1),temp_id(2),1)==0&&onhand~=-1&&energy>=Data.(list{onhand+1}).Cost
energy=energy-Data.(list{onhand+1}).Cost;
set(TextEnergyHdl,'string',num2str(energy));
map(temp_id(1),temp_id(2),1)=onhand;
map(temp_id(1),temp_id(2),2)=1;
onhand=0;
onhandfunc()
end
if onhand==-1&&map(temp_id(1),temp_id(2),1)~=0
map(temp_id(1),temp_id(2),1)=0;
onhand=0;
set(DrawScoopHdl,'alphaData',flipud(Data.cardbank.Scoop.Alpha));
onhandfunc()
end
end
%==========================================================================
function PvZgame(~,~)
sunControlTimer=sunControlTimer+1;
for i=1:bkg.MapSize(stagenumber,1)
for j=1:bkg.MapSize(stagenumber,2)
temp_sunPicNum=mod(sunControlTimer,sun.Len)+1;
temp_CData=sun.CData(:,:,:,temp_sunPicNum);
temp_Alpha=sun.Alpha(:,:,temp_sunPicNum);
tempCData1=double(temp_CData(:,:,1))./255;
tempCData2=double(temp_CData(:,:,2))./255;
tempCData3=double(temp_CData(:,:,3))./255;
tempCData1(double(temp_Alpha)==0)=nan;
tempCData2(double(temp_Alpha)==0)=nan;
tempCData3(double(temp_Alpha)==0)=nan;
tempCDataNan(:,:,1)=tempCData1;
tempCDataNan(:,:,2)=tempCData2;
tempCDataNan(:,:,3)=tempCData3;
set(DrawSunProducedHdl(i,j),...
'CData',flipud(tempCDataNan));
map(i,j,2)=map(i,j,2)+1;
temp_time=map(i,j,2);
temp_type=list{map(i,j,1)+1};
temp_len=Data.(temp_type).Len;
temp_size=Data.(temp_type).Size;
temp_Cdata=Data.(temp_type).Sprite(:,:,:,mod(floor(temp_time),temp_len)+1);
temp_Alpha=Data.(temp_type).Alpha(:,:,mod(floor(temp_time),temp_len)+1);
if map(i,j,1)==2
temp_time=mod(temp_time,180);
set(DrawPlantsHdl(i,j),'XData',[0 temp_size(1)]+zeroplace(1)+(j-1)*(gridsize(1))+min(j,6)*gridsize(4),...
'YData',[0 temp_size(2)]+zeroplace(2)+(i-1)*(gridsize(3))+min(j,6)*gridsize(2),...
'CData',flipud(temp_Cdata).*(1+max(0,temp_time-120)*0.01),...
'alphaData',flipud(temp_Alpha));
if temp_time==50
set(DrawSunProducedHdl(i,j),'visible','off');
temp_pos=[zeroplace(1)+(j-1)*(gridsize(1))+min(j,6)*gridsize(4),...
zeroplace(2)+(i-1)*(gridsize(3))+min(j,6)*gridsize(2)];
[temp_x,temp_y]=meshgrid(1:sun.Size(1),1:sun.Size(2));
set(DrawSunProducedHdl(i,j),'XData',temp_x+temp_pos(1),...
'YData',temp_y+temp_pos(2));
end
if temp_time==179
producesun([i j])
end
else
set(DrawPlantsHdl(i,j),'XData',[0 temp_size(1)]+zeroplace(1)+(j-1)*(gridsize(1))+min(j,6)*gridsize(4),...
'YData',[0 temp_size(2)]+zeroplace(2)+(i-1)*(gridsize(3))+min(j,6)*gridsize(2),...
'CData',flipud(temp_Cdata),...
'alphaData',flipud(temp_Alpha));
end
end
end
%==================================================================
if mod(sunControlTimer,160)==0
set(DrawSunHdl,'visible','on')
temp_pos=[randi(bkg.MapSize(stagenumber,1)),randi(bkg.MapSize(stagenumber,2))];
sunTargetPos=[zeroplace(1)+(temp_pos(2)-1)*(gridsize(1))+min(temp_pos(2),6)*gridsize(4),...
zeroplace(2)+(temp_pos(1)-1)*(gridsize(3))+min(temp_pos(2),6)*gridsize(2)]+randi(50,[1 2]);
sunPos=sunTargetPos+[0 600];
end
if sum(abs(sunPos-sunTargetPos))>1
sunPos=sunPos-[0 5];
end
[temp_x,temp_y]=meshgrid(1:sun.Size(1),1:sun.Size(2));
set(DrawSunHdl,'XData',temp_x+sunPos(1),...
'YData',temp_y+sunPos(2),...
'CData',flipud(getCDataNan(sun.CData(:,:,:,mod(sunControlTimer,sun.Len)+1),sun.Alpha(:,:,mod(sunControlTimer,sun.Len)+1))));
%==================================================================
end
end边栏推荐
- all3dp.com网站中全部Arduino项目(2022.7.1)
- ubuntu20.04安装mysql8
- [golang syntax] be careful with the copy of slices
- 460. LFU cache bidirectional linked list
- Simply encapsulate JS and apply it
- Ubuntu 20.04 installing mysql8
- 2022-2-14 learning xiangniuke project - Section 7 account setting
- Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
- 软件测试答疑篇
- centos8安裝mysql8.0.22教程
猜你喜欢

2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions

KMP idea and template code

Opencv LBP features

Using QA band and bit mask in Google Earth engine

3D printer G code command: complete list and tutorial

JVM class loading mechanism

Disable access to external entities in XML parsing

Disable access to external entities in XML parsing

Installation du tutoriel MySQL 8.0.22 par centos8

测试 - 用例篇
随机推荐
Php/js cookie sharing across domains
Visual studio import
Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
Appnuim environment configuration and basic knowledge
LS1046nfs挂载文件系统
Straighten elements (with transition animation)
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Gee: use of common mask functions in remote sensing image processing [updatemask]
Database batch insert data
Basic use of form
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
KMP idea and template code
Foreign trade marketing website system development function case making
Visual Studio導入
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
php/js cookie共享跨域的问题
【技术随记-08】
1037 Magic Coupon
Zzuli:1067 faulty odometer
Fabric. JS activation input box
