|
贪吃蛇源程序(18) /* function:judge if the sanke can eat food.the method like */ /* this:provided that the snake move a step based */ /* on its next move direction,and if this place */ /* have food,then the snake can eat food. */ int willeatfood() { /* 1 means will eat food ,and 0 means won't eat food */ int tempx,tempy; switch(snake_head.next_move) { case MOVE_UP: tempx = snake_head.posx; tempy = snake_head.posy - SCALE; break; case MOVE_LEFT: tempx = snake_head.posx - SCALE; tempy = snake_head.posy; break; case MOVE_DOWN: tempx = snake_head.posx; tempy = snake_head.posy + SCALE; break; case MOVE_RIGHT: tempx = snake_head.posx + SCALE; tempy = snake_head.posy; break; default: break; } if(getpixel(tempx,tempy) == FOOD_COLOR) return 1; return 0; } /* sub function: addonefood() */ /* function: this function will lengthen the snake body */ /* this function is important because it will */ /* not only locate memory for new snake body, */ /* but also handle the relationship of pointer*/ /* between the new snake body and its previous*/ /* snake body. */ void addonefood() { FOOD_INFOR_PTR traceon; snake_head.eatenC ++ ; traceon = snake_head.next; if(snake_head.next == NULL) /* haven't eaten any food */
|