【发布时间】:2012-12-03 14:24:16
【问题描述】:
如果同时满足两个不同的函数,matlab脚本是否可以同时运行它们?
在这种情况下,我正在 matlab 上制作一个两人格斗游戏作为一个项目:在任何时候如果两个玩家都说尝试跳跃。
通过单独的带有 for 循环的 if 语句执行此操作会使一个玩家停在半空中,而另一个玩家完成他的跳跃,然后第一个玩家像往常一样继续他的跳跃。
目前我已经对这些动作进行了“硬编码”,并希望将它们转换为函数。
下面给出了两种跳跃的示例。
这两个玩家目前也是积木,需要转换成精灵,所以每一个动作,比如来回走,原则上也会有一个for循环,所以这个问题对我的项目来说是最重要的。
if double(c(1)) == 30 && double(c(2)) == 0 && jump == 0 % up
jump=1;
for dt=0:18
dy=dy+20*0.1;
y = y + dy;
set(player,'Position',[x y w h]);
pause(0.07)
if double(c(1))==122 || double(c(2))==122 || double(c(3))==122 %check for punch
if abs(x-x2)<=64 && hit2==0
h2=h2-10;
hit2=1;
x=x;
if x<x2
x2=x2+2*dx;
elseif x>x2
x2=x2-2*dx;
end
if h2<=0
disp('YOU WIN');
else
set(health2,'position',[640-h2 0 h2 20])
end
set(player2,'position',[x2 y2 wp hp])
end
elseif double(c(1))==120 || double(c(2))==120 || double(c(3))==120 %check for kick
if abs(x-x2)<=70 && hit2==0
h2=h2-15;
hit2=1;
x=x;
if x<x2
if x2>=580
x2=580;
elseif x2<580
x2=x2+6*dx;
end
elseif x>x2
if x2<=0;
x2=0;
elseif x2>0
x2=x2-6*dx;
end
end
if h2<=0
disp('YOU WIN');
else
set(health2,'position',[640-h2 0 h2 20])
end
set(player2,'position',[x2 y2 wp hp])
end
end
end
dy=-dy;
y=126;
jump=0;
hit2=0;
end
if double(f(1))==105 && double(f(2))==0 && jump2 == 0 %player 2 up
jump2=1;
for dt2=0:1:18
dy2=dy2+20*0.1;
y2=y2+dy2;
set(player2,'position',[x2 y2 wp hp]);
pause(0.07)
if double(f(1))==103 || double(f(2))==103 || double(f(3))==103 %Player 2 check for punch
if abs(x-x2)<=64 && hit1==0
h1=h1-10;
hit1=1;
x2=x2;
if x<x2
if x>=580
x=580;
elseif x<580
x=x-2*dx;
end
elseif x>x2
if x<=0
x=0;
elseif x>0
x=x+2*dx;
end
end
if h1<=0
disp('Player 2 YOU WIN');
else
set(health,'position',[0 0 h1 20])
end
set(player2,'position',[x2 y2 wp hp])
end
elseif double(f(1))==104 || double(f(2))==104 || double(f(3))==104 %check for kick
if abs(x-x2)<=70 && hit1==0
h1=h1-15;
hit1=1;
x=x;
if x<x2
if x>=580
x=580;
elseif x<580
x=x+6*dx;
end
elseif x>x2
if x<=0;
x=0;
elseif x>0
x=x-6*dx;
end
end
if h1<=0
disp('Player 2 YOU WIN');
else
set(health1,'position',[0 0 h1 20])
end
set(player,'position',[x y w h])
end
end
end
dy2=-dy2; %#ok<*NASGU>
y2=126;
jump2=0;
hit1=0;
end
【问题讨论】:
-
我建议以不同的方式解决这个问题。给定任何时间 t,有一组给定的动作 A 要执行(A 可以是空的)。收集要执行的操作,然后执行,然后显示结果。
标签: multithreading matlab parallel-processing