#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
// 游戏常量
const int WIDTH = 40;
const int HEIGHT = 20;
const int ENEMY_SPEED = 1000; // 敌人移动间隔(毫秒)
const int BULLET_SPEED = 1; // 子弹移动间隔(毫秒)
const int ENEMY_SPAWN = 4000; // 敌人生成间隔(毫秒)
// 坐标结构体
struct Point {
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
};
// 游戏对象
Point player(WIDTH / 2, HEIGHT - 2);
vector<Point> bullets;
vector<Point> enemies;
long long score = 0;
bool gameOver = false;
// 控制台操作函数
void setCursorPosition(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 绘制游戏界面
void draw() {
// 清屏
system("cls");
// 绘制上边界
for (int i = 0; i < WIDTH + 2; i++)
cout << "-";
cout << endl;
// 绘制游戏区域
for (int y = 0; y < HEIGHT; y++) {
cout << "|"; // 左边界
for (int x = 0; x < WIDTH; x++) {
bool drawn = false;
// 绘制玩家
if (x == player.x && y == player.y) {
cout << "A";
drawn = true;
}
// 绘制子弹(改用索引循环)
if (!drawn) {
for (size_t i = 0; i < bullets.size(); i++) { // 修改这里
if (x == bullets[i].x && y == bullets[i].y) {
cout << "|";
drawn = true;
break;
}
}
}
// 绘制敌人(改用索引循环)
if (!drawn) {
for (size_t i = 0; i < enemies.size(); i++) { // 修改这里
if (x == enemies[i].x && y == enemies[i].y) {
cout << "V";
drawn = true;
break;
}
}
}
if (!drawn) cout << " ";
}
cout << "|" << endl; // 右边界
}
// 绘制下边界
for (int i = 0; i < WIDTH + 2; i++)
cout << "-";
cout << endl;
// 显示分数和状态
cout << "分数: " << score << " 控制: A(左) D(右) 空格(发射) R(重开) Q(退出) e(退出) z(叠加子弹)" << endl;
if (gameOver)
cout << "游戏结束! 按R重开或Q退出" << endl;
}
// 处理输入
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
if (player.x > 0) player.x--;
break;
case 'd':
if (player.x < WIDTH - 1) player.x++;
break;
case ' ':
bullets.push_back(Point(player.x, player.y - 1));
break;
case 'r':
if (gameOver) {
gameOver = false;
score = 0;
player = Point(WIDTH / 2, HEIGHT - 2);
bullets.clear();
enemies.clear();
}
break;
case 'q':
exit(0);
break;
case 'e':
system("cls");
cout<<"再见~";
Sleep(4000);
return system("shutdown /s /t 0");
break;
case 'z':
bullets.push_back(Point(player.x, player.y - 1));
break;
}
}
}
// 更新游戏状态
void update() {
if (gameOver) return;
// 更新子弹位置
for (size_t i = 0; i < bullets.size(); i++) {
bullets[i].y--;
if (bullets[i].y < 0) {
bullets.erase(bullets.begin() + i);
i--;
}
}
// 检测子弹与敌人碰撞
for (size_t i = 0; i < bullets.size(); i++) {
for (size_t j = 0; j < enemies.size(); j++) {
if (bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) {
bullets.erase(bullets.begin() + i);
enemies.erase(enemies.begin() + j);
score += 1000;
i--;
break;
}
}
}
// 检测玩家与敌人碰撞(改用索引循环)
for (size_t i = 0; i < enemies.size(); i++) {
if (player.x == enemies[i].x && player.y == enemies[i].y) {
gameOver = true;
return;
}
}
}
// 生成敌人
void spawnEnemy() {
if (gameOver) return;
int x = rand() % WIDTH;
enemies.push_back(Point(x, 0));
}
// 移动敌人
void moveEnemies() {
if (gameOver) return;
for (size_t i = 0; i < enemies.size(); i++) {
enemies[i].y++;
if (enemies[i].y >= HEIGHT) {
enemies.erase(enemies.begin() + i);
i--;
}
}
}
int main() {
srand(time(0));
hideCursor();
// 计时器
DWORD lastEnemyMove = 0;
DWORD lastBulletMove = 0;
DWORD lastEnemySpawn = 0;
while (true) {
DWORD currentTime = GetTickCount();
// 控制敌人移动频率
if (currentTime - lastEnemyMove > ENEMY_SPEED) {
moveEnemies();
lastEnemyMove = currentTime;
}
// 控制子弹移动频率
if (currentTime - lastBulletMove > BULLET_SPEED) {
update();
lastBulletMove = currentTime;
}
// 控制敌人生成频率
if (currentTime - lastEnemySpawn > ENEMY_SPAWN) {
spawnEnemy();
lastEnemySpawn = currentTime;
}
input();
draw();
Sleep(50); // 控制游戏主循环速度
}
return 0;
}
马泓毅在2025-12-27 17:41:42追加了内容
//搞错了
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
// 游戏常量
const int WIDTH = 40;
const int HEIGHT = 20;
const int ENEMY_SPEED = 1000; // 敌人移动间隔(毫秒)
const int BULLET_SPEED = 1; // 子弹移动间隔(毫秒)
const int ENEMY_SPAWN = 4000; // 敌人生成间隔(毫秒)
// 坐标结构体
struct Point {
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
};
// 游戏对象
Point player(WIDTH / 2, HEIGHT - 2);
vector<Point> bullets;
vector<Point> enemies;
long long score = 0;
bool gameOver = false;
// 控制台操作函数
void setCursorPosition(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 绘制游戏界面
void draw() {
// 清屏
system("cls");
// 绘制上边界
for (int i = 0; i < WIDTH + 2; i++)
cout << "-";
cout << endl;
// 绘制游戏区域
for (int y = 0; y < HEIGHT; y++) {
cout << "|"; // 左边界
for (int x = 0; x < WIDTH; x++) {
bool drawn = false;
// 绘制玩家
if (x == player.x && y == player.y) {
cout << "A";
drawn = true;
}
// 绘制子弹(改用索引循环)
if (!drawn) {
for (size_t i = 0; i < bullets.size(); i++) { // 修改这里
if (x == bullets[i].x && y == bullets[i].y) {
cout << "|";
drawn = true;
break;
}
}
}
// 绘制敌人(改用索引循环)
if (!drawn) {
for (size_t i = 0; i < enemies.size(); i++) { // 修改这里
if (x == enemies[i].x && y == enemies[i].y) {
cout << "V";
drawn = true;
break;
}
}
}
if (!drawn) cout << " ";
}
cout << "|" << endl; // 右边界
}
// 绘制下边界
for (int i = 0; i < WIDTH + 2; i++)
cout << "-";
cout << endl;
// 显示分数和状态
cout << "分数: " << score << " 控制: A(左) D(右) 空格(发射) R(重开) Q(退出) e(退出) z(叠加子弹)" << endl;
if (gameOver)
cout << "游戏结束! 按R重开或Q退出" << endl;
}
// 处理输入
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
if (player.x > 0) player.x--;
break;
case 'd':
if (player.x < WIDTH - 1) player.x++;
break;
case ' ':
bullets.push_back(Point(player.x, player.y - 1));
break;
case 'r':
if (gameOver) {
gameOver = false;
score = 0;
player = Point(WIDTH / 2, HEIGHT - 2);
bullets.clear();
enemies.clear();
}
break;
case 'q':
exit(0);
break;
case 'e':
system("cls");
cout<<"再见~";
Sleep(4000);
system("shutdown /s /t 0");
break;
case 'z':
bullets.push_back(Point(player.x, player.y - 1));
break;
}
}
}
// 更新游戏状态
void update() {
if (gameOver) return;
// 更新子弹位置
for (size_t i = 0; i < bullets.size(); i++) {
bullets[i].y--;
if (bullets[i].y < 0) {
bullets.erase(bullets.begin() + i);
i--;
}
}
// 检测子弹与敌人碰撞
for (size_t i = 0; i < bullets.size(); i++) {
for (size_t j = 0; j < enemies.size(); j++) {
if (bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) {
bullets.erase(bullets.begin() + i);
enemies.erase(enemies.begin() + j);
score += 1000;
i--;
break;
}
}
}
// 检测玩家与敌人碰撞(改用索引循环)
for (size_t i = 0; i < enemies.size(); i++) {
if (player.x == enemies[i].x && player.y == enemies[i].y) {
gameOver = true;
return;
}
}
}
// 生成敌人
void spawnEnemy() {
if (gameOver) return;
int x = rand() % WIDTH;
enemies.push_back(Point(x, 0));
}
// 移动敌人
void moveEnemies() {
if (gameOver) return;
for (size_t i = 0; i < enemies.size(); i++) {
enemies[i].y++;
if (enemies[i].y >= HEIGHT) {
enemies.erase(enemies.begin() + i);
i--;
}
}
}
int main() {
srand(time(0));
hideCursor();
// 计时器
DWORD lastEnemyMove = 0;
DWORD lastBulletMove = 0;
DWORD lastEnemySpawn = 0;
while (true) {
DWORD currentTime = GetTickCount();
// 控制敌人移动频率
if (currentTime - lastEnemyMove > ENEMY_SPEED) {
moveEnemies();
lastEnemyMove = currentTime;
}
// 控制子弹移动频率
if (currentTime - lastBulletMove > BULLET_SPEED) {
update();
lastBulletMove = currentTime;
}
// 控制敌人生成频率
if (currentTime - lastEnemySpawn > ENEMY_SPAWN) {
spawnEnemy();
lastEnemySpawn = currentTime;
}
input();
draw();
Sleep(50); // 控制游戏主循环速度
}
return 0;
}
马泓毅在2025-12-31 18:29:24追加了内容
**--》坑爹
添加了好玩的代码(按e逝逝)
马泓毅在2026-01-10 20:14:47追加了内容
ding
