您的位置:首页 > 运维架构 > Linux

Linux SDL PC端打地鼠效果

2013-11-19 22:14 316 查看
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"SDL.h"
#include"SDL_imageFilter.h"
#include"SDL_gfxPrimitives.h"
#include"SDL_ttf.h"
#include"time.h"

#define WIDTH 640
#define HEIGHT 480
#define NUM_RANDOM 512
#define BORDER 10
static SDL_Surface *screen =NULL;

typedef struct Picset
{
int number;
int x,y;
int w, h;
}Picset;
SDL_Surface *second = NULL;

Picset picset[5]={
{0,35,130,105,120},
{1,15,200,105,120},
{2,500,100,105,120},
{3,470,175,105,120},
{4,380,280,105,120}
};

//初始化SDL
void Init()
{
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1))
{
fprintf(stderr,"SDL init error:%s",SDL_GetError());
exit(-1);
}
TTF_Init();
SDL_WM_SetCaption( "mice play", NULL );

}
//欢迎界面
void welcome()
{
SDL_Surface *screen2=SDL_SetVideoMode(640,480,0,SDL_SWSURFACE);
SDL_Surface * welcome = SDL_LoadBMP("welcome.bmp");
if(welcome==NULL)
{
printf("welcome error");
}
SDL_BlitSurface(welcome,0,screen2,0);
SDL_UpdateRect(screen2,0,0,0,0);
SDL_FreeSurface( welcome );

}
//主界面
void drawEllipse(char *filename)
{
screen=SDL_SetVideoMode(640,480,0,SDL_SWSURFACE);
SDL_Surface *second = (SDL_Surface *)SDL_LoadBMP(filename);
if(second==NULL)
{
printf("second error");
}
SDL_BlitSurface(second,0,screen,0);
SDL_UpdateRect(screen,0,0,0,0);

/*filledEllipseRGBA(screen,80,310,WIDTH/10,HEIGHT/16,240,230,140,250);
filledEllipseRGBA(screen,100,240,WIDTH/11,HEIGHT/16,240,230,140,250);
filledEllipseRGBA(screen,560,220,WIDTH/10,HEIGHT/16,240,230,140,250);
filledEllipseRGBA(screen,530,290,WIDTH/10,HEIGHT/16,240,230,140,250);
filledEllipseRGBA(screen,450,390,WIDTH/9,HEIGHT/14,240,230,140,250);

SDL_UpdateRect(screen,0,0,0,0);*/
SDL_FreeSurface( second );
}

//显示固定字
void display_word(char *word,int x,int y)
{
SDL_Rect setword;
SDL_Surface *message = NULL;
TTF_Font *font = NULL;
SDL_Color textColor = { 0,0,0 };
font = TTF_OpenFont( "test.ttf", 25 );
setword.x=x;
setword.y=y;
message = TTF_RenderText_Solid( font, word, textColor );
SDL_BlitSurface(message,NULL,screen,&setword);
SDL_UpdateRects(screen,1,&setword);
TTF_CloseFont(font);
}
//显示次数
void display_num(int num,int x,int y)
{
char str[40];
TTF_Font *font = NULL;
snprintf(str,40,"%d",num);
SDL_Rect src,dst;
SDL_Color textColor = { 0, 0, 255 };
font = TTF_OpenFont( "test.ttf", 25 );

dst.x=x;
dst.y=y;
SDL_Surface *message = NULL;
message = TTF_RenderText_Solid( font, str, textColor );
src.x=x;
src.y=y;
src.w=message->w ;
src.h=message ->h;
SDL_BlitSurface(message,NULL,screen,&dst);
SDL_UpdateRects(screen,1,&dst);
TTF_CloseFont(font);

}
//局部擦掉之前显示的次数
void deletenum(int x,int y)
{
SDL_Rect src,dst;

src.x=x;
src.y=y;
src.w=80;
src.h=30;

dst.x=x;
dst.y=y;

if(!second) return ;
SDL_BlitSurface(second,&src,screen,&dst);
SDL_UpdateRects(screen,1,&dst);
}
//加载老鼠图片
SDL_Surface *load_Image(char *filename,int X,int Y)
{
SDL_Surface* image = NULL;
SDL_Surface* optimizedImage = NULL;

image = (SDL_Surface *)IMG_Load( filename);

if( image != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( image );
if( optimizedImage != NULL )
{
Uint32 colorkey = SDL_MapRGB( optimizedImage->format,0xFF, 0xFF, 0xFF );
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}
SDL_Rect src,dst;

src.x=0;
src.y=0;
src.w=105;
src.h=120;

dst.x=X;
dst.y=Y;
SDL_BlitSurface(optimizedImage,&src,screen,&dst);
SDL_UpdateRects(screen,1,&dst);
SDL_FreeSurface(image);
SDL_FreeSurface(optimizedImage);
}
//局部擦除老鼠图片
void deletemouse(int X,int Y,int w,int h)
{
SDL_Rect src,dst;

src.x=X;
src.y=Y;
src.w=w;
src.h=h;

dst.x=X;
dst.y=Y;

if(!second) return ;
SDL_BlitSurface(second,&src,screen,&dst);
SDL_UpdateRects(screen,1,&dst);
}
/*int mythread()
{
int this = SDL_GetTicks() / 1000 ;
while(1)
{

int second,mid;
second=SDL_GetTicks()/1000;
mid=second-this;
printf("%d",mid);
if(mid>=20)
{
drawEllipse("over.bmp");
SDL_Delay(4000);
break;
}
display_num(mid,50,30);
//SDL_Delay(500);
deletenum(50,30);
SDL_Delay(10);
}
}*/
//监听鼠标事件
void listener()
{
int over;
over=0;
int num=1;
int k=0;
int x=0;int y=0;
int this;

//SDL_Thread *thread;

//thread = SDL_CreateThread(mythread , NULL);

this=SDL_GetTicks()/1000;
while(over==0)
{

SDL_Event event;
if(SDL_PollEvent(&event))
{
int second,mid;
second=SDL_GetTicks()/1000;
mid=second-this;
printf("%d",mid);
if(mid>=20)
{
drawEllipse("over.bmp");
SDL_Delay(4000);
break;
}
display_num(mid,50,30);
//SDL_Delay(500);
//deletenum(50,30);

switch(event.type)
{
case SDL_QUIT:
over =1;
case SDL_MOUSEBUTTONUP:
x=event.button.x;
y=event.button.y;
printf("%d,%d\n",x,y);

if(( x > picset[k].x ) && ( x < picset[k].x +picset[k].w ) && ( y > picset[k].y ) && ( y < picset[k].y + picset[k].h ))
{
deletemouse(picset[k].x,picset[k].y,105,120);

load_Image("t2.png",picset[k].x,picset[k].y/3*3.4);
deletemouse(picset[k].x,picset[k].y/3*3.4,105,100);
load_Image("t3.png",picset[k].x,picset[k].y/3*3.7);
deletemouse(picset[k].x,picset[k].y/3*3.7,105,50);

display_word("number",550,10);
display_word("left time",50,10);

deletenum(550,30);
display_num(num,550,30);

k=rand()%5;
load_Image("mouses.bmp",picset[k].x,picset[k].y);
printf("k=%d\n",k);
num++;
}

}
}
}

}
int main(int argc,char *argv[])
{
int i=0;
Init();
int X=35;
int Y=130;
welcome();
SDL_Delay(2000);
second =(SDL_Surface *)SDL_LoadBMP("tu3.bmp");
drawEllipse("go.bmp");
SDL_Delay(1000);
drawEllipse("tu3.bmp");
load_Image("mouses.bmp",X,Y);
listener();
SDL_Quit();
return 0;
}

// gcc pcfinal.c -o pcfinal -I/usr/pt/include/SDL -L/usr/pt/lib -lSDL_gfx -lSDL_image -lfreetype -lSDL_ttf
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SDL Linux PC端