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

Username:   Password: 
 RegisterRegister   
 Controlling a Character and Having an Object Move at the Same Time
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Greyscale




PostPosted: Sun Dec 14, 2014 10:31 pm   Post subject: Controlling a Character and Having an Object Move at the Same Time

What is it you are trying to achieve?
I'm making a dungeon crawler game, where the player controls a character and there is an enemy moving, guarding an object. The player has to get past the enemy to get to the object.


What is the problem you are having?
I can't get the player to be able to control the character and have the enemy move automatically at the same time. ie. I can't get two different parts to run at the same time.


Describe what you have tried to solve this problem
I've tried making processes of the enemy movement part, and forking it. Found out that forking should really only be used with music or audio.
I tried to call the two parts in as subprograms at the same time, got an error.
And of course, I looked online to see if anyone had a similar problem to mine. I didn't find any.

If I have indeed missed a post that deals with this same issue, please link me to it. Thanks!

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:

View.Set ("graphics:900;900, offscreenonly,nobuttonbar")

var x, y: int
var key : array char of boolean

x := 100
y := 440

% draws the level
procedure level
    Draw.Fill (0, 0, black, black)
   
    Draw.FillBox (0, 400, 150, 500, white)     
    Draw.FillBox (130, 400, 150, 500, grey)     
    Draw.FillBox (0, 400, 20, 500, grey)
   
   
    Draw.FillBox (200, 400, 400, 500, white)   
    Draw.FillBox (200, 400, 220, 500, grey)
    Draw.FillBox (300, 200, 350, 700, white) 
   
    Draw.FillBox (350, 650, 800, 700, white)   
    Draw.FillBox (700, 600, 750, 750, white)
    Draw.FillOval (775, 675, 10, 10, brightgreen) 
   
    Draw.FillBox (350, 200, 600, 250, white)   
   
    Draw.FillBox (550, 250, 600, 700, white) 
   
    Draw.FillBox (600, 400, 900, 500, white) 
    Draw.FillBox (880, 400, 900, 500, grey)
end level

% game loop
loop       
    % player movement and collision detection
    Input.KeyDown (key)
    if key ('w') then
        if whatdotcolour (x + 20, y + 21) not= black and whatdotcolour (x, y + 21) not= black then
            y := y + 5
        end if
    elsif key ('s') then
        if whatdotcolour (x + 20, y - 1) not= black and whatdotcolour (x, y - 1) not= black then
            y := y - 5
        end if
    end if
   
    if key ('d') then
        if whatdotcolour (x + 21, y) not= black and whatdotcolour (x + 21, y + 20) not= black then
            x := x + 5
        end if
    elsif key ('a') then
        if whatdotcolour (x - 1, y + 20) not= black and whatdotcolour (x - 1, y) not= black then
            x := x - 5
        end if
    end if
   
    Draw.FillBox (x, y, x + 20, y + 20, brightred)
    View.Update
    delay (10)
    cls
   
    % enemy movement
    for yEnemy : 600 .. 730
        level
        Draw.FillBox(720, yEnemy, 740, yEnemy+20, blue)
        View.Update
        delay(5)
        cls
    end for
        for decreasing yEnemy : 730 .. 600
        level
        Draw.FillBox(720, yEnemy, 740, yEnemy+20, blue)
        View.Update
        delay(5)
        cls
    end for
end loop



Please specify what version of Turing you are using
Open Turing
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sun Dec 14, 2014 10:50 pm   Post subject: RE:Controlling a Character and Having an Object Move at the Same Time

You need to learn about the idea of a game loop. Right now, you use a separate loop for everything that moves. The player gets a loop, the enemies get a loop, projectiles get their own loop, etc. To get everything to move together, you need to put everything into the same loop instead. Every time that loop completes an iteration, every animation will have advanced by one frame. It will check keyboard/mouse input once, calculate and update all sprite positions for the current frame once, do one frame's worth of other game logic and draw everything once.

It should look something like this:

code:

loop
    Mouse.Where (mx, my, mb) %get mouse state, if you're using the mouse
    Input.KeyDown (char) %get keyboard state
    if char (KEY_UP_ARROW) then %move the player character 1 frame
         playerY += 1
    end if
    if enemy1Y < playerY then %move enemy one frame
        enemy1Y += 1
    end if
    drawPlayer
    drawEnemy1
end loop


Obviously that code won't run but it should give you an idea of what your code should look like.
Greyscale




PostPosted: Mon Dec 15, 2014 4:38 pm   Post subject: Re: Controlling a Character and Having an Object Move at the Same Time

Hey, thanks for the answer. It was really helpful! The program runs fine, now all I have to do is clean it up a bit.
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 1  [ 3 Posts ]
Jump to:   


Style:  
Search: