21xrx.com
2025-03-22 08:18:11 Saturday
文章检索 我的文章 写文章
C++贪吃蛇代码编写
2023-06-23 05:17:34 深夜i     23     0
C++ 贪吃蛇 代码 编写 游戏

贪吃蛇是一款经典的游戏,在许多人心中都有着儿时的回忆。而用C++编写贪吃蛇游戏,则是许多程序员的乐趣之一。下面,我将分享一份C++贪吃蛇代码。

首先,我们需要了解一下贪吃蛇的规则。贪吃蛇游戏中,玩家需要控制蛇头移动,吃掉地图中的食物。每当蛇吃到一个食物,蛇的身体就会增长一节。同时,如果蛇的头部碰到了地图边缘或者自己的身体,那么游戏就结束了。

在C++中实现贪吃蛇,需要用到图形界面库。这里我们选择使用Windows API来实现图形界面。代码中需要定义一个窗口,并在窗口上绘制好矩形和蛇的形状。

接下来,我们需要处理蛇的移动。在每一帧更新中,我们需要根据玩家输入来改变蛇的方向,并且判断蛇是否会与自己相撞或者撞墙。如果玩家成功吃到一个食物,那么蛇的身体长度就要增加。

在代码的实现过程中,需要用到一些数据结构来辅助处理。比如可以使用数组来存储蛇的身体位置,使用队列来存储蛇的方向信息。同时,还需要使用随机数生成器来产生食物的位置。

下面是一份简单的C++贪吃蛇代码,实现了基本的游戏规则和移动逻辑。可以通过优化代码并加入更多的游戏元素,来使得游戏更加有趣。

#include <windows.h>
#define MAX_SIZE 100
#define BLOCK_SIZE 10
#define WIDTH 50
#define HEIGHT 50
struct Position
 int x;
 int y;
;
Position snake[MAX_SIZE];
int length = 1;
int direction;
bool isEating = false;
Position food;
int prevDirection;
Position head;
bool isPlaying = true;
void update();
void drawBox(HDC hdc, int x, int y) {
 Rectangle(hdc, x, y, x + BLOCK_SIZE, y + BLOCK_SIZE);
}
void drawSnake(HDC hdc) {
 for (int i = 0; i < length; i++) {
  drawBox(hdc, snake[i].x * BLOCK_SIZE, snake[i].y * BLOCK_SIZE);
 }
}
void drawFood(HDC hdc) {
 drawBox(hdc, food.x * BLOCK_SIZE, food.y * BLOCK_SIZE);
}
void draw(HDC hdc) {
 drawSnake(hdc);
 drawFood(hdc);
}
void generateFood() {
 do {
  food.x = rand() % WIDTH;
  food.y = rand() % HEIGHT;
 } while (food.x == snake[0].x && food.y == snake[0].y);
}
void checkCollision() {
 if (snake[0].x < 0 || snake[0].x >= WIDTH ||
   snake[0].y < 0 || snake[0].y >= HEIGHT)
  isPlaying = false;
 
 for (int i = 1; i < length; i++) {
  if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
   isPlaying = false;
  
 }
}
void move() {
 prevDirection = direction;
 if (direction == 0) {
  snake[0].y--;
 } else if (direction == 1) {
  snake[0].x++;
 } else if (direction == 2) {
  snake[0].y++;
 } else if (direction == 3) {
  snake[0].x--;
 }
 if (snake[0].x == food.x && snake[0].y == food.y) {
  isEating = true;
  generateFood();
  length++;
 }
 if (isEating) {
  for (int i = length - 1; i > 0; i--) {
   snake[i] = snake[i - 1];
  }
  isEating = false;
 } else {
  for (int i = length - 1; i > 0; i--) {
   snake[i] = snake[i - 1];
  }
 }
}
void update() {
 if (!isPlaying)
  return;
 
 move();
 checkCollision();
 Sleep(100);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
 HDC hdc;
 PAINTSTRUCT ps;
 switch(msg) {
  case WM_CREATE:
   srand(GetTickCount());
   snake[0].x = 0;
   snake[0].y = 0;
   direction = 1;
   generateFood();
   SetTimer(hwnd, 1, 100, NULL);
   break;
  case WM_PAINT:
   hdc = BeginPaint(hwnd, &ps);
   draw(hdc);
   EndPaint(hwnd, &ps);
   break;
  case WM_TIMER:
   InvalidateRect(hwnd, NULL, FALSE);
   update();
   break;
  case WM_KEYDOWN:
   if (wParam == VK_UP && prevDirection != 2)
    direction = 0;
    else if (wParam == VK_RIGHT && prevDirection != 3)
    direction = 1;
    else if (wParam == VK_DOWN && prevDirection != 0)
    direction = 2;
    else if (wParam == VK_LEFT && prevDirection != 1)
    direction = 3;
   
   break;
  case WM_DESTROY:
   KillTimer(hwnd, 1);
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hwnd, msg, wParam, lParam);
 }
 return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
 WNDCLASS wc;
 memset(&wc, 0, sizeof(WNDCLASS));
 wc.hInstance = hInstance;
 wc.lpfnWndProc = WndProc;
 wc.lpszClassName = "Snake";
 RegisterClass(&wc);
 HWND hwnd = CreateWindow(wc.lpszClassName, "Snake", WS_OVERLAPPEDWINDOW,
              CW_USEDEFAULT, CW_USEDEFAULT, WIDTH * BLOCK_SIZE + 16,
              HEIGHT * BLOCK_SIZE + 39, NULL, NULL, hInstance, NULL);
 ShowWindow(hwnd, nShowCmd);
 MSG msg;
 while(GetMessage(&msg, NULL, 0, 0)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return msg.wParam;
}

总的来说,用C++编写贪吃蛇游戏是个不错的挑战,也能够提高我们的编程技巧。希望这份代码能够给大家带来启发,让大家尽情地发挥自己的创造力和想象力,创造出更加丰富多彩的贪吃蛇游戏!

  
  

评论区