Minigame: Clicking on a grid...please keep me afloat here! (solved)

Started by Bearstonaut, Thu 30/06/2011 21:43:07

Previous topic - Next topic

Bearstonaut

Hi all,

I'm literally at the finish-line on developing an ambitious 50-room game that has consumed me for the past 6 months.  Considering I am the world's worst programmer, this has forced me to learn on the go, and I couldn't have done it without the help of everyone here.

Anyway, enough of my blathering, here's the problem:

I'm happy with how the puzzles have worked out, but the ending really would benefit from the inclusion of a minigame.  Lazily, I was hoping that something existed in the public realm that I could modify enough to fit nicely with my own story and aesthetics.  Sadly, I've gone through the forums and agsresources carefully and it appears the only minigames that survived the collapse of geocities are the Demoquest slot machine, combination locks and a game of Battleship.  I took a stab at a slider puzzle but eventually decided it was beyond my capabilities and abandoned it for now.  So it looks like I'm going to have to write something from scratch that is a lot simpler, and I would appreciate some support.

Story-wise, your protagonist needs to keep an AI from losing "her" cool and killing everyone by "re-routing" her "logical, heuristic, etc." subroutines to her "ethical" subroutines as they begin to fail.  In terms of what this means for gameplay mechanics -- I have no idea.

My thinking is that I could display a 10x10 grid...and the player must simply click on grid-squares as they begin to change color to "reset" them.  This getting gradually more difficult as the number of squares changing color every second gradually increases.  I imagine for every square that does successfully change color, it increases the likelihood and speed of its surrounding squares from doing to same, so the player needs to work to avoid an escalating cascade effect.  Ultimately, the player need only keep the whole grid from changing color long-enough to trigger the end-game sequence.  Credits will roll.  Players will cheer.

I figure this is more manageable than, say, anything else I can think of.

But I'm at a loss regarding where to start.

Please help!

Khris

You start with an array:

Code: ags
// top of room script

int f[100];

#define threshold 30


As for the mechanics, I was thinking each square starts out with a random value ranging from, say, 0 to 20.
Then, each frame i.e. 40 times per second, a square is picked randomly. Its value is increased by 1 plus the number of surrounding squares that already changed color.
Say 30 is the threshold, once a square reaches 31 it changes color and becomes clickable. Clicking it resets the counter back to 0 (or again a random value).
We can always adjust all these values later on.

As for the technical side:

Code: ags
bool grid_active;

void init_grid() {

  int i;
  while (i < 100) {
    f[i] = Random(20);
    i++;
  }
  grid_active = true;
}

int neighbors(int i) {

  int re;
  if (i % 10 && f[i-1] > threshold) re++;  // left
  if (i > 9 && f[i-10] > threshold) re++;  // up
  if (i % 10 < 9 && f[i+1] > threshold) re++;  // right
  if (i < 90 && f[i+10] > threshold) re++; // down
  return re;
}


This assigns random values and calculates the number of changed neighbors.

Drawing the grid:
Code: ags
#define square_size 20
#define xoffset 60
#define yoffset 20

int draw_grid() {
  DrawingSurface*ds = Room.GetDrawingSurfaceForBackground();
  
  int i, x, y;
  int re;
  while(i < 100) {

    if (f[i] > threshold) {
      re++;  // count reds
      ds.DrawingColor = Game.GetColorFromRGB(255, 0, 0);  // red
    }
    else ds.DrawingColor = Game.GetColorFromRGB(0, 255, 0);  // green

    x = (i%10)*square_size+xoffset;
    y = (i/10)*square_size+yoffset;
    ds. DrawRectangle(x, y, x + square_size-1, y + square_size-1);
    i++;
  }
  ds.Release();
  return re;
}


In room_RepExec (which you create via room events as usual):
Code: ags
  if (grid_active) {
    int i = Random(99);
    if (f[i] <= threshold) f[i] += 1 + neighbors(i);

    if (draw_grid() == 100) {
      grid_active = false;
      Display("GAME OVER");
    }
  }


What's left is clicks:

Code: ags
void on_mouse_click(MouseButton button) {

  int x = (mouse.x - xoffset)/square_size;
  if (x < 0 || x > 9) return;
  int y = (mouse.y - yoffset)/square_size;
  if (y < 0 || y > 9) return;
 
  int i = y*10 + x;

  if (f[i] > threshold) f[i] = Random(20);  // reset square
}


Now call init_grid() in room_Load() or after the AI starts loosing her cool and off you go.

Bearstonaut

Thank you Khris.  This has been a tremendous help to me.  Exactly what I had envisioned!

The RepExec victory condition,

Quote
   if (draw_grid() == 100) {
     grid_active = false;
     Display("GAME OVER");
   }

Isn't working.  The game never picks up an integer value for draw_grid()  greater than 0.  The filling of red squares just continues.  Any ideas?

monkey0506

#3
Well he forgot to return a value from the function, so the return value is defaulting to the integer default value, which is 0 (zero).

I'm not sure what value he meant to return if the "victory" result is 100, but therein is the problem.

Edit: Upon further examination of the code, I think that if you add the following line to the end of the draw_grid function you'll get the expected results:

Code: ags
  return (100 - re);


Since your grid is 10x10, obviously there's 100 tiles in total, and re is the number of red tiles, so (100 - re) would give the number of green tiles. So I'm guessing once they're all green then victory is achieved..?

Bearstonaut

Thanks monkE3y_05_06,

return (re); gives me my failure outcome, which is what I'm going for.  Ultimately the player will be fighting time just trying to keep the AI from its ultimate collapse.

I will be sure to mention you both, and everyone else who has given me a hand, in the credits.

Thanks again.

mystazsea

Or you could do it all on a Custom Gui with your own Graphical Background image and use Buttons which line up visually for each Grid node that you need...in your case a 10 by 10 grid of Any visual style of Node you like
then script each Button  you need ..that way each grid node or Square you need can have it own
Animated View Graphic or press-able/Click-able state
connect this to its own script and bobs your uncle!!!
So if you follow my drift ..each button has a connection to the Array or Variables you set out
in  the mechanics of the room/Gui, the Back end..
The Gui Visual will handle the Front end Graphics for you...

just make sure each button is connected in the right way on the coding side...
and remember that you can also call a Move Function onto a Button and call Just about any other  functions to each Button via normal Script commands
so you can for example you can have a graphic that represents anything ..the player, enemies, what ever..and then have text boxes or anything else so you can Simulate feedback and Computer functions...like making the user input commands via a keyboard using pre defined commands ..so in essence your Player can "hack" the Grid by using Text commands that They type in...
This will Move or block paths on the node based Grid you created ..
Yeah Id recommend using the this in conjunction with the Array mentioned by Khris..
The advantage of using a Gui is that it is already has pre built  Graphical Functionality
You can rapidly prototype your Working idea in Prebuilt Buttons then later once its working swap the graphic image sources with your own artwork ..
Easy!

One More or less instant Grid based Graphical Mini game that uses  in built functionality of Customizable Gui's

never underestimate the power of Guis

:) :)
Trousers on then put on shoes...

Khris

I wrote the code in here, pasted it into a default game to test it, added the line, then forgot to transfer the change back here :)

It's supposed to say "return re;", correct.

mystazsea:
While that is a good idea in theory, GUIs have a limit of 30 controls. You could use multiple GUIs and stuff, but displaying the squares, animating them, etc. doesn't really warrant using a GUI. Other stuff like a text box to hack the grid, that's another matter, so yeah, one can always include all kinds of fancy things :)


monkey0506

I reversed the meaning of the return value, but I blame Bear's use of the term "victory". :P In any case, glad you sorted it out in the end.

mystazsea

yeah Sounds like a cool game ...anything that  involves re routing any kind of AI subroutine to avoid escalating Failure is always a good idea..
IMOH

You get my vote!!!
I'd pay good money to play that...haha
cant wait to try it out..

good luck with your release date!!!!
:)
Trousers on then put on shoes...

SMF spam blocked by CleanTalk