提示:所有游戏仅供 Windows 系统使用。
你可以通过左边的标题栏快速查看版本。
《僵尸庄园》
- 源码下载地址:https://wwana.lanzoue.com/i4oqR47ftfri
密码:yxnb
最新版快捷下载
版本:v1.1.2
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
#if __cplusplus < 201107L
#define unordered_map map
#endif
typedef pair<string,int> Item;
typedef pair<int,int> Pii;
const int WIDTH = 300;
const int HEIGHT = 300;
int RENDER_SIZE = (19 - 1) / 2;
const int LOSE_SCORE = -1;
const int WIN_SCORE = 200;
const int dx[] = {0,0,-1,1};
const int dy[] = {-1,1,0,0};
unordered_map<string,pair<int,int> > CHEST_CAN_OPEN;
char world[HEIGHT + 5][WIDTH + 5];
bitset<WIDTH + 5> chest[HEIGHT + 5];
bitset<WIDTH + 5> apple[HEIGHT + 5];
int zombie_hp[HEIGHT + 5][WIDTH + 5];
bitset<WIDTH + 5> zombie[HEIGHT + 5];
bitset<WIDTH + 5> zombie_hurt[HEIGHT + 5];
bitset<WIDTH + 5> vis[HEIGHT + 5];
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
int pl_x = HEIGHT / 2,pl_y = WIDTH / 2;
int pl_hp = 20;
int score = 0,combo = 0;
short player_mode = 0; // default:0 hurt:1 heal:2
unordered_map<string,int> bag;
string hand_item_name = "";
enum ForeColor{
Black = 30,Red = 31,Green = 32,Yellow = 33,
Blue = 34,Magenta = 35,Cyan = 36,White = 37,
BrightBlack = 90,BrightRed = 91,BrightGreen = 92,
BrightYellow = 93,BrightBlue = 94,BrightMagenta = 95,
BrightCyan = 96,BrightWhite = 97,
};
enum BackColor{
BBlack = 40,BRed = 41,BGreen = 42,BYellow = 43,
BBlue = 44,BMagenta = 45,BCyan = 46,BWhite = 47,
BBrightBlack = 100,BBrightRed = 101,BBrightGreen = 102,
BBrightYellow = 103,BBrightBlue = 104,BBrightMagenta = 105,
BBrightCyan = 106,BBrightWhite = 107
};
namespace GameTool{
inline int Euclid_dis(pair<int,int> pos1,pair<int,int> pos2){
int x1 = pos1.first,y1 = pos1.second;
int x2 = pos2.first,y2 = pos2.second;
int res = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
return res;
};
void SlowOutput(string str,int speed = 50){
for(int i = 0;i < str.length();i++){
char ch = str[i];
cout << ch;
if(ch == ' ' || ch == '\n') continue;
Sleep(speed);
}
}
void CursorMove(int x,int y){
COORD pos = {(SHORT)x,(SHORT)y};
SetConsoleCursorPosition(h,pos);
}
void OpenVT(){
DWORD dwMode = 0;
if(GetConsoleMode(h,&dwMode)){
dwMode |= 0x0004;
if(SetConsoleMode(h,dwMode)){}
else{
cout << "请使用 Win10 以上的系统游玩!";
exit(0);
}
}
}
void HideCursor(){
CONSOLE_CURSOR_INFO cinfo;
GetConsoleCursorInfo(h,&cinfo);
cinfo.bVisible = false;
SetConsoleCursorInfo(h,&cinfo);
}
void ShowCursor(){
CONSOLE_CURSOR_INFO cinfo;
GetConsoleCursorInfo(h,&cinfo);
cinfo.bVisible = true;
SetConsoleCursorInfo(h,&cinfo);
}
inline void SetTextColor(ForeColor fg = Black){
cout << "\033[" << (int)fg << "m";
}
inline void SetBackColor(BackColor bg = BBrightWhite){
cout << "\033[" << (int)bg << "m";
}
inline void SetTextCustomColor(string RGB){
cout << "\033[38;2;" << RGB << "m";
}
inline void ResetColor(){
SetTextColor();
SetBackColor();
}
};
struct BFSNode{
int x,y;
pair<int,int> last;
};
void GenerateApple(int cnt){
for(int i = 1;i <= cnt;i++){
int apple_x = rand() % HEIGHT + 1;
int apple_y = rand() % WIDTH + 1;
if(apple[apple_x][apple_y]) continue;
if(world[apple_x][apple_y] == '#') continue;
if(world[apple_x][apple_y] == 'O') continue;
apple[apple_x][apple_y] = true;
}
return;
}
void GenerateChest(int cnt){
for(int i = 1;i <= cnt;i++){
int chest_x = rand() % HEIGHT + 1;
int chest_y = rand() % WIDTH + 1;
if(chest[chest_x][chest_y]) continue;
if(world[chest_x][chest_y] == '#') continue;
if(world[chest_x][chest_y] == 'O') continue;
chest[chest_x][chest_y] = true;
}
return;
}
void InitItemMap(){
CHEST_CAN_OPEN["生命恢复药剂"] = make_pair(1,3); // 30%
CHEST_CAN_OPEN["积分"] = make_pair(4,7); // 40%
CHEST_CAN_OPEN["剑"] = make_pair(8,10); // 30%
return;
}
void InitMap(){
for(int i = 1;i <= HEIGHT;i++){
for(int j = 1;j <= WIDTH;j++){
if(rand() % 10 == 0){
world[i][j] = '#';
} else{
world[i][j] = '.';
}
}
}
for(int i = 0;i <= HEIGHT + 1;i++){
world[i][0] = '#';
world[i][WIDTH + 1] = '#';
}
for(int i = 0;i <= WIDTH + 1;i++){
world[0][i] = '#';
world[HEIGHT + 1][i] = '#';
}
GenerateApple(HEIGHT * WIDTH / 100);
GenerateChest(HEIGHT * WIDTH / 2000);
return;
}
void RenderScreen(bool render_info = true){
for(int i = pl_x - RENDER_SIZE;i <= pl_x + RENDER_SIZE;i++){
for(int j = pl_y - RENDER_SIZE;j <= pl_y + RENDER_SIZE;j++){
if(GameTool::Euclid_dis(make_pair(pl_x,pl_y),make_pair(i,j)) > RENDER_SIZE * RENDER_SIZE){
cout << ' ';
} else if(i < 0 || i > HEIGHT + 1 || j < 0 || j > WIDTH + 1){
cout << ' ';
} else if(i == pl_x && j == pl_y){
if(player_mode == 1){
GameTool::SetTextColor(Red);
} else if(player_mode == 2){
GameTool::SetTextCustomColor("255;105;180"); // pink
} else{
GameTool::SetTextColor(Green);
}
player_mode = 0;
cout << 'P';
GameTool::ResetColor();
} else if(apple[i][j]){
GameTool::SetTextColor(BrightRed);
cout << 'A';
GameTool::ResetColor();
} else if(zombie[i][j]){
if(zombie_hurt[i][j]){
GameTool::SetTextColor(Red);
if(zombie_hp[i][j] > 0) zombie_hurt[i][j] = false;
} else{
GameTool::SetTextColor(Green);
GameTool::SetBackColor(BRed);
}
cout << 'Z';
GameTool::ResetColor();
} else if(chest[i][j]){
GameTool::SetTextColor(Black);
GameTool::SetBackColor(BYellow);
cout << 'G';
GameTool::ResetColor();
} else{
cout << world[i][j];
}
cout << ' ';
}
cout << '\n';
}
if(render_info == false) return;
cout << "\n - X 坐标:" << pl_x << " Y 坐标:" << pl_y << string(10,' ') << '\n';
cout << " - 积分:" << score << string(20,' ') << '\n';
cout << " - 连击数: " << combo << string(20,' ') << '\n' << string(20,' ') << '\n';
cout << " - 血量: " << pl_hp << string(20,' ') << '\n' << string(20,' ') << '\n';
if(combo >= 3){
cout << " 系统赠送了你 " << floor(sqrt(combo - 2)) << " 积分\n" << string(20,' ') << '\n';
}
cout << " - 背包:" << string(20,' ') << '\n';
for(unordered_map<string,int>::iterator item = bag.begin();item != bag.end();item++){
if(item->first == hand_item_name){
cout << " > " << item->first << " " << item->second << " 个 <" << string(20,' ') << '\n';
} else{
cout << " " << item->first << " " << item->second << " 个" << string(20,' ') << '\n';
}
}
cout << string(30,' ') << '\n' << string(30,' ') << '\n';
return;
}
void SummonZombie(){
int limit_x,limit_y,down_limit_x,down_limit_y;
if(score > WIN_SCORE && rand() % 2 == 0){
limit_x = HEIGHT;
limit_y = WIDTH;
down_limit_x = HEIGHT - 20;
down_limit_y = WIDTH - 20;
} else{
limit_x = min(HEIGHT,pl_x + RENDER_SIZE * 4);
limit_y = min(WIDTH,pl_y + RENDER_SIZE * 4);
down_limit_x = max(1,pl_x - RENDER_SIZE * 4);
down_limit_y = max(1,pl_y - RENDER_SIZE * 4);
}
int randX = rand() % (limit_x - down_limit_x + 1) + down_limit_x;
int randY = rand() % (limit_y - down_limit_y + 1) + down_limit_y;
if(world[randX][randY] == '#') return;
if(world[randX][randY] == 'O') return;
zombie_hp[randX][randY] = 20;
zombie[randX][randY] = true;
return;
}
void RemoveDiedZombie(){
int limit_x = min(HEIGHT,pl_x + RENDER_SIZE * 4);
int limit_y = min(WIDTH,pl_y + RENDER_SIZE * 4);
int down_limit_x = max(1,pl_x - RENDER_SIZE * 4);
int down_limit_y = max(1,pl_y - RENDER_SIZE * 4);
for(int i = down_limit_x;i <= limit_x;i++){
for(int j = down_limit_y;j <= limit_y;j++){
if(zombie_hp[i][j] <= 0){
zombie_hp[i][j] = 0;
zombie[i][j] = false;
zombie_hurt[i][j] = false;
}
}
}
return;
}
void ZForwardToP(){
int limit_x = min(HEIGHT,pl_x + RENDER_SIZE * 4);
int limit_y = min(WIDTH,pl_y + RENDER_SIZE * 4);
int down_limit_x = max(1,pl_x - RENDER_SIZE * 4);
int down_limit_y = max(1,pl_y - RENDER_SIZE * 4);
for(int i = down_limit_x;i <= limit_x;i++){
for(int j = down_limit_y;j <= limit_y;j++){
vis[i][j] = false;
}
}
BFSNode start;
start.x = pl_x,start.y = pl_y,start.last = make_pair(-1,-1);
queue<BFSNode> q;
q.push(start);
while(!q.empty()){
BFSNode now = q.front();
q.pop();
int x = now.x;
int y = now.y;
int lastx = now.last.first;
int lasty = now.last.second;
if(
x < down_limit_x || x > limit_x ||
y < down_limit_y || y > limit_y
){
continue;
}
if(vis[x][y]) continue;
vis[x][y] = true;
if(world[x][y] == '#') continue;
if(world[x][y] == 'O') continue;
if(apple[x][y]) continue;
if(chest[x][y]) continue;
if(zombie[x][y]){
if(lastx != -1 && lasty != -1 && zombie[lastx][lasty] == false){
zombie[x][y] = false;
zombie[lastx][lasty] = true;
zombie_hp[lastx][lasty] = zombie_hp[x][y];
zombie_hp[x][y] = 0;
}
}
for(int i = 0;i < 4;i++){
BFSNode nxt;
nxt.x = x + dx[i];
nxt.y = y + dy[i];
nxt.last = make_pair(x,y);
q.push(nxt);
}
}
return;
}
void ZombieAttack(){
if(zombie[pl_x][pl_y] && zombie_hp[pl_x][pl_y] > 0){
score -= 3;
pl_hp -= 5;
combo = 0;
player_mode = 1;
zombie[pl_x][pl_y] = false;
}
return;
}
void UseHandItem(){
if(hand_item_name.empty()) return;
if(hand_item_name == "剑"){
for(int i = -2;i <= 2;i++){
for(int j = -2;j <= 2;j++){
int x = pl_x + i;
int y = pl_y + j;
if(zombie[x][y] && zombie_hp[x][y] > 0){
zombie_hp[x][y] -= 10;
zombie_hurt[x][y] = true;
}
}
}
} else if(hand_item_name == "生命恢复药剂"){
pl_hp = min(pl_hp + 15,20);
player_mode = 2;
}
bag[hand_item_name]--;
if(bag[hand_item_name] <= 0){
bag.erase(hand_item_name);
hand_item_name.clear();
}
return;
}
void ChargeRenderSize(){
system("cls");
GameTool::ShowCursor();
int r;
cout << " 注意:调整后请自行调整窗口大小。\n\n";
cout << "请输入新的渲染范围半径(输入 4~25 的整数):" ;
cin >> r;
cin.ignore(1024,'\n');
if(r < 4 || r > 25){
cout << "\n请勿设置不合理的渲染距离。\n\n";
GameTool::HideCursor();
system("pause & cls");
return;
}
RENDER_SIZE = r;
GameTool::HideCursor();
system("cls");
return;
}
void SwitchHandItem(int idx){
if(bag.size() >= idx){
unordered_map<string,int>::iterator target = bag.begin();
advance(target,idx - 1);
hand_item_name = (*target).first;
}
return;
}
void HandleKey(){
if(_kbhit()){
char ch = _getch();
ch = tolower(ch);
if(ch == 'a' && world[pl_x][pl_y - 1] != '#'){
pl_y--;
} else if(ch == 'd' && world[pl_x][pl_y + 1] != '#'){
pl_y++;
} else if(ch == 'w' && world[pl_x - 1][pl_y] != '#'){
pl_x--;
} else if(ch == 's' && world[pl_x + 1][pl_y] != '#'){
pl_x++;
} else if(ch == 'z'){
ChargeRenderSize();
} else if(ch == 'u'){
UseHandItem();
} else if(ch == '1'){
SwitchHandItem(1);
} else if(ch == '2'){
SwitchHandItem(2);
} else if(ch == 'q'){
cout << '\n';
system("pause & cls");
}
}
return;
}
void EatApple(){
if(apple[pl_x][pl_y] == false){
return;
}
score++;
combo++;
GenerateApple(1);
if(combo >= 3){
score += floor(sqrt(combo - 2));
}
apple[pl_x][pl_y] = false;
return;
}
void OpenChest(){
if(chest[pl_x][pl_y]){
system("cls");
cout << "正在开启宝箱,请稍后.....";
Sleep(5000);
system("cls");
unordered_map<string,int> open_items;
int item_cnt = rand() % 4 + 1;
while(item_cnt--){
int rand_num = rand() % 10 + 1;
for(unordered_map<string,pair<int,int> >::iterator item = CHEST_CAN_OPEN.begin();item != CHEST_CAN_OPEN.end();item++){
if(rand_num >= item->second.first && rand_num <= item->second.second){
if(item->first == "积分"){
open_items[item->first] += rand() % 6 + 5;
} else if(item->first == "剑"){
open_items[item->first] += 3;
} else{
open_items[item->first]++;
}
break;
}
}
}
cout << " 你开出了以下物品:\n";
for(unordered_map<string,int>::iterator item = open_items.begin();item != open_items.end();item++){
cout << " - " << item->first << "*" << item->second << '\n';
if(item->first == "积分"){
score += item->second;
} else{
bag[item->first] += item->second;
}
}
cout << "\n 物品已自动放入背包。\n\n";
chest[pl_x][pl_y] = false;
system("pause & cls");
}
return;
}
void HelloUI(){
using namespace GameTool;
system("cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.1.2 ==========\n\n");
SlowOutput(" 在这里,你需要收集地上的苹果,每个\n");
SlowOutput(" 苹果可以获得 1 积分。\n");
SlowOutput(" 初始状态你没有积分。\n\n");
SlowOutput(" 当你快速的吃掉 3 个以上的苹果,\n");
SlowOutput(" 系统将会奖励你积分,\n");
SlowOutput(" 数量以连击数量为准。\n\n");
SlowOutput(" 你开始会有 3s 的无敌时间,20点血。\n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.1.2 ==========\n\n",0);
SlowOutput(" 但是,庄园里的稻草人都变异了,\n");
SlowOutput(" 会变成僵尸追击你,\n");
SlowOutput(" 它们也拥有 20 点血。\n\n");
SlowOutput(" 如果你不幸被他们碰到,\n");
SlowOutput(" 你将会失去 3 个积分 和 5 点血。\n");
SlowOutput(" 但那只僵尸会消失不见。 \n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.1.2 ==========\n\n",0);
SlowOutput(" 庄园里每隔一会儿就会刷新一个宝箱,\n");
SlowOutput(" 里面可以开出物品如下: \n");
SlowOutput(" 剑、生命恢复药剂和积分。\n\n");
SlowOutput(" 剑的刷新几率为 30%(1 次 3 把),\n");
SlowOutput(" 生命恢复药剂几率为 30%。\n");
SlowOutput(" 积分几率为 40%。\n\n");
SlowOutput(" 剑可以对周围 5 * 5 僵尸造成 10 点伤害,\n");
SlowOutput(" 此物品为一次性品。\n\n");
SlowOutput(" 生命恢复药剂可以让你瞬间恢复 15 点血,\n");
SlowOutput(" 此物品为一次性品。\n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.1.2 ==========\n\n",0);
SlowOutput(" 当你在当前的情形中获得 200 积分,\n");
SlowOutput(" 在整个庄园右下角将会出现一个传送门,\n");
SlowOutput(" 当你进入这个传送门,你就逃出了这个庄园。\n\n");
SlowOutput(" 若你血量小于 0 或者 积分小于 0,\n");
SlowOutput(" 你将消失不见并永远变成一个僵尸。\n\n");
SlowOutput(" 祝你好运。 Good Luck. \n\n");
cout << " - ";
system("pause & cls");
cout << "========== 更新公告 ==========\n\n";
cout << " 1. 修复部分文案错误。\n";
cout << " 2. 增加生命恢复动画。\n";
cout << " 3. 增加主菜单。\n";
cout << " 4. 移除 config.dat,改为用户主动选择。\n";
cout << " 5. 如果开启传送门,僵尸将会有 50% 的几率刷新在传送门附近。\n";
cout << " 6. 加快僵尸速度,从原本的 20tick 变为 10tick。\n";
cout << " 7. 死亡后僵尸将一直保持受伤状态,直到被清除。\n";
cout << " 8. 修复 bug:玩家受伤动画错误。\n";
cout << " 9. 修复 bug:死亡后的僵尸仍可以攻击玩家。\n";
cout << " 10. 修复 bug:死亡后的僵尸受伤动画仍然存在,只是不显示。\n";
cout << " 11. 修复 bug:僵尸受伤动画和玩家共用导致可能出现无效受伤动画。\n";
cout << " 12. 修复 bug:输入大写 Q 时无法退出结束动画。\n\n";
cout << " - ";
system("pause & cls");
cout << "========== 游戏操作指南 ==========\n\n";
cout << " A 键:向左移动。\n";
cout << " D 键:向右移动。\n";
cout << " W 键:向上移动。\n";
cout << " S 键:向下移动。\n";
cout << " U 键:使用当前物品。\n";
cout << " Z 键:调整渲染距离。\n";
cout << " Q 键:暂停游戏。\n";
cout << " 数字 1 键:切换手持物品为背包内第 1 个物品。\n";
cout << " 数字 2 键:切换手持物品为背包内第 2 个物品。\n\n";
cout << " - ";
system("pause & cls");
SlowOutput("========== 游戏操作指南 ==========\n\n",0);
SlowOutput(" 庄园是一个 300 * 300 的正方形矩阵,\n");
SlowOutput(" '#' 代表墙,'.' 代表空地,\n");
SlowOutput(" 'A' 代表苹果,'Z' 代表僵尸,\n");
SlowOutput(" 'G' 代表宝箱。\n\n");
SlowOutput(" 庄园外围被一圈铁栏杆围着,你无法出入。\n\n");
SlowOutput(" 提示:建议使用旧版终端游玩。\n\n");
system("pause & cls");
return;
}
void MainUI(){
using namespace GameTool;
RENDER_SIZE = 4;
RenderScreen(false);
RENDER_SIZE = 9;
CursorMove(20,2);
cout << "僵尸庄园 v1.1.2";
CursorMove(21,4);
cout << "制作者:yuxiao";
CursorMove(0,10);
SlowOutput(" 1. 开始游戏\n");
SlowOutput(" 2. 查看帮助\n\n");
SlowOutput(" 输入序号以执行操作。");
while(true){
char ch = _getch();
if(ch == '1'){
break;
} else if(ch == '2'){
HelloUI();
}
}
system("cls");
return;
}
bool CheckResult(){
if(pl_hp <= 0 || score <= LOSE_SCORE){
system("cls");
cout << "你被僵尸残忍杀害!";
return true;
}
if(score >= WIN_SCORE && world[HEIGHT][WIDTH] != 'O'){
system("cls");
cout << "传送门已开启!";
for(int i = HEIGHT - 2;i <= HEIGHT;i++){
for(int j = WIDTH - 2;j <= WIDTH;j++){
world[i][j] = 'O';
apple[i][j] = false;
zombie[i][j] = false;
}
}
cout << "按 Q 键退出。";
while(tolower(_getch()) != 'q');
}
if(world[pl_x][pl_y] == 'O'){
system("cls");
cout << "你成功逃出了庄园!";
return true;
}
return false;
}
int main(){
system("color f0");
system("mode con cols=55 lines=40");
srand(time(NULL));
GameTool::OpenVT();
InitItemMap();
InitMap();
MainUI();
GameTool::HideCursor();
int timestamp = 1;
int last_score = 0;
while(true){
RenderScreen();
HandleKey();
EatApple();
OpenChest();
if(timestamp % 50 == 0){
GenerateChest(5);
}
if(timestamp > 60){
ZombieAttack();
}
if(timestamp % 10 == 0){
RemoveDiedZombie();
ZForwardToP();
}
if(timestamp % 40 == 0){
SummonZombie();
}
if(timestamp % 60 == 0){
if(score <= last_score && combo > 0){
combo = 0;
} else{
last_score = score;
}
}
if(CheckResult()) break;
timestamp++;
Sleep(50);
GameTool::CursorMove(0,0);
}
GameTool::ShowCursor();
cout << " 用时 " << timestamp - 1 << " tick。\n\n";
cout << "按 Q 键结束游戏。";
while(tolower(_getch()) != 'q');
return 0;
}《2D Minecraft》
-
源码下载地址:https://wwana.lanzoue.com/iK4iO45onecd
密码:yxnb注:下载完后把后缀名
.txt删掉,会自动变成.cpp。(需开启显示扩展名)