#include<stdio.h>
#include<stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#define down 1
#define left 2
#define right 3
#define up 4
SDL_Surface *screen;
SDL_Surface *start_background;
SDL_Surface *run_backgound;
SDL_Surface *snake_node;
SDL_Surface *food;
SDL_Surface *score;
SDL_Rect dst;
SDL_Color color;
TTF_Font *font;
char str[10];
typedef struct Body_Node{
int i,j;
struct Body_Node *p;
}Body_Node;
typedef struct Snake{
Body_Node *head,*tail;
int snake_long;
int direction;
}Snake;
typedef struct Food{
int i,j;
int num;
}Food;
SDL_Surface *LoadIMG(const char *name)
{
SDL_Surface *tmp, *final;
if ((tmp = IMG_Load (name)) == NULL)
fprintf (stderr, "load %s error\n", name);
final = SDL_DisplayFormat (tmp);
SDL_FreeSurface (tmp);
SDL_SetColorKey (final, SDL_SRCCOLORKEY | SDL_RLEACCEL,*(Uint32 *) final->pixels);
return final;
}
void Init()
{
if ((SDL_Init (SDL_INIT_AUDIO | SDL_INIT_VIDEO)) < 0)
fprintf (stderr, "init error\n");
if ((screen = SDL_SetVideoMode (720, 720, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
fprintf (stderr, "set video mode error\n");
if (TTF_Init() < 0)
fprintf(stderr, "TTF init error:%s\n", SDL_GetError());
atexit (SDL_Quit);
SDL_WM_SetCaption ("Greed Snake", NULL);
srand (time (NULL));
run_backgound = LoadIMG ("background.png");
start_background = LoadIMG ("start.jpg");
}
void Game_start()
{
int i,j;
int temp=30;
int x=0, y=0;
int num=0;
int flag=0;
dst.x = 0;
dst.y = 0;
dst.w = start_background->w;
dst.h = start_background->h;
SDL_BlitSurface (start_background, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
for(i=1; i<=31;)
{
for(j=1; j<= temp; j++)
{
snprintf(str, 10, "%d.gif", num);
start_background = LoadIMG (str);
SDL_Delay (15);
dst.x = x;
dst.y = y;
dst.w = start_background->w;
dst.h = start_background->h;
SDL_BlitSurface (start_background, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
num = (num+1)%82;
switch(flag)
{
case 0:x += 24;break;
case 1:y += 24;break;
case 2:x -= 24;break;
case 3:y -= 24;break;
}
}
i++;
temp = (32-i)-(31-i)%2;
switch(flag)
{
case 0:{x -= 24;y += 24;}break;
case 1:{x -= 24;y -= 24;}break;
case 2:{x += 24;y -= 24;}break;
case 3:{x += 24;y += 24;}break;
}
flag = (flag+1)%4;
}
}
void Game_background()
{
dst.x = 0;
dst.y = 0;
dst.w = run_backgound->w;
dst.h = run_backgound->h;
SDL_BlitSurface (run_backgound, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
}
void Game_end()
{
font = TTF_OpenFont("f", 100);
color.r = 255;
color.g = 0;
color.b = 0;
score=TTF_RenderText_Solid(font, "Game Over", color);
dst.x = 150;
dst.y = 350;
dst.w = score->w;
dst.h = score->h;
SDL_BlitSurface(score, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
}
void Draw_Score(Snake S)
{
snprintf(str, 10, "%d", S.snake_long);
font = TTF_OpenFont("f", 15);
color.r = 0;
color.g = 0;
color.b = 255;
score=TTF_RenderText_Solid(font, str, color);
dst.x = 278;
dst.y = 122;
dst.w = score->w;
dst.h = score->h;
SDL_BlitSurface(score, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
}
void Draw_Food(Food F)
{
snprintf(str, 10, "%d.gif", F.num);
food = LoadIMG (str);
dst.x = F.i*24;
dst.y = F.j*24+210;
dst.w = food->w;
dst.h = food->h;
SDL_BlitSurface (food, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
}
void Draw_Snake(Snake S)
{
int k;
Body_Node *tp;
tp=S.head;
for(k=0;k<S.snake_long;k=(k+1)%82)
{
snprintf(str, 10, "%d.gif", k);
snake_node = LoadIMG (str);
dst.x = tp->i*24;
dst.y = tp->j*24+210;
dst.w = snake_node->w;
dst.h = snake_node->h;
SDL_BlitSurface (snake_node, NULL, screen, &dst);
SDL_UpdateRects (screen, 1, &dst);
tp=tp->p;
SDL_FreeSurface(snake_node);
}
}
void Produce_Food(Food *F)
{
time_t t;
int i,j;
srand((unsigned)time(&t));
F->i=(rand()%29);
F->j=(rand()%20);
F->num=(rand()%16)+65;
}
void Produce_Snake(Snake *S)
{
Body_Node *temp;
temp=(Body_Node *)malloc(sizeof(Body_Node));
temp->i=4;
temp->j=3;
S->tail=temp;
temp=(Body_Node *)malloc(sizeof(Body_Node));
temp->i=5;
temp->j=3;
S->head=temp;
S->tail->p=S->head;
S->head->p=S->tail;
S->snake_long=2;
S->direction=right;
}
void Move_Snake(Snake *S)
{
switch(S->direction)
{
case left:{
S->tail->i=S->head->i-1;
S->tail->j=S->head->j;
break;
}
case down:{
S->tail->i=S->head->i;
S->tail->j=S->head->j+1;
break;
}
case right:{
S->tail->i=S->head->i+1;
S->tail->j=S->head->j;
break;
}
case up:{
S->tail->i=S->head->i;
S->tail->j=S->head->j-1;
break;
}
}
S->head=S->tail;
S->tail=S->tail->p;
}
贪吃蛇的编程代码void Change_Snake_Direction(SDL_Event event,Snake *S)
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
if(S->direction!=right)
S->direction=left;
break;
case SDLK_DOWN:
if(S->direction!=up)
S->direction=down;
break;
case SDLK_RIGHT:
if(S->direction!=left)
S->direction=right;
break;
case SDLK_UP:
if(S->direction!=down)
S->direction=up;
break;
default:;
}
}
void Add_Snake_Node(Snake *S)
{
Body_Node *tp;
tp=(Body_Node *)malloc(sizeof(Body_Node));
tp->p=S->tail;
S->head->p=tp;
S->snake_long++;
}
void Judge_life(Snake S,int *life)
{
int k;
Body_Node *tp=S.tail;
for(k=S.snake_long;k>=5;k--,tp=tp->p)
{
if(S.head->i==tp->i&&S.head->j==tp->j)
{
*life=0;
break;
}
}
if(S.head->i<0||S.head->i>28)
{
*life=0;
}
else if(S.head->j<0||S.head->j>19)
{
*life=0;
}
}
int main()
{
int life=1;
Snake S;
Food F;
SDL_Event event;
Init();
Produce_Food(&F);
Produce_Snake(&S);
Game_start();
Game_background();
while(life)
{
Game_background();
Draw_Food(F);
Draw_Snake(S);
Draw_Score(S);
SDL_Delay (300);
while (SDL_PollEvent (&event))
{
switch (pe)
{
case SDL_QUIT:
life = 0;
break;
case SDL_KEYDOWN:
Change_Snake_Direction(event,&S);
break;
default:;
}
}
Move_Snake(&S);
if(S.head->i==F.i&&S.head->j==F.j)
{
Add_Snake_Node(&S);
Produce_Food(&F);
}
Judge_life(S,&life);
}
Game_end();
getchar();
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论