2016年1月13日 星期三

SDL - Step6 貪食蛇



螢幕截圖:

#include<stdio.h>
#include<stdlib.h>
#include<SDL/SDL.h>
#include<time.h>
#include<stdbool.h>
#define length 800
#define wide 600
#define num 5
struct node
{
 SDL_Rect snake;
 struct node *next;
};
typedef struct node rect; 
int main(int argc,char** argv)
{
 SDL_Init(SDL_INIT_EVERYTHING);
 SDL_Surface *screen;
 screen=SDL_SetVideoMode(length,wide,32,SDL_SWSURFACE);
 SDL_WM_SetCaption("((((^.^))))****TaiQing's SDL Snake****((((^.^))))",NULL);
 bool running=true;
 Uint32 start; 
 Uint32 color=SDL_MapRGB(screen->format,0,0,0);
 Uint32 color1=SDL_MapRGB(screen->format,255,0,0);
 Uint32 color2=SDL_MapRGB(screen->format,255,255,255);
 int i,dx=-22,dy=0;
 srand(time(NULL));
 SDL_Rect food;
 food.x=rand()%36*22+4;
 food.y=rand()%27*22+14;
 food.w=20;
 food.h=20;
 rect *first,*current,*previous,*increase; 
 //first第一個節點 current正在處理的節點 previous前一節點指標 increase新節點
for(i=0;i<num;i++) 
 {
  current=(rect *) malloc(sizeof(rect));
  current->snake.x=(400+22*i);
  current->snake.y=300;
  current->snake.w=20;
  current->snake.h=20;
  if(i==0)
   first=current;
  else
   previous->next=current;
  current->next=NULL;
  previous=current;
 }
 while(running)
 {
  start=SDL_GetTicks();
  SDL_Event event;
  //keyboard  
  while(SDL_PollEvent(&event))
        {
   switch(event.type) 
   {
case SDL_QUIT:
      running=false;
      break;
     case SDL_KEYDOWN:
      switch(event.key.keysym.sym) 
      {
       case SDLK_ESCAPE:
        running=false;
        break;
       case SDLK_UP:
        if(dx!=0)        
         dy=-22;
        dx=0;         
        break;
       case SDLK_DOWN:
        if(dx!=0)        
         dy=22;
        dx=0;
        break;
       case SDLK_LEFT:
        if(dy!=0)
         dx=-22;
        dy=0;
        break;
       case SDLK_RIGHT:
        if(dy!=0)
         dx=22;
        dy=0;
        break;
      }
      break;     
   }
  }
  //move
  current=first;  
  while(current->next!=NULL)
   current=current->next;
  while(current!=first)
  {
   previous=first; 
   while(previous->next!=current)
    previous=previous->next;
   current->snake.x=previous->snake.x;
   current->snake.y=previous->snake.y;
   current=previous;
  }
  first->snake.x+=dx;
  first->snake.y+=dy;
  //eat food and change length
  if(first->snake.x==food.x && first->snake.y==food.y)
  {
   food.x=rand()%36*22+4;
   food.y=rand()%27*22+14;
   increase=(rect *) malloc(sizeof(rect));
   current=first;  
   while(current->next!=NULL)
    current=current->next;
   if(current->next==NULL)
   {
    increase->snake=current->snake;
    increase->next=NULL;
    current->next=increase;
   }
  }
  //die or dorder
  if(first->snake.x<0 || first->snake.x>780)
   running=false;
  if(first->snake.y<0 || first->snake.y>580)
   running=false;
  current=first;
  current=current->next;
  while(current!=NULL)
  {
   if(current->snake.x==first->snake.x && current->snake.y==first->snake.y)
    running=false;
   current=current->next;
  }
  //render
  SDL_FillRect(screen,&screen->clip_rect,color);
  SDL_FillRect(screen,&food,color2);
  current=first;    
  while(current!=NULL)
  {     
   SDL_FillRect(screen,&(current->snake),color1);
   current=current->next;  
  } 
 SDL_Flip(screen);
 SDL_Delay(100);    
 }
 SDL_Quit();
 return 0;  
}

沒有留言:

張貼留言