C++实现小游戏

目录

1. 打怪小游戏

2. 推箱子小游戏

 3.2048

 此文为C++小游戏更新第一期,关注我的主页,查看后面的更新

在此之前,大家可以看看哔哩哔哩的火山哥哥!里面有很多有意思的单机游戏,值得推荐!

1. 打怪小游戏

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;

double shanghai[20] = {0.6, 1.1, 2, 3.16, 5.5, 7, 10, 20, 50, 100, 146.23, 254.13, 312, 403, 601, 1023};

double bosshealth[20] = {2, 3, 4, 5.9, 8, 14, 19, 32, 73, 157, 200, 403, 801, 1200, 3630, 20123};
double wj_shanghai = 1, wj_health = 10, wj_max_health = 10, boss, wj_money = 0;
void chushihua();
void game();
void gongji();
void goumai();
void shangdian();
void zhujiemian();
void fangyu();
void cend();
void chushou();
void print(char[]);
int bishou = 0, caidao = 0, jian = 0, shenjian = 0;
double bishou_1 = 5, caidao_1 = 17, jian_1 = 58, shenjian_1 = 123.1;
int hat = 0, douhui = 0, hudun = 0, hunjia = 0, shendun = 0;
double hat_1 = 7, douhui_1 = 21, hudun_1 = 49, hunjia_1 = 89, shendun_1 = 210.4;

void cend() {
	system("cls");
	print("GAME OVER");
	exit(1);
}

void game() {
	int k;
	chushihua();
IO:
	printf("请输入对手等级 (0~15)\n");
	scanf("%d", &k);
	if (k > 15 || k < 0) {
		system("cls");
		goto IO;
	}
	boss = bosshealth[k];
	system("cls");
	while (wj_health >= 0) {
		srand(time(NULL));
	QP:
		printf("1.逃跑 2.进攻\n");
		char s = getch();
		if (s < '1' || s > '2') {
			system("cls");
			goto QP;
		}
		if (s == '1') {
			system("cls");
			zhujiemian();
		}
		system("cls");
		double l = shanghai[k] * ((rand() % 2) + 1) + fabs(double(rand() % 100 / 100 - 2));
		printf("对手对你造成了%lf点伤害\n", l);
		wj_health -= l;
		printf("你当前剩余血量:%lf\n", wj_health);
		if (wj_health <= 0)
			cend();
		double o = wj_shanghai * ((rand() % 2) + 1) + double(rand() % 10 / 10);
		boss -= o;
		printf("你对对手造成了%lf点伤害\n", o);
		printf("对手当前剩余血量:%lf\n\n", boss);
		if (boss <= 0) {
			printf("胜利!\n获得%lf金币\n\n当前剩余血量:%lf\n", shanghai[k] + 3, wj_health);
			wj_money += shanghai[k] + 3;
			printf("\n余额:%lf\n", wj_money);
			getch();
			if (k == 15) {
				printf("恭喜玩家!游戏胜利!\n");
				getch();
				exit(1);
			}
			system("cls");
			zhujiemian();
		}
	}
}

void zhujiemian() {
PO:
	printf("1.商店 2.战斗 3.回血 4.状态\n");
	char k = getch();
	if (k > '4' || k < '1') {
		system("cls");
		goto PO;
	}
	if (k == '1') {
		system("cls");
		shangdian();
		return;
	}
	if (k == '2') {
		system("cls");
		game();
		return;
	}
	if (k == '3') {
		system("cls");
		if (wj_money > 0) {
			wj_money = wj_money * 4 / 5 - 1;
			chushihua();
			wj_health = wj_max_health;
			printf("回血成功!\n");
			getch();
			system("cls");
			goto PO;
		} else {
			printf("余额不足!\n");
			getch();
			system("cls");
			goto PO;
		}
	}
	if (k == '4') {
		chushihua();
		system("cls");
		printf("生命值:%lf\n", wj_health);
		printf("最大生命值:%lf\n", wj_max_health);
		printf("攻击力:%lf\n", wj_shanghai);
		printf("金币:%lf\n", wj_money);
		getch();
		system("cls");
		goto PO;
	}
	if (k == '5') {
		string a;
		system("cls");
		printf("输入密码!\n");
		cin >> a;
		if (a == "songyudashuaibi" || a == "PI") {
			wj_money += 1000;
			printf("外挂生效\n");
			Sleep(1000);
			system("cls");
			goto PO;
		}
		printf("外挂失败\n");
		Sleep(1000);
		system("cls");
		goto PO;
	}
}

void shangdian() {
LK:
	printf("1.购买 2.返回主界面\n");
	char k = getch();
	if (k != '1' && k != '2') {
		system("cls");
		goto LK;
	}
	if (k == '1') {
		system("cls");
		goumai();
		goto LK;
	}
	if (k == '2') {
		system("cls");
		zhujiemian();
		return;
	}
}

void goumai() {
ML:
	printf("1.攻击 2.防御 3.返回主界面\n");
	char k = getch();
	if (k != '1' && k != '2' && k != '3') {
		system("cls");
		goto ML;
	}
	if (k == '1') {
		system("cls");
		gongji();
		goto ML;
	}
	if (k == '3') {
		system("cls");
		zhujiemian();
		return;
	}
	if (k == '2') {
		fangyu();
	}
}

void gongji() {
OP:
	system("cls");
	printf("0.返回上界面\n");
	printf("1.返回主界面\n");
	printf("2.匕首 5金币\n");
	printf("3.菜刀 17金币\n");
	printf("4.剑 68金币\n");
	printf("5.圣剑 210金币\n");
	printf("提醒:金币价格与伤害成正比\n");
	char k = getch();
	if (k < '0' || k > '5') {
		system("cls");
		goto OP;
	
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C++ 贪吃蛇小游戏实现示例: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN}; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); //清屏 for (int i = 0; i < width+2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width+2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x > width || x < 0 || y > height || y < 0) gameOver = true; for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); // 控制游戏速度 } return 0; } ``` 这是一个基本的贪吃蛇小游戏实现,其中用到了数组存储尾巴的坐标,用枚举变量表示贪吃蛇移动的方向。通过控制台输入和输出来实现游戏的交互和界面显示,同时利用 `Sleep()` 函数来控制游戏的速度。 运行程序后,使用键盘上的 WASD 键来控制贪吃蛇的移动,X 键退出游戏。每吃到一个水果得分加 10 分,当贪吃蛇碰到墙壁或自己的身体时游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值