用C語言如何實(shí)現(xiàn)貪吃蛇編程
貪吃蛇游戲在理論上是可以無限的進(jìn)行下去的(除了撞墻和咬到自己),那么游戲主體就一定是個(gè)循環(huán)。下面是小編為大家?guī)淼年P(guān)于用C語言如何實(shí)現(xiàn)貪吃蛇編程的知識(shí),歡迎閱讀。
貪吃蛇實(shí)現(xiàn)原理:
貪吃蛇游戲在理論上是可以無限的進(jìn)行下去的(除了撞墻和咬到自己),那么游戲主體就一定是個(gè)循環(huán)。
蛇是如何動(dòng)起來的?在這里就是通過不斷改變蛇的坐標(biāo),然后根據(jù)蛇的坐標(biāo)不斷刷新屏幕在視覺上形成蛇的移動(dòng)效果。
食物出現(xiàn)在隨機(jī)位置(當(dāng)然不能出現(xiàn)在障礙物和蛇身上)。
蛇能吃到食物其實(shí)就是蛇頭的坐標(biāo)與食物的坐標(biāo)重合時(shí)。
當(dāng)蛇咬到自己或者撞到墻的時(shí)候游戲結(jié)束(坐標(biāo)判斷)
#include
#include
#include
#include
#include
/pic/p>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define SNAKE 1 /pic/p>
#define FOOD 2 /pic/p>
#define BAR 3 /pic/p>
/pic/p>
char map[17][17] = {0};
/pic/p>
unsigned char snake[50] = {77};
/pic/p>
unsigned char food = 68;
/pic/p>
char len = 1;
/pic/p>
void tran(unsigned char num,unsigned char * x,unsigned char * y);
/pic/p>
void print_game(void);
/pic/p>
int get_dir(int old_dir);
/pic/p>
void move_snake(int dir);
/pic/p>
unsigned char generate_food(void);
/pic/p>
int isalive(void);
int main(void){
int dir = UP; /pic/p>
/pic/p>
while(1){
print_game(); /pic/p>
dir = get_dir(dir); /pic/p>
move_snake(dir); /pic/p>
if(!isalive()){ /pic/p>
break;
}
}
printf("Game Over! ");
return 0;
}
/pic/p>
void tran(unsigned char num,unsigned char * x,unsigned char * y){
*x = num >> 4;
*y = (unsigned char)(num << 4) >> 4; /pic/p>
/pic/p>
}
void print_game(void){
int i,j;
/pic/p>
for(j = 0;j < 17;j ++){
for(i = 0;i < 17;i ++){
/pic/p>
if(map[i][j] == 0){
put' ');
}
/pic/p>
else if(map[i][j] == SNAKE){
put'*');
}
/pic/p>
else if(map[i][j] == BAR){
put'#');
}
/pic/p>
else if(map[i][j] == FOOD){
put'$');
}
}
put' ');
}
Sleep(500); /pic/p>
system("cls"); /pic/p>
}
int get_dir(int old_dir){
int new_dir = old_dir;
/pic/p>
/pic/p>
/pic/p>
if(_kbhit()){
_getch(); /pic/p>
new_dir = _getch(); /pic/p>
/pic/p>
/pic/p>
/pic/p>
if(len > 1 && (abs(new_dir - old_dir) == 2 || abs(new_dir - old_dir) == 8)){
new_dir = old_dir;
}
}
return new_dir;
}
void move_snake(int dir){
int last = snake[0],current; /pic/p>
int i,j;
int grow=0; /pic/p>
unsigned char x, y,fx,fy; /pic/p>
tran(food, &fx, &fy); /pic/p>
tran(snake[0], &x, &y); /pic/p>
switch (dir){ /pic/p>
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
/pic/p>
/pic/pic/p>
snake[0] = ((x ^ 0) << 4) ^ y; /pic/p>
/pic/p>
/pic/p>
/pic/p>
if (snake[0] == food) {
grow = 1;
food = generate_food(); /pic/p>
}
for (i = 0; i
if (i == 0) /pic/p>
continue;
current = snake[i]; /pic/p>
snake[i] = last; /pic/p>
last = current; /pic/p>
}
/pic/p>
if (grow) {
snake[len] = last;
len++;
}
for (j = 0; j < 17; j ++){ /pic/p>
for (i = 0; i < 17; i ++){
if (i == 0 || i == 16 || j == 0 || j == 16){
map[i][j] = BAR;
}
else if (i == fx&&j == fy){
map[i][j] = FOOD;
}
else{
map[i][j] = 0;
}
}
for (i = 0; i < len; i++) { /pic/p>
tran(snake[i], &x, &y);
if (snake[i] > 0){
map[x][y] = SNAKE;
}
}
}
}
unsigned char generate_food(void)
{
unsigned char food_,fx,fy;
int in_snake=0,i;
/pic/p>
srand((unsigned int)time(NULL));
/pic/p>
do {
food_ = rand() % 255;/pic/p>
tran(food_, &fx, &fy);
for (i = 0; i < len; i++){
if (food_ == snake[i]){
/pic/p>
in_snake = 1;
}
}
} while (fx == 0 || fx == 16 || fy == 0 || fy == 16 || in_snake);
return food_;
}
int isalive(void)
{
int self_eat = 0;
int i;
unsigned char x, y;
tran(snake[0], &x, &y);
for (i = 1; i < len; i++){
if (snake[0] == snake[i]){
self_eat = 1;
}
}
/pic/p>
return (x == 0 || x == 16 || y == 0 || y >= 16 || self_eat) ? 0 : 1;
}
【用C語言如何實(shí)現(xiàn)貪吃蛇編程】相關(guān)文章:
如何學(xué)好C語言編程01-26
如何學(xué)習(xí)C語言編程03-19
鏈表的C語言實(shí)現(xiàn)方法編程學(xué)習(xí)02-22
C語言編程程序的內(nèi)存如何布局03-11
C語言編程基礎(chǔ)10-20
C編程語言概述11-04
C語言入門什么是編程語言12-13