贪吃蛇c++单文件版源代码
2026/8/7 14:45:15 网站建设 项目流程

Visual Studio 基于c++的桌面开发编写,要装EasyX图形库运行

编译要求

VS C++控制台程序 ISO C++17(最好是20)标准

代码

#include <iostream> #include <conio.h> #include <graphics.h> #include <ctime> #include <cstdlib> using namespace std; #define width 15 #define height 15 #define block_size 40 int map[height][width] = { 0 }; char snake_forward = 'd'; int snake_head[2] = { height / 2, width / 2 }; int snake_long = 3; int get_food = 0; int where_is_food[2] = { -1, -1 }; void food() { int a_end, b_end; srand((unsigned int)time(NULL)); while (1) { int a = rand() % width; int b = rand() % height; if (map[b][a] == 0) { a_end = a; b_end = b; break; } } map[b_end][a_end] = -1; where_is_food[0] = b_end; where_is_food[1] = a_end; } void the_beginning() { initgraph(width * block_size, height * block_size); setlinecolor(RGB(200, 200, 200)); cleardevice(); map[height / 2][width / 2 - 2] = 3; map[height / 2][width / 2 - 1] = 2; map[height / 2][width / 2] = 1; food(); setbkmode(TRANSPARENT); settextcolor(RGB(255, 0, 0)); settextstyle(13, 0, _T("宋体")); outtextxy(3, 3, _T("制作者:stu_mo")); FlushBatchDraw(); } void draw() { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (map[i][j] > 0) { setfillcolor(HSVtoRGB(map[i][j] * 10, 0.9, 1)); } else if (map[i][j] == -1) { setfillcolor(RGB(0, 255, 0)); } else { setfillcolor(RGB(150, 150, 150)); } fillrectangle(j * block_size, i * block_size, (j + 1) * block_size, (i + 1) * block_size); } } setbkmode(TRANSPARENT); settextcolor(RGB(255, 0, 0)); settextstyle(13, 0, _T("宋体")); outtextxy(3, 3, _T("制作者:stu_mo")); FlushBatchDraw(); } void menu() { COLORREF button_color_up = RGB(0, 200, 0); COLORREF button_color_down = RGB(0, 128, 0); COLORREF button_color = button_color_up; initgraph(width * block_size, height * block_size); draw(); setbkmode(TRANSPARENT); settextcolor(RGB(255, 0, 0)); settextstyle(65, 0, _T("宋体")); outtextxy(width * block_size / 2 - 95, height * block_size / 2 - 70, _T("贪吃蛇")); setfillcolor(button_color); fillroundrect(width * block_size / 2 - 50, height * block_size / 2 + 10, width * block_size / 2 + 50, height * block_size / 2 + 45, 20, 20); settextcolor(RGB(255, 255, 255)); settextstyle(30, 0, _T("宋体")); outtextxy(width * block_size / 2 - 36, height * block_size / 2 + 11, _T("start")); FlushBatchDraw(); while (1) { ExMessage msg; if (peekmessage(&msg, EM_MOUSE)) { if (msg.message == WM_LBUTTONDOWN) { if (msg.x >= width * block_size / 2 - 50 && msg.x <= width * block_size / 2 + 50 && msg.y >= height * block_size / 2 + 10 && msg.y <= height * block_size / 2 + 45) { button_color = button_color_down; setfillcolor(button_color); fillroundrect(width * block_size / 2 - 50, height * block_size / 2 + 10, width * block_size / 2 + 50, height * block_size / 2 + 45, 20, 20); settextstyle(30, 0, _T("宋体")); outtextxy(width * block_size / 2 - 36, height * block_size / 2 + 11, _T("start")); FlushBatchDraw(); } } if (msg.message == WM_LBUTTONUP) { button_color = button_color_up; setfillcolor(button_color); fillroundrect(width * block_size / 2 - 50, height * block_size / 2 + 10, width * block_size / 2 + 50, height * block_size / 2 + 45, 20, 20); settextstyle(30, 0, _T("宋体")); outtextxy(width * block_size / 2 - 36, height * block_size / 2 + 11, _T("start")); FlushBatchDraw(); if (msg.x >= width * block_size / 2 - 50 && msg.x <= width * block_size / 2 + 50 && msg.y >= height * block_size / 2 + 10 && msg.y <= height * block_size / 2 + 45) { break; } } } } } int snake_move() { if (snake_forward == 'd') { int a = snake_head[1] + 1; int b = snake_head[0]; if (map[b][a] > 0) { return -1; } if (snake_head[1] + 1 == where_is_food[1] && snake_head[0] == where_is_food[0]) { get_food = 1; } if (snake_head[1] + 1 >= width) { return 0; } else { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (map[i][j] > 0) { if (map[i][j] == snake_long && get_food != 1) { map[i][j] = 0; } else { map[i][j] = map[i][j] + 1; } if (get_food == 1) { snake_long++; get_food = 0; food(); } } } } map[snake_head[0]][snake_head[1] + 1] = 1; snake_head[1] = snake_head[1] + 1; } return 1; } if (snake_forward == 'w') { int a = snake_head[1]; int b = snake_head[0] - 1; if (map[b][a] > 0) { return -1; } if (snake_head[0] - 1 == where_is_food[0] && snake_head[1] == where_is_food[1]) { get_food = 1; } if (snake_head[0] <= 0) { return 0; } else { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (map[i][j] > 0) { if (map[i][j] == snake_long && get_food != 1) { map[i][j] = 0; } else { map[i][j] = map[i][j] + 1; } if (get_food == 1) { snake_long++; get_food = 0; food(); } } } } map[snake_head[0] - 1][snake_head[1]] = 1; snake_head[0] = snake_head[0] - 1; } return 1; } if (snake_forward == 'a') { int a = snake_head[1] - 1; int b = snake_head[0]; if (map[b][a] > 0) { return -1; } if (snake_head[0] == where_is_food[0] && snake_head[1] - 1 == where_is_food[1]) { get_food = 1; } if (snake_head[1] <= 0) { return 0; } else { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (map[i][j] > 0) { if (map[i][j] == snake_long && get_food != 1) { map[i][j] = 0; } else { map[i][j] = map[i][j] + 1; } if (get_food == 1) { snake_long++; get_food = 0; food(); } } } } map[snake_head[0]][snake_head[1] - 1] = 1; snake_head[1] = snake_head[1] - 1; } return 1; } if (snake_forward == 's') { int a = snake_head[1]; int b = snake_head[0] + 1; if (map[b][a] > 0) { return -1; } if (snake_head[0] + 1 == where_is_food[0] && snake_head[1] == where_is_food[1]) { get_food = 1; } if (snake_head[0] + 1 >= height) { return 0; } else { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (map[i][j] > 0) { if (map[i][j] == snake_long && get_food != 1) { map[i][j] = 0; } else { map[i][j] = map[i][j] + 1; } if (get_food == 1) { snake_long++; get_food = 0; food(); } } } } map[snake_head[0] + 1][snake_head[1]] = 1; snake_head[0] = snake_head[0] + 1; } return 1; } } int main() { int back, snake_speed = 15, change_speed_flag = 0; menu(); the_beginning(); draw(); while (1) { char input = snake_forward; for (int i = 1; i <= snake_speed; i++) { if (_kbhit()) { input = _getch(); if (input == 'w' || input == 'a' || input == 's' || input == 'd') { if (input == snake_forward) { snake_speed = 5; } else { snake_forward = input; } } } else { if (change_speed_flag <= 5) { change_speed_flag++; } else { change_speed_flag = 0; snake_speed = 15; } } Sleep(50); } back = snake_move(); if (back == 0 || back == -1) { break; } else { draw(); } } if (back == 0) { setbkmode(TRANSPARENT); settextcolor(RGB(255, 0, 0)); settextstyle(50, 0, _T("宋体")); outtextxy(width * block_size / 2 - 160, height * block_size / 2 - 40, _T("别和墙比硬度!")); FlushBatchDraw(); } else { setbkmode(TRANSPARENT); settextcolor(RGB(255, 0, 0)); settextstyle(50, 0, _T("宋体")); outtextxy(width * block_size / 2 - 180, height * block_size / 2 - 40, _T("这里禁止吃自己!")); FlushBatchDraw(); } system("pause"); closegraph(); return 0; }

运行成果

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询