📄 p2.c
字号:
#include <SDL.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <time.h>#include "SDL_draw.h" /*把SDL_draw库的头文件包含进来*/#define SCREEN_W 640#define SCREEN_H 480#define TEST_NUM 10#define OUT_TIME 5.0#define DELAY_TIME 500Uint32 RED,YELLOW,BLUE,GREEN,WHITE,BLACK;Uint8 *keys;int color,x,y,rec_w,rec_h,i;SDL_Surface *screen; /*屏幕指针*/SDL_Event event; //SDL事件 double t[TEST_NUM]; void inti_graph();void define_color();void init();void rand_data();void draw_rec();void output(double t[TEST_NUM]);int main(){ init(); for(i=0;i<TEST_NUM;i++){ int flag=0; struct timeval tv1,tv2; draw_rec(); gettimeofday(&tv1,NULL); while(flag==0) while ( SDL_PollEvent(&event) ) { //循环接受键盘事件,直到退出 keys = SDL_GetKeyState(NULL); //得到键盘键的状态 if(keys[SDLK_ESCAPE]) exit(0); else if(keys[SDLK_SPACE]) goto next; else switch(color){ case 1: if(keys['r']){ gettimeofday(&tv2,NULL); flag=1; } break; case 2: if(keys['y']){ gettimeofday(&tv2,NULL); flag=1; } break; case 3: if(keys['b']){ gettimeofday(&tv2,NULL); flag=1; } break; case 4: if(keys['g']){ gettimeofday(&tv2,NULL); flag=1; } break; case 5: if(keys['w']){ gettimeofday(&tv2,NULL); flag=1; } break; default: break; } } SDL_Delay(DELAY_TIME); t[i]=(tv2.tv_sec-tv1.tv_sec)+(tv2.tv_usec-tv1.tv_usec)/1000000.0; next : ; Draw_FillRect(screen,x,y,rec_w,rec_h,BLACK); } output(t); }void inti_graph(){ if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) { /*初始化视频子系统*/ fprintf(stderr, "无法初始化SDL: %s\n", SDL_GetError()); exit(1); } /*设置视频模式*/ screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 16, SDL_SWSURFACE); if ( screen == NULL ) { fprintf(stderr, "无法设置640x480x16位色的视频模式:%s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); /*退出*/ }void define_color(){ RED=SDL_MapRGB(screen->format, 255,0,0); YELLOW=SDL_MapRGB(screen->format, 255,255,0); BLUE=SDL_MapRGB(screen->format, 0,0,255); GREEN=SDL_MapRGB(screen->format, 0,255,0); WHITE=SDL_MapRGB(screen->format, 255,255,255); BLACK=SDL_MapRGB(screen->format,0,0,0);}void init(){ inti_graph(); define_color(); srand((int)time(0)); for(i=0;i<TEST_NUM;i++) t[i]=OUT_TIME; }void rand_data(){ color=1+(int)(5.0*rand()/(RAND_MAX+1.0)); x=(int)(540.0*rand()/(RAND_MAX+1.0)); y=(int)(400.0*rand()/(RAND_MAX+1.0)); rec_w=30+(int)(80.0*rand()/(RAND_MAX+1.0)); rec_h=30+(int)(80.0*rand()/(RAND_MAX+1.0));}void draw_rec(){ rand_data(); switch(color){ case 1:Draw_FillRect(screen,x,y,rec_w,rec_h,RED); break; case 2:Draw_FillRect(screen,x,y,rec_w,rec_h,YELLOW); break; case 3:Draw_FillRect(screen,x,y,rec_w,rec_h,BLUE); break; case 4:Draw_FillRect(screen,x,y,rec_w,rec_h,GREEN); break; case 5:Draw_FillRect(screen,x,y,rec_w,rec_h,WHITE); break; } SDL_UpdateRect(screen,0,0,0,0); }void output(double t[TEST_NUM]){ double sum=0; printf("\n**************************************************************************"); printf("\nYour responce times for each single test are :\n"); for(i=0;i<TEST_NUM;i++){ printf("%lf ",t[i]); sum=sum+t[i]; } printf("\nThe total time you take is: %lf seconds",sum); printf("\nThe average time you take is: %lf seconds\n",sum/TEST_NUM); if(sum/TEST_NUM<1.0) printf("You are great! Congratulations!!!"); else printf("Don`t lose heart,try again!!!"); printf("\n***************************************************************************\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -