Is it possible for a matlab script to run two different functions at the same time if the conditions required for them are fulfilled at the same time?
In this instance, i am making a two player fighting game on matlab as a project: at any time if both players say try to jump.
Executing this via separate if statements with for loops within them makes one player stop in mid air while the other completes his jump and then the first player continues his jump as usual.
Currently I have 'hardcoded' the actions and would like to convert them into functions.
Examples of the two jumps are given below.
The two players are also currently blocks and will have to be converted into sprites, so every action, like walking back and forth, will also have a for loop in principle, so this question is paramount to my project.
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
It is not possible for Matlab to run more than 1 function at a time. Matlab is a strictly single-threaded programing environment, i.e., it executes the commands in the scripts in sequence. User can not write any multi-threaded code in Matlab directly. Some in-built Matlab functions do support multi-threading, and you can e.g. write a multi-threaded MEX function, but there are severe limitations: Matlab MEX interface (e.g., memory allocation) is not thread safe, so you either allocate in one thread, or use barriers before any call to Matlab functionality.
Sometimes you can use the timer functionality to "interrupt" the execution of a program and do something in the mean time, but there is still only one execution path at any given moment.
Also, you can start multiple workers in the Parallel Processing Toolbox. However, the "master" script is still single threaded and waits for the completion of the workers to obtain the results of computations.
What you want is called threaded operations. Matlab has pretty limited support for such things, but there is some. In particular, there is the batch command. Of course, this assumes you have the parallel processing toolkit.
batch start_function
In general, however, the same thing could be accomplished by a more careful loop, where you first get actions, then make the actions happen. Some actions could happen over the course of several frames, if you carefully keep track of the states. To turn your code into this would be complex, but let me show you the basic idea (This code won't run, but should show you roughly what to do):
player1_jump=false
player2_jump=false;
while(true)
key=getKey();
if key==PLAYER1_JUMP_KEY
player1_jump=true;
end
if key==PLAYER2_JUMP_KEY
player2_jump=true;
end
if player1_jump
%Move player 1 one step
if (done) %Put in your own criteria
player1_jump=false;
end
end
if player2_jump
%Move player 2 one step
if (done) %Put in your own criteria
player2_jump=false;
end
end
end
Note that you'd have to keep track of where in the jump each player is, etc. Also, a small pause statement would be required to get the gui updated. But the general pattern should hold, and I'll leave you to work out the details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With