Tuesday, June 22, 2021

【GAMEMAKER】Array

 Information about object: object0

Sprite: sprite0
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

/*Introduction/*
-The Variable Format
Here, we initialize variables. So, we'll initialize the arrays here.
A variable takes the format of owner.name[array] comparison value
For example, owner is everyone, as in global, the name is active, the array
is the index of this object. since we are initializing, the comparison would
be equals, telling it to set the variable to value.
global.active[object_index] = true
-The loop
There are many different kind of loops, but probably the best one for
arrays is the for loop. The for loop uses a variable which it keeps thrack
of throughout the loop, which can also be called from within. This is good
for initializing many arrays at once. Example, I have 100 arrays, and
instead of typing 100 lines to set them all to 0, I can do a for loop
which would only take 3 lines to set them all. See the executed code for
a valid example.
-The array value
Arrays must be greater than or equal to zero, and less than or equal to
32000. Beyond these bounds will return an error. Arrays must be a whole
number. This means that it may not contain a decimal like 1.2 or 4.86.
Such values will be rounded off to the nearest whole number, without
returning an error. Arrays may not contain a string (error), but may
contain a variable, or even another arrayed variable, as long as the
value of the variable follows the above rules.
-This script
Here, we initialize 100 arrays, and set them all to 0. Starts at 0, ends at
99.
*/

for (m = 0; m < 100; m += 1) {
 global.item[m] = 0
}

//Next, we tell all items that they are not active

with (all) {
 global.active[object_index] = false
}

//Nobody is active

global.primary = false

/*This example will show you how to pick random numbers between 1 and 100,
but never pick the same number twice. Now I'm going to initialize
some things that will be needed for this.*/

for (m = 1; m <= 100; m += 1) {
 global.drawn[m] = false //false = 0
}
global.top = 0
global.drawn[0] = true //true = 1. Although we never use the 0'th array, it
                       //prevents an error that may occur later
Mouse Event for Left Pressed:
execute code:

/*Using arrays to your advantage/*
-Sorting through all arrays for the one you are looking for
Here, we will look through all the arrays to see if there is a number that
has not been drawn yet.
Please note, m is just a letter I like to use for loops. You can use whatever
variable name you want. You can use however many letters you want to name
your variable, but 1 letter is easier to type than 5 letters.
*/

ok = 0  //it is not ok to continue... yet.
for (m = 1; m <= 100; m += 1) {
 if global.drawn[m] = 0 { //look through all arrays for one that is still 0.
  ok = 1  //if one is found, we may continue.
  break   //break out of this loop, since we don't need to keep going.
 }
}

if ok = 0 {  //if none were found.
 show_message("No more")
 exit   //the exit statement breaks out of a script. It works the same as
}       //an else statement.

//And here's the hard core stuff. This is where we pick the random number.
//Now we introduce you to a new loop, the while loop. As long as the contents
//are true, the loop will continue. Hint, it is ideal if somewhere within
//the loop you change something so that the contents are no longer true.
while (global.drawn[global.top] = 1) {
 global.top = floor(random(100))+1 //pick an integer between 1 and 100.
}
//fortunately, we don't have to pick a random number before the while loop
//AND within the while loop, because the contents are already true.
//If this is the first time around, remember that drawn[0] is true, and top
//is 0. If it's the second time around, it will take the following steps
//into account, instead.
global.drawn[global.top] = true
Draw Event:
execute code:

/*What the user sees/*
-Putting everything together and making it effect the game.
Now we're ready to let the user know what's going on. Without this step,
the user would be blind as to why he is clicking a black square...
What we are going to do is draw the unpicked numbers, not really caring
if they go off the screen, since this isn't a professional program.
We will also display the currently active number next to the black box.
And, we'll draw all drawn values on the right.
Please note, when you draw something, it MUST go in the draw event.
When an object has a draw event, if the object is set to visible, it will
draw whatever it is told to draw, and NOT draw itself. You must tell it to
draw itself in the draw event.
*/

//Draw me, since I've disappeared due to my draw event
draw_sprite(sprite_index,image_single,x,y)
//I normally make a script for this, called "draw_me"

//Let the user know what to do... A # sign indicates a new line.
draw_text(200,0,
 "Click the black square to draw a random number.
 #Hit enter to restart"
)

//Current value is easy... remember that when using numbers in place of a
//string, we must turn the number into a string, otherwise you won't see
//anything. 1 = "". string(1) = "1".
draw_text(x+64,y+8,string(global.top))

//All undrawn values.
draw_text(0,0,"Undrawn")
yy = 16 //the y coordinate to draw the value
for (m = 1; m <= 100; m += 1) {  //run through all arrays
 if global.drawn[m] = 0 {  //if this one hasn't been drawn yet
  draw_text(0,yy,string(m))  //draw it
  yy += 16  //increase the y coordinates.
 }
}

//All drawn values.
xx = room_width - string_width("Drawn") //set the x to fit "Drawn" in there.
draw_text(xx,0,"Drawn")
yy = 16  //This is what I call a temporary variable. It is used and reused,
         //and then ignored when it isn't needed.
for (m = 1; m <= 100; m += 1) {
 if global.drawn[m] > 0 {  //here we use the greater than comparison.
  draw_text(xx,yy,string(m))
  yy += 16
 }
}
Key Press Event for <Enter> Key:
execute code:

game_restart()

No comments:

Post a Comment

End of Summer Sale ☀️😎

20% OFF Inside!🤯 ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏...