Esempio n. 1
0
gboolean
Tetris::keyPressHandler(GtkWidget *widget, GdkEvent *event, Tetris *t)
{
	int keyval;
	bool res = false;

	if (t->timeoutId == 0)
		return FALSE;

	keyval = toupper(((GdkEventKey*)event)->keyval);

	if (keyval == toupper(t->movePause))
	{
		t->togglePause();
		return TRUE;
	}

	if (t->paused)
		return FALSE;

	if (keyval == toupper(t->moveLeft)) {
		res = t->field->moveBlockLeft();
		if (res)
			games_sound_play ("slide");
		t->onePause = false;
	} else if (keyval == toupper(t->moveRight)) {
		res = t->field->moveBlockRight();
		if (res)
			games_sound_play ("slide");
		t->onePause = false;
	} else if (keyval == toupper(t->moveRotate)) {
		res = t->field->rotateBlock(rotateCounterClockWise);
		if (res)
			games_sound_play ("turn");
		t->onePause = false;
	} else if (keyval == toupper(t->moveDown)) {
		if (!t->fastFall && !t->onePause) {
			t->fastFall = true;
			g_source_remove (t->timeoutId);
			t->timeoutId = g_timeout_add (10, timeoutHandler, t);
			res = true;
		}
	} else if (keyval == toupper(t->moveDrop)) {
		if (!t->dropBlock) {
			t->dropBlock = true;
			t->field->dropBlock();
			t->manageFallen();
			res = TRUE;
		}
	}

	t->field->redraw();

	return res;
}
Esempio n. 2
0
void
Tetris::endOfGame()
{
	if (paused) togglePause();
	gtk_action_set_sensitive (pause_action, FALSE);
	gtk_action_set_sensitive (end_game_action, FALSE);
	gtk_action_set_sensitive (preferences_action, TRUE);

	color_next = -1;
	blocknr_next = -1;
	rot_next = -1;
	preview->previewBlock(-1, -1, -1);
	gtk_widget_queue_draw(preview->getWidget());
	field->hidePauseMessage();
	field->showGameOverMessage();
	games_sound_play ("gameover");
	inPlay = false;

	if (scoreFrame->getScore() > 0)
	{
		int pos = high_scores->add (scoreFrame->getScore());
		high_scores->show (GTK_WINDOW (w), pos);
	}

	field->setUseTarget (false);
}
Esempio n. 3
0
static void
gnibbles_worm_handle_bonus (GnibblesWorm *worm)
{
  ClutterActor *actor = NULL;

  if ((board->walls[worm->xhead][worm->yhead] != EMPTYCHAR) &&
    (board->walls[worm->xhead][worm->yhead] != WARPLETTER)) {
    actor = gnibbles_worm_get_head_actor (worm);
    g_signal_connect_after (
      clutter_actor_animate (actor, CLUTTER_EASE_OUT_QUINT, 420,
                            "scale-x", 1.45, "scale-y", 1.45,
                            "fixed::scale-gravity", CLUTTER_GRAVITY_CENTER,
                            NULL),
      "completed", G_CALLBACK (worm_grok_scale_down), actor);
    gnibbles_worm_grok_bonus (worm);

    if ((board->walls[worm->xhead][worm->yhead] == BONUSREGULAR + 'A') &&
        !gnibbles_boni_fake (boni, worm->xhead, worm->yhead)) {

      gnibbles_boni_remove_bonus_final (boni, worm->xhead, worm->yhead);

      if (boni->numleft != 0)
        gnibbles_board_level_add_bonus (board, 1);

    } else
        gnibbles_boni_remove_bonus_final (boni, worm->xhead, worm->yhead);
  }

  if (board->walls[worm->xhead][worm->yhead] == WARPLETTER) {
    gnibbles_warpmanager_worm_change_pos (warpmanager, worm);
    games_sound_play ("teleport");
  }
}
Esempio n. 4
0
void
Tetris::manageFallen()
{
	field->fallingToLaying();
	games_sound_play ("land");

	int levelBefore = scoreFrame->getLevel();

	int levelAfter = scoreFrame->scoreLines (field->checkFullLines());
	if (levelAfter != levelBefore)
		games_sound_play ("gnometris");
	if ((levelBefore != levelAfter) || fastFall)
		generateTimer(levelAfter);

	if (field->isFieldEmpty ())
		scoreFrame->scoreLastLineBonus ();

	generate();
}
Esempio n. 5
0
int
Tetris::gameNew(GtkAction *action, void *d)
{
	Tetris *t = (Tetris*) d;

	if (t->timeoutId)
	{
		g_source_remove(t->timeoutId);
		t->timeoutId = 0;

		/* Catch the case where we started a new game without
		 * finishing the old one. */
		if ((t->scoreFrame->getScore() > 0) && t->inPlay)
			t->high_scores->add (t->scoreFrame->getScore());
	}

	t->inPlay = true;

	int level = t->cmdlineLevel ? t->cmdlineLevel : t->startingLevel;

	t->fastFall = false;

	t->scoreFrame->setLevel(level);
	t->scoreFrame->setStartingLevel(level);

	t->field->setUseTarget (t->useTarget);

	t->generateTimer (level);
	t->field->emptyField(t->line_fill_height,t->line_fill_prob);

	t->scoreFrame->resetScore();
	t->paused = false;

	t->field->generateFallingBlock();
	t->field->redraw();

	t->preview->previewBlock(blocknr_next, rot_next, color_next);
	gtk_widget_queue_draw(t->preview->getWidget());

	gtk_action_set_visible(t->pause_action, TRUE);
	gtk_action_set_visible(t->resume_action, FALSE);
	gtk_action_set_sensitive(t->pause_action, TRUE);
	gtk_action_set_sensitive(t->end_game_action, TRUE);
	gtk_action_set_sensitive(t->preferences_action, FALSE);

	t->field->hidePauseMessage();
	t->field->hideGameOverMessage();

	games_sound_play ("gnometris");

	return TRUE;
}
Esempio n. 6
0
static void
gnibbles_worm_grok_bonus (GnibblesWorm *worm)
{
  int i;

  if (gnibbles_boni_fake (boni, worm->xhead, worm->yhead)) {
    g_timeout_add (1, (GtkFunction) gnibbles_worm_reverse, worm);
    games_sound_play ("reverse");
    return;
  }

  switch (board->walls[worm->xhead][worm->yhead] - 'A') {
    case BONUSREGULAR:
      boni->numleft--;
      worm->change += (NUMBONI - boni->numleft) * GROWFACTOR;
      worm->score += (NUMBONI - boni->numleft) * current_level;
      games_sound_play ("gobble");
      break;
    case BONUSDOUBLE:
      worm->score += (worm->length + worm->change) * current_level;
      worm->change += worm->length + worm->change;
      games_sound_play ("bonus");
      break;
    case BONUSHALF:
      if (worm->length + worm->change > 2) {
        worm->score += ((worm->length + worm->change) / 2) * current_level;
        gnibbles_worm_reduce_tail (worm,
                                  (g_list_length (worm->list)
                                  + worm->change) / 2);
        worm->change -= (g_list_length (worm->list) + worm->change) / 2;
        games_sound_play ("bonus");
      }
      break;
    case BONUSLIFE:
      worm->lives += 1;
      games_sound_play ("life");
      break;
    case BONUSREVERSE:
      for (i = 0; i < properties->numworms; i++)
        if (worm != worms[i])
          g_timeout_add (1, (GSourceFunc)
                         gnibbles_worm_reverse, worms[i]);
      games_sound_play ("reverse");
      break;
  }
}
Esempio n. 7
0
gint
gnibbles_move_worms (void)
{
  gint i, j, olddir;
  gint status = 1, nlives = 1;
  gint *dead;

  dead = g_new (gint, properties->numworms);

  for (i = 0; i < properties->numworms; i++) {
    olddir = worms[i]->direction;
    if (!worms[i]->human) {
      gnibbles_worm_ai_move (worms[i]);
    }
  }

  if (boni->missed > MAXMISSED)
    for (i = 0; i < properties->numworms; i++)
      if (worms[i]->score)
        worms[i]->score--;

  for (i = 0; i < boni->numbonuses; i++) {
    if (!(boni->bonuses[i]->countdown--)) {
      if (boni->bonuses[i]->type == BONUSREGULAR && !boni->bonuses[i]->fake) {
        gnibbles_boni_remove_bonus (boni,
                                    boni->bonuses[i]->x,
                                    boni->bonuses[i]->y);
        boni->missed++;
        gnibbles_board_level_add_bonus (board, 1);
      } else {
        gnibbles_boni_remove_bonus (boni,
                                    boni->bonuses[i]->x,
                                    boni->bonuses[i]->y);
      }
    }
  }

  for (i = 0; i < properties->numworms; i++) {
    dead[i] = !gnibbles_worm_test_move_head (worms[i]);
    status &= !dead[i];
  }

  for (i = 0; i < properties->numworms; i++) {
    if (!dead[i] && worms[i]->lives > 0 && !worms[i]->stop)
      gnibbles_worm_move_tail (worms[i]);
  }

  for (i = 0; i < properties->numworms; i++) {
    if (!dead[i] && worms[i]->lives > 0 && !worms[i]->stop)
      gnibbles_worm_move_head (worms[i]);
  }

  for (i = 0; i < properties->numworms; i++) {
    for (j = 0; j < properties->numworms; j++) {
      if (i != j
          && worms[i]->xhead == worms[j]->xhead
          && worms[i]->yhead == worms[j]->yhead
          && worms[i]->lives > 0
          && worms[j]->lives > 0
          && !worms[i]->stop)
        dead[i] = TRUE;
    }
  }

  for (i = 0; i < properties->numworms; i++) {
    if (dead[i]) {
      if (properties->numworms > 1)
        worms[i]->score *= .7;
      if (!gnibbles_worm_lose_life (worms[i])) {
        /* One of the worms lost one life, but the round continues. */
        gnibbles_worm_reset (worms[i]);
        games_sound_play ("crash");
      }
    }
  }

  if (status & GAMEOVER) {
    games_sound_play ("crash");
    games_sound_play ("gameover");
    return GAMEOVER;
  }

  for (i = 0; i < properties->numworms; i++) {
    if (worms[i]->human && worms[i]->lives <= 0)
      return GAMEOVER;
  }

  for (i = 0; i < properties->numworms; i++) {
    if (worms[i]->lives > 0)
      nlives += 1;
  }

  if (nlives == 1 && (properties->ai + properties->human > 1)) {
    /* There is one player left, the other AI players are dead,
     * and that player has won! */
    return VICTORY;
  } else if (nlives == 0) {
    /* There was only one worm, and it died. */
    return GAMEOVER;
  }
   /* Noone died, so the round can continue. */

  g_free (dead);
  return CONTINUE;
}