-1
\$\begingroup\$

I have the following space invaders code which is working pretty well.

On invader creation:

//initialise invader counters
step_counter=100;
move_counter=0;
left=true;
move_speed=1;
speed_counter=0;

And on invader step:

if step_counter > 0 {
   step_counter -= move_speed;
   // Move every fifty steps
   if step_counter % 50 == 0 {
        if left == true {
            x += 15;
            move_counter++;
            // At right of screen, drop and change direction; speed up
            if move_counter == 4 {
                left = false;
                y += 15;
                speed_counter++
                if speed_counter == 5 {
                    move_speed += 1;
                    speed_counter = 0;
                }
            }
        }
        else {
            x -= 15;
            move_counter--;
            // At left of screen, drop and change direction; speed up
            if move_counter == -4 {
                left = true;
                y += 15;
                speed_counter++
                if speed_counter == 5 {
                    move_speed += 1;
                    speed_counter = 0;
                }
            }
        }
    }
}
else {
    // reset counter over 100 steps
    step_counter = 100;
}

This is working really nicely, and gives a feeling very close to the original arcade game. However, for some reason the invaders get stuck two thirds of the way down the screen and refuse to go any further.

And ideas why?

\$\endgroup\$
  • \$\begingroup\$ Can you get a screen shot and open it in Gimp or Photoshop and use a rectangle select to count the pixels? This will, at most, tell you the y boundary and allow you to figure out a mathematical reason behind the behavior. \$\endgroup\$ – Krythic Nov 24 '16 at 3:21
  • \$\begingroup\$ That's a really interesting idea. Wouldn't have though of that, thanks. \$\endgroup\$ – The Master Priest Nov 24 '16 at 10:50
4
\$\begingroup\$

I'm trying to run the code through in my head, so my apologies here.

Could it possibly be that roughly 2/3 of the way down the screen, that the move_speed happens such that you can't get to a step_counter that's a multiple of 50 anymore?

Your loop starts by decrementing step_counter by move_speed. For example, if step_counter starts at 100 and move_speed is 3, it'll go to 97 in the first iteration of the loop. As the loop progresses, step_counter will get to 52 and 49, but never 50. You'll never move, because the step_counter will never be a multiple of 50 (it's never 100 when the check is made, and it can never get to precisely 50).

You might try outputting step_counter and/or move_speed to a log file to check.

\$\endgroup\$
  • \$\begingroup\$ I'm pretty sure you're right on this but I'll have a look and make sure. Looks like I'll have to figure out another solution for incrementing the speed... I'm new to Gamemaker, so at first tried using console.log to output these variables as I would have in JavaScript... but this function isn't present. I also tried the inbuilt debugger, which while it has a space for local and global variables didn't seem to want to list instance variables. I'll look into getting step_counter and move_speed to output to a log file, as if that's possible in gamemaker it could be very useful. Thanks! \$\endgroup\$ – The Master Priest Nov 24 '16 at 10:47
2
\$\begingroup\$

For anyone who might be interested, this is the solution I've implemented and am 95% happy with:

Invader oncreation script:

//initialise invader step counter
step_counter=100;
move_counter=0;
left=true;
speed_counter=100;

Invader onstep script:

if step_counter > 0 {
   step_counter -= 1;
   // Only move when step gets to 10
   if step_counter == 10 {
        if left == true {
            x += 15;
            move_counter++;
            // Increase speed over 12 (if statement prevents invaders getting stuck bug)
            if speed_counter > 12 {
                speed_counter -= 2;
            }
            // At right of screen, drop and change direction
            if move_counter == 4 {
                left = false;
                y += 15;
            }
        }
        else {
            x -= 15;
            move_counter--;
            // Increase speed if over counter over 12 (if statement prevents invaders getting stuck bug)
            if speed_counter > 12 {
                speed_counter -= 2;
            }
            // At left of screen, drop and change direction
            if move_counter == -4 {
                left = true;
                y += 15;
            }
        }
    }
}
else {
    // reset counter over *speed_counter* steps
    step_counter = speed_counter;
}

The other answer was helpful me in working this out!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.