/* Increment the score by the specifed amount and redraw the score bar: */ void increment_score(nbstate *state, int points) { state->scores.s += points; /* Increment the current score. */ draw_scores(state); /* Redraw the score bar onto the canvas. */ /* Copy the bar onto the output window. */ draw_canvas(state, 0, 0, state->canvaswidth, state->scores.h); }
// callback function that tells OpenGL how to redraw window void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); // clear the display switch(GAMESTATE) { //Game not yet started case 0: showStartScreen(); LEFT_SCORE = 0; RIGHT_SCORE = 0; BALL_WARP = 1; PADDLE_WARP = 1; //Display Name //Display Controls break; } if(PAUSED) { showPausedStr(); } draw_scores(); left_paddle.draw(); right_paddle.draw(); draw_outline(); ball.draw(); glFlush(); glutSwapBuffers(); }
void controls_draw(int x, int y, state_t *state) { if((x > SEL_X + SEL_BUFFER || y < SEL_Y - SEL_BUFFER) && y < BOT_BAR - BOT_BAR_BUFFER) draw_prelight_grid(x, y, state); draw_scores(state); bar_draw(state); sel_draw(state); }
/* read_new_scores re-reads in the score file info just in case it has been changed. */ void read_new_scores() { flock(scorefd,LOCK_EX); read_scores(); flock(scorefd,LOCK_UN); XFillRectangle(display, scorepixmap, ctable[CBLACK].smallgc, 0, 0, SCOREWIDTH, SCOREHEIGHT); draw_scores(-1); }
int main() { int button_code; int pause_state = 0; int reset_state = 0; setup_gui(); // Seed random num generator srand (time(NULL)); box_fam = create_gen(gen_size); randomise_gen(box_fam); ifit = -1; for (int i=0; i<ngen; i++) { // Score generation score_gen(box_fam); printf("gen %i\r", i); fflush(stdout); // Draw fittest of the fit jfit = fittest_cov(box_fam); if (ifit != jfit) { ifit = jfit; clear_window(); draw_boarder(); draw_cover(& box_fam[ifit]); draw_pause_button(pause_state); draw_reset_button(reset_state); flush_window(); printf("gen %i, %f\n", i, nboxes*pow(box_fam[ifit].l,2)); add_score(i, nboxes*pow(box_fam[ifit].l,2)); } if (window_redraw_required()) { clear_window(); draw_boarder(); draw_cover(& box_fam[ifit]); draw_pause_button(pause_state); draw_reset_button(reset_state); draw_scores(); flush_window(); } button_code = handle_button_presses(); while (button_code != 0 || pause_state || reset_state) { if (pause_state || reset_state) button_code = blocking_handle_button_presses(); switch (button_code) { case -1: // Redraw Required clear_window(); draw_boarder(); draw_cover(& box_fam[ifit]); draw_pause_button(pause_state); draw_reset_button(reset_state); draw_scores(); flush_window(); break; case 1: // Play/Pause Button if (pause_state) { printf("\rPlay! \n"); pause_state = 0; } else { printf("\rPause! \n"); pause_state = 1; } draw_pause_button(pause_state); flush_window(); break; case 2: // Reset/Start Button if (reset_state) { printf("\rStart! \n"); reset_state = 0; } else { printf("\rReset! \n"); print_best(); i = 0; randomise_gen(box_fam); score_gen(box_fam); reset_state = 1; reset_scores(); flush_window(); } clear_window(); draw_boarder(); draw_cover(& box_fam[ifit]); draw_pause_button(pause_state); draw_reset_button(reset_state); flush_window(); break; default: printf("\rButton Error %i\n ", button_code); exit(button_code); } button_code = handle_button_presses(); } // Evolve Generation new_gen(box_fam); } // Clear last gen n line printf(" \r"); fflush(stdout); close_gui(); print_best(); printf("Press enter to close\n"); while( getchar() != '\n' ); return 0; }
void Game::play() { bool launched = false; // is the ball moving bool quit = false; while( !quit ) { // Event handling while( event.poll() ) { if( event.type() == SDL_QUIT ) quit = true; else if( event.type() == SDL_KEYDOWN ) { switch( event.key() ) { case SDLK_ESCAPE : quit = true; break; case SDLK_SPACE : case SDLK_RETURN : if( !launched ) { launched = true; ball.begin_moving(); } break; default : break; } } else if( event.type() == SDL_MOUSEBUTTONDOWN && !launched ) { launched = true; ball.begin_moving(); } player.handle_events( event ); } // Logic if( launched ) // only move the paddles if the game has started { ArtificialIntelligence::ai.move_paddle( computer , ball.pos ); computer.move(); ball.move(); if( ball.handle_collision( sdl::Rect( PADDLE_X_DISP , player.y , PADDLE_WIDTH , PADDLE_HEIGHT ) ) || ball.handle_collision( sdl::Rect( SCREEN_WIDTH - PADDLE_X_DISP - PADDLE_WIDTH , computer.y , PADDLE_WIDTH , PADDLE_HEIGHT ) ) ) { sound_man.play( blip_sound ); } } if( ball.pos.x < PADDLE_X_DISP ) { ai_score++; ball.reset_pos(); launched = false; sound_man.play( lose_sound ); } else if( ball.pos.x > SCREEN_WIDTH - PADDLE_WIDTH - PADDLE_X_DISP ) { player_score++; ball.reset_pos(); launched = false; sound_man.play( lose_sound ); } if( ai_score == MAX_SCORE || player_score == MAX_SCORE ) { end_screen(); return; } // Rendering screen.blit( ORIGIN , background ); player.show(); computer.show(); ball.show(); draw_scores(); screen.flip(); // Capping if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) SDL_Delay( 1000 / FRAMES_PER_SECOND - fps.get_ticks() ); } }
/* Make the current level active. This mainly consists of destroying the old * background image and loading the new one, destroying the splash image, * possibly loading a new splash image depending on the state, copying over the * new level data into the current level state, then redrawing the entire game * area. */ void set_level_active(nbstate *state) { int i; level *lev; grid *g, *gg; int bgchanged; sprite *s = NULL; power *p, *pnext; GR_PIXMAP_ID ctmp; char *backgroundfile; /* Destroy the old splash image sprite: */ destroy_sprite(state, state->splash); /* If we're on the title screen: */ if(state->state == STATE_TITLESCREEN) { /* Set the background file to the title background file: */ backgroundfile = state->titlebackground; /* Set the tiled state appropriately: */ state->backgroundtiled = state->titlebackgroundtiled; /* Try to load the title screen splash graphic (if it doesn't * work nothing bad will happen- load_sprite() will print an * error message and draw_splash() will not draw anything.) */ state->splash = load_sprite(state, state->titlesplash, -1, -1); } else { /* Not on the title screen. */ /* Find the level structure for the current level number: */ for(lev = state->levels, i = 1; i < state->level; lev = lev->next, i++); /* Set the current number of bricks and background info: */ state->numbricks = lev->numbricks; backgroundfile = lev->backgroundname; state->backgroundtiled = lev->backgroundtiled; /* If we're in the "game won" state, try to load the appropriate * splash image: */ if(state->state == STATE_GAMEWON) { state->splash = load_sprite(state, state->gamewonsplash, -1, -1); /* If we're in the "game lost" state, try to load the * appropriate splash image: */ } else if(state->state == STATE_GAMELOST) { state->splash = load_sprite(state, state->gamelostsplash, -1, -1); } else { /* We must be in the STATE_RUNNING state. */ /* No splash image: */ state->splash = NULL; /* Copy this levels game grid into the current game * grid: */ g = state->grid; gg = lev->grid; for(i = 0; i < state->width * state->height; i++) *g++ = *gg++; } } /* If there was a background filename specified: */ if(backgroundfile) { /* If there is a current background sprite with a filename * and the filename is the same as the new background * filename, the background file has not changed. Otherwise, * assume that it has. */ if(state->background && state->background->fname && !strcmp(backgroundfile, state->background->fname)) bgchanged = 0; else bgchanged = 1; /* No background filename was specified, so assume it has changed (to * a blank black frame): */ } else bgchanged = 1; /* If the background image has changed, try to load the new one: */ if(bgchanged && !(s = load_sprite(state, backgroundfile, -1, -1))) { /* If it fails, try to make a new empty sprite and colour it * in black (the 16*16 pixels is purely arbitrary- make it too * large and it uses a lot of memory, make it too small and * we spend ages painting hundreds of tiny tiles onto the * background. */ if(!(s = make_empty_sprite(state, backgroundfile, 16, 16))) { /* If that fails too (shouldn't happen under normal * circumstances), issue a warning and keep the old * background image sprite: */ s = state->background; } else { /* Fill in the new dummy background black: */ GrSetGCForeground(state->gc, GR_COLOR_BLACK); GrFillRect(s->p, state->gc, 0, 0, 16, 16); /* Make it tiled. FIXME: it would make more sense to * have a "no background image" option which simply * coloured in the background black: */ state->backgroundtiled = 1; } } /* If we have made a new background image sprite: */ if(bgchanged && s != state->background) { /* Destroy the old one: */ destroy_sprite(state, state->background); /* Set the background to the new sprite: */ state->background = s; } /* Empty the list of power boxes: */ for(p = state->powers; p; p = pnext) { pnext = p->next; free(p); } state->powers = NULL; /* If fading has been requested, we want to fade the new level in, so * swap the canvasses around so the current screen is the old canvas, * then draw the new screen and make the new canvas, then start the * process of fading it in: */ if(state->faderate) { /* Swap the canvasses around: */ ctmp = state->oldcanvas; state->oldcanvas = state->canvas; state->canvas = ctmp; /* Remember the state we're fading into: */ state->nextstate = state->state; /* Go into the fading state: */ state->state= STATE_FADING; /* Initialise the fade level as completely opaque: */ state->fadelevel = 256; } /* Clear the whole game area to the background image: */ draw_background(state, 0, 0, state->canvaswidth, state->canvasheight); /* If we're not on the title screen or fading to the title screen: */ if(state->state != STATE_TITLESCREEN && !(state->state == STATE_FADING && state->nextstate == STATE_TITLESCREEN)) { /* Draw the bricks: */ draw_bricks(state, 0, 0, state->canvaswidth, state->canvasheight); draw_scores(state); /* Draw the scores bar. */ draw_balls(state); /* Draw the row of balls. */ draw_bat(state); /* Draw the bat. */ /* Draw the current ball unless the game is over: */ if(state->state != STATE_GAMELOST && state->state != STATE_GAMEWON) draw_ball(state); } draw_splash(state); /* Draw the splash graphic (if there is one). */ /* If we're fading, remember the new canvas and generate the first * frame of the fade: */ if(state->state == STATE_FADING) { /* Swap the canvasses around: */ ctmp = state->newcanvas; state->newcanvas = state->canvas; state->canvas = ctmp; /* Reduce the opacity: */ state->fadelevel -= state->faderate; /* Generate the first frame: */ GrCopyArea(state->canvas, state->gc, 0, 0, state->canvaswidth, state->canvasheight, state->newcanvas, 0, 0, 0); GrCopyArea(state->canvas, state->gc, 0, 0, state->canvaswidth, state->canvasheight, state->oldcanvas, 0, 0, GR_CONST_BLEND | state->fadelevel); } /* Copy the entire redrawn canvas to the output window: */ draw_canvas(state, 0, 0, state->canvaswidth, state->canvasheight); }