📄 wblx.c
字号:
#include <stdlib.h>#include <SDL.h>#include <SDL_mixer.h>#include "font.h"#define TICK_INTERVAL 40#define TIME_LEFT 1#define START 1#define SETUP 2#define EXIT 3void wblx();void game_init();void game_over();void draw_box(SDL_Surface *surface,int x,int y,int w,int h,Uint32 color);int main_menu();void setup_menu();void data_init();void game_main_loop();void disp_result(int);SDL_Surface *screen;SDL_Surface *background;Uint8 game_runing;int Sound=1;Mix_Chunk *beep;int SVolume=128;Uint8 keys[4];Uint8 hits;Uint32 lastTick,curTick;int tick_interval=40;int time_slice=1;int time_left;int count;struct hz_info{ unsigned char word[2]; unsigned char code[4]; int index; int x,y; int time; int kill; SDL_Surface *Sprite; SDL_Rect SrcMap; SDL_Rect DesMap;}hz[4];/* ------------------------------ */int main(){ wblx();}/* ------------------------------ */void wblx(){ int chose; game_init(); for(;;){ chose=main_menu(); if(chose==START){game_runing=1;game_main_loop();} else if(chose==SETUP)setup_menu(); else if(chose==EXIT)break; } game_over();}/* --------------------------- */int is_same(Uint8 *str1,Uint8 *str2,int length){ int i; for(i=0;i<length;i++)if(str1[i]!=str2[i])return 0; return 1;}/* --------------------------- */void ClearSurface(SDL_Surface *surface){ Uint32 color; SDL_Rect clip; clip.x=0; clip.y=0; clip.w=surface->w; clip.h=surface->h; color=SDL_MapRGB(surface->format,0,0,0); SDL_FillRect(surface,&clip,color);}/* --------------------------- */void Create_hzSprite(int i){ int j; int LL=20; float RR=100.0; int WW=136; int Range=3333; int color; hz[i].time=0; hz[i].x=LL+1+(int)(RR*rand()/(RAND_MAX+1.0))+i*WW; hz[i].y=(1+(int)(100.0*rand()/(RAND_MAX+1.0))); hz[i].index=1+(int)((float)Range*rand()/(RAND_MAX+1.0))-1; hz[i].word[0]=wbTAB[hz[i].index*6+0]; hz[i].word[1]=wbTAB[hz[i].index*6+1]; for(j=0;j<4;j++)hz[i].code[j]=wbTAB[hz[i].index*6+2+j]; hz[i].kill=0; if(hz[i].Sprite==NULL){ printf("Error!\n"); exit(-1); } hz[i].Sprite=SDL_DisplayFormat(hz[i].Sprite); ClearSurface(hz[i].Sprite); SDL_SetColorKey(hz[i].Sprite,SDL_SRCCOLORKEY, SDL_MapRGB(hz[i].Sprite->format,0,0,0)); color=SDL_MapRGB(screen->format,255,255,255); cputchar2x(hz[i].Sprite,0,0,color,hz[i].word); hz[i].SrcMap.x=hz[i].DesMap.x=hz[i].x; hz[i].SrcMap.y=hz[i].DesMap.y=hz[i].y; hz[i].SrcMap.w=hz[i].DesMap.w=32; hz[i].SrcMap.h=hz[i].DesMap.h=32; hz[i].DesMap.y=0; hz[i].SrcMap.y=0;}/* ------------------------------ */void PlaySound(Mix_Chunk *Snd){ int channel; if(Sound){ Mix_Volume(1,SVolume); Mix_PlayChannel(1,Snd,0); }}/* ------------------------------ */void game_init(){ int i; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)<0){ fprintf(stderr,"Can't init SDL:%s\n",SDL_GetError()); exit(1); } screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); if(screen==NULL){ fprintf(stderr,"Can't set mode 640x480:%s\n",SDL_GetError()); exit(1); } SDL_ShowCursor(0); // Create backgroud surface background=SDL_CreateRGBSurface(SDL_SRCALPHA,640,480,16, screen->format->Rmask,screen->format->Gmask, screen->format->Bmask,screen->format->Amask); if(background==NULL){ printf("Out of memory!\n"); exit(1); } background=SDL_DisplayFormat(background); // Font init font_init(); // Hz sprite init for(i=0;i<4;i++){ hz[i].Sprite=SDL_CreateRGBSurface(SDL_SRCALPHA,32,32,16, screen->format->Rmask,screen->format->Gmask, screen->format->Bmask,screen->format->Amask); if(hz[i].Sprite==NULL){ printf("Out of memory!\n"); exit(1); } } // Init sound if(Sound){ if(Mix_OpenAudio(22050,AUDIO_S16,1,1024)<0){ fprintf(stderr,"Warning:Couldn't set 22050 Hz 16-bit audio\n-Redson:%s\n", SDL_GetError()); Sound=0; } beep=Mix_LoadWAV("./wav/kill.wav"); }}/* ------------------------------ */void game_over(){ int i; font_free(); if(background!=NULL)SDL_FreeSurface(background); for(i=0;i<4;i++)if(hz[i].Sprite!=NULL)SDL_FreeSurface(hz[i].Sprite); atexit(SDL_Quit); printf("Http://linside.8u8.com\n");}/* ------------------------------ */void draw_box(SDL_Surface *surface,int x,int y,int w,int h,Uint32 color){ SDL_Rect box; box.x=x;box.y=y; box.w=w;box.h=h; SDL_FillRect(surface,&box,color); SDL_UpdateRect(screen,x,y,w,h);}/* ------------------------------ */int get_key(){ SDL_Event event; for(;;){ SDL_WaitEvent(&event); switch(event.type){ case SDL_KEYDOWN: if(event.key.keysym.sym==SDLK_ESCAPE){ printf("ESC!\n"); return; } else if(event.key.keysym.sym==SDLK_RETURN){ game_runing=1; data_init(); return; } break; case SDL_QUIT: printf("SDL QUIT.\n"); exit(0); } }}/* ------------------------------ */int main_menu(){ int key; int select=0; Uint32 color; SDL_Surface *logo; SDL_Event event; SDL_Rect logoMap={0,0,32,32}; SDL_Rect Mask={190,200,32,320}; SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0)); color=SDL_MapRGB(screen->format,0,40,130); draw_box(screen,80,20,480,80,color); color=SDL_MapRGB(screen->format,255,255,255); draw_box(screen,90,30,460,60,color); color=SDL_MapRGB(screen->format,0,40,130); draw_box(screen,92,32,456,56,color); color=SDL_MapRGB(screen->format,255,226,65); ecprint2x(screen,60,22,color,"五笔练习 for Linux V0.6.8"), /*color=SDL_MapRGB(screen->format,255,255,255);*/ ecprint2x(screen,120,100,color,"开 始 练 习"); ecprint2x(screen,120,130,color,"参 数 设 置"); ecprint2x(screen,120,160,color,"退 出 游 戏"); SDL_UpdateRect(screen,0,0,0,0); logo=SDL_LoadBMP("bmp/logo.bmp"); if(logo==NULL)return EXIT; SDL_SetColorKey(logo,SDL_SRCCOLORKEY,SDL_MapRGB(logo->format,255,0,255)); SDL_SetAlpha(logo,SDL_SRCALPHA,255); logo=SDL_DisplayFormat(logo); SDL_BlitSurface(logo,NULL,screen,&Mask); SDL_UpdateRect(screen,Mask.x,Mask.y,Mask.w,Mask.h); Mask.w=32;Mask.h=320; for(;;){ SDL_WaitEvent(&event); switch(event.type){ case SDL_KEYDOWN: if(event.key.keysym.sym==SDLK_RETURN||event.key.keysym.sym==SDLK_SPACE){ return (select+1); } if(event.key.keysym.sym==SDLK_ESCAPE){ return EXIT; } if(event.key.keysym.sym==SDLK_UP){ select--;if(select<0)select=2; logoMap.x=190;logoMap.y=200+select*60; SDL_FillRect(screen,&Mask,SDL_MapRGB(screen->format,0,0,0)); SDL_BlitSurface(logo,NULL,screen,&logoMap); SDL_UpdateRect(screen,Mask.x,Mask.y,Mask.w,Mask.h); SDL_UpdateRect(screen,logoMap.x,logoMap.y,logoMap.w,logoMap.h); } if(event.key.keysym.sym==SDLK_DOWN){ select++;if(select>2)select=0; logoMap.x=190;logoMap.y=200+select*60; SDL_FillRect(screen,&Mask,SDL_MapRGB(screen->format,0,0,0)); SDL_BlitSurface(logo,NULL,screen,&logoMap); SDL_UpdateRect(screen,Mask.x,Mask.y,Mask.w,Mask.h); /*SDL_UpdateRect(screen,logoMap.x,logoMap.y,logoMap.w,logoMap.h);*/ } break; case SDL_QUIT: return EXIT; } } return EXIT;}/* ------------------------------ */void setup_menu(){ int i; Uint32 color; int select=0; static int sub_select[3]={1,1,0}; char code[2]; char *menu_item[]={ "难 度: 低", "难 度: 中", "难 度: 高", "速 度: 慢", "速 度: 中", "速 度: 快", "时 间: 1 分钟", "时 间: 5 分钟", "时 间: 8 分钟" }; SDL_Event event; SDL_Rect Mask={190,200,32,320}; SDL_Rect Mask2={240,260,200,32}; // Show title SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0)); color=SDL_MapRGB(screen->format,0,40,130); draw_box(screen,80,20,480,80,color); color=SDL_MapRGB(screen->format,255,255,255); draw_box(screen,90,30,460,60,color); color=SDL_MapRGB(screen->format,0,40,130); draw_box(screen,92,32,456,56,color); color=SDL_MapRGB(screen->format,255,226,65); ecprint2x(screen,60,22,color," 参 数 设 置 "); SDL_UpdateRect(screen,0,0,0,0); // Show menu for(i=0;i<3;i++)ecprint2x(screen,120,100+i*30,color, menu_item[i*3+sub_select[i]]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -