Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 more than one movement at the same time
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
(O_o)




PostPosted: Fri Jan 13, 2006 5:31 pm   Post subject: more than one movement at the same time

hi
i'm trying to make a game where i have a box and i can move it with the arrow keys, and also want to move circles across the screen at the same time. when ever i try to do it, only the circles move. I was wonder if someone could explain to me how to do both at the same time.



game.t
 Description:

Download
 Filename:  game.t
 Filesize:  6.14 KB
 Downloaded:  95 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
(O_o)




PostPosted: Fri Jan 13, 2006 5:42 pm   Post subject: (No subject)

sorry for the double post but if you don't want to download the program here it is

code:

%declare variables and screen
setscreen("graphics : 400;400")


%set variables for moving and ship
    var chars : array char of boolean
    var x1, y1, x2, y2 : int

%ship variables
    x1 := 308
    y1 := 195
    x2 := 349
    y2 := 206

%set vars for shooting
    var sx1, sy1, sx2, sy2, shoot : int

    shoot:= 0
    sx1 := x1+5
    sy1 := y1+12
    sx2 := x2-5
    sy2 := y2+12
   
%set variables for asteroids
var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 :int := 0

var b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 :int
b1:= Rand.Int(0,maxx)
b2:= Rand.Int(0,maxx)
b3:= Rand.Int(0,maxx)
b4:= Rand.Int(0,maxx)
b5:= Rand.Int(0,maxx)
b6:= Rand.Int(0,maxx)
b7:= Rand.Int(0,maxx)
b8:= Rand.Int(0,maxx)
b9:= Rand.Int(0,maxx)
b10:= Rand.Int(0,maxx)
   
%var for intro button   
    var x,y,b : int

%background, for intro
drawfillbox (0,0,maxx,maxy,black)

for i : 0 .. 250 %stars in background
    drawdot (Rand.Int (0, maxx), Rand.Int (0, maxy), 0)
end for

%draw button
drawbox (250, 100, 400, 175, 0)

%button/title/autor/insructions
Font.Draw ("Start", 290, 130,(Font.New ("arial:20:bold,")),white)
Font.Draw ("ASTEROID BLASTER", 125, 325,(Font.New ("arial:30:bold,")),white)

Font.Draw ("Instructions", 290, 75,(Font.New ("arial:10:bold,")),white)
Font.Draw ("Shoot the asteroids as they come down. Each time you shoot one, the next one comes faster.", 23, 55,(Font.New ("arial:10:bold,")),white)
Font.Draw ("If you let one go past you or if you get hit by one then you lose!", 125, 35,(Font.New ("arial:10:bold,")),white)

% mouse button
loop                                                    % A
mousewhere (x,y,b)

if x>250 and x<400 and y>100 and y<175 and b=1 then     % B
    delay (5)
cls

%background
drawfillbox (0,0,maxx,maxy,black)

for i : 0 .. 250 %stars in background
    drawdot (Rand.Int (0, maxx), Rand.Int (0, maxy), 0)
end for
 
%draw ship   
    drawfillbox (x1, y1, x2, y2, 0)

%set movement
loop                                                    % C
    Input.KeyDown (chars) %set input key down for movement
    locate (1, 1)
   
%up arrow/direction
    if chars (KEY_UP_ARROW) then                        % D
        if y2 < 380 then                        %stop from going off the screen
            drawfillbox (x1, y1, x2, y2, 255)   %to cover box
            y1 += 5                             %to move box
            y2 += 5
            drawfillbox (x1, y1, x2, y2, 0)     %box
            delay (0)
        end if

%right arrow
    elsif chars (KEY_RIGHT_ARROW) then
        if x2 < maxx then                       %stop from going off the screen
            drawfillbox (x1, y1, x2, y2, 255)   %to cover box
            x1 += 5                             %to move box
            x2 += 5
            drawfillbox (x1, y1, x2, y2, 0)     %box
            delay (0)
        end if

%left arrow
    elsif chars (KEY_LEFT_ARROW) then
        if x1 > 0 then                          %stop from going off the screen
            drawfillbox (x1, y1, x2, y2, 255)   %to cover box
            x1 -= 5                             %to move box
            x2 -= 5
            drawfillbox (x1, y1, x2, y2, 0)     %box
            delay (0)
        end if

%down arrow
    elsif chars (KEY_DOWN_ARROW) then
        if y1 > 0 then                          %stop from going off the screen
            drawfillbox (x1, y1, x2, y2, 255)   %to cover box
            y1 -= 5                             %to move box
            y2 -= 5
            drawfillbox (x1, y1, x2, y2, 0)     %box
            delay (0)
        end if
    end if                                              % D

%make shooting   
    if chars (KEY_ENTER) then
        shoot:=1
    end if
    if shoot = 1 then     
        drawfillbox (sx1, sy1, sx2, sy2, 255)   %to cover shot
        sy1 += 10                               %to move box
        sy2 += 10
        drawfillbox (sx1, sy1, sx2, sy2, 14)    %shot   
    end if
        if sy1> maxy then
            shoot := 0
        end if
        if shoot = 0 then
            sx1 := x1+5
            sy1 := y1+12
            sx2 := x2-5
            sy2 := y2+12
        end if
delay (50) 

    loop
        drawoval (b1 , maxy-a1, 20, 20, 0)
        delay (18)
        drawoval (b1 , maxy-a1, 20, 20, 255)
        a1:= a1+1
    exit when a1 = 400
    end loop

    delay (1000)   
       
    loop
        if a1 > 200 then
        drawoval (b2 , maxy-a2, 20, 20, 0)
        delay (16)
        drawoval (b2 , maxy-a2, 20, 20, 255)
        a2:= a2+1
        end if
    exit when a2 = 400
    end loop

    delay (1000)
   
    loop
        if a2 > 200 then
        drawoval (b3 , maxy-a3, 20, 20, 0)
        delay (14)
        drawoval (b3 , maxy-a3, 20, 20, 255)
        a3:= a3+1
        exit when a3 = 400
        end if
    end loop

    delay (1000)   
       
    loop
        if a3 > 200 then
        drawoval (b4 , maxy-a4, 20, 20, 0)
        delay (12)
        drawoval (b4 , maxy-a4, 20, 20, 255)
        a4:= a4+1
        end if
    exit when a4 = 400
    end loop
   
    delay (1000)   
       
    loop
        if a4 > 200 then
        drawoval (b5 , maxy-a5, 20, 20, 0)
        delay (10)
        drawoval (b5 , maxy-a5, 20, 20, 255)
        a5:= a5+1
        end if
    exit when a5 = 400
    end loop
   
    delay (1000)

    loop
        if a5 > 200 then
        drawoval (b6 , maxy-a6, 20, 20, 0)
        delay (8)
        drawoval (b6 , maxy-a6, 20, 20, 255)
        a6:= a6+1
    exit when a6 = 400
    end if
    end loop

    delay (1000)   
       
    loop
        if a6 > 200 then
        drawoval (b7 , maxy-a7, 20, 20, 0)
        delay (6)
        drawoval (b7 , maxy-a7, 20, 20, 255)
        a7:= a7+1
        end if
    exit when a7 = 400
    end loop

    delay (1000)
   
    loop
        if a7 > 200 then
        drawoval (b8 , maxy-a8, 20, 20, 0)
        delay (4)
        drawoval (b8 , maxy-a8, 20, 20, 255)
        a8:= a8+1
        exit when a8 = 400
        end if
    end loop

    delay (1000)   
       
    loop
        if a8 > 200 then
        drawoval (b9 , maxy-a9, 20, 20, 0)
        delay (2)
        drawoval (b9 , maxy-a9, 20, 20, 255)
        a9:= a9+1
        end if
    exit when a9 = 400
    end loop
   
    delay (1000)   
       
    loop
        if a9 > 200 then
        drawoval (b10 , maxy-a10, 20, 20, 0)
        delay (0)
        drawoval (b10 , maxy-a10, 20, 20, 255)
        a10:= a10+1
        end if
    exit when a10 = 400
    end loop

end loop                                                % C

end if                                                  % B

end loop                                                % A
iker




PostPosted: Fri Jan 13, 2006 5:58 pm   Post subject: (No subject)

instead of having all of the directions in one if statement..
code:

if key (KEY_LEFT_ARROW) then
left
elsif key (KEY_RIGHT_ARROW) then
right
elsif key (KEY_UP_ARROW) then
up
elsif key (KEY_DOWN_ARROW) then
down
end if

try puting them in separate if statements, opposite directions such as up and down can be in the same one..
code:

if key (KEY_UP_ARROW) then
up
elsif key (KEY_DOWN_ARROW) then
down
end if

if key (KEY_LEFT_ARROW) then
left
elsif key (KEY_RIGHT_ARROW) then
right
end if

hope that helps
(O_o)




PostPosted: Fri Jan 13, 2006 6:26 pm   Post subject: (No subject)

thanks for the tip
i treid it but it didn't work
i can move the ship until add the asteroids, then it i cant move it anymore.

so if i take out this part:
code:

loop
        drawoval (b1 , maxy-a1, 20, 20, 0)
        delay (18)
        drawoval (b1 , maxy-a1, 20, 20, 255)
        a1:= a1+1
    exit when a1 = 400
    end loop

    delay (1000)   
       
    loop
        if a1 > 200 then
        drawoval (b2 , maxy-a2, 20, 20, 0)
        delay (16)
        drawoval (b2 , maxy-a2, 20, 20, 255)
        a2:= a2+1
        end if
    exit when a2 = 400
    end loop

    delay (1000)
   
    loop
        if a2 > 200 then
        drawoval (b3 , maxy-a3, 20, 20, 0)
        delay (14)
        drawoval (b3 , maxy-a3, 20, 20, 255)
        a3:= a3+1
        exit when a3 = 400
        end if
    end loop

    delay (1000)   
       
    loop
        if a3 > 200 then
        drawoval (b4 , maxy-a4, 20, 20, 0)
        delay (12)
        drawoval (b4 , maxy-a4, 20, 20, 255)
        a4:= a4+1
        end if
    exit when a4 = 400
    end loop
   
    delay (1000)   
       
    loop
        if a4 > 200 then
        drawoval (b5 , maxy-a5, 20, 20, 0)
        delay (10)
        drawoval (b5 , maxy-a5, 20, 20, 255)
        a5:= a5+1
        end if
    exit when a5 = 400
    end loop
   
    delay (1000)

    loop
        if a5 > 200 then
        drawoval (b6 , maxy-a6, 20, 20, 0)
        delay (8)
        drawoval (b6 , maxy-a6, 20, 20, 255)
        a6:= a6+1
    exit when a6 = 400
    end if
    end loop

    delay (1000)   
       
    loop
        if a6 > 200 then
        drawoval (b7 , maxy-a7, 20, 20, 0)
        delay (6)
        drawoval (b7 , maxy-a7, 20, 20, 255)
        a7:= a7+1
        end if
    exit when a7 = 400
    end loop

    delay (1000)
   
    loop
        if a7 > 200 then
        drawoval (b8 , maxy-a8, 20, 20, 0)
        delay (4)
        drawoval (b8 , maxy-a8, 20, 20, 255)
        a8:= a8+1
        exit when a8 = 400
        end if
    end loop

    delay (1000)   
       
    loop
        if a8 > 200 then
        drawoval (b9 , maxy-a9, 20, 20, 0)
        delay (2)
        drawoval (b9 , maxy-a9, 20, 20, 255)
        a9:= a9+1
        end if
    exit when a9 = 400
    end loop
   
    delay (1000)   
       
    loop
        if a9 > 200 then
        drawoval (b10 , maxy-a10, 20, 20, 0)
        delay (0)
        drawoval (b10 , maxy-a10, 20, 20, 255)
        a10:= a10+1
        end if
    exit when a10 = 400
    end loop


and try to move it works
so i only can't move when this part comes in
Tony




PostPosted: Fri Jan 13, 2006 7:08 pm   Post subject: (No subject)

that is because you have a bunch of loops with delays in them. It takes quite a while for those loops to finish, and you're not checking for any input while inside the loops.

no input -- no movement
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
(O_o)




PostPosted: Fri Jan 13, 2006 7:44 pm   Post subject: (No subject)

oooo
i get what you mean
so you're saying that i need to get the program to check for inputs while the asteroids fall.
hmmm
how would i do that though?
Tony




PostPosted: Fri Jan 13, 2006 8:31 pm   Post subject: (No subject)

step procedures

code:

loop
   check input
   you move 1 step
   asteroid moves 1 step
end loop


currently you have your loops set up so that an asteroid would move from beginning to end before letting go of the controls. Try to figure out how to let the asteroid move just 1 step, let go of the controls, and resume from where it left of next time Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
(O_o)




PostPosted: Fri Jan 13, 2006 8:46 pm   Post subject: (No subject)

i'm not sure of what you saying....
what do you mean by letting go of the controls?
do you mean that i should get the program to check for inputs constantly before each move of the asteroid?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Jan 13, 2006 9:14 pm   Post subject: (No subject)

(O_o) wrote:
do you mean that i should get the program to check for inputs constantly before each move of the asteroid?

not only check, but also process and move the user accordingly
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
(O_o)




PostPosted: Fri Jan 13, 2006 9:30 pm   Post subject: (No subject)

so do you mean somthing like this?
code:

    loop
   
    if chars (KEY_UP_ARROW) then               
        if y2 < 380 then                        %stop from going off the screen
            drawfillbox (x1, y1, x2, y2, 255)   %to cover box
            y1 += 5                             %to move box
            y2 += 5
            drawfillbox (x1, y1, x2, y2, 0)     %box
        end if

        drawoval (b1 , maxy-a1, 20, 20, 0)
        delay (18)
        drawoval (b1 , maxy-a1, 20, 20, 255)
        a1:= a1+1
    exit when a1 = 400
    end loop
Tony




PostPosted: Fri Jan 13, 2006 9:50 pm   Post subject: (No subject)

not quite

your asteroid will not move unless UP_ARROW is pressed Razz

try to separate everything into procedures
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
(O_o)




PostPosted: Sat Jan 14, 2006 12:03 pm   Post subject: (No subject)

example?
iker




PostPosted: Sat Jan 14, 2006 3:12 pm   Post subject: (No subject)

heres an example
code:

proc asteroidmove
    asteroidy -= asteroid speed
end asteroidmove

proc detectcollision
    if asteroidy = paddley and asteroidx = paddlex then
        %boom
    end if
end detectcollision

proc paddlemove
    if key (KEY_UP_ARROW) then
        up
    elsif key (KEY_DOWN_ARROW) then
        down
    end if

    if key (KEY_LEFT_ARROW) then
        left
    elsif key (KEY_RIGHT_ARROW) then
        right
    end if
end paddlemove

loop
    paddlemove
    asteroidmove
    detectcollison
end loop
(O_o)




PostPosted: Sun Jan 15, 2006 11:41 am   Post subject: (No subject)

wow..
it worked!
thanks alot!
but, there were two problems.....

one being that as the asteroids come down they flash...but i'm not sure if that's fixable...

two that the collision wouldn't work. the bulletjust goes through the asteriod...
Cervantes




PostPosted: Sun Jan 15, 2006 11:56 am   Post subject: (No subject)

Use View.Update.
http://www.compsci.ca/v2/viewtopic.php?t=3077
http://www.compsci.ca/v2/viewtopic.php?t=193
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 26 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: