Esempio n. 1
0
void render_game(GameData* data, DrawConfig* config){
    /*
     * Clear the window prior to any rendering
     */
    render_clear(config); 

    /*
     * Draw all componenets of the came
     */
    render_background(data,config);
    render_all_pipes(data,config);
    render_score(data,config);
    render_bird(data,config);

    /*
     * Render GameOver message if the user has lost
     */
    if(is_gameover(data)){
        render_gameover_message(data,config);
    }

    /*
     * Update the users view to reflect changes 
     */
    SDL_RenderPresent(config->renderer);
}
Esempio n. 2
0
void G2048::get_newone(){
    if (is_gameover())
        return;
    size_t i = rand() % 4;
    size_t j = rand() % 4;
    int n = rand() % 4;
    if (!game[i][j]) game[i][j] = n?2:4;
    else get_newone();
}
Esempio n. 3
0
int main(int argc, char **argv)
{
	SDL_Event event;
	Uint8 mstat;
	int xw, yh, optc;
	int bombs = -1, width = -1, height = -1;
	struct option const longopts[] = {
		{"width", 1, NULL, 'w'},
		{"height", 1, NULL, 'h'},
		{"bombs", 1, NULL, 'b'},
		{"help", 0, NULL, 'H'},
		{NULL, 0, NULL, 0}
	};

	progname = argv[0];

	while ((optc = getopt_long(argc, argv, "w:h:b:H", longopts, NULL)) != -1) {
		switch (optc) {
			case 'w':
				width = atoi(optarg);
				break;
			case 'h':
				height = atoi(optarg);
				break;
			case 'b':
				bombs = atoi(optarg);
				break;
			case 'H':
				usage(0);
				exit(EXIT_SUCCESS);
			default:
				usage(1);
				exit(EXIT_FAILURE);
		}
	}
	usage(0);

	/* Use default value if invalid argument is given. */
	if (width < 1 || width > 99)
		width = W_DEF;
	if (height < 1 || height > 99)
		height = H_DEF;
	if (bombs < 1 || bombs > width*height)
		bombs = width*height / 6;

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "Error: %s\n", SDL_GetError());
		exit(EXIT_FAILURE);
	}
	atexit(SDL_Quit);

	init_graphic(width, height);
	atexit(deinit_graphic);

	init_game(width, height, bombs);
	atexit(deinit_game);

	start_game();

	while (SDL_WaitEvent(&event) >= 0) {
		switch (event.type) {
			case SDL_MOUSEBUTTONDOWN:
				if (is_gameover()) {
					start_game();
					break;
				}

				xw = event.motion.x;
				yh = event.motion.y;
				pixel2pos(&xw, &yh);

				mstat = SDL_GetMouseState(NULL, NULL);
				if(mstat & SDL_BUTTON(1) || mstat & SDL_BUTTON(2)) 
					open_box(get_box(xw, yh));
				else if (mstat & SDL_BUTTON(3))
					set_flag(get_box(xw, yh));
				break;
			case SDL_QUIT:
				putchar('\n');
				exit(EXIT_SUCCESS);
				break;
			default:
				break; /* Nothing. */
		}
	}

	return 0;
}