在800*600的地图上,蛇的初始长度为3节,用数组记录每一节的位置(每个正方形左上角的坐标),每一节为长度为10的正方形,初始方向向右。随机生成30个障碍物的的位置,随机生成食物的位置。吃到食物之后(即蛇头与食物位置相同),蛇的长度增加一节,每次移动时将数组的每一个元素向后移一位,第一位根据方向改变坐标。这样最后一位坐标在没吃到食物时会移到无法表现出来的位置,也可以用于吃到食物时的增长。

移动之后判断是否碰到障碍物或出界或咬到自身,用窗口命令提示游戏结束,并跳出死循环,程序结束。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<graphics.h>
#include<time.h>
#include<conio.h>
using namespace std;
struct point{
    int x,y;
}obstacle[40];
struct {
    int num;
    struct point xy[10000];
    int dir;
}snake;
struct {
    struct point xy;
    int flag;

}food;
void drawsnake() {
    int i;
    for (i = 0; i < snake.num; i++) {
        setlinecolor(BLACK);
        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() {
    int i = snake.num;
    for (; i > 0; i--) {
        snake.xy[i].x = snake.xy[i - 1].x;
        snake.xy[i].y = snake.xy[i - 1].y;
    }
    switch (snake.dir) {
    case 0:snake.xy[0].y -= 10;
        break;
    case 1:snake.xy[0].y += 10;
        break;
    case 2:snake.xy[0].x -= 10;
        break;
    case 3:snake.xy[0].x += 10;
        break;
    }
}
bool check() {
    int i;
    for (i = 1; i < snake.num; i++) {
        if (snake.xy[0].x == snake.xy[i].x&&snake.xy[0].y == snake.xy[i].y) {
            return false;
        }
    }
    if (snake.xy[0].x < 0 || snake.xy[0].y < 0 || snake.xy[0].y>600 || snake.xy[0].x>800) {
        return false;
    }
    for (i = 0; i < 30; i++) {
        if (snake.xy[0].x == obstacle[i].x&&snake.xy[0].y == obstacle[i].y) {
            return false;
        }
    }
}
void keyDown() {
    char ch = '\0';
    ch = _getch();
    switch (ch) {
    case 'd':
    case 'D':
        if (snake.dir != 2) {
            snake.dir = 3;
        }
        break;
    case 'w':
    case 'W':
        if (snake.dir != 1) {
            snake.dir = 0;
        }
        break;
    case 'a':
    case 'A':
        if (snake.dir != 3) {
            snake.dir = 2;
        }
        break;
    case 's':
    case 'S':
        if (snake.dir != 0) {
            snake.dir = 1;
        }
        break;
    default:
        break;
    }
}
void drawob() {
    int i;
    setlinecolor(BLACK);
    setfillcolor(BLACK);
    for (i = 0; i < 30; i++) {
        fillrectangle(obstacle[i].x, obstacle[i].y, obstacle[i].x + 10, obstacle[i].y + 10);
    }
}
int main() {
    srand((unsigned int)time(0));
    HWND hwnd=initgraph(800, 600);
    setbkcolor(RGB(255, 255, 255));
    cleardevice();
    snake.num = 3;
    snake.xy[0].x = 20;
    snake.xy[1].x = 10;
    snake.xy[2].x = 0;
    snake.xy[0].y = 0;
    snake.xy[1].y = 0;
    snake.xy[2].y = 0;
    snake.dir = 3;
    food.flag = 0;
    int i;

    for (i = 0; i < 30; i++) {
        obstacle[i].x = rand() % 75 * 10 + 10;
        obstacle[i].y = rand() % 55 * 10 + 10;

    }
    drawsnake();
    while (1) {
        if (food.flag == 0) {
            food.flag = 1;
            food.xy.x = rand() % 80 * 10;
            food.xy.y = rand() % 60 * 10;
        }
        movesnake();
        if (snake.xy[0].x == food.xy.x&& snake.xy[0].y == food.xy.y) {
            food.flag = 0;
            snake.num++;
        }
        if (check() == false) {
            MessageBox(hwnd, "Gameover","Game over!",0);
            break;
        }
        cleardevice();
        setlinecolor(BLACK);
        setfillcolor(WHITE);
        fillrectangle(food.xy.x, food.xy.y, food.xy.x + 10, food.xy.y + 10);
        drawsnake();
        drawob();
        char text[100];
        settextcolor(BLACK);
        outtextxy(720, 20, "WASD控制");
        sprintf(text, "num=%d", snake.num);
        outtextxy(720, 40, text);
        while (_kbhit() ){
            keyDown();
        }
        Sleep(100);
    }
    //system("pause");
    return 0;
}
12-26 03:18