用C++写一个贪吃蛇程序
时间: 2023-11-26 18:04:48 浏览: 72
用C++写的贪吃蛇程序源代码
5星 · 资源好评率100%
以下是一个简单的贪吃蛇程序示例,使用C++和SFML库编写:
```c++
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace sf;
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int GRID_SIZE = 10;
struct SnakeSegment {
int x;
int y;
};
enum class Direction { None, Up, Down, Left, Right };
int main() {
// Initialize the window
RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Snake Game");
window.setFramerateLimit(10);
// Initialize the snake
vector<SnakeSegment> snake(1, {WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2});
Direction direction = Direction::None;
// Initialize the food
Vector2i food;
food.x = rand() % (WINDOW_WIDTH / GRID_SIZE) * GRID_SIZE;
food.y = rand() % (WINDOW_HEIGHT / GRID_SIZE) * GRID_SIZE;
RectangleShape foodShape({GRID_SIZE, GRID_SIZE});
foodShape.setFillColor(Color::Red);
foodShape.setPosition(food.x, food.y);
while (window.isOpen()) {
// Handle events
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
} else if (event.type == Event::KeyPressed) {
switch (event.key.code) {
case Keyboard::Up:
direction = Direction::Up;
break;
case Keyboard::Down:
direction = Direction::Down;
break;
case Keyboard::Left:
direction = Direction::Left;
break;
case Keyboard::Right:
direction = Direction::Right;
break;
default:
break;
}
}
}
// Move the snake
if (direction != Direction::None) {
for (int i = snake.size() - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
switch (direction) {
case Direction::Up:
snake[0].y -= GRID_SIZE;
break;
case Direction::Down:
snake[0].y += GRID_SIZE;
break;
case Direction::Left:
snake[0].x -= GRID_SIZE;
break;
case Direction::Right:
snake[0].x += GRID_SIZE;
break;
default:
break;
}
}
// Check for collision with food
if (snake[0].x == food.x && snake[0].y == food.y) {
snake.push_back(snake[snake.size() - 1]);
food.x = rand() % (WINDOW_WIDTH / GRID_SIZE) * GRID_SIZE;
food.y = rand() % (WINDOW_HEIGHT / GRID_SIZE) * GRID_SIZE;
foodShape.setPosition(food.x, food.y);
}
// Check for collision with walls and body
if (snake[0].x < 0 || snake[0].x >= WINDOW_WIDTH || snake[0].y < 0 || snake[0].y >= WINDOW_HEIGHT) {
window.close();
}
for (int i = 1; i < snake.size(); i++) {
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
window.close();
}
}
// Clear the window
window.clear(Color::Black);
// Draw the food
window.draw(foodShape);
// Draw the snake
RectangleShape segmentShape({GRID_SIZE, GRID_SIZE});
segmentShape.setFillColor(Color::Green);
for (int i = 0; i < snake.size(); i++) {
segmentShape.setPosition(snake[i].x, snake[i].y);
window.draw(segmentShape);
}
// Display the window
window.display();
}
return 0;
}
```
这个程序使用了SFML图形库来创建窗口并绘制图形。它定义了一个`SnakeSegment`结构体来表示贪吃蛇的每个段落,以及一个`Direction`枚举类型来表示贪吃蛇的方向。在`main()`函数中,它使用`vector`类型来表示整条贪吃蛇,并初始化了窗口、贪吃蛇和食物。
在主循环中,程序首先处理事件,包括关闭窗口和按键事件。如果用户按下方向键,则程序将方向设置为相应的方向。然后,程序根据设置的方向移动贪吃蛇。如果贪吃蛇头部与食物重合,则程序将在贪吃蛇的末尾添加一个节,重新生成食物。
最后,程序绘制食物和贪吃蛇,并检查贪吃蛇是否与窗口边界或自身相撞。如果是,程序将关闭窗口,游戏结束。
阅读全文