发布时间:2024-10-11 11:44:50
本内容由, 集智数据集收集发布,仅供参考学习,不代表集智官方赞同其观点或证实其内容的真实性,请勿用于商业用途。
编写一个带UI界面的贪吃蛇游戏可以使用C++结合图形库来实现。一个常用的库是SFML(SimpleandFastMultimediaLibrary),它提供了图形、窗口和事件处理等功能,非常适合用于简单的游戏开发。
以下是一个基于C++和SFML的贪吃蛇游戏的基本实现:
#include <SFML/Graphics.hpp>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace sf;
const int WINDOW_WIDTH = 400;
const int WINDOW_HEIGHT = 400;
const int GRID_SIZE = 20;
const int WIDTH = WINDOW_WIDTH / GRID_SIZE;
const int HEIGHT = WINDOW_HEIGHT / GRID_SIZE;
// 贪吃蛇的方向
enum Direction { Up, Down, Left, Right };
struct SnakeSegment {
int x, y;
};
class Snake {
public:
std::vector<SnakeSegment> segments;
Direction direction;
bool alive;
Snake() {
Reset();
}
void Reset() {
segments.clear();
segments.push_back({ WIDTH / 2, HEIGHT / 2 });
direction = Right;
alive = true;
}
void Move() {
if (!alive) return;
SnakeSegment newHead = segments.front();
switch (direction) {
case Up: newHead.y--; break;
case Down: newHead.y++; break;
case Left: newHead.x--; break;
case Right: newHead.x++; break;
}
if (newHead.x < 0 || newHead.y < 0 || newHead.x >= WIDTH || newHead.y >= HEIGHT || CollidesWithSelf(newHead)) {
alive = false;
return;
}
segments.insert(segments.begin(), newHead);
}
void Grow() {
SnakeSegment tail = segments.back();
segments.push_back(tail);
}
bool CollidesWithSelf(SnakeSegment head) {
for (size_t i = 1; i < segments.size(); i++) {
if (segments[i].x == head.x && segments[i].y == head.y) {
return true;
}
}
return false;
}
void Draw(RenderWindow& window) {
RectangleShape shape(Vector2f(GRID_SIZE - 1, GRID_SIZE - 1));
shape.setFillColor(Color::Green);
for (auto& segment : segments) {
shape.setPosition(segment.x * GRID_SIZE, segment.y * GRID_SIZE);
window.draw(shape);
}
}
};
class Food {
public:
int x, y;
Food() {
Respawn();
}
void Respawn() {
x = rand() % WIDTH;
y = rand() % HEIGHT;
}
void Draw(RenderWindow& window) {
RectangleShape shape(Vector2f(GRID_SIZE - 1, GRID_SIZE - 1));
shape.setFillColor(Color::Red);
shape.setPosition(x * GRID_SIZE, y * GRID_SIZE);
window.draw(shape);
}
};
int main() {
srand(static_cast<unsigned>(time(0)));
RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "贪吃蛇");
window.setFramerateLimit(10); // 控制帧率
Snake snake;
Food food;
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}
if (snake.alive) {
if (Keyboard::isKeyPressed(Keyboard::Up) && snake.direction != Down) {
snake.direction = Up;
}
else if (Keyboard::isKeyPressed(Keyboard::Down) && snake.direction != Up) {
snake.direction = Down;
}
else if (Keyboard::isKeyPressed(Keyboard::Left) && snake.direction != Right) {
snake.direction = Left;
}
else if (Keyboard::isKeyPressed(Keyboard::Right) && snake.direction != Left) {
snake.direction = Right;
}
snake.Move();
if (snake.segments.front().x == food.x && snake.segments.front().y == food.y) {
snake.Grow();
food.Respawn();
}
}
else {
// 按空格键重置游戏
if (Keyboard::isKeyPressed(Keyboard::Space)) {
snake.Reset();
food.Respawn();
}
}
// 绘制部分
window.clear(Color::Black);
snake.Draw(window);
food.Draw(window);
window.display();
}
return 0;
}
RectangleShape
绘制蛇的身体和食物,并根据蛇的移动实时更新画面。本站将定期更新分享一些python机器学习的精选代码