赞
踩
//在代码中取消 Unicode 编码的宏定义,让后续编译都以 MBCS 编码进行 #undef UNICODE #undef _UNICODE #include<stdio.h> #include<conio.h> // _getch _kbhit #include<time.h> #include<graphics.h> //需安装easyX //界面大小,可直接修改 #define M 600 #define N 400 typedef struct { int x, y; }point;//坐标xy,与数学的坐标略有不同 struct snake { point xy[100]; int position; int lenth; }snake; struct food { int flag = 0;//判断食物是否存在 point fdxy; int grade = 0; }food; enum position { up, down, left, right };//枚举 //蛇,初始化蛇的位置 void startsnake() { //蛇头 snake.xy[0].x = 20; snake.xy[0].y = 0; snake.xy[1].x = 10; snake.xy[1].y = 0; snake.xy[2].x = 0; snake.xy[2].y = 0; //蛇初始化方向 snake.position = right; snake.lenth = 3; } //画蛇(要安装easy_X),颜色会不断变化 void drawsnake() { for (int i = 0; i < snake.lenth; i++) { setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10); } } void movesnake() { //蛇身往前移动一格 for (int i = snake.lenth - 1; i > 0; i--) { snake.xy[i].x = snake.xy[i - 1].x; snake.xy[i].y = snake.xy[i - 1].y; } //蛇头方向移动 switch (snake.position) { case up: snake.xy[0].y = snake.xy[0].y - 10; break; case down: snake.xy[0].y = snake.xy[0].y + 10; break; case left: snake.xy[0].x = snake.xy[0].x - 10; break; case right: snake.xy[0].x = snake.xy[0].x + 10; break; } } //随机生成食物 void showfood() { food.fdxy.x = rand() % (M/10) * 10;//0~590 food.fdxy.y = rand() % (N/10) * 10;//0~390 //循环用于判断是否与蛇重合 for (int i = 0; i < snake.lenth; i++) { if (snake.xy[i].x == food.fdxy.x && snake.xy[i].y == food.fdxy.y) { food.fdxy.x = rand() % 60 * 10; food.fdxy.y = rand() % 40 * 10; } } } // void drawfood() { setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); fillrectangle(food.fdxy.x, food.fdxy.y, food.fdxy.x + 10, food.fdxy.y + 10); } void eatfood() { if (snake.xy[0].x == food.fdxy.x && snake.xy[0].y == food.fdxy.y) { snake.lenth++; food.flag = 0;//这里看下main函数 food.grade += 10; } } //键盘操作 void keydown() { char dqown = _getch(); switch (dqown) { case 'W': case 'w': if (snake.position != down) snake.position = up; break; case 'A': case 'a': if (snake.position != right) snake.position = left; break; case 'S': case 's': if (snake.position != up) snake.position = down; break; case 'D': case 'd': if (snake.position != left) snake.position = right; break; case '9':Sleep(5000);//按下 9 暂停 5 秒 (可自行更改) } } void showgrade() { char Grade[20] = ""; sprintf_s(Grade, "grade:%d", food.grade); outtextxy(250, 20, Grade); } //撞墙则死,碰自己不死 void dead() { if (snake.xy[0].x == M || snake.xy[0].x < 0 || snake.xy[0].y < 0 || snake.xy[0].y == N) { char over[20] = "Game Over!"; outtextxy(250, 50, over); system("pause"); exit(0); } } int main() { srand((unsigned int)time(NULL));//播种 initgraph(M, N); //画面大小,可自行随意更改 setbkcolor(RGB(110, 120, 119));//背景颜色 //初始化蛇 startsnake(); drawsnake(); while (1) { cleardevice();//清屏 movesnake(); drawsnake(); if (food.flag == 0)//判断是否生成食物 { showfood(); food.flag = 1; } drawfood(); if (_kbhit())//判断是否键盘操作 { keydown(); } eatfood(); showgrade(); dead(); Sleep(100);//这个可以看作蛇的移动速度 } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。