C++簡單項目--貪吃蛇


在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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM