void ball_step(void) { int x, y; switch ( game_get_state() ) { case START: remaining = 3; init_ball(); break; case NEXTSTAGE: init_ball(); break; case REMAINING: break; case RUNNING: x = old_x + dx; y = old_y + dy; if ( x != old_x && ( x <= 0 || LCD_WIDTH-ball.width < x )) { dx = dx > 0 ? -1*BALL_DX : BALL_DX; return; } if ( y != old_y && y <= 0 ) { dy = dy > 0 ? -1*BALL_DY : BALL_DY; return; } else if ( y!= old_y && LCD_HEIGHT-ball.width < y ) { if ( remaining <= 1 ) { game_set_state(DEAD); } else { draw_box(&ball, ball.x, ball.y, COLOR_BLACK); remaining--; init_ball(); game_set_state(REMAINING); } return; } move_box(&ball, x, y, BALL_COLOR); old_x = x; old_y = y; break; case CLEAR: draw_box(&ball, old_x, old_y, BALL_COLOR); break; case DEAD: break; case RESTART: remaining = 3; init_ball(); break; } }
void move_ball(t_all *all) { int ret; if ((ret = set_ball_pos(&all->ball.pos.y, all->ball.pos.y + all->ball.vspeed.y, -1.5, 1)) <= 0) { if (ret == -1) all->ball.vspeed.y = -all->ball.vspeed.y; else { init_ball(&all->ball); init_ship(&all->ship); if (--all->life == 0) ft_print_shell(all, "Vous avez perdu !!"); all->cur = 1; } } if (set_ball_pos(&all->ball.pos.x, all->ball.pos.x + all->ball.vspeed.x, -1, 1) == -1) all->ball.vspeed.x = -all->ball.vspeed.x; if (check_ship_collision(&all->ball, &all->ship) == TRUE) ball_rebound(&all->ball, &all->ship); if (check_brick_collision(&all->ball, &all->map)) all->score += 10; }
void world::reset_game() { delete p1; delete p2; delete m_ball; m_collisions = 0; init_ball(); init_paddels(); }
void winner_kick_ball(int round){ extern int rank, winner, england_score, brazil_score; if (winner == rank) { if (round == ROUND_A){ //round A england post is left side if (ENG_RANKS(rank)){ pass_ball_action(KICKRIGHT); if(RIGHT_GOAL){ england_score++; init_ball(); } } if (BRA_RANKS(rank)) { pass_ball_action(KICKLEFT); if(LEFT_GOAL){ brazil_score++; init_ball(); } } } if (round == ROUND_B){ //round B england post is right side if (ENG_RANKS(rank)){ pass_ball_action(KICKLEFT); if(LEFT_GOAL){ england_score++; init_ball(); } } if (BRA_RANKS(rank)) { pass_ball_action(KICKRIGHT); if(RIGHT_GOAL){ brazil_score++; init_ball(); } } } player.kicked = 1; // player kicked this turn } else { if(ENG_RANKS(rank) || BRA_RANKS(rank)){ player.kicked = -1; //player does not kick this turn } } }
static void load_level(t_game *game, t_level *level) { game->level_height = level->height; game->level_width = level->width; game->blocks = level->blocks; game->offset_x = WIN_WIDTH / level->width - 1; game->offset_y = (WIN_HEIGHT - 200) / level->height - 1; game->started = 1; game->win = 0; init_ball(game); }
void main() #endif { game_t game; /* Initialize the front end */ fe_init(); /* Initialize the game */ /* Create the game */ memset(&game, 0, sizeof(game_t)); /* Create the paddle */ init_paddle(&game.paddle, SPECIAL_NONE, (SCREEN_WIDTH/2)-(PADDLE_WIDTH/2)); /* Init the balls (only one active) */ init_ball(&game.p_balls[0], game.paddle.x+(PADDLE_WIDTH/2), PADDLE_Y-PADDLE_HEIGHT, BALL_STATE_ACTIVE|BALL_STATE_HOLDING); game.nr_balls = 1; /* Init the specials */ game.lives = NR_LIVES; /* Set the numbe of levels */ if (fe_load_data(game.p_levels, 0, (FIELD_WIDTH+1)*FIELD_HEIGHT, "Zarkanoid")) fe_finalize(); /* Could not load the levels */ game.levels = (game.p_levels[0]-'0')*10+(game.p_levels[1]-'0'); /* Number of levels */ game.level = 5*((game.p_levels[3]-'0')*10+(game.p_levels[4]-'0')); /* Starting level (skip 5 levels at a time) */ #if defined(TARGET_SDL) /* Set the level to start at to the argument */ if (argc > 1) game.level = atoi(argv[1]); #endif /* Load the first level */ goto_level(&game); /* Draw the game screen */ draw_screen(&game); fe_sleep(100); /* Start the game */ do_game(&game); /* Clean up afterwards */ fe_finalize(); #if defined(TARGET_SDL) return 0; #endif }
static t_level *init_level(void) { t_level *level; level = (t_level *)ft_memalloc(sizeof(t_level)); if (!level) exit(EXIT_FAILURE); level->active_bricks = 0; level->score = INIT_SCORE; level->lives = INIT_LIVES; level->brick_list = NULL; level->ball = init_ball(); level->pad = init_pad(); return (level); }
int GLUI_Rotation::iaction_mouse_down_handler( int local_x, int local_y ) { copy_float_array_to_ball(); init_ball(); local_y = (int)(2.0 * ball->center[1] - local_y); ball->mouse_down( local_x, local_y ); /* printf( "%d %d - %f %f\n", local_x, local_y, ball->center[0], ball->center[1] ); */ copy_ball_to_float_array(); spinning = false; return false; }
/* Called when the player finishes a level (move to next) */ static int handle_level_finished(game_t *p_game) { fe_clear_screen(); draw_screen(p_game); /* Move the paddle to starting place */ init_paddle(&p_game->paddle, PADDLE_NORMAL, (SCREEN_WIDTH/2)-(PADDLE_WIDTH/2)); /* Init the balls (only one active) */ memset(p_game->p_balls, 0, sizeof(ball_t)*MAX_BALLS); init_ball(&p_game->p_balls[0], p_game->paddle.x+(PADDLE_WIDTH/2), PADDLE_Y-PADDLE_HEIGHT, BALL_STATE_ACTIVE|BALL_STATE_HOLDING); p_game->nr_balls = 1; p_game->state = 0; /* Clear the specials */ memset(p_game->p_specials, 0, sizeof(special_t)*MAX_SPECIALS); p_game->level++; return goto_level(p_game); }
void world::init() { load_backround("linux.bmp"); init_ball(); init_paddels(); }
int main() { SDL_Surface *temp; /* Initialize SDL’s video system and check for errors */ if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } /* Make sure SDL_Quit gets called when the program exits! */ atexit(SDL_Quit); /* Attempt to set a 640x480 8 bit color video mode */ screen = SDL_SetVideoMode(640, 480, 8,SDL_DOUBLEBUF); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("numbermap.bmp"); if (temp == NULL) { printf("Unable to load numbermap.bmp.\n"); return 1; } /* Set the numbermaps colorkey. */ Uint32 colorkey = SDL_MapRGB(temp->format, 255, 0, 255); SDL_SetColorKey(temp, SDL_SRCCOLORKEY, colorkey); //convert the numbermaps surface to the same type as the screen numbermap = SDL_DisplayFormat(temp); if (numbermap == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("title.bmp"); if (temp == NULL) { printf("Unable to load numbermap.bmp.\n"); return 1; } /* Set the numbermaps colorkey. */ SDL_SetColorKey(temp, SDL_SRCCOLORKEY, colorkey); //convert the numbermaps surface to the same type as the screen title = SDL_DisplayFormat(temp); if (numbermap == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("gameover.bmp"); if (temp == NULL) { printf("Unable to load gameover.bmp.\n"); return 1; } //convert the end surface to the same type as the screen end = SDL_DisplayFormat(temp); if (end == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); /* Initialize the ball position data. */ init_ball(); int quit = 0; int state = 0; Uint8 *keystate = 0; Uint32 next_game_tick = SDL_GetTicks(); int sleep = 0; int r = 0; /* Animate */ while (quit == 0) { /* Update SDL's internal input state information. */ SDL_PumpEvents(); /* Grab a snapshot of the keyboard. */ keystate = SDL_GetKeyState(NULL); /* Respond to input. */ if (keystate[SDLK_ESCAPE]) { quit = 1; } if (keystate[SDLK_DOWN]) { move_paddle(0); } if (keystate[SDLK_UP]) { move_paddle(1); } //draw the background draw_background(); //display main menu if (state == 0 ) { if (keystate[SDLK_SPACE]) { state = 1; } //draw menu draw_menu(); //display gameover } else if (state == 2) { if (keystate[SDLK_SPACE]) { state = 0; //delay for a little bit so the space bar press dosnt get triggered twice //while the main menu is showing SDL_Delay(500); } if (r == 1) { //if player 1 is AI if player 1 was human display the return value of r not 3 draw_game_over(3); } else { //display gameover draw_game_over(r); } //display the game } else if (state == 1){ //check score r = check_score(); if (r == 1) { state = 2; } else if (r == 2){ state = 2; } //paddle ai movement move_paddle_ai(); /* Move the balls for the next frame. */ move_ball(); //draw net draw_net(); //draw paddles draw_paddle(); /* Put the ball on the screen. */ draw_ball(); //draw the score draw_player_0_score(); //draw the score draw_player_1_score(); } /* Ask SDL to update the entire screen. */ SDL_Flip(screen); next_game_tick += 1000 / 60; sleep = next_game_tick - SDL_GetTicks(); if( sleep >= 0 ) { SDL_Delay(sleep); } } return 0; }
/* This routine moves each ball by its motion vector. */ static void move_ball() { /* Move the ball by its motion vector. */ ball.x += ball.dx; ball.y += ball.dy; /* Turn the ball around if it hits the edge of the screen. */ if (ball.x < 0) { score[1] += 1; init_ball(); } if (ball.x > screen->w - 10) { score[0] += 1; init_ball(); } if (ball.y < 0 || ball.y > screen->h - 10) { ball.dy = -ball.dy; } //check for collision with the paddle int i; for (i = 0; i < 2; i++) { int c = check_collision(ball, paddle[i]); //collision detected if (c == 1) { //ball moving left if (ball.dx < 0) { ball.dx -= 1; //ball moving right } else { ball.dx += 1; } //change ball direction ball.dx = -ball.dx; //change ball angle based on where on the paddle it hit int hit_pos = (paddle[i].y + paddle[i].h) - ball.y; if (hit_pos >= 0 && hit_pos < 7) { ball.dy = 4; } if (hit_pos >= 7 && hit_pos < 14) { ball.dy = 3; } if (hit_pos >= 14 && hit_pos < 21) { ball.dy = 2; } if (hit_pos >= 21 && hit_pos < 28) { ball.dy = 1; } if (hit_pos >= 28 && hit_pos < 32) { ball.dy = 0; } if (hit_pos >= 32 && hit_pos < 39) { ball.dy = -1; } if (hit_pos >= 39 && hit_pos < 46) { ball.dy = -2; } if (hit_pos >= 46 && hit_pos < 53) { ball.dy = -3; } if (hit_pos >= 53 && hit_pos <= 60) { ball.dy = -4; } //ball moving right if (ball.dx > 0) { //teleport ball to avoid mutli collision glitch if (ball.x < 30) { ball.x = 30; } //ball moving left } else { //teleport ball to avoid mutli collision glitch if (ball.x > 600) { ball.x = 600; } } } } }
int main(void) { /** SDL2 initialized: */ if (sdl_init(SDL_INIT_VIDEO) != 0) { fprintf(stdout, "SDL initialize failed (%s)\n", SDL_GetError()); exit(EXIT_FAILURE); } /** Window creation: */ SDL_Window *pWindow = NULL; pWindow = sdl_create_window("Balls", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN); if (pWindow == NULL) { fprintf(stderr, "Windows creation error: (%s)\n", SDL_GetError()); exit(EXIT_FAILURE); } /** Renderer creation. */ SDL_Renderer *pRenderer; if ((pRenderer = sdl_create_renderer(pWindow, -1, SDL_RENDERER_ACCELERATED)) == NULL) { fprintf(stderr, "SDL Renderer creating error (%s)\n", SDL_GetError()); exit(EXIT_FAILURE); } /** Random seed initialized. **/ struct timeval tv; gettimeofday(&tv, NULL); srand((unsigned int) tv.tv_usec); /** Define the balls and initialized the balls. **/ Ball *red_ball = init_ball('r'); Ball *green_ball = init_ball('g'); Ball *blue_ball = init_ball('b'); Ball *yellow_ball = init_ball('y'); Ball *pink_ball = init_ball('p'); Ball *turquoise_ball = init_ball('t'); const int FPS = 90; const int FRAME_DELAY = 1000 / FPS; set_loop(true); while (loop) { /** SDL2 mainloop. **/ int frame_start = SDL_GetTicks(); /** Check if a ball collide the border rounded box: **/ check_collide_borders(red_ball); check_collide_borders(green_ball); check_collide_borders(blue_ball); check_collide_borders(yellow_ball); check_collide_borders(pink_ball); check_collide_borders(turquoise_ball); /** Increment the balls positions: **/ translate_ball(red_ball); translate_ball(green_ball); translate_ball(blue_ball); translate_ball(yellow_ball); translate_ball(pink_ball); translate_ball(turquoise_ball); clear(pRenderer, 127, 127, 127, 127); /** Displaying the interior black boxe using a SDL2_gfx function. **/ boxRGBA(pRenderer, 32, 32, WIDTH - 32, HEIGHT - 32, 0, 0, 0, 255); /** Display the balls: **/ display_ball(pRenderer, red_ball); display_ball(pRenderer, green_ball); display_ball(pRenderer, blue_ball); display_ball(pRenderer, yellow_ball); display_ball(pRenderer, pink_ball); display_ball(pRenderer, turquoise_ball); display(pRenderer); update(pWindow); get_events(); int frame_end = SDL_GetTicks() - frame_start; if (frame_end < FRAME_DELAY) { SDL_Delay(FRAME_DELAY - frame_end); } } free_ball(red_ball); free_ball(green_ball); free_ball(blue_ball); free_ball(yellow_ball); free_ball(pink_ball); free_ball(turquoise_ball); clean_up(pRenderer, pWindow); SDL_Quit(); exit(EXIT_SUCCESS); }
/* The main game loop */ static void do_game(game_t *p_game) { uint8_t exit=FALSE; while(!exit) { uint16_t input = get_input(p_game); uint16_t fire; int i; exit = input & FE_EVENT_EXIT; fire = input & FE_EVENT_SELECT; /* Move the paddle (according to input) */ update_paddle(&p_game->paddle, input & FE_EVENT_LEFT ? 4:0, input & FE_EVENT_RIGHT ? 4:0); /* Handle the ball(s) */ p_game->free_ball = -1; for (i=0; i<MAX_BALLS; i++) { /* Unused balls are skipped */ if (!(p_game->p_balls[i].state & BALL_STATE_ACTIVE)) { draw_ball(&p_game->p_balls[i]); p_game->free_ball = i; continue; } /* The player is holding the ball */ if (p_game->p_balls[i].state & BALL_STATE_HOLDING) { p_game->p_balls[i].lastx = p_game->p_balls[i].x; p_game->p_balls[i].x += p_game->paddle.x-p_game->paddle.lastx; if (fire) { /* The player relases the ball. The following will * clear the HOLDING-bit. It will also shoot the * ball away upwards. */ p_game->p_balls[i].dx = 1; p_game->p_balls[i].state &= (0xff ^ BALL_STATE_HOLDING); bounce_ball_paddle(p_game, &p_game->p_balls[i]); } } else if (paddle_ball_collide(&p_game->paddle, &p_game->p_balls[i])) { /* Hold the ball if the pad is sticky */ if (p_game->paddle.type & SPECIAL_STICKY) { p_game->p_balls[i].dy = 0; p_game->p_balls[i].dx = 0; p_game->p_balls[i].state |= BALL_STATE_HOLDING; p_game->paddle.type--; /* Sticky for maximum 3 rounds */ } else bounce_ball_paddle(p_game, &p_game->p_balls[i]); } /* Check collisions against blocks */ block_ball_collide(p_game, &p_game->p_balls[i]); draw_ball(&p_game->p_balls[i]); /* Update the ball (and check if it moves out of the playfield) */ if (update_ball(p_game, &p_game->p_balls[i])) { /* Ball is out of bounds - Remove it */ init_ball(&p_game->p_balls[i], 0, 0, 0); p_game->nr_balls--; } } /* Handle the specials */ p_game->free_special = -1; for (i=0; i<MAX_SPECIALS; i++) { int res; /* Always draw the specials for timing-reasons */ draw_special(&p_game->p_specials[i]); /* Inactive special - continue */ if (!p_game->p_specials[i].type) { p_game->free_special = i; continue; } /* This moves the special and also checks if it collides with the paddle */ res = update_special(p_game, &p_game->p_specials[i]); switch(res) { case 1: /* Player took the special */ switch(p_game->p_specials[i].type) { case SPECIAL_EXTRA_BALL: if (p_game->free_ball != -1) /* Check if there is free space for the ball */ { p_game->nr_balls++; init_ball(&p_game->p_balls[p_game->free_ball], p_game->paddle.x+PADDLE_WIDTH/2, PADDLE_Y-PADDLE_HEIGHT, BALL_STATE_ACTIVE); } break; case SPECIAL_LONG_PADDLE: p_game->paddle.width = PADDLE_LONG_WIDTH; break; case SPECIAL_SHORT_PADDLE: p_game->paddle.width = PADDLE_SHORT_WIDTH; break; case SPECIAL_EXTRA_LIFE: if (p_game->lives < MAX_LIVES) { p_game->lives++; draw_lives(p_game); } break; case SPECIAL_STICKY: /* * For the sticky and destroyers, we use the bits * from the flag and downwards as counters. * * i.e: SPECIAL_STICKY is the third bit, which means * that type becomes 0b00000111. type is then * lowered when the ball hits the paddle and after * zeroing the lower bits, the SPECIAL_STICKY bit is * finally cleared. This gives a maximum of three * sticky rounds. * * These two only adds 30 bytes to the binary :-) */ p_game->paddle.type = (SPECIAL_STICKY | (SPECIAL_STICKY-1)); break; case SPECIAL_DESTROYER: p_game->p_balls[0].state |= (BALL_STATE_DESTROYER | (BALL_STATE_DESTROYER-1)); break; case SPECIAL_FLOOR: p_game->state |= (SPECIAL_FLOOR | 2); /* Three times */ fe_fill_area(0, SCREEN_HEIGHT-FLOOR_HEIGHT, SCREEN_WIDTH, FLOOR_HEIGHT); break; } /* Fall through */ case -1: /* Remove the special */ p_game->nr_specials--; fe_clear_area(p_game->p_specials[i].x, p_game->p_specials[i].y-1, SPECIAL_WIDTH, SPECIAL_HEIGHT+1); p_game->p_specials[i].type = SPECIAL_NONE; break; } } /* No balls left in play */ if (!p_game->nr_balls) { if (--p_game->lives == 0) exit = TRUE; /* No lives left! */ else { /* Restart the game */ p_game->nr_balls = 1; draw_screen(p_game); init_paddle(&p_game->paddle, SPECIAL_NONE, SCREEN_WIDTH/2-PADDLE_WIDTH/2); init_ball(&p_game->p_balls[0], p_game->paddle.x+p_game->paddle.width/2, PADDLE_Y-PADDLE_HEIGHT, BALL_STATE_HOLDING | BALL_STATE_ACTIVE); } } /* No blocks left to remove */ if (!p_game->nr_blocks) { fe_sleep(LEVEL_SLEEP_PERIOD); if (handle_level_finished(p_game) < 0) exit = TRUE; /* Last level */ } draw_paddle(&p_game->paddle); /* Sleep for a short while. */ #if !defined(TARGET_REX) fe_sleep(SLEEP_PERIOD); #endif /* TARGET_REX */ } }
int wait_event(SDL_Renderer *renderer) { bool quit; int y_p1; int y_p2; SDL_Event event; SDL_Rect raq_p1; SDL_Rect raq_p2; SDL_Rect ball; SDL_Rect bold; quit = false; init_ball(&ball, &bold); y_p1 = 240; y_p2 = 240; while (!quit) { printf("x"); SDL_Delay(2); SDL_PollEvent(&event); switch (event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: { quit = true; return (0); } case SDLK_UP: { if (y_p1 > 0) y_p1--; else break; break; } case SDLK_DOWN: { if (y_p1 < 480 - 64) y_p1++; else break; break; } case SDLK_LEFT: { if (y_p2 < 480 - 64) y_p2++; else break; break; } case SDLK_RIGHT: { if (y_p2 > 0) y_p2--; else break; break; } } break; } pos_raq_p1(&raq_p1, &y_p1); pos_raq_p2(&raq_p2, &y_p2); ball_move(&ball, &bold, &raq_p1, &raq_p2); print_game(renderer, &raq_p1, &raq_p2, &ball); } }