Computer Science Canada

Creating moving object within loop, without stopping the rest of the program

Author:  kiddcamel [ Thu Jan 17, 2019 11:15 am ]
Post subject:  Creating moving object within loop, without stopping the rest of the program

So, I've been working on a game, and up until this point I have stationary obstacles which end the loop if they are hit. However, I am now adding moving obstacles, but in order to do so I'm using a for loop. But using one makes it so I can't do anything until the for loop is done. How would I make it so everything else executes at the same time that the line is moving? I tried using a procedure called movingline but that didn't work either. Help would be appreciated Smile

%Global Variables
%Box 1 (moves)
var box1_x1 : int := 0
var box1_y1 : int := 0
var box1_x2 : int := 10
var box1_y2 : int := 10
%Box 2 (obstacle)
var box2_x1 : int := 15
var box2_y1 : int := 0
var box2_x2 : int := 40
var box2_y2 : int := 200
%Box 3 (obstacle)
var box3_x1 : int := 30
var box3_y1 : int := 400
var box3_x2 : int := 60
var box3_y2 : int := 215
%Moving line 1 (obstacle)
var line1_x1 : int := 500
var line1_y1 : int := 25
var line1_x2 : int := 600
var line1_y2 : int := 200
%Movement
var chars : array char of boolean
%Borders
var topmax : int := 395
var bottommax : int := 5
var rightsidemax : int := 635
var leftsidemax : int:= 5
%Font
var font4 : int
var font3 : int
font4 := Font.New ("Palatino:18:Bold,Italic")
font3 := Font.New ("Palatino:12:Bold,Italic")
%Main Code
%Moving line procedure
procedure movingline
for up : 0 .. 150
Draw.ThickLine (line1_x1, up, line1_x2, up, 10, 12)
delay (6)
cls
end for

for decreasing down : 150 .. 0
Draw.ThickLine (line1_x1, down, line1_x2, down, 10, 12)
delay (6)
cls
end for
end movingline

loop
%draw the boxes and obstacles
cls
drawfillbox (box1_x1, box1_y1, box1_x2, box1_y2, 9)
drawfillbox (box2_x1, box2_y1, box2_x2, box2_y2, 12)
drawfillbox (box3_x1, box3_y1, box3_x2, box3_y2, 12)
movingline

delay (20)
%Move box right
Input.KeyDown (chars)
if chars (KEY_RIGHT_ARROW) and box1_x1 <= rightsidemax and box1_x2 <= rightsidemax then
box1_x1 := box1_x1 + 4
box1_x2 := box1_x2 + 4
end if
%Move box left
if chars (KEY_LEFT_ARROW) and box1_x1 >= leftsidemax and box1_x2 >= leftsidemax then
box1_x1 := box1_x1 - 4
box1_x2 := box1_x2 - 4
end if
%Move box up
if chars (KEY_UP_ARROW) and box1_y1 <= topmax and box1_y2 <= topmax then
box1_y1 := box1_y1 + 4
box1_y2 := box1_y2 + 4
end if
%Move box down
if chars (KEY_DOWN_ARROW) and box1_y1 >= bottommax and box1_y2 >= bottommax then
box1_y1 := box1_y1 - 4
box1_y2 := box1_y2 - 4
drawfillbox (box2_x1, box2_y1, box2_x2, box2_y2, 12)
end if
%Conditions
%Obstacle 1
if box1_x2 > box2_x1 then
if box1_y1 < box2_y2 then
if box1_x1 < box2_x2 then
cls
Font.Draw ("you suck.", 260, 300, font4, 12)
exit
end if
end if
end if
%Obstacle 2
if box1_x2 > box3_x1 then
if box1_y2 > box3_y2 then
if box1_x1 < box3_x2 then
cls
Font.Draw ("you suck.", 260, 300, font4, 12)
exit
end if
end if
end if
%Timer
end loop

Author:  Insectoid [ Fri Jan 18, 2019 11:50 am ]
Post subject:  RE:Creating moving object within loop, without stopping the rest of the program

Why does a moving object need its own loop? Why can't it share the same loop as everything else?


: