gpt4 book ai didi

SDL 中的跳棋游戏

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:57 25 4
gpt4 key购买 nike

我正在尝试制作西洋跳棋游戏和 atm 我正在使用 SDL 做接口(interface),但我只是在学习 C 和 SDL,如何移动我添加到屏幕上的表面?我希望它尽可能简单,只需从 X 移除并在 Y 上显示,如何移除表面以使其出现在屏幕上的另一个位置?这是我的代码:

#include "SDL.h"
#define BRANCA 2
#define PRETA 1
#define DAMA 2
#define NORMAL 1

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *pecaPreta = NULL;
SDL_Surface *pecaBranca = NULL;
SDL_Surface *pecaDamaPreta = NULL;
SDL_Surface *pecaDamaBranca = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;

SDL_Surface *load_image(char * filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename);

if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );

if( optimizedImage != NULL )
{
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}

return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
SDL_Rect offset;

offset.x = x;
offset.y = y;

SDL_BlitSurface( source, NULL, destination, &offset );
}

void inserePeca(int tipo, int posX, int posY, int cor)
{
switch(cor)
{
case 1:
switch (tipo)
{
case 1:
apply_surface(posX, posY, pecaPreta, screen);
break;
case 2:
apply_surface(posX, posY, pecaDamaPreta, screen);
break;
}
break;
case 2:
switch (tipo)
{
case 1:
apply_surface(posX, posY, pecaBranca, screen);
break;
case 2:
apply_surface(posX, posY, pecaDamaBranca, screen);
break;
}
break;
}
}

int main()
{
int quit = 0;
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}

screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

if( screen == NULL )
{
return 1;
}

//Set the window caption
SDL_WM_SetCaption( "Jogo de Damas 0.1b", NULL );

//Load the images
pecaPreta = load_image( "pecapreta.bmp" );
pecaBranca = load_image("pecabranca.bmp");
pecaDamaPreta = load_image("pecadamapreta.bmp");
pecaDamaBranca = load_image("pecadamabranca.bmp");
background = load_image( "tabuleiro.bmp" );

//Apply the background to the screen
apply_surface( 0, 0, background, screen );

inserePeca(DAMA, 0,0, BRANCA);
inserePeca(NORMAL, 80,0, PRETA);

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
while( quit == 0 )
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = -1;
}
}
}

//Free the surfaces
SDL_FreeSurface( pecaPreta );
SDL_FreeSurface( background );

//Quit SDL
SDL_Quit();

return 0;
}

如您所见,我在“inserePeca”上添加了一个 block ,我想在创建它之后移动它

最佳答案

屏幕缓冲区不会将您在其上绘制的所有内容作为单独的项目保存——它只保存所有绘制操作的最终结果。因此,您不能只绘制背景,然后在其上绘制一 block ,然后四处移动该 block ——您需要根据需要的更改重新绘制屏幕上受影响的部分。

你还有棋子的图片,还有背景图片;移动你绘制的一 block 的方法很简单,就是通过再次blit将背景恢复到原来的位置,然后将这 block blit到新的位置。不过,无需重新绘制整个屏幕和所有部分,您可以只绘制更改的区域:只对背景的一部分进行 blit 以删除旧方 block ,然后将这部分 blit 到新方 block 上。

以下函数类似于您的 apply_surface() 函数,但它不是将整个源图像复制到目标的给定坐标,而是从中复制给定宽度和高度的区域源图像的给定坐标到目标的相同坐标。然后,这可用于恢复一小部分屏幕的背景。

/* Blit a region from src to the corresponding region in dest.  Uses the same
* x and y coordinates for the regions in both src and dest. w and h give the
* width and height of the region, respectively.
*/
void erase_rect( int x, int y, int w, int h, SDL_Surface *src, SDL_Surface *dest)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
offset.w = w;
offset.h = h;
SDL_BlitSurface( src, &offset, dest, &offset );
}

因此,如果您的正方形是 50x50,并且您需要将一 block 棋子从 (120, 40) 的正方形移动到 (170, 90) 的正方形,您可以执行如下操作:

/* erase old 50x50 square at (120,40) (to background image) */
erase_rect( 120, 40, 50, 50, background, screen );
/* draw piece at new position of (170,90) */
inserePeca(NORMAL, 170, 90, PRETA);

关于SDL 中的跳棋游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com