1
\$\begingroup\$

I am new to game development and I have a game where the car moves and picks the monster.But now I want to automate the game so it looks like it is played automatically. The control keys are given by the update function which is as below:

update(){
        if (keys.ArrowUp) { // Player holding up
            this.y -= this.speed * frameTime;
            this.dir = Math.PI * 0; // set direction
        }
        if (keys.ArrowDown) { // Player holding down
            this.y += this.speed * frameTime;
            this.dir = Math.PI * 1; // set direction
          }
          if (keys.ArrowLeft) { // Player holding left
            this.x -= this.speed * frameTime;
            this.dir = Math.PI * 1.5; // set direction
        }
        if (keys.ArrowRight) { // Player holding right
            this.x += this.speed * frameTime;
            this.dir = Math.PI * 0.5; // set direction
        }        
        if(Math.sign(this.speed) === -1){ // filp directio of second car
            this.dir += Math.PI; // set direction
        }

So Eventually I need this update function to be automated.Automated in the sense you can think of the one car moves 3 seconds straight and 3 seconds right and so on.But the other car follows a different movement. Incase my code is not sufficient the working pen is here and I just want to automate the process so that the cars move in different directions and the game is automated.

Any help is appreciated

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Please avoid linking to external resources as volatile as codepen, as it will change as you modify it to make it work, making this question lose all its value. Please use the edit box features to put the code here. \$\endgroup\$
    – Vaillancourt
    Commented Apr 18, 2017 at 0:43
  • \$\begingroup\$ I edited and put the code which I need to automate.But the pen is for just reference if this is not sufficient \$\endgroup\$
    – Josh
    Commented Apr 18, 2017 at 1:55
  • 1
    \$\begingroup\$ Well; what do you want your game to do automatically? Drive towards the monster? Make the monster attack the car? What your movement code looks like is irrelevant in this case; it's the behavior we need to know. \$\endgroup\$
    – Charanor
    Commented Apr 18, 2017 at 1:59
  • \$\begingroup\$ @Charanor Drive towards the monster actually. I need the monster to be stable and the car to attack the monster and when one monster is attacked then create a new monster for that monster and so on. \$\endgroup\$
    – Josh
    Commented Apr 18, 2017 at 2:11
  • \$\begingroup\$ @Charanor Is my comment clear enough or should I need to explain more \$\endgroup\$
    – Josh
    Commented Apr 18, 2017 at 4:37

1 Answer 1

1
\$\begingroup\$

Your Update function holds too many responsibilities. It looks like it currently is responsible for:

  • Handling a tick
  • Processing user input
  • Updating car position
  • Updating car direction

By delegating responsibilities you can simplify things enough so that you can do more things, such as moving computer controlled cars.

update(){
    if (keys.ArrowUp)
       MoveUp();
    else if (keys.ArrowDown)
       MoveDown();
    else if (keys.ArrowLeft)
       MoveLeft();
    else if (keys.ArrowRight)
       MoveRight();
    else
       Stop();
}

From there you can make a car into an independent class with many instances. With a single instance, the function will then look like this:

update(){
    if (keys.ArrowUp)
       playerCar.MoveUp();
    else if (keys.ArrowDown)
       playerCar.MoveDown();
    else if (keys.ArrowLeft)
       playerCar.MoveLeft();
    else if (keys.ArrowRight)
       playerCar.MoveRight();
    else
       playerCar.Stop();
}

You can now split off the responsibility of handling input:

 MoveEnum = {
    Up: 0,
    Down: 1,
    Left: 2,
    Right: 3
    Stop: 4
 }

 update(){
    var direction = MoveEnum.Stop;
    if (keys.ArrowUp)
       direction = MoveEnum.Up;
    else if (keys.ArrowDown)
       direction = MoveEnum.Down;
    else if (keys.ArrowLeft)
       direction = MoveEnum.Left;
    else if (keys.ArrowRight)
       direction = MoveEnum.Right;
    playerCar.Move(direction);
}

... and hand off the input handling into it's own function so we get:

update(){
    var direction = GetMoveDirection(keys);
    playerCar.Move(direction);
}

or

update(){
    playerCar.Move(GetMoveDirection(keys));
}

At this point, introducing computer controlled cars becomes easier:

update(){
    playerCar.Move(GetMoveDirection(keys));
    computerCar.Move(FigureOutComputerDirection());
}

The content of FigureOutComputerDirection() is up to you. If you just want to randomly move the car, you could for example look at the time and chose a new direction every second.

\$\endgroup\$
1
  • \$\begingroup\$ Yeah thanks for the valuable explanation.I managed to do it by this method \$\endgroup\$
    – Josh
    Commented Apr 21, 2017 at 6:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .