C++双人射击小游戏爆改版
·
添加了血量显示、回血点、随机扣血(0-100)、灯效闪烁、3图、win7-win11全兼容、BGM播放(把PVZHE_MAIN_MUSIC.mp3改成你的音乐文件名)
//单机版枪战游戏,喜欢就拿走,把赞留下
// DEV C++ 要在工具-编译选项-"在链接时加入以下参数"框中填入 -lwinmm
// 最好添新建一个文本文件,命名为system.txt,内容为1或0,1为开灯效,0为关灯效
//by floatiy with wenbo
#define _CRT_NONSTDC_NO_WARNINGS
#define hurt (rand()%100)
#define light_red if(shezhi==1){system("color C0"); Sleep(50); system("color 0F");}
#define light_gre if(shezhi==1){system("color 20"); Sleep(10); system("color 0F");}
#include<iostream>
#include<cstdio>
#include<windows.h>
#include<mmsystem.h>
#include<conio.h>
#include<fstream>
#pragma comment(lib, "winmm.lib")
using namespace std;
fstream file;
bool shezhi = 0, b = 1;
int SIZ = 20;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
HANDLE hCon = NULL;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void create_heal_point(int x, int y, int amount);
void draw_heal_point(int x, int y);
void check_heal_point();
// 新增回血点结构体
struct HealPoint {
int x, y;
bool active;
int heal_amount;
int cooldown; // 冷却时间(毫秒)
int last_used;
} heal_points[10]; // 最多10个回血点
int heal_point_count = 0;
void SetColor(Color c) {
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
SYSTEMTIME sys;
//sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek
struct PLAYER {
int x, y;
int hp;
int gun;
int direct;
} p1, p2;
int map[1005][1005];
int myabs(int x) { // 避免与标准库abs冲突
if (x < 0) return -x;
return x;
}
void locate(int x, int y) {
coord.X = y - 1;
coord.Y = x - 1;
SetConsoleCursorPosition(hout, coord);
}
void print_map() {
locate(1, 1);
SetColor(GRAY);
for (int i = 1; i <= SIZ; i++) {
cout << "██";
}
locate(SIZ, 1);
for (int i = 1; i <= SIZ; i++) cout << "██";
for (int i = 2; i < SIZ; i++) {
locate(i, 1);
cout << "██";
locate(i, SIZ * 2 - 1);
cout << "██";
}
// ? 绘制回血点(始终显示)
for (int i = 0; i < heal_point_count; i++) {
if (heal_points[i].active) {
draw_heal_point(heal_points[i].x, heal_points[i].y);
}
}
locate(SIZ + 1, 1);
SetColor(WHITE);
}
void create_tree(int x, int y) {
map[x][y] = map[x + 1][y] = map[x - 1][y] = map[x][y + 1] = map[x][y - 1] = 2;
}
void use_map(int x) {
if (x == 1) {
SIZ = 20;
SetColor(DARKGREEN);
map[16][6] = map[15][6] = map[17][6] = map[16][7] = map[16][5] = map[14][13] = map[13][12] = map[13][13] = 2;
for (int i = 2; i < SIZ; i++) {
for (int j = 2; j < SIZ; j++) {
if (map[i][j] == 2) {
locate(i, j * 2 - 1);
cout << "██";
}
}
}
// 地图1 - 修复后可踩到的回血点
create_heal_point(4, 8, 100); // 左上角空地
create_heal_point(16, 16, 100); // 右下角空地
SetColor(GRAY);
for (int i = 5; i <= 15; i++) {
map[i][i] = 1;
locate(i, i * 2 - 1);
cout << "██";
}
SetColor(WHITE);
}
else if (x == 2) {
SIZ = 30;
SetColor(GRAY);
for (int i = 4; i <= 26; i++) {
if (i == 13 || i == 14 || i == 15) continue;
map[i][4] = map[4][i] = map[26][i] = map[i][26] = 1;
}
for (int i = 1; i <= SIZ; i++) {
for (int j = 1; j <= SIZ; j++) {
if (map[i][j] == 1) {
locate(i, j * 2 - 1);
cout << "██";
}
}
}
SetColor(DARKGREEN);
for (int i = 10; i <= 20; i++) {
if (i == 13 || i == 17) continue;
map[i][10] = map[10][i] = map[20][i] = map[i][20] = 2;
}
create_tree(5, 5);
create_tree(18, 18);
for (int i = 1; i <= SIZ; i++) {
for (int j = 1; j <= SIZ; j++) {
if (map[i][j] == 2) {
locate(i, j * 2 - 1);
cout << "██";
}
}
}
create_heal_point(8, 8, 150);
create_heal_point(22, 22, 150);
create_heal_point(15, 5, 150);
SetColor(WHITE);
}
else if (x == 3) {
SIZ = 35;
SetColor(GRAY);
// 外墙
for (int i = 1; i <= SIZ; i++) {
map[i][1] = map[1][i] = map[SIZ][i] = map[i][SIZ] = 1;
}
// 内部迷宫墙
for (int i = 5; i <= 30; i++) {
if (i != 10 && i != 25) {
map[i][10] = map[10][i] = map[i][25] = map[25][i] = 1;
}
}
map[10][15] = 0;
// 打印墙
for (int i = 1; i <= SIZ; i++) {
for (int j = 1; j <= SIZ; j++) {
if (map[i][j] == 1) {
locate(i, j * 2 - 1);
cout << "██";
}
}
}
// 森林障碍(四个角落)
SetColor(DARKGREEN);
create_tree(5, 5);
create_tree(5, 30);
create_tree(30, 5);
create_tree(30, 30);
for (int i = 1; i <= SIZ; i++) {
for (int j = 1; j <= SIZ; j++) {
if (map[i][j] == 2) {
locate(i, j * 2 - 1);
cout << "██";
}
}
}
// 添加回血点
create_heal_point(18, 18, 200); // 中心大回血点
create_heal_point(8, 15, 100);
create_heal_point(25, 15, 100);
SetColor(WHITE);
}
else if (x == 4)
{
int y;
cout << "0关闭\n1打开\n3返回\n";
cin >> y;
if (y == 0 || y == 1) {
shezhi = y;
file << shezhi;
b = 0;
cout << "修改成功\n";
system("cls");
return;
}
else {
b = 0;
system("cls");
return;
}
}
else {
cout << "'" << x << "'" << "was not a map,please cin 1 to 3!";
Sleep(2666);
exit(0);
}
}
void cleanbody(int x, int y);
void putbody(int x, int y, int z);
void player_init() {
p1.hp = 1000;
p2.hp = 1000;
p1.gun = p2.gun = 1;
p1.direct = 4;
p2.direct = 2;
p1.x = 2;
p1.y = 2;
p2.x = SIZ - 1;
p2.y = SIZ - 1;
putbody(p1.x, p1.y, 1);
putbody(p2.x, p2.y, 2);
}
void mapinit() {
for (int i = 1; i <= SIZ; i++) {
map[i][1] = map[1][i] = map[SIZ][i] = map[i][SIZ] = 1;
}
}
void init() {
printf("请选择地图?\n1: 小地图(远点架枪),\n2: 中地图(隧道追逐),\n3: 大地图迷宫(极限拉扯)\n4:设置(开关灯效)\n");
int x;
cin >> x;
system("cls");
use_map(x);
if (b == 1) {
mapinit();
print_map();
player_init();
}
}
void putbody(int x, int y, int z) {
if (z == 1) SetColor(BLUE);
else if (z == 2) SetColor(RED);
locate(x, y * 2 - 1);
cout << "██";
SetColor(WHITE);
}
void cleanbody(int x, int y) {
locate(x, y * 2 - 1);
cout << " ";
// ? 如果这个位置是回血点,重绘它
for (int i = 0; i < heal_point_count; i++) {
if (heal_points[i].x == x && heal_points[i].y == y && heal_points[i].active) {
draw_heal_point(x, y);
break;
}
}
}
/*
LIST
direct:
w 1
a 2
s 3
d 4
gun:
usp 1
mimigun 2
awp 3
block:
void 0
stone 1
tree 2
player 3
clip 4
*/
bool judge(int x, int y) {
if (map[x][y] == 1) return false; // 墙
if (map[x][y] == 2) return false; // 树
// ? 允许踩回血点
return true;
}
bool judge_gun(int x, int y) { //掉血
if (map[x][y] == 1) return 0; // 墙
if (map[x][y] == 2) return 0; // 树
if (map[x][y] == 5) return 0; // ? 回血点也挡子弹
if (map[x][y] == 3) { // 玩家
if (p1.x == x && p1.y == y) {
p1.hp -= hurt;
light_red;
SetColor(YELLOW);
locate(1, 1);
cout << "P1:" << p1.hp << "P2:" << p2.hp << '_';
}
else {
p2.hp -= hurt;
light_red;
SetColor(YELLOW);
locate(1, 1);
cout << "P1:" << p1.hp << "P2:" << p2.hp << '_';
}
return 0;
}
return 1;
}
int cnt = 0;
struct Clip {
int x, y;
int derect;
int force;
int start;
bool flag;
} clip[1000000];
void create_clip(int y, int x, int a, int b) {
int X = 0, Y = 0;
if (y == 1) {
if (!judge_gun(a - 1, b)) return;
X = a - 1;
Y = b;
}
else if (y == 2) {
if (!judge_gun(a, b - 1)) return;
X = a;
Y = b - 1;
}
else if (y == 3) {
if (!judge_gun(a + 1, b)) return;
X = a + 1;
Y = b;
}
else if (y == 4) {
if (!judge_gun(a, b + 1)) return;
X = a;
Y = b + 1;
}
cnt++;
GetLocalTime(&sys);
clip[cnt].start = sys.wMilliseconds + sys.wSecond * 1000 + sys.wMinute * 60000;
clip[cnt].x = X;
clip[cnt].y = Y;
if (x == 1) {
clip[cnt].derect = p1.direct;
}
else if (x == 2) {
clip[cnt].derect = p2.direct;
}
}
void shoot(int x) {
if (x == 1) {
create_clip(p1.direct, 1, p1.x, p1.y);
}
else if (x == 2) {
create_clip(p2.direct, 2, p2.x, p2.y);
}
}
void clean_clip(int x, int y) {
// ? 如果是回血点,不清除
if (map[x][y] == 5) return;
locate(x, y * 2 - 1);
cout << " ";
}
void print_clip(int x, int y, int i) {
if (clip[i].flag) return;
// ? 如果是回血点,不打印子弹
if (map[x][y] == 5) return;
locate(x, y * 2 - 1);
SetColor(YELLOW);
cout << "''";
}
void clipmove() {
GetLocalTime(&sys);
int t = sys.wMilliseconds + sys.wSecond * 1000 + sys.wMinute * 60000;
for (int i = 1; i <= cnt; i++) {
if (clip[i].flag) continue;
if (myabs(clip[i].start - t) > 50) {
clip[i].start = t;
int x = clip[i].x;
int y = clip[i].y;
if (clip[i].derect == 1) {
if (!judge_gun(clip[i].x - 1, clip[i].y)) {
clip[i].flag = 1;
clean_clip(x, y);
continue;
}
clean_clip(clip[i].x, clip[i].y);
clip[i].x--;
print_clip(clip[i].x, clip[i].y, i);
}
else if (clip[i].derect == 2) {
if (!judge_gun(clip[i].x, clip[i].y - 1)) {
clip[i].flag = 1;
clean_clip(x, y);
continue;
}
clean_clip(clip[i].x, clip[i].y);
clip[i].y--;
print_clip(clip[i].x, clip[i].y, i);
}
else if (clip[i].derect == 3) {
if (!judge_gun(clip[i].x + 1, clip[i].y)) {
clip[i].flag = 1;
clean_clip(x, y);
continue;
}
clean_clip(clip[i].x, clip[i].y);
clip[i].x++;
print_clip(clip[i].x, clip[i].y, i);
}
else if (clip[i].derect == 4) {
if (!judge_gun(clip[i].x, clip[i].y + 1)) {
clip[i].flag = 1;
clean_clip(x, y);
continue;
}
clean_clip(clip[i].x, clip[i].y);
clip[i].y++;
print_clip(clip[i].x, clip[i].y, i);
}
else if (map[clip[i].x][clip[i].y] == 5) {
clip[i].flag = 1;
continue;
}
}
}
}
void judge_hp() {
int x = p1.hp;
int y = p2.hp;
if (x < 0 && y < 0 && x > y) swap(x, y);
if (x <= 0) {
locate(1, 1);
system("cls");
printf("GAME OVER!\n右边那个赢了!");
Sleep(5000);
printf("\n-MADE BY Floatiy-");
}
else if (y <= 0) {
locate(1, 1);
system("cls");
printf("GAME OVER!\n左边那个赢了!");
Sleep(5000);
printf("\n-MADE BY Floatiy-");
}
}
void prog() {
char ch;
while (true) {
if (kbhit()) {
ch = getch();
if ((ch == 'w' || ch == 'W') && judge(p1.x - 1, p1.y)) {
p1.direct = 1;
cleanbody(p1.x, p1.y);
map[p1.x][p1.y] = 0;
putbody(--p1.x, p1.y, 1);
map[p1.x][p1.y] = 3;
}
else if (ch == '8' && judge(p2.x - 1, p2.y)) {
p2.direct = 1;
cleanbody(p2.x, p2.y);
map[p2.x][p2.y] = 0;
putbody(--p2.x, p2.y, 2);
map[p2.x][p2.y] = 3;
}
else if ((ch == 'a' || ch == 'A') && judge(p1.x, p1.y - 1)) {
p1.direct = 2;
cleanbody(p1.x, p1.y);
map[p1.x][p1.y] = 0;
putbody(p1.x, --p1.y, 1);
map[p1.x][p1.y] = 3;
}
else if (ch == '4' && judge(p2.x, p2.y - 1)) {
p2.direct = 2;
cleanbody(p2.x, p2.y);
map[p2.x][p2.y] = 0;
putbody(p2.x, --p2.y, 2);
map[p2.x][p2.y] = 3;
}
else if ((ch == 's' || ch == 'S') && judge(p1.x + 1, p1.y)) {
p1.direct = 3;
cleanbody(p1.x, p1.y);
map[p1.x][p1.y] = 0;
putbody(++p1.x, p1.y, 1);
map[p1.x][p1.y] = 3;
}
else if (ch == '5' && judge(p2.x + 1, p2.y)) {
p2.direct = 3;
cleanbody(p2.x, p2.y);
map[p2.x][p2.y] = 0;
putbody(++p2.x, p2.y, 2);
map[p2.x][p2.y] = 3;
}
else if ((ch == 'd' || ch == 'D') && judge(p1.x, p1.y + 1)) {
p1.direct = 4;
cleanbody(p1.x, p1.y);
map[p1.x][p1.y] = 0;
putbody(p1.x, ++p1.y, 1);
map[p1.x][p1.y] = 3;
}
else if (ch == '6' && judge(p2.x, p2.y + 1)) {
p2.direct = 4;
cleanbody(p2.x, p2.y);
map[p2.x][p2.y] = 0;
putbody(p2.x, ++p2.y, 2);
map[p2.x][p2.y] = 3;
}
else if (ch == '0') {
shoot(2);
}
else if (ch == ' ') {
shoot(1);
}
Sleep(20);
}
// 检查回血点
check_heal_point();
clipmove();
judge_hp();
}
}
void welcome() {
printf("操作方法:\n玩家1 wasd控制移动,空格攻击\n玩家2 数字小键盘4568控制移动,0攻击\n");
Sleep(2000);
}
// 创建回血点
void create_heal_point(int x, int y, int amount) {
if (heal_point_count >= 10) return;
heal_points[heal_point_count].x = x;
heal_points[heal_point_count].y = y;
heal_points[heal_point_count].active = true;
heal_points[heal_point_count].heal_amount = amount;
heal_points[heal_point_count].cooldown = 5000; // 5秒冷却
heal_points[heal_point_count].last_used = 0;
map[x][y] = 5; // 设置为回血点类型
heal_point_count++;
}
// 绘制回血点
void draw_heal_point(int x, int y) {
locate(x, y * 2 - 1);
SetColor(GREEN);
cout << "+";
SetColor(WHITE);
}
// 检查是否在回血点上并处理回血
void check_heal_point() {
GetLocalTime(&sys);
int current_time = sys.wMilliseconds + sys.wSecond * 1000 + sys.wMinute * 60000;
for (int i = 0; i < heal_point_count; i++) {
if (!heal_points[i].active) continue;
// P1
if (p1.x == heal_points[i].x && p1.y == heal_points[i].y) {
if (current_time - heal_points[i].last_used >= heal_points[i].cooldown) {
p1.hp = min(1000, p1.hp + heal_points[i].heal_amount);
heal_points[i].last_used = current_time;
locate(SIZ + 2, 1);
light_gre;
SetColor(GREEN);
cout << "P1回复了 " << heal_points[i].heal_amount << " 点血量! ";
SetColor(YELLOW);
locate(1, 1);
cout << "P1:" << p1.hp << "P2:" << p2.hp << '_';
SetColor(WHITE);
}
}
// P2
if (p2.x == heal_points[i].x && p2.y == heal_points[i].y) {
if (current_time - heal_points[i].last_used >= heal_points[i].cooldown) {
p2.hp = min(1000, p2.hp + heal_points[i].heal_amount);
heal_points[i].last_used = current_time;
locate(SIZ + 3, 1);
light_gre;
SetColor(GREEN);
cout << "P2回复了 " << heal_points[i].heal_amount << " 点血量! ";
SetColor(YELLOW);
locate(1, 1);
cout << "P1:" << p1.hp << "P2:" << p2.hp << '_';
SetColor(WHITE);
}
}
}
}
void read(void) {
file.open("system.txt", ios::in | ios::out);
if (!file.is_open()){
cout << "[警告]设置文件打开失败\n";
return;
}
char c;
file >> c;
shezhi = c - '0';
}
int main() {
while (1) {
read();
welcome();
GetLocalTime(&sys);
init();
if (b == 1) {
//打开音乐文件
mciSendString(TEXT("open PVZHE_MAIN_MUSIC.mp3 alias s1 "), NULL, 0, NULL);
mciSendString(TEXT("play s1 repeat"), NULL, SND_RESOURCE, NULL);
prog();
mciSendString(TEXT("close s1"), NULL, 0, NULL);
}
}
return 0;
}
更多推荐



所有评论(0)