c++面向对象简单贪吃蛇
时间: 2023-08-03 12:40:24 浏览: 199
好的,我可以为您提供一个简单的C++面向对象贪吃蛇游戏的代码示例,如下所示:
```cpp
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 20;
const int height = 20;
class Snake {
private:
int x, y;
public:
Snake() {
x = width / 2;
y = height / 2;
}
void move(int dx, int dy) {
x += dx;
y += dy;
if (x < 0) x = width - 1;
if (x > width - 1) x = 0;
if (y < 0) y = height - 1;
if (y > height - 1) y = 0;
}
int getX() {
return x;
}
int getY() {
return y;
}
};
class Fruit {
private:
int x, y;
public:
Fruit() {
x = rand() % width;
y = rand() % height;
}
int getX() {
return x;
}
int getY() {
return y;
}
void changePosition() {
x = rand() % width;
y = rand() % height;
}
};
class Game {
private:
bool gameOver;
Snake snake;
Fruit fruit;
public:
Game() {
gameOver = false;
}
void draw() {
system("cls");
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0) {
cout << "#";
}
if (i == snake.getY() && j == snake.getX()) {
cout << "O";
} else if (i == fruit.getY() && j == fruit.getX()) {
cout << "F";
} else {
cout << " ";
}
if (j == width - 1) {
cout << "#";
}
}
cout << endl;
}
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
}
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
snake.move(-1, 0);
break;
case 'd':
snake.move(1, 0);
break;
case 'w':
snake.move(0, -1);
break;
case 's':
snake.move(0, 1);
break;
case 'x':
gameOver = true;
break;
}
}
}
void logic() {
if (snake.getX() == fruit.getX() && snake.getY() == fruit.getY()) {
fruit.changePosition();
} else {
snake.move(0, 0);
}
}
void run() {
while (!gameOver) {
draw();
input();
logic();
Sleep(50);
}
}
};
int main() {
Game game;
game.run();
return 0;
}
```
这个程序使用了三个类:`Snake`、`Fruit`和`Game`。`Snake`类表示蛇,`Fruit`类表示水果,`Game`类表示整个游戏。在`Game`类的`run`函数中,程序会不断地循环执行`draw`、`input`和`logic`函数,直到游戏结束。`draw`函数用于绘制游戏界面,`input`函数用于处理用户输入,`logic`函数用于处理游戏逻辑。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)