pvz(半AI作品)
2025-10-14 16:13:27
发布于:山东
#include<bits/stdc++.h>
#include <windows.h>
using namespace std;
// 游戏常量
const int WIDTH=30;
const int HEIGHT=5;
// 游戏对象基类
class GameObject{
public:
int x,y;
char symbol;
int health;
virtual void update()=0;
};
// 植物类
class Plant : public GameObject{
public:
Plant(int x,int y){
this->x=x;
this->y=y;
symbol='P';
health=3;
}
void update() override{
// 植物基础逻辑
}
};
// 僵尸类
class Zombie : public GameObject {
public:
Zombie(int x, int y) {
this->x = x;
this->y = y;
symbol = 'Z';
health = 5;
}
void update() override {
x--; // 向左移动
if (x < 0) x = WIDTH - 1;
}
};
// 游戏主类
class Game {
private:
vector<GameObject*> objects;
int sunCount;
bool running;
public:
Game() : sunCount(50), running(true) {}
void init() {
// 初始化游戏地图
}
void update() {
for (auto obj : objects) {
obj->update();
}
// 碰撞检测
// 资源生成
}
void render() {
system("cls");
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
bool drawn = false;
for (auto obj : objects) {
if (obj->x == x && obj->y == y) {
cout << obj->symbol;
drawn = true;
break;
}
}
if (!drawn) cout << ".";
}
cout << endl;
}
cout << "Sun: " << sunCount << endl;
}
void input() {
if (_kbhit()) {
char key = _getch();
if (key == 'q') running = false;
if (key == 'p') {
// 添加植物
objects.push_back(new Plant(5, 2));
}
if (key == 'z') {
// 添加僵尸
objects.push_back(new Zombie(WIDTH-1, rand() % HEIGHT));
}
}
}
void run() {
init();
while (running) {
update();
render();
input();
Sleep(200);//控制游戏速度
}
}
};
int main() {
Game game;
game.run();
return 0;
}
这里空空如也









有帮助,赞一个