編譯環境:vs 2019
成像圖

接下來請看代碼和注釋:
//這里有一些是多余的
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<signal.h>
#include<errno.h>
#include<stdarg.h>
#include<setjmp.h>
#include<graphics.h>//需安裝easy_X,幾秒鐘的事
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() % 60 * 10;//0~590
food.fdxy.y = rand() % 40 * 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== 600 || snake.xy[0].x<0 || snake.xy[0].y<0|| snake.xy[0].y == 400)
{
char over[20] = "Game Over!";
outtextxy(250, 50,over);
system("pause");
exit(0);
}
}
int main()
{
srand((unsigned int)time(NULL));//播種
initgraph(600, 400); //畫面大小,可自行隨意更改
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/258847.html
標籤:其他
