打僵尸小游戏!!!
2024-08-19 18:58:57
发布于:广东
操作说明:
WSAD移动
空格射击
游戏内容:
1~5关每关僵尸+1
6关以上一局6只僵尸,有一只两血首领Z,每关僵尸移速+1,不再增加数量
注意事项:
1.别喷
2.别骂
3.别炒作者
4.有bug可以提出,我会尽力修改
祝游玩愉快~
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
const int WIDTH = 24;
const int HEIGHT = 12;
const double ZOMBIE_MOVE_INTERVAL = 2; // Base interval in seconds
const double BULLET_MOVE_INTERVAL = 0.1; // seconds
const double SHOOTING_INTERVAL = 0.1; // seconds
const int ZOMBIE_ATTACK_INTERVAL = 3; // seconds
struct Position {
int x, y;
};
struct Bullet {
Position pos;
char direction; // 'U' for Up, 'D' for Down, 'L' for Left, 'R' for Right
char symbol; // '-' or '|'
};
struct Zombie {
Position pos;
int health;
chrono::steady_clock::time_point lastAttackTime;
};
struct Player {
Position pos;
char direction; // 'U' for Up, 'D' for Down, 'L' for Left, 'R' for Right
int health;
};
void printBoard(Player player, vector<Zombie>& zombies, vector<Bullet>& bullets, int wave) {
system("cls");
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (x == player.pos.x && y == player.pos.y) {
if (player.direction == 'U') cout << '^';
else if (player.direction == 'D') cout << 'v';
else if (player.direction == 'L') cout << '<';
else if (player.direction == 'R') cout << '>';
} else {
bool printed = false;
for (const auto& zombie : zombies) {
if (x == zombie.pos.x && y == zombie.pos.y) {
cout << (zombie.health > 1 ? 'Z' : 'z');
printed = true;
break;
}
}
if (!printed) {
for (const auto& bullet : bullets) {
if (x == bullet.pos.x && y == bullet.pos.y) {
cout << bullet.symbol;
printed = true;
break;
}
}
}
if (!printed) {
cout << ' ';
}
}
}
cout << endl;
}
cout << "Player Health: " << player.health << " ";
cout << "Zombies Remaining: " << zombies.size() << " ";
cout << "Wave: " << wave << " ";
cout << "Zombie Health: " << (wave >= 6 ? "2" : "1") << endl;
}
void handleInput(char input, Player& player, vector<Bullet>& bullets, chrono::steady_clock::time_point& lastShootTime) {
auto currentTime = chrono::steady_clock::now();
chrono::duration<double> elapsed = currentTime - lastShootTime;
switch (input) {
case 'w':
player.pos.y = max(0, player.pos.y - 1);
player.direction = 'U';
break;
case 's':
player.pos.y = min(HEIGHT - 1, player.pos.y + 1);
player.direction = 'D';
break;
case 'a':
player.pos.x = max(0, player.pos.x - 1);
player.direction = 'L';
break;
case 'd':
player.pos.x = min(WIDTH - 1, player.pos.x + 1);
player.direction = 'R';
break;
case ' ':
if (elapsed.count() >= SHOOTING_INTERVAL) {
if (player.direction == 'U') bullets.push_back({player.pos, 'U', '|'});
else if (player.direction == 'D') bullets.push_back({player.pos, 'D', '|'});
else if (player.direction == 'L') bullets.push_back({player.pos, 'L', '-'});
else if (player.direction == 'R') bullets.push_back({player.pos, 'R', '-'});
lastShootTime = currentTime;
}
break;
case 'q':
exit(0);
break;
}
}
void moveZombies(vector<Zombie>& zombies, Player player, double speed) {
for (auto& zombie : zombies) {
if (zombie.pos.x < player.pos.x) zombie.pos.x += speed;
if (zombie.pos.x > player.pos.x) zombie.pos.x -= speed;
if (zombie.pos.y < player.pos.y) zombie.pos.y += speed;
if (zombie.pos.y > player.pos.y) zombie.pos.y -= speed;
}
}
void moveBullets(vector<Bullet>& bullets) {
for (auto& bullet : bullets) {
switch (bullet.direction) {
case 'U':
bullet.pos.y--;
break;
case 'D':
bullet.pos.y++;
break;
case 'L':
bullet.pos.x--;
break;
case 'R':
bullet.pos.x++;
break;
}
}
}
void checkBulletCollisions(vector<Bullet>& bullets, vector<Zombie>& zombies, Player& player) {
auto it = bullets.begin();
while (it != bullets.end()) {
bool hit = false;
for (auto zIt = zombies.begin(); zIt != zombies.end(); ) {
if (it->pos.x == zIt->pos.x && it->pos.y == zIt->pos.y) {
zIt->health -= 1;
if (zIt->health <= 0) {
zIt = zombies.erase(zIt);
} else {
++zIt;
}
hit = true;
break;
} else {
++zIt;
}
}
if (hit) {
it = bullets.erase(it);
} else {
++it;
}
}
for (const auto& zombie : zombies) {
if (zombie.pos.x == player.pos.x && zombie.pos.y == player.pos.y) {
player.health -= 1;
if (player.health <= 0) {
cout << "Game Over! You have been defeated." << endl;
exit(0);
}
}
}
}
void checkZombieAttacks(vector<Zombie>& zombies, Player& player) {
auto currentTime = chrono::steady_clock::now();
for (auto& zombie : zombies) {
chrono::duration<double> elapsed = currentTime - zombie.lastAttackTime;
if (elapsed.count() >= ZOMBIE_ATTACK_INTERVAL) {
if (zombie.pos.x == player.pos.x && zombie.pos.y == player.pos.y) {
player.health -= 1;
if (player.health <= 0) {
cout << "Game Over! You have been defeated." << endl;
exit(0);
}
}
zombie.lastAttackTime = currentTime;
}
}
}
void generateZombies(vector<Zombie>& zombies, int wave) {
zombies.clear();
for (int i = 0; i < wave; ++i) {
Zombie newZombie;
do {
newZombie.pos.x = rand() % WIDTH;
newZombie.pos.y = rand() % HEIGHT;
} while (newZombie.pos.x == WIDTH / 2 && newZombie.pos.y == HEIGHT / 2);
if (wave >= 6 && i == wave - 1) {
newZombie.health = 2;
} else {
newZombie.health = 1;
}
newZombie.lastAttackTime = chrono::steady_clock::now();
zombies.push_back(newZombie);
}
}
int main() {
srand(static_cast<unsigned>(time(0)));
Player player = {{WIDTH / 2, HEIGHT / 2}, 'U', 3};
vector<Zombie> zombies;
vector<Bullet> bullets;
int wave = 1;
generateZombies(zombies, wave);
auto lastZombieMoveTime = chrono::steady_clock::now();
auto lastBulletMoveTime = chrono::steady_clock::now();
auto lastZombieGenerationTime = chrono::steady_clock::now();
auto lastShootTime = chrono::steady_clock::now();
while (true) {
if (_kbhit()) {
char input = _getch();
handleInput(input, player, bullets, lastShootTime);
}
auto currentTime = chrono::steady_clock::now();
chrono::duration<double> elapsedZombieMove = currentTime - lastZombieMoveTime;
chrono::duration<double> elapsedBulletMove = currentTime - lastBulletMoveTime;
chrono::duration<double> elapsedZombieGeneration = currentTime - lastZombieGenerationTime;
if (elapsedZombieMove.count() >= ZOMBIE_MOVE_INTERVAL) {
moveZombies(zombies, player, 1.0);
lastZombieMoveTime = currentTime;
}
if (elapsedBulletMove.count() >= BULLET_MOVE_INTERVAL) {
moveBullets(bullets);
checkBulletCollisions(bullets, zombies, player);
lastBulletMoveTime = currentTime;
}
if (elapsedZombieGeneration.count() >= 20) {
wave++;
generateZombies(zombies, wave);
lastZombieGenerationTime = currentTime;
}
checkZombieAttacks(zombies, player);
printBoard(player, zombies, bullets, wave);
this_thread::sleep_for(chrono::milliseconds(50));
}
return 0;
}
这里空空如也
有帮助,赞一个