C++#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
// 游戏区域大小
const int WIDTH = 40;
const int HEIGHT = 20;
const int MAX_BULLETS = 10;
const int MAX_ENEMIES = 20;
// 玩家结构
struct Player {
int x;
int score;
int lives;
};
// 子弹结构
struct Bullet {
int x;
int y;
bool active;
};
// 敌人结构
struct Enemy {
int x;
int y;
bool active;
};
// 游戏类
class Game {
private:
Player player;
Bullet bullets[MAX_BULLETS];
Enemy enemies[MAX_ENEMIES];
bool gameOver;
int level;
int enemyMoveCounter; // 敌人移动计数器
int enemyMoveDelay; // 敌人移动延迟(越大越慢)
public:
Game() {
// 初始化玩家
player.x = WIDTH / 2;
player.score = 0;
player.lives = 5;
gameOver = false;
level = 1;
// 初始化敌人移动参数
enemyMoveCounter = 0;
enemyMoveDelay = 3; // 每2帧移动一次,原来是1(速度减半)
// 初始化子弹
for(int i = 0; i < MAX_BULLETS; i++) {
bullets[i].active = false;
}
// 初始化敌人
for(int i = 0; i < MAX_ENEMIES; i++) {
enemies[i].active = false;
}
srand(time(NULL));
}
// 绘制游戏界面
void draw() {
system("cls"); // 清屏
// 显示游戏信息
cout << "得分: " << player.score << " 生命: " << player.lives
<< " 等级: " << level << " 敌人速度: " << (1.0/enemyMoveDelay) << endl;
cout << "========================================" << endl;
// 绘制游戏区域
for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
// 绘制边界
if(y == 0 || y == HEIGHT-1) {
cout << "=";
}
else if(x == 0 || x == WIDTH-1) {
cout << "|";
}
// 绘制玩家
else if(y == HEIGHT-2 && x == player.x) {
cout << "A";
}
// 绘制子弹
else {
bool drawn = false;
// 绘制玩家子弹
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i].active && bullets[i].x == x && bullets[i].y == y) {
cout << "|";
drawn = true;
break;
}
}
// 绘制敌人
if(!drawn) {
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].active && enemies[i].x == x && enemies[i].y == y) {
cout << "V";
drawn = true;
break;
}
}
}
if(!drawn) {
cout << " ";
}
}
}
cout << endl;
}
// 显示操作说明
cout << "========================================" << endl;
cout << "操作: A左移 D右移 空格射击 ESC退出" << endl;
if(gameOver) {
cout << "\n游戏结束! 最终得分: " << player.score << endl;
}
}
// 处理输入
void input() {
if(_kbhit()) {
char key = _getch();
switch(key) {
case 'a':
case 'A':
if(player.x > 1) player.x--;
break;
case 'd':
case 'D':
if(player.x < WIDTH-2) player.x++;
break;
case ' ': // 空格键发射子弹
for(int i = 0; i < MAX_BULLETS; i++) {
if(!bullets[i].active) {
bullets[i].x = player.x;
bullets[i].y = HEIGHT-3;
bullets[i].active = true;
break;
}
}
break;
case 27: // ESC键
gameOver = true;
break;
// 添加速度控制快捷键(调试用)
case '+':
if(enemyMoveDelay > 1) enemyMoveDelay--;
break;
case '-':
enemyMoveDelay++;
break;
}
}
}
// 更新游戏逻辑
void update() {
if(gameOver) return;
// 移动子弹
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i].active) {
bullets[i].y--;
if(bullets[i].y <= 0) {
bullets[i].active = false;
}
}
}
// 随机生成敌人 - 减少生成频率
if(rand() % (20 - level) == 0) { // 原来是(10 - level)
for(int i = 0; i < MAX_ENEMIES; i++) {
if(!enemies[i].active) {
enemies[i].x = rand() % (WIDTH-2) + 1;
enemies[i].y = 1;
enemies[i].active = true;
break;
}
}
}
// 移动敌人 - 使用计数器控制速度
enemyMoveCounter++;
if(enemyMoveCounter >= enemyMoveDelay) {
enemyMoveCounter = 0;
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].active) {
enemies[i].y++;
// 敌人到达底部
if(enemies[i].y >= HEIGHT-2) {
enemies[i].active = false;
player.lives--;
if(player.lives <= 0) {
gameOver = true;
}
}
}
}
}
// 检测碰撞:子弹 vs 敌人
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i].active) {
for(int j = 0; j < MAX_ENEMIES; j++) {
if(enemies[j].active) {
if(bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) {
bullets[i].active = false;
enemies[j].active = false;
player.score += 10;
// 每100分升一级,适当调整敌人速度
if(player.score % 100 == 0) {
level++;
// 每升一级,敌人稍微加快,但保持比原来慢
if(enemyMoveDelay > 1 && level % 2 == 0) {
enemyMoveDelay--;
}
}
break;
}
}
}
}
}
// 检测碰撞:敌人 vs 玩家
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].active) {
if(enemies[i].x == player.x && enemies[i].y == HEIGHT-2) {
enemies[i].active = false;
player.lives--;
if(player.lives <= 0) {
gameOver = true;
}
}
}
}
}
// 运行游戏
void run() {
while(!gameOver) {
draw();
input();
update();
Sleep(200); // 控制游戏速度
}
draw(); // 显示最终画面
}
// 开始游戏
void start() {
cout << "====== 射击小游戏 ======" << endl;
cout << "游戏规则:" << endl;
cout << "1. 控制飞船(A)左右移动" << endl;
cout << "2. 按空格键发射子弹" << endl;
cout << "3. 消灭敌人(V)获得分数" << endl;
cout << "4. 敌人到达底部会减少生命" << endl;
cout << "5. 你有3条生命" << endl;
cout << "6. 敌人速度已调慢!" << endl;
cout << "\n调试快捷键:+ 加速 - 减速" << endl;
cout << "\n按任意键开始游戏..." << endl;
_getch();
run();
}
};
int main() {
// 设置控制台标题
SetConsoleTitle("射击小游戏 - 敌人速度减慢版 - Dev-C++");
// 创建游戏并开始
Game game;
game.start();
cout << "\n游戏结束,按任意键退出..." << endl;
_getch();
return 0;
}