#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
// 方向枚举
enum Direction { UP, DOWN, LEFT, RIGHT };
// 点类,用于表示位置
class Point {
public:
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
bool operator==(const Point& p) const {
return x == p.x && y == p.y;
}
};
// 蛇类
class Snake {
private:
vector<Point> body;
Direction direction;
bool alive;
public:
Snake(int x, int y) {
body.push_back(Point(x, y));
body.push_back(Point(x - 1, y));
body.push_back(Point(x - 2, y));
direction = RIGHT;
alive = true;
}
void changeDirection(Direction newDirection) {
// 防止180度转向
if ((direction == UP && newDirection == DOWN) ||
(direction == DOWN && newDirection == UP) ||
(direction == LEFT && newDirection == RIGHT) ||
(direction == RIGHT && newDirection == LEFT))
return;
direction = newDirection;
}
void move() {
Point head = body[0];
switch (direction) {
case UP: head.y--; break;
case DOWN: head.y++; break;
case LEFT: head.x--; break;
case RIGHT: head.x++; break;
}
// 检查是否撞墙或自噬
if (head.x < 0 || head.x >= 80 || head.y < 0 || head.y >= 20) {
alive = false;
return;
}
for (size_t i = 0; i < body.size(); i++) {
if (head == body[i]) {
alive = false;
return;
}
}
// 移动蛇
body.insert(body.begin(), head);
body.pop_back();
}
bool isAlive() const {
return alive;
}
vector<Point> getBody() const {
return body;
}
Point getHead() const {
return body[0];
}
void grow() {
Point tail = body.back();
body.push_back(tail);
}
bool checkCollision(Point p) const {
for (size_t i = 0; i < body.size(); i++) {
if (body[i] == p) return true;
}
return false;
}
};
// 食物类
class Food {
private:
Point position;
int width, height;
public:
Food(int w, int h) : width(w), height(h) {
respawn();
}
void respawn() {
position.x = rand() % (width - 2) + 1;
position.y = rand() % (height - 2) + 1;
}
Point getPosition() const {
return position;
}
};
// 游戏类
class Game {
private:
Snake snake;
Food food;
int score;
bool gameOver;
int width, height;
public:
Game(int w = 80, int h = 20) : snake(w/2, h/2), food(w, h), score(0), gameOver(false), width(w), height(h) {
srand(time(0));
}
void processInput() {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w': snake.changeDirection(UP); break;
case 's': snake.changeDirection(DOWN); break;
case 'a': snake.changeDirection(LEFT); break;
case 'd': snake.changeDirection(RIGHT); break;
case 'q': gameOver = true; break;
}
}
}
void update() {
snake.move();
if (!snake.isAlive()) {
gameOver = true;
return;
}
if (snake.getHead() == food.getPosition()) {
snake.grow();
food.respawn();
score += 10;
// 确保食物不会出现在蛇身上
while (snake.checkCollision(food.getPosition())) {
food.respawn();
}
}
}
void render() {
system("cls"); // 清屏
// 绘制顶部边界
for (int i = 0; i < width; i++)
cout << "#";
cout << endl;
// 绘制游戏区域
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
bool isSnakePart = false;
for (size_t i = 0; i < snake.getBody().size(); i++) {
if (snake.getBody()[i].x == x && snake.getBody()[i].y == y) {
cout << (i == 0 ? 'O' : 'o');
isSnakePart = true;
break;
}
}
if (!isSnakePart) {
if (food.getPosition().x == x && food.getPosition().y == y)
cout << 'X';
else if (x == 0 || x == width - 1)
cout << '#';
else
cout << ' ';
}
}
cout << endl;
}
// 绘制底部边界
for (int i = 0; i < width; i++)
cout << "#";
cout << endl;
// 显示分数
cout << "Score: " << score << endl;
cout << "Use WASD to control, Q to quit" << endl;
}
bool isGameOver() const {
return gameOver;
}
void run() {
while (!gameOver) {
processInput();
update();
render();
Sleep(100); // 控制游戏速度
}
cout << "Game Over!" << endl;
cout << "Final Score: " << score << endl;
}
};
int main() {
Game game;
game.run();
return 0;
}
贪吃蛇游戏代码
最新推荐文章于 2025-12-05 16:02:17 发布

6965

被折叠的 条评论
为什么被折叠?



