用c++写一个射击小游戏
时间: 2024-01-08 08:55:57 浏览: 35
好的,下面是一个简单的射击小游戏的代码示例:
```c++
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <vector>
#include <time.h>
using namespace std;
const int MapWidth = 20;
const int MapHeight = 20;
class Object {
protected:
int x, y;
public:
Object(int _x, int _y) :x(_x), y(_y) {}
virtual void Draw(char map[MapHeight][MapWidth + 1]) const = 0;
int GetX() const {
return x;
}
int GetY() const {
return y;
}
};
class Player : public Object {
private:
int direction; // 0:上,1:右,2:下,3:左
public:
Player(int _x, int _y) :Object(_x, _y), direction(0) {}
void Move() {
switch (direction) {
case 0: // 上
if (y > 0) y--;
break;
case 1: // 右
if (x < MapWidth - 1) x++;
break;
case 2: // 下
if (y < MapHeight - 1) y++;
break;
case 3: // 左
if (x > 0) x--;
break;
}
}
void SetDirection(int _direction) {
direction = _direction;
}
virtual void Draw(char map[MapHeight][MapWidth + 1]) const {
map[y][x] = '@';
}
};
class Enemy : public Object {
public:
Enemy(int _x, int _y) :Object(_x, _y) {}
virtual void Draw(char map[MapHeight][MapWidth + 1]) const {
map[y][x] = 'T';
}
};
class Bullet : public Object {
private:
int direction; // 0:上,1:右,2:下,3:左
public:
Bullet(int _x, int _y, int _direction) :Object(_x, _y), direction(_direction) {}
bool Move() {
switch (direction) {
case 0: // 上
if (y > 0) {
y--;
return true;
}
break;
case 1: // 右
if (x < MapWidth - 1) {
x++;
return true;
}
break;
case 2: // 下
if (y < MapHeight - 1) {
y++;
return true;
}
break;
case 3: // 左
if (x > 0) {
x--;
return true;
}
break;
}
return false;
}
virtual void Draw(char map[MapHeight][MapWidth + 1]) const {
map[y][x] = '*';
}
};
class Game {
private:
char map[MapHeight][MapWidth + 1];
Player player;
vector<Enemy> enemies;
vector<Bullet> bullets;
public:
Game() :player(1, 1) {
// 初始化地图
for (int i = 0; i < MapHeight; i++) {
for (int j = 0; j < MapWidth; j++) {
if (i == 0 || i == MapHeight - 1 || j == 0 || j == MapWidth - 1) {
map[i][j] = '#'; // 外围墙壁
}
else {
map[i][j] = ' ';
}
}
map[i][MapWidth] = '\0';
}
// 初始化敌人
srand((unsigned)time(NULL));
for (int i = 0; i < 5; i++) {
int x = rand() % (MapWidth - 2) + 1;
int y = rand() % (MapHeight - 2) + 1;
enemies.push_back(Enemy(x, y));
}
}
void Draw() {
system("cls"); // 清屏
// 绘制地图
for (int i = 0; i < MapHeight; i++) {
cout << map[i] << endl;
}
}
void Input() {
if (_kbhit()) { // 判断是否有键盘输入
switch (_getch()) {
case 'w':
player.SetDirection(0);
break;
case 'd':
player.SetDirection(1);
break;
case 's':
player.SetDirection(2);
break;
case 'a':
player.SetDirection(3);
break;
case ' ':
bullets.push_back(Bullet(player.GetX(), player.GetY(), player.GetDirection()));
break;
}
}
}
void Logic() {
// 移动玩家
player.Move();
// 移动敌人
for (int i = 0; i < enemies.size(); i++) {
int direction = rand() % 4;
switch (direction) {
case 0: // 上
if (enemies[i].GetY() > 0) enemies[i] = Enemy(enemies[i].GetX(), enemies[i].GetY() - 1);
break;
case 1: // 右
if (enemies[i].GetX() < MapWidth - 1) enemies[i] = Enemy(enemies[i].GetX() + 1, enemies[i].GetY());
break;
case 2: // 下
if (enemies[i].GetY() < MapHeight - 1) enemies[i] = Enemy(enemies[i].GetX(), enemies[i].GetY() + 1);
break;
case 3: // 左
if (enemies[i].GetX() > 0) enemies[i] = Enemy(enemies[i].GetX() - 1, enemies[i].GetY());
break;
}
}
// 移动子弹
for (int i = 0; i < bullets.size(); i++) {
if (!bullets[i].Move()) {
bullets.erase(bullets.begin() + i);
i--;
continue;
}
// 检测子弹是否击中敌人
for (int j = 0; j < enemies.size(); j++) {
if (bullets[i].GetX() == enemies[j].GetX() && bullets[i].GetY() == enemies[j].GetY()) {
enemies.erase(enemies.begin() + j);
bullets.erase(bullets.begin() + i);
i--;
break;
}
}
}
// 更新地图
for (int i = 0; i < MapHeight; i++) {
for (int j = 0; j < MapWidth; j++) {
map[i][j] = ' ';
}
map[i][MapWidth] = '\0';
}
player.Draw(map);
for (int i = 0; i < enemies.size(); i++) {
enemies[i].Draw(map);
}
for (int i = 0; i < bullets.size(); i++) {
bullets[i].Draw(map);
}
}
void Run() {
while (true) {
Draw();
Input();
Logic();
Sleep(100); // 暂停一段时间,减缓游戏速度
}
}
};
int main() {
Game game;
game.Run();
return 0;
}
```
这个小游戏比较简单,实现了玩家移动、射击、敌人移动和击中检测等功能。你可以根据自己的需要,添加更多的游戏元素和功能,比如加入更多的敌人、更多种类的子弹、更复杂的敌人移动方式和攻击方式等。