#include "main.h"
SDL_Surface* screen; //The screen Surface
SDL_Surface* Snake_Body; //The snake surface (shape to draw)
SDL_Rect rectangle1; //Red border
SDL_Rect rectangle2; //Black Board
SDL_Event event; //Keyboard handling
direction facing;
std::vector<SDL_Rect> snake (3); //Snake body locations
SDL_Surface *LoadSnake()
{
SDL_Surface* temporary;
Uint32 rmask,gmask,bmask,amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
temporary = SDL_CreateRGBSurface(SDL_SWSURFACE,5,5,32,rmask,gmask,bmask,amask); //Make an empty surface
SDL_FillRect(temporary,NULL,SDL_MapRGB(screen->format,0,255,0)); //Fill it with color
return temporary; //return it
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1 )
return false;
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
if(!screen)
return false;
SDL_WM_SetCaption("Snake v0.1",NULL);
Snake_Body = LoadSnake();
rectangle1.x = 0; //Red border
rectangle1.y = 0;
rectangle1.w = 500;
rectangle1.h = 500;
rectangle2.x = 25; //Black background
rectangle2.y = 25;
rectangle2.w = 450;
rectangle2.h = 450;
return true;
}
SDL_Surface *loadImage(const char* filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename);
if (loadedImage)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void pollInput()
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
end = true;
}
if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
end = true; break;
case SDLK_UP:
facing = up; break;
case SDLK_DOWN:
facing = down; break;
case SDLK_RIGHT:
facing = right; break;
case SDLK_LEFT:
facing = left; break;
}
}
}
}
void Update()
{
for(unsigned int i = 1; i < snake.size(); i++)
{
snake[i] = snake[i-1];
}
switch(facing)
{
case up:
snake[0].y -= 5; break;
case down:
snake[0].y += 5; break;
case left:
snake[0].x -= 5; break;
case right:
snake[0].x += 5; break;
}
}
void SetUp()
{
snake[0].x = 250;
snake[0].y = 250;
snake[1].x = 250 + 15;
snake[1].y = 250;
snake[2].x = 250 + 15 + 15;
snake[2].y = 250;
}
void Draw()
{
unsigned short i;
int x;
SDL_FillRect(screen,&rectangle1,SDL_MapRGB(screen->format,255,0,0)); //Red Border
SDL_FillRect(screen,&rectangle2,SDL_MapRGB(screen->format,0,0,0)); //Black Board
for(i = 0; i < snake.size(); i++)
{
assert(SDL_BlitSurface(Snake_Body,NULL,screen,&snake[i]) == 0);
}
SDL_Flip(screen);
}
void Finish()
{
SDL_FreeSurface(Snake_Body);
SDL_Quit();
}
我有这个代码,这是一个用SDL编写的蛇游戏我正在研究,但是,我似乎有问题。
SDL很好地绘制了背景,但由于某种原因它不会画蛇。 我在那里设置一个断言来检查SDL_BlitSurface函数是否成功,它返回一个正常值(0),即使屏幕上没有显示任何内容。
我将蛇节点的纹理设置为单个绿色矩形,Snake_Body,我编码了它需要在std :: vector容器中绘制的位置,所以我可以调用SDL_BlitSurface()函数向量中的每个元素,我已经跟踪了值并且它们正在正确地更改,但由于某种原因,未绘制图像stil。我也尝试用相同位置的SDL_FillRect()替换我的SDL_BlitSurface()函数,但它不会改变任何东西。
编辑:我在此编辑下面添加了我的主循环:
#include "main.h"
bool end = false;
Uint32 time;
int main(int argc,char* argv[])
{
if(!init())
return 1;
SetUp();
time = SDL_GetTicks();
while(!end)
{
if(time+1000<SDL_GetTicks())
{
Update();
time = SDL_GetTicks();
}
pollInput();
Draw();
}
Finish();
return 0;
}
答案 0 :(得分:1)
您似乎没有对蛇的位置进行任何检查,以确保它保持在屏幕上。所以当它离开屏幕时,它会保持关闭状态,永远不会再回来。我的猜测是你没有正确调节帧速率,或者你根本没有调节它(我需要看你的主循环),所以蛇离开屏幕的速度太快你甚至都看不到它。 / p>
如果您不拨打Update
功能会怎样?
答案 1 :(得分:0)
如果您发布了整个代码,则会调用SetUp。这不会设置蛇,它可能在屏幕之外的某处,因为向量包含垃圾。此外,即使SDL_BlitSurface忽略dstrect宽度和高度,最好将这些值初始化为一些有意义的值。