- C++
C++基础语法的游戏项目
- 2025-2-22 15:47:09 @
基于学生已掌握的C++基础语法,重新设计了一组更具剧情化和互动性的游戏项目,每个项目都包含隐藏彩蛋和成长系统:
- 魔法学院入学试炼(动态剧情选择)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int intelligence = 3, courage = 3, day = 1;
char choice;
cout << "欢迎来到霍格沃茨魔法学院!\n";
while(day <= 7 && intelligence > 0 && courage > 0) {
cout << "\n=== 第" << day << "天 ==="
<< "\n智慧:" << intelligence << " 勇气:" << courage
<< "\n遇到:1.图书馆 2.禁林 3.魔药课\n选择:";
cin >> choice;
int challenge = rand() % 3 + 1;
if(choice == '1') {
cout << (challenge == 1 ? "解开古代符文谜题!智慧+2" : "被平斯夫人训斥") << endl;
intelligence += (challenge == 1) ? 2 : -1;
} else if(choice == '2') {
cout << (courage > 2 ? "击退八眼巨蛛!勇气+3" : "受伤逃回") << endl;
courage += (courage > 2) ? 3 : -2;
} else {
cout << (rand()%2 ? "成功调制福灵剂" : "坩埚爆炸") << endl;
intelligence += (rand()%2) ? 1 : -1;
}
if(day == 4) {
cout << "\n★ 收到神秘地图!所有属性+1 ★\n";
intelligence++; courage++;
}
day++;
}
cout << "\n毕业成绩:"
<< (intelligence + courage > 10 ? "优秀巫师" : "还需努力")
<< endl;
return 0;
}
亮点:霍格沃茨剧情、属性成长系统、隐藏彩蛋
知识点:多条件嵌套、字符输入处理、三元运算符
- 恐龙乐园大冒险(生存建造类)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int food = 10, materials = 5, dinosaurs = 0;
char action;
cout << "欢迎来到侏罗纪乐园!\n";
for(int hour=9; hour<=18; hour++) {
cout << "\n=== " << hour << ":00 ==="
<< "\n食物:" << food << " 材料:" << materials
<< " 恐龙:" << dinosaurs
<< "\n行动:F觅食/B建造/D驯养 > ";
cin >> action;
switch(action) {
case 'F':
food += rand()%6 + 3;
cout << "采集到" << (food > 15 ? "超级浆果" : "蘑菇") << endl;
break;
case 'B':
if(materials >= 3) {
materials -= 3;
cout << "建造了" << (rand()%2 ? "防御墙" : "喂食站") << endl;
}
break;
case 'D':
if(food > 5 && dinosaurs < 3) {
food -= 5;
dinosaurs++;
cout << "驯服三角龙!" << endl;
}
break;
}
if(hour%3 == 0) {
int disaster = rand()%3;
cout << (disaster==0 ? "暴风雨!" : "恐龙暴动!") << "损失资源\n";
food -= 2; materials -= 1;
}
}
cout << "\n★★★ 最终评分:" << (dinosaurs*10 + materials*2 + food) << " ★★★\n";
return 0;
}
亮点:时间管理系统、随机灾难事件、建造策略
知识点:for循环控制、switch综合应用、资源管理
- 机器人足球杯(物理模拟竞技)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int scoreA = 0, scoreB = 0;
int power, angle;
cout << "机器人世界杯开赛!\n";
for(int round=1; round<=5; round++) {
cout << "\n=== 第" << round << "局 ==="
<< "\n当前比分 " << scoreA << " : " << scoreB
<< "\n输入击球力量(1-10)和角度(0-90):";
cin >> power >> angle;
bool isGoal = false;
int defense = rand()%10 + 1;
if(power > defense) {
int trajectory = (power * angle) / 10;
isGoal = (trajectory > 15 && trajectory < 85);
}
if(isGoal) {
cout << "★ 进球得分! ★\n";
scoreA++;
} else {
cout << (rand()%2 ? "射偏了!" : "被守门员拦截!") << endl;
if(rand()%3 == 0) scoreB++;
}
if(round == 3) {
cout << "\n★ 解锁超级射门!力量翻倍 ★\n";
power *= 2;
}
}
cout << "\n最终比分 " << scoreA << " : " << scoreB << endl;
return 0;
}
亮点:简易物理引擎、技能解锁系统、动态对抗
知识点:数学运算应用、逻辑运算符组合、循环控制
- 太空垃圾清理者(重力模拟游戏)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int debris = 20, energy = 100, time = 0;
char command;
cout << "地球轨道清洁行动开始!\n";
while(debris > 0 && energy > 0) {
cout << "\n剩余垃圾:" << debris << " 能量:" << energy
<< "\n操作:W前进/S收集/D维修 > ";
cin >> command;
int cost = 0;
switch(command) {
case 'W':
cost = 5;
debris -= rand()%3;
break;
case 'S':
cost = 8;
debris -= (energy > 30) ? 4 : 2;
break;
case 'D':
cost = 3;
energy += 15;
break;
}
energy -= cost;
time++;
if(time%4 == 0) {
cout << "引力波动!能量消耗加倍\n";
energy -= 10;
}
if(energy > 150) {
cout << "★ 能量超载!激活激光清扫 ★\n";
debris -= 5;
energy = 100;
}
}
cout << "\n任务" << (debris <=0 ? "成功" : "失败")
<< "!耗时" << time << "分钟\n";
return 0;
}
亮点:重力环境模拟、能量管理系统、紧急事件
知识点:状态机概念、复合运算、循环中断条件
教学升级方案:
-
难度调节技巧:
- 在魔法学院项目中添加「黑魔法防御课」分支
- 为足球游戏增加「风速干扰」变量
- 在太空项目中设计「太阳能充电」机制
-
跨项目联动:
- 用魔法学院属性解锁恐龙乐园的特殊建筑
- 太空项目的能量核心可提升足球机器人动力
- 收集所有项目的隐藏成就解锁终极彩蛋
-
竞技化改造:
- 双人模式:通过交替输入实现对战
- 添加实时积分排行榜功能
- 设计成就系统(例如连续5次正确解锁称号)
每个项目预留了3个扩展接口(用TODO注释标记),方便后续学习函数后升级。建议采用「项目盲盒」形式教学,让孩子通过试玩反推代码逻辑,培养逆向思维能力。
1 条评论
-
mrhowe SU @ 2025-2-22 16:38:51
#include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> // 用于实时输入 using namespace std; int main() { srand(time(0)); // 玩家属性 int hp = 20, attack = 3, score = 0; char player = 'P', dir; // 地牢地图(10x10) string map = "##########\n" "#........#\n" "#.E......#\n" "#....#...#\n" "#..E.....#\n" "#....#..E#\n" "#........#\n" "#...E....#\n" "#........#\n" "##########"; // 玩家初始位置 int px = 1, py = 1; bool running = true; cout << "WSAD移动,J攻击,Q退出\n"; while(running && hp > 0) { system("cls"); // 清屏 // 显示地图和状态 cout << "生命值:" << hp << " 攻击力:" << attack << " 分数:" << score << endl; for(int i=0; i<map.size(); i++){ if(i/10 == py && i%10 == px) cout << player; else cout << map[i]; } // 实时输入 if(_kbhit()){ dir = _getch(); int newX = px, newY = py; // 移动处理 switch(toupper(dir)){ case 'W': newY--; break; case 'S': newY++; break; case 'A': newX--; break; case 'D': newX++; break; case 'J': // 攻击 for(int dy=-1; dy<=1; dy++) for(int dx=-1; dx<=1; dx++) if(map[(py+dy)*10 + px+dx] == 'E'){ map[(py+dy)*10 + px+dx] = '.'; score += 10; if(rand()%4 == 0) hp += 2; // 25%概率掉落治疗 } break; case 'Q': running = false; } // 碰撞检测 if(map[newY*10 + newX] != '#' && map[newY*10 + newX] != 'E'){ px = newX; py = newY; } } // 敌人随机移动 for(int i=0; i<map.size(); i++){ if(map[i] == 'E' && rand()%3 == 0){ // 33%概率移动 int move = rand()%4; int target = i + (move==0?-1 : move==1?1 : move==2?-10 : 10); if(map[target] == '.'){ map[target] = 'E'; map[i] = '.'; } } } // 敌人攻击检测 for(int i=0; i<map.size(); i++){ if(map[i] == 'E'){ int ex = i%10, ey = i/10; if(abs(ex-px) <=1 && abs(ey-py) <=1){ hp -= 1 + rand()%2; } } } // 随机生成新敌人 if(rand()%10 == 0 && score > 30){ int pos = rand()%60 + 11; if(map[pos] == '.') map[pos] = 'E'; } } cout << "\n游戏结束!最终得分:" << score << endl; return 0; }
- 1