【原创】贪吃蛇 大家进来提点意见
程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #define TIME 200 void rect(void); void snake_init(POINT *snake); void gotoxy(int,int); int main(void) { int i,j,last_ch,num=0; int start,end; int ch = 'd'; int food = 0; POINT snake[100]; POINT snake_food; rect(); snake_init(snake); start = 4; end = 0; gotoxy(snake[end].x,snake[end].y); printf("*****"); while(1) { while(!kbhit()) { switch(ch) { case 'a': if(last_ch == 'd') { ch = last_ch; break; } _sleep(TIME); gotoxy(snake[end].x,snake[end].y); printf(" "); for(j=0;j < start;j++) snake[j] = snake[j+1]; gotoxy(snake[start].x-1,snake[start].y); printf("*"); snake[start].x -= 1; break; case 'd': if(last_ch == 'a') { ch = last_ch; break; } _sleep(TIME); gotoxy(snake[end].x,snake[end].y); printf(" "); for(j=0;j < start;j++) snake[j] = snake[j+1]; gotoxy(snake[start].x+1,snake[start].y); printf("*"); snake[start].x += 1; break; case 's': if(last_ch == 'w') { ch = last_ch; break; } _sleep(TIME); gotoxy(snake[end].x,snake[end].y); printf(" "); for(j=0;j < start;j++) snake[j] = snake[j+1]; gotoxy(snake[start].x,snake[start].y+1); printf("*"); snake[start].y += 1; break; case 'w': if(last_ch == 's') { ch = last_ch; break; } _sleep(TIME); gotoxy(snake[end].x,snake[end].y); printf(" "); for(j=0;j < start;j++) snake[j] = snake[j+1]; gotoxy(snake[start].x,snake[start].y-1); printf("*"); snake[start].y -= 1; break; default: ch = last_ch; break; } /* 撞墙处理 */ if(snake[start].x <= 0 || snake[start].x >= 42 || snake[start].y <=0 || snake[start].y >= 13) { gotoxy(17,5); printf("你输了\n"); gotoxy(13,6); system("pause"); gotoxy(0,0); rect(); snake_init(snake); start = 4; end = 0; gotoxy(snake[end].x,snake[end].y); printf("*****"); ch = 'd'; food = 0; num = 0; } /* 撞到自己 */ for(i=start-1;i >= 0;i--) { if(snake[start].x == snake[i].x && snake[start].y == snake[i].y) { gotoxy(17,5); printf("你输了\n"); gotoxy(13,6); system("pause"); gotoxy(0,0); rect(); snake_init(snake); start = 4; end = 0; gotoxy(snake[end].x,snake[end].y); printf("*****"); ch = 'd'; food = 0; num = 0; } } /* 生成食物 */ if(!food) { srand(time(0)); snake_food.x = rand() % 40 + 2; snake_food.y = rand() % 11 + 2; gotoxy(snake_food.x,snake_food.y); printf("*"); food = 1; } /* 吃到食物 */ if(snake[start].x == snake_food.x && snake[start].y == snake_food.y) { start += 1; snake[start].x = snake_food.x; snake[start].y = snake_food.y; gotoxy(65,6); num += 10; printf("%d",num); food = 0; } last_ch = ch; } ch = getch(); } return 0; } void gotoxy(int X, int Y) { HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; coord.X = X; coord.Y = Y; SetConsoleCursorPosition( hOutput, coord ); } void rect(void) { printf("┌────────────────────┐\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │ 你的分数为 0\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("│ │\n"); printf("└────────────────────┘\n"); } void snake_init(POINT *snake) { snake[0].x = 2; snake[0].y = 1; snake[1].x = 3; snake[1].y = 1; snake[2].x = 4; snake[2].y = 1; snake[3].x = 5; snake[3].y = 1; snake[4].x = 6; snake[4].y = 1; }
[ 本帖最后由 xxwpk007 于 2010-7-13 18:41 编辑 ]