Ejemplo n.º 1
0
int Blob::collides_with (Blob* obj) {
  if (this->check_collide (obj)) {
    int xx = abs(this->x - obj->x);
    int yy = abs(this->y - obj->y);
    double d = (sqrt ((xx * xx) + (yy * yy))); // Pythagorean theorem
    Blob *giver = this;
    Blob *reciever = obj;
    if (this->radius > obj->radius) {
      giver = obj;
      reciever = this;
    }
    double oldArea = giver->Area;
    double overlap = (giver->radius + reciever->radius) - d;
    if (overlap < 0) overlap = 0; //-overlap;
    giver->radius -= overlap;
    if (giver->radius < 0.5) {
      giver->radius = 0.0;
      giver->Area = 0;
    } else giver->calc_area();
    reciever->Area += oldArea - giver->Area;
    reciever->calc_radius();
    return 1;
  }
  return 0;
}
Ejemplo n.º 2
0
int gameloop () {
  //setup game
  mainmenu();

  blobs.clear();
  for (int p = 0; p < tot_players; p++) {
    blobs.push_back(Blob(players_start[p][0], players_start[p][1], player_radius));
    blobs.back().playerid = p + 1;
  }
  for (int rad = 10; rad >= 1; rad -= 2) { // 1 blob or radius 10 to 10 blobs of radius 1.
    for (int num = 0; num < 2 * (11 - rad); num++) {
      int tries = 3;
      Blob* newblob = new Blob();
      newblob->radius = 3 * rad;
      newblob->calc_area ();
      while (tries) {
        tries--;
        newblob->x = (rand() % (SCREENW - (2 * (int)newblob->radius))) + (int)newblob->radius;
        newblob->y = (rand() % (SCREENH - (2 * (int)newblob->radius))) + (int)newblob->radius;
        newblob->dx = (rand () % 5 - 2) / 2;
        newblob->dy = (rand () % 5 - 2) / 2;
        if (blobs.size()) {
          int c;
          for (c = 0; c < blobs.size() && !newblob->check_collide (&blobs[c]); c++);
          if (c == blobs.size()) {
            blobs.push_back(*newblob);
            tries = 0;
          }
        } else blobs.push_back (*newblob);
      }
      delete newblob;
    }
  }

  int numplayers = tot_players;
  tick = 0;
  while ((!key[KEY_ESC]) && (!key[KEY_F1]) && (numplayers > 1)) {
    while (!tick) rest(1);
    // main game goes here.
    while (tick) {
      tick--;

      numplayers = moveplayers ();

      for (int c = 0; c < blobs.size(); c++) {
        blobs[c].x += blobs[c].dx;
        blobs[c].y += blobs[c].dy;
        if ((blobs[c].x - blobs[c].radius) < 0) {
          blobs[c].x = blobs[c].radius;
          blobs[c].dx = -blobs[c].dx;
        }
        if ((blobs[c].y - blobs[c].radius) < 0) {
          blobs[c].y = blobs[c].radius;
          blobs[c].dy = -blobs[c].dy;
        }
        if ((blobs[c].x + blobs[c].radius) > SCREENW) {
          blobs[c].x = SCREENW - blobs[c].radius;
          blobs[c].dx = -blobs[c].dx;
        }
        if ((blobs[c].y + blobs[c].radius) > SCREENH) {
          blobs[c].y = SCREENH - blobs[c].radius;
          blobs[c].dy = -blobs[c].dy;
        }

        for (int d = c + 1; d < blobs.size(); d++) {
          if (blobs[c].radius < 0.5) continue;
          blobs[c].collides_with(&blobs[d]);
        }
        if (blobs[c].radius < 0.5) {
          blobs.erase(blobs.begin() + c);
          c--;
        }
      }

      // draw screen
      for (int c = 0; c < blobs.size(); c++) {
        int col = makecol (156, 128 - (c * (128.0 / blobs.size())), (c * (128.0 / blobs.size())));
        for (int p = 0; p < tot_players; p++) {
          if (blobs[c].playerid == p + 1)
            col = player_colors[p][0];
        }
        circlefill (buffer, blobs[c].x, blobs[c].y, (int)blobs[c].radius, col);
        col = makecol (255, 128 - (c * (128.0 / blobs.size())), (c * (128.0 / blobs.size())));
        for (int p = 0; p < tot_players; p++) {
          if (blobs[c].playerid == p + 1)
            col = player_colors[p][1];
        }
        circlefill (buffer, blobs[c].x + (blobs[c].radius / 6), blobs[c].y - (blobs[c].radius / 6), (int)(3 * blobs[c].radius / 4), col);
        if (blobs[c].radius > 10) {
//          textprintf_centre_ex(buffer, font, blobs[c].x + blobs[c].dx, blobs[c].y - 3 + blobs[c].dy, makecol(255,255,255), -1, "oo");
//          textprintf_centre_ex(buffer, font, blobs[c].x + blobs[c].dx - 1, blobs[c].y - 5 + blobs[c].dy, makecol(255,255,255), -1, "..");
          textprintf_centre_ex(buffer, font, blobs[c].x + blobs[c].dx, blobs[c].y - 5 + blobs[c].dy, makecol(0,0,0), -1, face);
        }
      }
      blit(buffer, screen, 0, 0, 0, 0, SCREENW, SCREENH);
      clear_to_color(buffer, makecol(0, 0, 0));
    }
  }

  if (numplayers == 2) return 0;
  textout_centre_ex(screen, font, "GAME OVER", SCREENW / 2, SCREENH / 2 - 20, makecol(255, 255, 255), -1);
  if (numplayers == 0)
    textout_centre_ex(screen, font, "No winner", SCREENW / 2, SCREENH / 2 + 20, makecol(255, 255, 255), -1);
  else if (blobs[0].playerid)
    textprintf_centre_ex(screen, font, SCREENW / 2, SCREENH / 2, player_colors[blobs[0].playerid - 1][0], -1, "Playser %d wins", blobs[0].playerid);
  textout_centre_ex(screen, font, "Press ENTER", SCREENW / 2, SCREENH / 2 + 20, makecol(255, 255, 255), -1);
  while(!key[KEY_ENTER] && !key[KEY_SPACE] && !key[KEY_ESC]&& !key[KEY_F1]) { rest(1); }
  blobs.clear();
  return 1;
}