提示:所有游戏仅供 Windows 系统使用。
《僵尸庄园》
v1.0
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <queue>
#include <bitset>
using namespace std;
const int WIDTH = 1000;
const int HEIGHT = 1000;
const int RENDER_SIZE = (25 - 1) / 2;
const int LOSE_SCORE = -1;
const int WIN_SCORE = 100;
const char PORTAL_CHAR = 'O';
const int dx[] = {0,0,-1,1};
const int dy[] = {-1,1,0,0};
char map[HEIGHT + 5][WIDTH + 5];
bitset<WIDTH + 5> apple[HEIGHT + 5];
bitset<WIDTH + 5> zombie[HEIGHT + 5];
bool vis[HEIGHT + 5][WIDTH + 5];
int pl_x = HEIGHT / 2,pl_y = WIDTH / 2;
int score = 0,combo = 0;
int zombie_live = 0;
namespace GameTool{
double Euclid_dis(pair<int,int> pos1,pair<int,int> pos2){
int x1 = pos1.first,y1 = pos1.second;
int x2 = pos2.first,y2 = pos2.second;
double res = sqrt(double((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
return res;
}
void SlowOutput(string str,int speed = 50){
for(char ch : str){
cout << ch;
if(ch == ' ' || ch == '\n') continue;
Sleep(speed);
}
}
};
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(map[apple_x][apple_y] == PORTAL_CHAR) continue;
apple[apple_x][apple_y] = true;
}
}
void InitMap(){
for(int i = 1;i <= HEIGHT;i++){
for(int j = 1;j <= WIDTH;j++){
if(rand() % 10 == 0){
map[i][j] = '#';
} else{
map[i][j] = '.';
}
}
}
for(int i = 0;i <= HEIGHT + 1;i++){
map[i][0] = '#';
map[i][WIDTH + 1] = '#';
}
for(int i = 0;i <= WIDTH + 1;i++){
map[0][i] = '#';
map[HEIGHT + 1][i] = '#';
}
for(int i = 1;i <= HEIGHT * WIDTH / 100;i++){
int x = rand() % HEIGHT + 1;
int y = rand() % WIDTH + 1;
if(map[x][y] == '#') continue;
apple[x][y] = true;
}
}
void RenderScreen(){
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({pl_x,pl_y},{i,j}) > RENDER_SIZE){
cout << ' ';
} else if(i < 0 || i > HEIGHT + 1 || j < 0 || j > WIDTH + 1){
cout << ' ';
} else if(i == pl_x && j == pl_y){
cout << 'P';
} else if(apple[i][j]){
cout << 'A';
}else if(zombie[i][j]){
cout << 'Z';
} else{
cout << map[i][j];
}
cout << ' ';
}
cout << '\n';
}
cout << "\n - X 坐标:" << pl_x << " Y 坐标:" << pl_y << '\n';
cout << " - 积分: " << score << '\n';
cout << " - 连击数: " << combo << "\n\n";
cout << " - 存活僵尸数: " << zombie_live << "\n\n";
if(combo >= 3){
cout << " 系统赠送了你 " << floor(sqrt(combo - 2)) << " 积分\n";
}
cout << flush;
}
void SummonZombie(){
int limit_x = min(HEIGHT,pl_x + 30);
int limit_y = min(WIDTH,pl_y + 30);
int down_limit_x = max(1,pl_x - 30);
int down_limit_y = max(1,pl_y - 30);
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(map[randX][randY] == '#') return;
if(map[randX][randY] == PORTAL_CHAR) return;
zombie[randX][randY] = true;
}
void ZForwardToP(){
for(int i = max(1,pl_x - 30);i <= min(HEIGHT,pl_x + 30);i++){
for(int j = max(1,pl_y - 30);j <= min(WIDTH,pl_y + 30);j++){
vis[i][j] = false;
}
}
queue<BFSNode> q;
q.push({pl_x,pl_y,{-1,-1}});
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 < max(1,pl_x - 30) || x > min(HEIGHT,pl_x + 30) ||
y < max(1,pl_y - 30) || y > min(WIDTH,pl_y + 30)
){
continue;
}
if(vis[x][y]) continue;
vis[x][y] = true;
if(map[x][y] == '#') continue;
if(zombie[x][y]){
if(lastx != -1 && lasty != -1 && zombie[lastx][lasty] == false){
zombie[x][y] = false;
zombie[lastx][lasty] = true;
}
}
for(int i = 0;i < 4;i++){
int xx = x + dx[i];
int yy = y + dy[i];
q.push({xx,yy,{x,y}});
}
}
}
void HandleKey(){
if(_kbhit()){
char ch = _getch();
if(ch == 'a' && map[pl_x][pl_y - 1] != '#'){
pl_y--;
} else if(ch == 'd' && map[pl_x][pl_y + 1] != '#'){
pl_y++;
} else if(ch == 'w' && map[pl_x - 1][pl_y] != '#'){
pl_x--;
} else if(ch == 's' && map[pl_x + 1][pl_y] != '#'){
pl_x++;
} else if(ch == 'q'){
cout << "\n" << flush;
system("pause");
}
}
if(pl_y < 1) pl_y = 1;
if(pl_y > WIDTH) pl_y = WIDTH;
if(pl_x < 1) pl_x = 1;
if(pl_x > HEIGHT) pl_x = HEIGHT;
}
void EatApple(){
if(apple[pl_x][pl_y] == false){
return;
}
score++;
combo++;
if(combo >= 3){
score += floor(sqrt(combo - 2));
}
apple[pl_x][pl_y] = false;
for(int i = 1;i <= HEIGHT;i++){
for(int j = 1;j <= WIDTH;j++){
if(zombie[i][j]){
zombie[i][j] = false;
zombie_live = max(0,zombie_live - 1);
return;
}
}
}
}
void ZombieAttack(){
if(zombie[pl_x][pl_y]){
score -= 3;
combo = 0;
zombie_live--;
zombie[pl_x][pl_y] = false;
}
}
void HelloUI(){
using namespace GameTool;
SlowOutput("========== 欢迎来到 僵尸庄园v1.0 ==========\n\n");
SlowOutput(" 在这里,你需要收集地上的苹果,每个\n");
SlowOutput(" 苹果可以获得 1 积分。\n");
SlowOutput(" 初始状态你没有积分。\n\n");
SlowOutput(" 当你快速的吃掉 3 个以上的苹果,\n");
SlowOutput(" 系统将会奖励你积分,\n");
SlowOutput(" 数量以连击数量为准。\n");
SlowOutput(" 你开始会有 3s 的无敌时间。\n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.0 ==========\n\n",0);
SlowOutput(" 但是,庄园里的稻草人都变异了,\n");
SlowOutput(" 会变成僵尸追击你。\n\n");
SlowOutput(" 如果你不幸被他们碰到,\n");
SlowOutput(" 你将会失去 3 个积分。\n");
SlowOutput(" 但那只僵尸会消失不见。 \n\n");
SlowOutput(" 每当你吃下一个苹果,\n");
SlowOutput(" 将会随机抽选一只僵尸消失不见。\n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.0 ==========\n\n",0);
SlowOutput(" 当你在当前的情形中获得 100 积分\n");
SlowOutput(" 在整个庄园右下角将会出现一个出口,\n");
SlowOutput(" 当你进入这个出口,你就逃出了这个庄园。\n\n");
SlowOutput(" 若你积分小于 0,\n");
SlowOutput(" 你将消失不见并永远变成一个僵尸。\n\n");
SlowOutput(" 祝你好运。 Good Luck. \n\n");
cout << " - ";
system("pause & cls");
cout << "========== 游戏操作指南 ==========\n\n";
cout << " A 键:向左移动。\n";
cout << " D 键:向右移动。\n";
cout << " W 键:向上移动。\n";
cout << " S 键:向下移动。\n";
cout << " Q 键:暂停游戏。\n\n";
cout << " - ";
system("pause & cls");
SlowOutput("========== 游戏操作指南 ==========\n\n",0);
SlowOutput(" 庄园是一个 1000 * 1000 的正方形矩阵,\n");
SlowOutput(" '#' 代表墙,'.' 代表空地,\n");
SlowOutput(" 'A' 代表苹果,'Z' 代表僵尸。\n\n");
SlowOutput(" 庄园外围被一圈铁栏杆围着,你无法出入。\n\n");
SlowOutput(" - 准备好了吗?");
system("pause");
}
bool CheckResult(){
if(score <= LOSE_SCORE){
system("cls");
cout << "你被僵尸残忍杀害!" << flush;
return true;
}
if(score >= WIN_SCORE && map[1000][1000] != PORTAL_CHAR){
system("cls");
cout << "传送门已开启!";
for(int i = 998;i <= 1000;i++){
for(int j = 998;j <= 1000;j++){
map[i][j] = PORTAL_CHAR;
}
}
cout << "按 Q 键退出。" << flush;
while(_getch() != 'q');
}
if(map[pl_x][pl_y] == PORTAL_CHAR){
system("cls");
cout << "你成功逃出了庄园!" << flush;
return true;
}
return false;
}
int main(){
HelloUI();
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
srand(time(NULL));
system("color f0");
system("mode con cols=60 lines=35");
InitMap();
int timestamp = 1;
int last_score = 0;
while(true){
RenderScreen();
HandleKey();
EatApple();
if(timestamp % 100 == 0){
GenerateApple(10);
}
if(timestamp > 60){
ZombieAttack();
}
if(timestamp % 10 == 0){
ZForwardToP();
}
if(timestamp % 60 == 0){
SummonZombie();
zombie_live++;
}
if(timestamp % 60 == 0){
if(score <= last_score){
combo = 0;
} else{
last_score = score;
}
}
if(CheckResult()) break;
timestamp++;
Sleep(50);
system("cls");
}
int end = time(NULL);
cout << " 用时 " << (timestamp - 1) / 20 << " 秒。\n\n";
cout << "按 Q 键结束游戏。" << flush;
while(_getch() != 'q');
return 0;
}v1.0.2
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#include <queue>
#include <bitset>
using namespace std;
const int WIDTH = 300;
const int HEIGHT = 300;
int RENDER_SIZE = (19 - 1) / 2;
const int LOSE_SCORE = -1;
const int WIN_SCORE = 100;
const int dx[] = {0,0,-1,1};
const int dy[] = {-1,1,0,0};
char map[HEIGHT + 5][WIDTH + 5];
bitset<WIDTH + 5> apple[HEIGHT + 5];
bitset<WIDTH + 5> zombie[HEIGHT + 5];
bitset<WIDTH + 5> vis[HEIGHT + 5];
int pl_x = HEIGHT / 2,pl_y = WIDTH / 2;
int score = 0,combo = 0;
int zombie_live = 0;
enum Color{
BLACK = 0,BLUE = 1,GREEN = 2,CYAN = 3,
RED = 4,MAGENTA = 5,YELLOW = 6,WHITE = 7,
GRAY = 8,LIGHT_BLUE = 9,LIGHT_GREEN = 10,
LIGHT_CYAN = 11,LIGHT_RED = 12,LIGHT_MAGENTA = 13,
LIGHT_YELLOW = 14,BRIGHT_WHITE = 15
};
namespace GameTool{
double Euclid_dis(pair<int,int> pos1,pair<int,int> pos2){
int x1 = pos1.first,y1 = pos1.second;
int x2 = pos2.first,y2 = pos2.second;
double res = sqrt(double((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
return res;
}
void SlowOutput(string str,int speed = 50){
for(char ch : str){
cout << ch;
if(ch == ' ' || ch == '\n') continue;
Sleep(speed);
}
}
void CursorMove(int x,int y){
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {(SHORT)x,(SHORT)y};
SetConsoleCursorPosition(h,pos);
}
void HideCursor(){
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cinfo;
GetConsoleCursorInfo(h,&cinfo);
cinfo.bVisible = false;
SetConsoleCursorInfo(h,&cinfo);
}
void ShowCursor(){
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cinfo;
GetConsoleCursorInfo(h,&cinfo);
cinfo.bVisible = true;
SetConsoleCursorInfo(h,&cinfo);
}
void SetTextColor(int fg = RED,int bg = BRIGHT_WHITE){
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h,(bg << 4) | fg);
}
};
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(map[apple_x][apple_y] == 'O') continue;
apple[apple_x][apple_y] = true;
}
}
void InitMap(){
for(int i = 1;i <= HEIGHT;i++){
for(int j = 1;j <= WIDTH;j++){
if(rand() % 10 == 0){
map[i][j] = '#';
} else{
map[i][j] = '.';
}
}
}
for(int i = 0;i <= HEIGHT + 1;i++){
map[i][0] = '#';
map[i][WIDTH + 1] = '#';
}
for(int i = 0;i <= WIDTH + 1;i++){
map[0][i] = '#';
map[HEIGHT + 1][i] = '#';
}
GenerateApple(HEIGHT * WIDTH / 100);
}
void RenderScreen(){
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({pl_x,pl_y},{i,j}) > RENDER_SIZE){
cout << ' ';
} else if(i < 0 || i > HEIGHT + 1 || j < 0 || j > WIDTH + 1){
cout << ' ';
} else if(i == pl_x && j == pl_y){
GameTool::SetTextColor(GREEN);
cout << 'P';
GameTool::SetTextColor(BLACK);
} else if(apple[i][j]){
GameTool::SetTextColor(LIGHT_RED);
cout << 'A';
GameTool::SetTextColor(BLACK);
}else if(zombie[i][j]){
GameTool::SetTextColor(GREEN,RED);
cout << 'Z';
GameTool::SetTextColor(BLACK);
} else{
cout << map[i][j];
}
cout << ' ';
}
cout << '\n';
}
cout << "\n - X 坐标:" << pl_x << " Y 坐标:" << pl_y << '\n';
cout << " - 积分: " << score << '\n';
cout << " - 连击数: " << combo << "\n\n";
cout << " - 存活僵尸数: " << zombie_live << "\n\n";
if(combo >= 3){
cout << " 系统赠送了你 " << floor(sqrt(combo - 2)) << " 积分\n";
}
}
void SummonZombie(){
int limit_x = min(HEIGHT,pl_x + 30);
int limit_y = min(WIDTH,pl_y + 30);
int down_limit_x = max(1,pl_x - 30);
int down_limit_y = max(1,pl_y - 30);
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(map[randX][randY] == '#') return;
if(map[randX][randY] == 'O') return;
zombie[randX][randY] = true;
}
void ZForwardToP(){
for(int i = max(1,pl_x - 30);i <= min(HEIGHT,pl_x + 30);i++){
for(int j = max(1,pl_y - 30);j <= min(WIDTH,pl_y + 30);j++){
vis[i][j] = false;
}
}
queue<BFSNode> q;
q.push({pl_x,pl_y,{-1,-1}});
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 < max(1,pl_x - 30) || x > min(HEIGHT,pl_x + 30) ||
y < max(1,pl_y - 30) || y > min(WIDTH,pl_y + 30)
){
continue;
}
if(vis[x][y]) continue;
vis[x][y] = true;
if(map[x][y] == '#') continue;
if(zombie[x][y]){
if(lastx != -1 && lasty != -1 && zombie[lastx][lasty] == false){
zombie[x][y] = false;
zombie[lastx][lasty] = true;
}
}
for(int i = 0;i < 4;i++){
int xx = x + dx[i];
int yy = y + dy[i];
q.push({xx,yy,{x,y}});
}
}
}
void ChargeRenderSize(){
system("cls");
int r;
cout << " 注意:调整后请自行调整窗口大小。\n\n";
cout << "请输入新的渲染范围半径:" ;
cin >> r;
if(r < 4 || r > 50){
cout << "请勿设置不合理的渲染距离。\n";
system("pause");
return;
}
RENDER_SIZE = r;
system("cls");
}
void HandleKey(){
if(_kbhit()){
char ch = _getch();
ch = tolower(ch);
if(ch == 'a' && map[pl_x][pl_y - 1] != '#'){
pl_y--;
} else if(ch == 'd' && map[pl_x][pl_y + 1] != '#'){
pl_y++;
} else if(ch == 'w' && map[pl_x - 1][pl_y] != '#'){
pl_x--;
} else if(ch == 's' && map[pl_x + 1][pl_y] != '#'){
pl_x++;
} else if(ch == 'z'){
ChargeRenderSize();
} else if(ch == 'q'){
cout << '\n';
system("pause & cls");
}
}
}
void EatApple(){
if(apple[pl_x][pl_y] == false){
return;
}
score++;
combo++;
if(combo >= 3){
score += floor(sqrt(combo - 2));
}
apple[pl_x][pl_y] = false;
for(int i = 1;i <= HEIGHT;i++){
for(int j = 1;j <= WIDTH;j++){
if(zombie[i][j]){
zombie[i][j] = false;
zombie_live = max(0,zombie_live - 1);
return;
}
}
}
}
void ZombieAttack(){
if(zombie[pl_x][pl_y]){
score -= 3;
combo = 0;
zombie_live--;
zombie[pl_x][pl_y] = false;
}
}
void HelloUI(){
using namespace GameTool;
SlowOutput("========== 欢迎来到 僵尸庄园v1.0.2 ==========\n\n");
SlowOutput(" 在这里,你需要收集地上的苹果,每个\n");
SlowOutput(" 苹果可以获得 1 积分。\n");
SlowOutput(" 初始状态你没有积分。\n\n");
SlowOutput(" 当你快速的吃掉 3 个以上的苹果,\n");
SlowOutput(" 系统将会奖励你积分,\n");
SlowOutput(" 数量以连击数量为准。\n\n");
SlowOutput(" 你开始会有 3s 的无敌时间。\n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.0.2 ==========\n\n",0);
SlowOutput(" 但是,庄园里的稻草人都变异了,\n");
SlowOutput(" 会变成僵尸追击你。\n\n");
SlowOutput(" 如果你不幸被他们碰到,\n");
SlowOutput(" 你将会失去 3 个积分。\n");
SlowOutput(" 但那只僵尸会消失不见。 \n\n");
SlowOutput(" 每当你吃下一个苹果,\n");
SlowOutput(" 将会随机抽选一只僵尸消失不见。\n\n");
cout << " - ";
system("pause & cls");
SlowOutput("========== 欢迎来到 僵尸庄园v1.0.2 ==========\n\n",0);
SlowOutput(" 当你在当前的情形中获得 100 积分,\n");
SlowOutput(" 在整个庄园右下角将会出现一个出口,\n");
SlowOutput(" 当你进入这个出口,你就逃出了这个庄园。\n\n");
SlowOutput(" 若你积分小于 0,\n");
SlowOutput(" 你将消失不见并永远变成一个僵尸。\n\n");
SlowOutput(" 祝你好运。 Good Luck. \n\n");
cout << " - ";
system("pause & cls");
cout << "========== 更新公告 ==========\n\n";
cout << " 1. 优化了视觉,僵尸、玩家、苹果将\n 显示不同的颜色。\n";
cout << " 2. 修复了部分文案bug。 \n";
cout << " 3. 修复bug:锁定大写时无法移动。\n";
cout << " 4. 降低了游戏难度,地图大小从 1000 * 1000\n 变为 300 * 300。\n";
cout << " 5. 渲染范围可根据需求调整。\n\n";
cout << " - ";
system("pause & cls");
cout << "========== 游戏操作指南 ==========\n\n";
cout << " A 键:向左移动。\n";
cout << " D 键:向右移动。\n";
cout << " W 键:向上移动。\n";
cout << " S 键:向下移动。\n";
cout << " Z 键:调整渲染距离。\n";
cout << " Q 键:暂停游戏。\n\n";
cout << " - ";
system("pause & cls");
SlowOutput("========== 游戏操作指南 ==========\n\n",0);
SlowOutput(" 庄园是一个 300 * 300 的正方形矩阵,\n");
SlowOutput(" '#' 代表墙,'.' 代表空地,\n");
SlowOutput(" 'A' 代表苹果,'Z' 代表僵尸。\n\n");
SlowOutput(" 庄园外围被一圈铁栏杆围着,你无法出入。\n\n");
SlowOutput(" 注意:如果你的电脑上安装了联想电脑管家,\n");
SlowOutput(" 建议将其暂时关闭,因为这个软件可能导致终端卡顿。\n\n");
SlowOutput(" - 准备好了吗?");
system("pause & cls");
}
bool CheckResult(){
if(score <= LOSE_SCORE){
system("cls");
cout << "你被僵尸残忍杀害!";
return true;
}
if(score >= WIN_SCORE && map[HEIGHT][WIDTH] != 'O'){
system("cls");
cout << "传送门已开启!";
for(int i = HEIGHT - 2;i <= HEIGHT;i++){
for(int j = WIDTH - 2;j <= WIDTH;j++){
map[i][j] = 'O';
}
}
cout << "按 Q 键退出。";
while(_getch() != 'q');
}
if(map[pl_x][pl_y] == 'O'){
system("cls");
cout << "你成功逃出了庄园!";
return true;
}
return false;
}
int main(){
HelloUI();
srand(time(NULL));
system("color f0");
system("mode con cols=55 lines=35");
GameTool::HideCursor();
InitMap();
int timestamp = 1;
int last_score = 0;
while(true){
RenderScreen();
HandleKey();
EatApple();
if(timestamp % 100 == 0){
GenerateApple(10);
}
if(timestamp > 60){
ZombieAttack();
}
if(timestamp % 10 == 0){
ZForwardToP();
}
if(timestamp % 60 == 0){
SummonZombie();
zombie_live++;
}
if(timestamp % 60 == 0){
if(score <= last_score){
combo = 0;
} else{
last_score = score;
}
}
if(CheckResult()) break;
timestamp++;
Sleep(50);
GameTool::CursorMove(0,0);
}
GameTool::ShowCursor();
cout << " 用时 " << (timestamp - 1) / 20 << " 秒。\n\n";
cout << "按 Q 键结束游戏。";
while(_getch() != 'q');
return 0;
}