Drag And Drop Coding levels?

I

Isla

Guest
Hey, everyone!
I (very recently) started using gamemaker and finished the tutorials. I've now got a fully-functioning remake of Asteroids, and I'd like to implement a feature where every time you finish a level, you get one more asteroid than in the previous one. As you get 50 for shooting a big one, and then 75 for the smaller ones, that means you get 200 points per asteroid in total. I was wondering if there's a way to make a function that adds more asteroids depending on the score, but automatic. Thanks to the tutorial, I have the coding that only activates if the screen is empty (which stops it from looping). The first level starts with 5 asteroids, so after that, you have 1000 points.
So far, I've only got about:

if (global.points = 1000)
{
if (!instance_exists(obj_asteroid))
{
instance_create(random(room_width),random(room_height),obj_asteroid);
instance_create(random(room_width),random(room_height),obj_asteroid);
instance_create(random(room_width),random(room_height),obj_asteroid);
instance_create(random(room_width),random(room_height),obj_asteroid);
instance_create(random(room_width),random(room_height),obj_asteroid);
}
}


How would I go about adding more of these, without just copying this code, adding an extra line of
instance_create(random(room_width),random(room_height),obj_asteroid);
and making the if-statement:
if (global.points = 2000)
I'd like something that said something like (global.points - global.points from the last level) /200 = amount of asteroids added. How would I go about adding that?
 
H

Husi012

Guest
Hi
First problem: you can use a repeat loop:
Code:
repeat(5){
instance_create(random(room_width),random(room_height),obj_asteroid);
}
Second problem:
You'll need to create a second global variable, where you save the last global points. For example: global.last_points
In every Level you completed you save the points in this variable. global.last_points=global.points;
Now you can change the if-statement like this:
if((global.points-global.last_points)/200 == instance_number(obj_asteroid))
eventually yoou can change == to >= because the amount could greather than the given value ;)
 

Tsa05

Member
Welcome to the GameMaker forums!

Husi has provided most of the information there; let's see if I can organize it a bit:
First, I think that it might also be useful to keep track of how many asteroids you'd like to have. Your game might start with something like this:

global.points = 0;
global.asteroidNumber = 5;

It looks like the code you provided is going in the Step event, right? You are checking whether the score has reached the amount needed to clear all asteroids? I assume that 5 Asteroids are created when the game begins. So we'll replace 1000 points with a simple formula instead.

We know that the number of points needed for this level is equal to global.asteroidNumber times 200.
Imagine that there were levels with 1, 2, 3, and 4 asteroids. The scores for these would be:
1*200
2*200
3*200
4*200

So, the score needed for any level could be computer like this:
Code:
var tempSum = 0;
for(var i = global.asteroidNumber; i>=0; i-=1)
{
     tempSum += i*200;
}
The code above creates a temp value and increments it by 200*counting number. It starts with a counting number (i) equal to asteroidNumber (5), and then counts down (i-=1) until it reaches zero. Each counting number is multiplied by 200.

But that code assumes you have levels that go all the way down to 1 asteroid. You're starting with 5! So we revise by setting the looping condition to 5 instead of zero:
Code:
var tempSum = 0;
for(var i = global.asteroidNumber; i>=5; i-=1)
{
     tempSum += i*200;
}
So, tempSum now holds the amount of points needed to cleat the level, given the current number of asteroids. Applying this to your code:

Code:
if (global.points == tempSum)
{
     if (!instance_exists(obj_asteroid))
     {
          global.asteroidNumber = global.asteroidNumber + 1;
          repeat(global.asteroidNumber)
          {
               instance_create(random(room_width),random(room_height),obj_asteroid);
          }
     }
}
Something like that ought to work! It's keeping track of the number of asteroids, then calculating how many points are needed to clear a level, which is sort of the reverse of what you asked for (working out asteroids based on points). Sorry about that; it just seemed that keeping the number and adding one each level would more directly provide the numbers you need!
 
R

Ratatosque

Guest
Why do you want to count in the points?
Just count the number of asteroids per level, create that many, and add one after that.

A simple solution could be this for loop.

//in some start event you set the basic asteroid count:
global.asteroid_count=5;

//in the step event, in case all asteroids are shot or left the room:

if !instance_exists(obj_Asteroid)
{

//this for loop starts at i=1, does something if i is lower or equal to the asteroid count, and then adds 1 to i
//basically it repeats the action in brackets as long as i is lower or equal to global.asteroid_count
for (i=1;i<=global.asteroid_count;i+=1)
{

//in our case it creates an asteroid in each repetition
instance_create(random(room_width),random(room_height),obj_Asteroid);
}

//when the loop is over the variable for number of asteroids is increased by one, so the next loop repeats one additional time
global.asteroid_count+=1;
}


With this solution however the level ends when there are no more asteroids, not when you reached a certain score.
 
Last edited by a moderator:
Top