コード例 #1
0
    bool Move(int di)
    {
        //std::cerr <<p_[0]<<p_[1]<< "\n";
        auto tmp = p_;
        if (di == 0) {
            tmp[0]++;
        } else if (di < 0) {
            tmp[1]--;
        } else {
            tmp[1]++;
        }

        if (is_collision(vmat_, tmp, smat_)) {
            if (di == 0) {
                or_assign(vmat_, p_, smat_);
                auto sv = get_shape(vmat_);
                auto sa = get_shape(smat_);
                collapse(std::min(p_[0]+sa[0], sv[0]-1), std::max(0, p_[0]));
            }
            return false;
        }
        // std::cerr << V2d(tmp) <<" not collis\n";

        p_ = tmp; //std::cerr << V2d(p_) << "\n";
        if (di == 0) {
            td_ = time(0);
        }
        return true;
    }
コード例 #2
0
int		get_best_type(t_pathfinding *c, int x, int y)
{
  int		i;
  int		n;
  int		max;
  int		res;
  t_coord	tmp;

  max = 10000000;
  res = -1;
  tmp.x = x;
  tmp.y = y;
  if (is_collision(c->engine, c->entity, &tmp))
    return (-1);
  for (n = 0; c->entity->data->type[n]; n++)
    for (i = 0; i < c->engine->db.nb_types; i++)
      {
	if (&c->engine->db.types[i] == c->entity->data->type[n])
	  {
	    if (c->mymap[i][y][x] && c->mymap[i][y][x] < max)
	      res = i;
	  }
      }
  return (res);
}
コード例 #3
0
 bool rotate()
 {
     auto tmp = smat_;
     rotate90_right(tmp);
     if (is_collision(vmat_, p_, tmp)) {
         return false;
     }
     std::swap(smat_,tmp);
     return true;
 }
コード例 #4
0
ファイル: JlibSDL.cpp プロジェクト: jaztec/Herder
 bool J2DPhysicalGameObject::check_supported_by_object(J2DGameObject* target) {
     unsigned int dir = is_collision(*target->get_inner(), *this->get_inner());
     if (dir == JLIB_COLLIDE_BOTTOM ||
             dir == JLIB_COLLIDE_BOTTOMLEFT ||
             dir == JLIB_COLLIDE_BOTTOMRIGHT ||
             dir == JLIB_COLLIDE_RIGHT ||
             dir == JLIB_COLLIDE_LEFT)
         return true;
     else
         return false;
 }
コード例 #5
0
ファイル: sphere_collision.c プロジェクト: elhmn/Raytracer
double				sphere_collision(t_ray *ray, void *data,
										t_obj *obj, t_rt *rt)
{
	double			d;
	t_data_sphere	*dat;

	d = -1;
	dat = (t_data_sphere*)data;
	if (ray && dat && rt && obj)
		d = is_collision(ray, dat, obj, rt);
	return (d);
}
コード例 #6
0
ファイル: board.cpp プロジェクト: JohnBurchell/Games
void Board::rotate_piece(Tetris_Shape &shape)
{
	//Increment current rotation
	int old_rotation = shape.rotation();
	int rotation_ = (shape.rotation() + 1) % 4;
	//Set the new rotation
	shape.change_rotation(rotation_);

	if (is_collision(shape))
	{
		//If collision occurs, revert. Else continue.
		shape.change_rotation(old_rotation);
	}
}
コード例 #7
0
    bool next_round()
    {
        pop_preview(&smat_);

        p_[1] = int(vmat_[0].size() - smat_[0].size()) / 2;
        p_[0] = -int(smat_.size()-1);
        while (p_[0] <= 0) {
            Point tmp = p_;
            if (is_collision(vmat_, p_, smat_)) {
                or_assign(vmat_, p_, smat_);
                over();
                return 0;
            }
            if (p_[0] == 0)
                break;
            ++p_[0];
        }
        td_ = time(0);
        return 1;
    }
コード例 #8
0
ファイル: planner.cpp プロジェクト: lakshayg/mpel
Path Planner::solve(ProblemDefinition pdef)
{
    // find point closest to given points in graph
    if (is_collision(_ws.map, pdef.start)) return Path();
    if (is_collision(_ws.map, pdef.goal)) return Path();
    Path p;
    Point in, out;

    // find some vertices in graph close to start and goal and connect them
    Graph tmp_g = _g;
    size_t nneigh = std::min((size_t)5, tmp_g.vertex_list().size());
    std::vector<Point> start_neigh
        = k_best(tmp_g.vertex_list().begin(), tmp_g.vertex_list().end(), nneigh, dcomp(pdef.start));
    std::vector<Point> goal_neigh
        = k_best(tmp_g.vertex_list().begin(), tmp_g.vertex_list().end(), nneigh, dcomp(pdef.goal));

    size_t in_segment = 0, out_segment = 0; // check the number of edges which could be connected
    for (size_t i = 0; i < nneigh; ++i) {
        Segment s1 = Segment(pdef.start, start_neigh[i]);
        if (not is_collision(_ws.map, s1)) {
            tmp_g.add_edge(pdef.start, start_neigh[i], distance(pdef.start, start_neigh[i]));
            in_segment++;
        }

        Segment s2 = Segment(pdef.goal, goal_neigh[i]);
        if (not is_collision(_ws.map, s2)) {
            tmp_g.add_edge(pdef.goal, goal_neigh[i], distance(pdef.goal, goal_neigh[i]));
            out_segment++;
        }
    }

    auto t0 = high_resolution_clock::now();
    // check if the terminal points were successfully inserted
    // it may happen that they could not be inserted because the graph
    // built by graph builder is not sufficiently dense or the user did
    // not use a graph builder at all due to which the graph is empty
    if (in_segment > 0 and out_segment > 0) {
        p = _pc.graph_search(tmp_g, pdef.start, pdef.goal);
    }
    auto t1 = high_resolution_clock::now();
    t_search = duration_cast<microseconds>(t1 - t0).count();

    t0 = high_resolution_clock::now();
    if (p.size() == 0) { // if a path was not found
        if (in_segment == 0 or out_segment == 0) {
            std::cout << "[Planner::solve] Terminal points could not be inserted in the graph, "
                      << "check if the graph is sufficiently dense" << std::endl;
        } else {
            // it may happen that the graph search did not find a path because
            // the graph made by the graph builder is not connected.
            std::cout << "[Planner::solve] The computed graph is not connected, "
                      << "graph search did not find a path" << std::endl;
        }
        // we return a straight line joining start and goal hoping that the interpolator
        // will be able to find a path. If no interpolator is used then the returned
        // path is a straight line between start and goal points.
        p.push_back(pdef.start);
        p.push_back(pdef.goal);
    }
    Path ret = _pc.interpolator(_ws.map, p);
    t1 = high_resolution_clock::now();
    t_interp = duration_cast<microseconds>(t1 - t0).count();

    if (ret.back() != pdef.goal) {
        // check if path was computed and completely interpolated
        std::cout << "[Planner::solve] Could not find a path!" << std::endl;
    }

    return ret;
}
コード例 #9
0
ファイル: Game.cpp プロジェクト: bogdanstefan/Tetris
/**
* Main game cycle, handles user interaction and checks if game is over
*/
void Game::play() {

	// run indefinitely
	while (1) {
		auto b1 = new Board();
		auto b2 = new Board();
		srand(time(NULL)); //random seed set as current time

		int game_over = 0;
		int winner;

		while (!game_over) {
			display_help();
			display_score(b1, b2);

			// if any board needs a piece, add it
			if (b1->needs_piece) {
				int next = rand() % 7;
				Piece new_piece(next);
				if (b1->is_collision(new_piece)) {
					game_over = 1;
					winner = 2;
				}
				b1->set_curr_piece(new_piece);
			}

			if (b2->needs_piece) {
				int next = rand() % 7;
				Piece new_piece(next);
				if (b2->is_collision(new_piece)) {
					game_over = 1;
					winner = 1;
				}
				b2->set_curr_piece(new_piece);
			}

			Piece current1 = b1->get_curr_piece(), potential1;
			Piece current2 = b2->get_curr_piece(), potential2;
			char move;

			// draw boards
			b1->draw(b2);

			move = wait_for_char(WAIT_TIME); // wait for input

			// quit immediatly
			if (move == 'q') {
				winner = 1;
				break;
			}

			if (move == 'w') {
				current1.move(UP);
				if (!b1->is_collision(current1))
					b1->set_curr_piece(current1);
				continue;
			}
			else if (move == 's') {
				current1.move(DOWN);
				if (!b1->is_collision(current1))
					b1->set_curr_piece(current1);
				continue;
			}
			else if (move == 'a') {
				current1.rotate();
				if (!b1->is_collision(current1))
					b1->set_curr_piece(current1);
				continue;
			}

			else if (move == 'i') {
				current2.move(UP);
				if (!b2->is_collision(current2))
					b2->set_curr_piece(current2);
				continue;
			}
			else if (move == 'k') {
				current2.move(DOWN);
				if (!b2->is_collision(current2))
					b2->set_curr_piece(current2);
				continue;
			}
			else if (move == 'l') {
				current2.rotate();
				if (!b2->is_collision(current2))
					b2->set_curr_piece(current2);
				continue;
			}

			// check if current piece of the boards has completely fallen
			potential1 = current1;
			potential2 = current2;


			potential1.move(RIGHT);
			if (!b1->is_collision(potential1))
				b1->set_curr_piece(potential1);
			else {
				b1->add_piece(current1);
				b1->needs_piece = true;
			}

			potential2.move(RIGHT);
			if (!b2->is_collision(potential2))
				b2->set_curr_piece(potential2);
			else {
				b2->add_piece(current2);
				b2->needs_piece = true;
			}

			int adjust1 = b1->delete_needed_lines();
			int adjust2 = b2->delete_needed_lines();

			// for each deleted line adjust the base of the boards accordingly 
			for (int i = 0; i < adjust1; i++)
			{
				b1->adjust_middle(DOWN);
				b2->adjust_middle(UP);
			}

			for (int i = 0; i < adjust2; i++)
			{
				b2->adjust_middle(DOWN);
				b1->adjust_middle(UP);
			}
		}

		// ask for input, if input is 'y' start another game
		cout << "\nPlayer " << winner << " WINS!\n\n";
		cout << "Play again? (Y or N)\n";
		char resume = getch();
		if (resume == 'n')
			break;

	}
}
コード例 #10
0
ファイル: blasteroids.c プロジェクト: pasoev/blasteroids
int main(int argc, char *argv[]) {

    int c;

    while((c = getopt(argc, argv, "hv")) != -1) {
        switch(c) {
        case 'h':
            puts(HELP);
            return 0;
        case 'v':
            printf("version %.1f\n", PROGRAM_VERSION);
            return 0;
        }
    }

    if(!al_init()) {
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
    }

    if(argc == 2 && !strcmp(argv[1], "-v")) {
        printf("version %.1f\n", PROGRAM_VERSION);
        return 0;
    }

    bool key[5] = {false, false, false, false, false};
    bool doexit = false;
    bool redraw = true;

    timer = al_create_timer(1.0 / FPS);
    if(!timer) {
        fprintf(stderr, "couldn't initialize timer.\n");
        return -1;
    }

    display = al_create_display(SCREEN_W, SCREEN_H);
    if(!display) {
        fprintf(stderr, "failed to create the display.\n");
        al_destroy_timer(timer);

        return -1;
    }

    al_set_window_title(display, "blasteroids");

    al_init_primitives_addon();

    if(!al_install_keyboard()) {
        fprintf(stderr, "failed to initialize keyboard.\n");
        al_destroy_display(display);
        al_destroy_timer(timer);

        return -1;
    }

    /* Making the sky look like sky */

    colors[0] = al_map_rgba(255, 100, 255, 128);
    colors[1] = al_map_rgba(255, 100, 100, 255);
    colors[2] = al_map_rgba(100, 100, 255, 255);

    for (layer = 0; layer < 3; layer++) {
        for (star = 0; star < NUM_STARS/3; star++) {
            Point *p = &stars[layer][star];
            p->x = rand() % SCREEN_W;
            p->y = rand() % SCREEN_H;
        }
    }


    start = al_get_time() * 1000;
    now = start;
    elapsed = 0;
    frame_count = 0;
    program_start = al_get_time();


    /* done with the sky. Now making the ship. */
    Spaceship *ship = init_ship();
    List *a = (List *)summon_asteroids(NUMBER_ASTEROIDS);
    ListElmt *astElmt = list_head(a);


    if(!ship) {
        fprintf(stderr, "couldn't create bitmap.\n");
        al_destroy_timer(timer);
        al_destroy_display(display);
        return -1;
    }

    Blast *blast = init_blast(ship->sx, ship->sy, ship->heading);

    event_queue = al_create_event_queue();
    if(!event_queue) {
        fprintf(stderr, "failed to create event queue.\n");
        al_destroy_display(display);
        al_destroy_timer(timer);
        return -1;
    }

    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_timer_event_source(timer));

    al_start_timer(timer);

    while(!doexit) {
        /* animate the sky, just sky, starts, but NOT objects. */

        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);

        if(ev.type == ALLEGRO_EVENT_TIMER) {

            if(key[UP])
                ship->speed += 0.04;

            if(key[RIGHT])
                ship->heading += 1.0f;

            if(key[DOWN])
                if(ship->speed > 0.05)
                    ship->speed -= 0.04;

            if(key[LEFT])
                ship->heading -= 1.0f;

            fly_ship(ship);
            // fire ze missile

            if(key[SPACE]) {
                blast->sx = ship->sx;
                blast->sy = ship->sy;
                blast->heading = ship->heading;
                blast->gone = 0;
            }

            float theta = head2theta(blast->heading);
            blast->sx += blast->speed * cos(theta);
            if(blast->sx <= 0 || blast->sx >= SCREEN_W) {
                blast->gone = 1;
            }

            blast->sy += blast->speed * sin(theta);
            if(blast->sy <= 0 || blast->sy >= SCREEN_H) {
                blast->gone = 1;
            }

            /* loop through the list of asteroids */

            astElmt = (astElmt->next)?astElmt->next : list_head(a);
            Asteroid *aster = astElmt->aster;

            /* asteroid eternity */
            if(aster->sx < 0 || aster->sx > SCREEN_W - 33)
                aster->sx = 0;

            if(aster->sy < 0 || aster->sy > SCREEN_H)
                aster->sy = 0;



            aster->twist += aster->rot_velocity;

            /* Fuzzy movement */
            if((int)aster->sx % 3 == 0)
                aster->sx += aster->speed;
            aster->sx += 0.9;
            if((int)aster->sy % 5 == 3)
                aster->sy += aster->speed;
            aster->sy += 0.9;

            aster->twist += 0.4;

            /* detect alteroid collision, but only if 5 seconds have
             passed since the last Death */

            Box s = {{ship->sx, ship->sy}, 16.0f, 20.0f};
            Box a = {{aster->sx, aster->sy}, 45.0f, 40.0f};

            if(ship->heading != -90.0f) {
                if(!aster->gone && !ship->gone && is_collision(&s, &a)) {
                    ship->color = al_map_rgb(0, 0, 255);
                }
            }

            /* detect asteroid being shot */

            Box b = {{blast->sx, blast->sy}, 120.0f, 3.0f};
            if(!(blast->gone) && !(aster->gone) && is_collision(&b, &a)) {
                aster->gone = 1;
            }
            redraw = true;
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {

            switch(ev.keyboard.keycode) {
            case ALLEGRO_KEY_UP:
                key[UP] = true;
                break;
            case ALLEGRO_KEY_RIGHT:
                key[RIGHT] = true;
                break;
            case ALLEGRO_KEY_DOWN:
                key[DOWN] = true;
                break;
            case ALLEGRO_KEY_LEFT:
                key[LEFT] = true;
                break;
            case ALLEGRO_KEY_SPACE:
                key[SPACE] = true;
                break;
            }
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
            switch(ev.keyboard.keycode) {
            case ALLEGRO_KEY_UP:
                key[UP] = false;
                break;
            case ALLEGRO_KEY_RIGHT:
                key[RIGHT] = false;
                break;
            case ALLEGRO_KEY_DOWN:
                key[DOWN] = false;
                break;
            case ALLEGRO_KEY_LEFT:
                key[LEFT] = false;
                break;
            case ALLEGRO_KEY_SPACE:
                key[SPACE] = false;
                break;
            case ALLEGRO_KEY_Q:
            case ALLEGRO_KEY_ESCAPE:
                doexit = true;
                break;
            }
        }

        if(redraw && al_is_event_queue_empty(event_queue)) {
            redraw = false;
            al_clear_to_color(al_map_rgb(0, 0, 0));

            if (frame_count < (1000/TARGET_FPS)) {
                frame_count += elapsed;
            }
            else {
                int X, Y;

                frame_count -= (1000/TARGET_FPS);

                for (star = 0; star < NUM_STARS/3; star++) {
                    Point *p = &stars[0][star];
                    al_draw_pixel(p->x, p->y, colors[0]);
                }
                /*	al_lock_bitmap(al_get_backbuffer(display), ALLEGRO_PIXEL_FORMAT_ANY, 0); */

                for (layer = 1; layer < 3; layer++) {
                    for (star = 0; star < NUM_STARS/3; star++) {
                        Point *p = &stars[layer][star];
                        // put_pixel ignores blending
                        al_put_pixel(p->x, p->y, colors[layer]);
                    }
                }

                /* Check that dots appear at the window extremes. */
                X = SCREEN_W - 1;
                Y = SCREEN_H - 1;
                al_put_pixel(0, 0, al_map_rgb_f(1, 1, 1));
                al_put_pixel(X, 0, al_map_rgb_f(1, 1, 1));
                al_put_pixel(0, Y, al_map_rgb_f(1, 1, 1));
                al_put_pixel(X, Y, al_map_rgb_f(1, 1, 1));

                /* al_unlock_bitmap(al_get_backbuffer(display)); */
                total_frames++;
            }

            now = al_get_time() * 1000;
            elapsed = now - start;
            start = now;

            for (layer = 0; layer < 3; layer++) {
                for (star = 0; star < NUM_STARS/3; star++) {
                    Point *p = &stars[layer][star];
                    p->y -= speeds[layer] * elapsed;
                    if (p->y < 0) {
                        p->x = rand() % SCREEN_W;
                        p->y = SCREEN_H;
                    }
                }
            }
            draw_ship(ship);

            if(!blast->gone) {
                draw_blast(blast);
            }
            draw_asteroids(a);
            al_flip_display();
        }
    }

    length = al_get_time() - program_start;

    /* printf("Length = %f\n", length); */

    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);

    return 0;
}