21xrx.com
2025-06-16 15:00:06 Monday
登录
文章检索 我的文章 写文章
贪吃蛇的C++代码
2023-07-13 11:07:30 深夜i     23     0
C++ 贪吃蛇 代码

贪吃蛇是一款非常经典的游戏,也是一个非常好的程序员练手项目。下面将分享贪吃蛇的C++代码实现方法。

首先,我们需要使用C++的图形库或者游戏引擎来创建窗口并显示游戏画面。这里以Win32 API为例,代码如下:

#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
    case WM_CLOSE:
      DestroyWindow(hwnd);
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
      return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
  const char CLASS_NAME[] = "SnakeWindowClass";
  const char TITLE[] = "Snake Game";
  
  WNDCLASSEX wc = {};
  wc.cbSize = sizeof(WNDCLASSEX);
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = WndProc;
  wc.hInstance = hInstance;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.lpszClassName = CLASS_NAME;
  
  RegisterClassEx(&wc);
  
  HWND hwnd = CreateWindowEx(
    0,              // Optional window styles.
    CLASS_NAME,         // Window class
    TITLE,            // Window text
    WS_OVERLAPPEDWINDOW,     // Window style
    CW_USEDEFAULT, CW_USEDEFAULT, // Position
    800, 600,          // Size
    NULL,            // Parent window  
    NULL,            // Menu
    hInstance,          // Instance handle
    NULL             // Additional application data
    );
  
  ShowWindow(hwnd, iCmdShow);
  
  MSG msg = {};
  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  
  return msg.wParam;
}

在窗口初始化完成之后,我们需要创建贪吃蛇,并在窗口中显示游戏画面。首先,我们需要定义贪吃蛇的结构体,包括贪吃蛇的长度、坐标、方向以及食物的坐标等信息:

struct Snake
{
  int length;
  int x[MAX_SNAKE_SIZE];
  int y[MAX_SNAKE_SIZE];
  int direction;
  
  int foodX;
  int foodY;
};

然后,在窗口中初始化贪吃蛇,并在窗口中绘制游戏画面。如下代码所示:

Snake snake;
snake.length = 3;
snake.x[0] = 2;
snake.y[0] = 1;
snake.x[1] = 1;
snake.y[1] = 1;
snake.x[2] = 0;
snake.y[2] = 1;
snake.direction = RIGHT;
snake.foodX = 10;
snake.foodY = 10;
HDC hdc = GetDC(hwnd);
RECT rect = {};
GetClientRect(hwnd, &rect);
while(true)
{
  DrawSnake(hdc, rect, snake);
  DrawFood(hdc, rect, snake.foodX, snake.foodY);
  if(!MoveSnake(snake))
  {
    MessageBox(hwnd, "Game over!", "Information", MB_OK);
    break;
  }
  if(IsGameOver(snake))
  {
    MessageBox(hwnd, "Game over!", "Information", MB_OK);
    break;
  }
  if(IsEatFood(snake))
  {
    snake.length++;
    GenerateFood(snake);
  }
  Sleep(100);
}

最后,我们需要实现贪吃蛇的移动、吃食物、死亡等逻辑。具体实现方法可以参考以下代码:

bool MoveSnake(Snake& snake)
{
  int oldX = snake.x[0];
  int oldY = snake.y[0];
  
  switch(snake.direction)
  {
    case LEFT:
      snake.x[0]--;
      break;
    case RIGHT:
      snake.x[0]++;
      break;
    case UP:
      snake.y[0]--;
      break;
    case DOWN:
      snake.y[0]++;
      break;
    default:
      break;
  }
  
  for(int i=1; i<snake.length; i++)
  {
    int t = snake.x[i];
    snake.x[i] = oldX;
    oldX = t;
    
    t = snake.y[i];
    snake.y[i] = oldY;
    oldY = t;
  }
  
  if(snake.x[0] < 0 || snake.x[0] >= MAX_X || snake.y[0] < 0 || snake.y[0] >= MAX_Y)
  
    return false;
  
  
  return true;
}
bool IsEatFood(Snake& snake)
{
  if(snake.x[0] == snake.foodX && snake.y[0] == snake.foodY)
  
    return true;
  
  
  return false;
}
void GenerateFood(Snake& snake)
{
  snake.foodX = rand() % MAX_X;
  snake.foodY = rand() % MAX_Y;
}
bool IsGameOver(Snake& snake)
{
  for(int i=1; i<snake.length; i++)
  {
    if(snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
    
      return true;
    
  }
  
  return false;
}

以上就是贪吃蛇的C++代码实现方法。通过这个项目的实践,程序员可以提升自己的C++编程能力,同时还能体验到动手实践的快感。

  
  
下一篇: VC++ 调试库

评论区