⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.c

📁 本人自己编写的Linux下的象棋程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "function.h"SDL_Surface* load_image( char *filename ){    SDL_Surface* loadedImage = NULL;    SDL_Surface* optimizedImage = NULL;    loadedImage = IMG_Load( filename );    if( loadedImage != NULL )    {        optimizedImage = (SDL_Surface*)SDL_DisplayFormat( loadedImage );        SDL_FreeSurface( loadedImage );    }    return optimizedImage;}void display_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip ){    SDL_Rect offset;    offset.x = x;     offset.y = y;       SDL_BlitSurface( source, clip, destination, &offset );}void init_qizi(){       qizi[RL_CAR].image = (SDL_Surface *)load_image(R_CAR);    qizi[RR_CAR].image = qizi[RL_CAR].image;    qizi[RL_HOR].image = (SDL_Surface *)load_image(R_HOR);    qizi[RR_HOR].image = qizi[RL_HOR].image;    qizi[RL_ELF].image = (SDL_Surface *)load_image(R_ELF);    qizi[RR_ELF].image = qizi[RL_ELF].image;    qizi[RL_NURSE].image = (SDL_Surface *)load_image(R_NURSE);    qizi[RR_NURSE].image = qizi[RL_NURSE].image;    qizi[RC_MAS].image = (SDL_Surface *)load_image(R_MAS);    qizi[R_BING1].image = (SDL_Surface *)load_image(R_BING);    qizi[R_BING2].image = qizi[R_BING1].image;    qizi[R_BING3].image = qizi[R_BING1].image;    qizi[R_BING4].image = qizi[R_BING1].image;    qizi[R_BING5].image = qizi[R_BING1].image;    qizi[RL_PAO].image = (SDL_Surface *)load_image(R_PAO);    qizi[RR_PAO].image = qizi[RL_PAO].image;        qizi[BL_CAR].image = (SDL_Surface *)load_image(B_CAR);    qizi[BR_CAR].image = qizi[BL_CAR].image;    qizi[BL_HOR].image = (SDL_Surface *)load_image(B_HOR);    qizi[BR_HOR].image = qizi[BL_HOR].image;    qizi[BL_ELF].image = (SDL_Surface *)load_image(B_ELF);    qizi[BR_ELF].image = qizi[BL_ELF].image;    qizi[BL_NURSE].image = (SDL_Surface *)load_image(B_NURSE);    qizi[BR_NURSE].image = qizi[BL_NURSE].image;    qizi[BC_MAS].image = (SDL_Surface *)load_image(B_MAS);    qizi[B_ZU1].image = (SDL_Surface *)load_image(B_ZU);    qizi[B_ZU2].image = qizi[B_ZU1].image;    qizi[B_ZU3].image = qizi[B_ZU1].image;    qizi[B_ZU4].image = qizi[B_ZU1].image;    qizi[B_ZU5].image = qizi[B_ZU1].image;    qizi[BL_PAO].image = (SDL_Surface *)load_image(B_PAO);    qizi[BR_PAO].image = qizi[BL_PAO].image;}void init_qipan(){    int x = 0;    int y = 0;    for(x = 0; x < 10; x++)    {    for (y = 0; y < 9; y++)        {            qipan[x][y] = 0;	}        }        qipan[0][0] = RL_CAR;    qipan[0][8] = RR_CAR;    qipan[0][1] = RL_HOR;    qipan[0][7] = RR_HOR;    qipan[0][2] = RL_ELF;    qipan[0][6] = RR_ELF;    qipan[0][3] = RL_NURSE;    qipan[0][5] = RR_NURSE;    qipan[0][4] = RC_MAS;    qipan[2][1] = RL_PAO;    qipan[2][7] = RR_PAO;    qipan[3][0] = R_BING1;    qipan[3][2] = R_BING2;    qipan[3][4] = R_BING3;    qipan[3][6] = R_BING4;    qipan[3][8] = R_BING5;            qipan[9][0] = BL_CAR;    qipan[9][8] = BR_CAR;    qipan[9][1] = BL_HOR;    qipan[9][7] = BR_HOR;    qipan[9][2] = BL_ELF;    qipan[9][6] = BR_ELF;    qipan[9][3] = BL_NURSE;    qipan[9][5] = BR_NURSE;    qipan[9][4] = BC_MAS;    qipan[7][1] = BL_PAO;    qipan[7][7] = BR_PAO;    qipan[6][0] = B_ZU1;    qipan[6][2] = B_ZU2;    qipan[6][4] = B_ZU3;    qipan[6][6] = B_ZU4;    qipan[6][8] = B_ZU5;   }int display_qizi(int x, int y, struct QIZI *qizi, SDL_Surface *screen, SDL_Surface *qipan_screen){	int i, j;	Uint32 color;	int radius = qizi->image->w / 2 - 2;	for(i = 0; i < qizi->image->w; i++)	{		for(j = 0; j < qizi->image->h; j++)		{			if(sqrt(pow(i - 1 - radius, 2) + pow(j - 1  - radius, 2)) < radius)			{				color = getpixel(qizi->image, i, j);				putpixel(screen, 10 + 60 * y + i, 10 + 60 * x + j, color);			}					}	}			    	SDL_UpdateRect(screen, y, x, qizi->image->w, qizi->image->h);    	return 0;	}void display_qipan(SDL_Surface* qipan_screen, SDL_Surface *screen){    display_surface(0,0,qipan_screen,screen,NULL);    if( SDL_Flip( screen ) == -1 )    {        return ; 	        }    	         int x = 0;    int y = 0;    for( x = 0; x < 10; x++)    {    for (y = 0; y < 9; y++)        {	    if (qipan[x][y] != 0)            display_qizi(x, y, &qizi[qipan[x][y]], screen, qipan_screen);	}        }}//将鼠标获得的屏幕上的点的坐标转化为棋盘坐标int exchange_pos(int screen_x, int screen_y, int *y, int *x){       int qizi_x;    int qizi_y;    int distance;    if ( screen_x < 5 || screen_x > 545  || screen_y < 5 || screen_y > 605)        return -1;		    if(screen_x < 35 )        *x = 0;    else		   {        *x = (screen_x - 35) / 60;        if( ((screen_x - 35)%60) > 30 )            (*x)++;   }   if(screen_y < 35)       *y = 0;   else   {            *y = (screen_y - 35) / 60;          if( ((screen_y - 35)%60) > 30 )            (*y)++;    }        qizi_x = (*x) * 60 + 35;    qizi_y = (*y) * 60 + 35;    distance = sqrt((screen_x - qizi_x) * (screen_x - qizi_x)  + (screen_y - qizi_y) * (screen_y - qizi_y));    if(distance > 30)        return -1;    else        return 0;}//起点和终点间的棋子个数int chess_count(int x0, int y0, int x, int y){    if((x0 != x) && (y0 != y))        return -1;	    int count = 0;    int start;    int end;    if(y == y0)    {        if(x0 < x)        {            start = x0 + 1;            end = x;        }        else        {            start = x+1;            end = x0;        }        while (start < end)        {            if(qipan[start++][y0] != 0)                count ++;          }    }    else if(x == x0)    {        if(y0 < y)        {            start = y0 + 1;            end = y;        }        else        {	   	            start = y+1;            end = y0;        }        while (start < end)        {            if(qipan[x0][start++] != 0)                count ++;        }    }    return count;}int friend(int chess1 ,int chess2 ){        if ((chess2 > 16) && (chess1 >16) || (chess2 <= 16) && (chess1 <= 16))    {             return 1;    }    return 0;        }//炮的运动规则int pao_rules(int x0, int y0, int x, int y, SDL_Surface *screen, SDL_Surface *qipan_screen){       int count;     count = chess_count(x0, y0, x, y);    if (((count == 1) && (qipan[x][y] != 0) && !friend(qipan[x0][y0] ,qipan[x][y])) || ((count == 0) && (qipan[x][y] == 0)))    {    	if(qipan[x][y] == RC_MAS || qipan[x][y] == BC_MAS)	{		move(x0, y0, x, y, screen, qipan_screen);				return 1;	}	else	{        	move(x0, y0, x, y, screen, qipan_screen);        	return 0;	}    }    return -1;                 }//车的运动规则int car_rules(int x0, int y0, int x, int y, SDL_Surface *screen, SDL_Surface *qipan_screen){    int count;     count = chess_count(x0, y0, x, y);    if ((count == 0) && ((qipan[x][y] != 0) && !friend(qipan[x0][y0] ,qipan[x][y]) ||  (qipan[x][y] == 0)))    {    	if(qipan[x][y] == RC_MAS || qipan[x][y] == BC_MAS)	{		move(x0, y0, x, y, screen, qipan_screen);		return 1;	}	else	{        	move(x0, y0, x, y, screen, qipan_screen);        	return 0;	}    }    return -1; }//红兵黑卒的运动规则int bing_zu_rules(int x0, int y0 ,int x, int y, SDL_Surface *screen, SDL_Surface *qipan_screen) {    if((qipan[x0][y0] <= 16) &&(qipan[x0][y0] >= 1))    {	    if(x0 >= 5)	    {	                  	    		if((x == (x0 +1) && y ==y0 || abs(y -y0) == 1 && x == x0))                {			return try_move(x0, y0, x, y, screen, qipan_screen);              } 	   }	   else	   {	       if((x == x0 + 1) && y == y0)	       {			return  try_move(x0, y0, x, y, screen, qipan_screen);    	       }	   	   }    }	       else if(qipan[x0][y0]  > 16)    {    	 if((x0 <= 4) && (x0 >= 1))	    {	        if (x == (x0 - 1) && y == y0 || abs(y - y0) == 1 && x == x0)	        {			return  try_move(x0, y0, x, y, screen, qipan_screen);		}	    	    }	    else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -