How to simultaneously run two procedures without process
Author |
Message |
hnchmc
|
Posted: Sun Jan 24, 2016 1:19 pm Post subject: How to simultaneously run two procedures without process |
|
|
What is it you are trying to achieve?
I am trying to run two procedures simultaneously without the use of processes. Any help is greatly appreciated.
What is the problem you are having?
As I have read, processes are to be avoided at all costs. I am currently in the final stages of making a small Pacman game and have ran into a small speed bump with the ghosts. Up to this point, I have been using processes to fork the ghost movement. However, when a ghost is moving any direction (for this example it will be right), and reaches an intersection and randomly chooses to continue right, he picks up speed (doubling his X- Axis increment). This does not happen when he, for example, is moving right then decides to go up. If I am in fact making sense of what I have read, it is the randomness of processes that is causing this. Therefore, I turned to this forum for help with running two procedures at a time (The ghost and the rest of the game). Long story short, if the ghost continues moving the way he was previously, he picks up speed. Else, it works like it should.
Describe what you have tried to solve this problem
Well, I have turned to my teacher for help on this matter. She could not figure out why the process was breaking the game. I asked two senior programmers, they too could not help. Finally, I have spent two days scouring the internet for a solution and yielded no direct solution.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Sorry if it's a little long...
Turing: |
var flag1, flag2, flag3, flag4 : boolean := false
var ghost1X : int := 435
var ghost1Y : int := 400
var eyes := int
var ghostspeed : int := 60
% Eyes are a picture with 4 pos (LEFT, RIGHT, DOWN, UP)
%Snippet directly out of my code
%These are the processes for moving the ghost
process g1 % MOVE LEFT
loop
exit when flag1 = false
if flag1 = true then
ghost1X := ghost1X - 5
delay (ghostspeed )
end if
end loop
end g1
process g2 % MOVE RIGHT
loop
exit when flag2 = false
if flag2 = true then
ghost1X := ghost1X + 5
delay (ghostspeed )
end if
end loop
end g2
process g3 % MOVE DOWN
loop
exit when flag3 = false
if flag3 = true then
ghost1Y := ghost1Y - 5
delay (ghostspeed )
end if
end loop
end g3
process g4 % MOVE UP
loop
exit when flag4 = false
if flag4 = true then
ghost1Y := ghost1Y + 5
delay (ghostspeed )
end if
end loop
end g4
%Procedure for the red ghost
proc redGhost
%Checking if the Ghost's X and Ghost's Y are a specific intersection
if ghost1X = 320 and ghost1Y = 530 then
randint (gDirection, 1, 2)
flag1 := false %Assigning all flags false to exit process loop
flag2 := false
flag3 := false
flag4 := false
if gDirection = 1 then
flag2 := true
fork g2
eyes := 2
elsif gDirection = 2 then
flag3 := true
fork g3
eyes := 3
end if
end if
if ghost1X = 550 and ghost1Y = 530 then
randint (gDirection, 1, 2)
flag1 := false
flag2 := false
flag3 := false
flag4 := false
ignore := true
if gDirection = 1 then
flag1 := true
fork g1
eyes := 1
elsif gDirection = 2 then
flag3 := true
fork g3
eyes := 3
end if
end if
if ghost1X = 550 and ghost1Y = 435 then
randint (gDirection, 1, 3)
flag1 := false
flag2 := false
flag3 := false
flag4 := false
if gDirection = 1 then
flag4 := true
fork g4
eyes := 4
elsif gDirection = 2 then
flag2 := true
fork g2
eyes := 2
elsif gDirection = 3 then
flag3 := true
fork g3
eyes := 3
end if
end if
|
Please specify what version of Turing you are using
Version 4.1 (To work with the school's version) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Sun Jan 24, 2016 5:54 pm Post subject: RE:How to simultaneously run two procedures without process |
|
|
The reason processes are to be avoided is because they run simultaneously (sort of). I explain why here. Trying to make procedures run simultaneously will only cause all of the same problems.
You need to completely restructure your program so that you don't need to run anything simultaneously. Right now, your code is organized so each procedure has its own loop where it does its own thing over and over and over again. You need to re-arrange it so that your procedures have no loops, and only do their thing once, and call all of the procedures in one big loop.
Right now your code looks like this:
code: | process A
loop
%do stuff
end loop
end A
process B
loop
%do stuff
end loop
end B
fork A
fork B |
It should look more like this:
code: |
procedure A
%do stuff
end A
procedure B
%do stuff
end B
loop
A
B
end loop |
Now we only have one loop in the entire program and have eliminated processes completely. |
|
|
|
|
![](images/spacer.gif) |
hnchmc
|
Posted: Sun Jan 24, 2016 7:21 pm Post subject: RE:How to simultaneously run two procedures without process |
|
|
Ok, thank you very much! I was fuzzy on what had made a process so buggy. Now to finish the game! |
|
|
|
|
![](images/spacer.gif) |
|
|