这里我制作了一个可以使用Dev-C++进行编译运行的小游戏。
目录
1. 游戏介绍
2. 游戏代码
3. 运行效果
游戏介绍
只是一款很简单的小游戏……
不过只有2.8mb左右,十分便携。(不是傻子都知道)
规则
游戏规则:
a) 用a与d左右移动 "i" 小人;
b) 躲避 "#" 障碍;
c) 获得 "$" 金钱, 一次100个金钱;
d) 碰到障碍后, 可以使用金钱来复活;
e) 中途可以按下空格键暂停.
具体规则请见游戏。
游戏代码
不消说,直接上代码……
#include<iostream>
#include<Windows.h>
#include<cstdlib>
#include<conio.h>
using namespace std;
#define textcolor(color) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color)//宏定义字体颜色效果
#define black 0
#define blue 1
#define green 2
#define lakeblue 3
#define red 4
#define purple 5
#define yellow 6
#define white 7
#define grey 8
#define lightblue 9
#define lightgreen 10
#define lightgreen_ 11
#define lightred 12
#define lightpurple 13
#define lightyellow 14
void home();//主页
void game();//游戏
void lose();//失败
void ifmtn();//说明
string mp[20]=
{
" ",
" ### # # ",
" ### #$# ",
" ### #$# ",
" #### $ # ",
" ### ## ",
" ## ## ",
" ## # ## ",
" # ##$## ",
" ## # # ",
" ## # ",
" ## # # # ",
" ## # #$# ",
" ## $ # # ",
" ## # # ",
" # # #$ # ",
" # # ",
" # # ",
" # # ",
" # # ",
};//地图(#障碍)
int x,y,money,stp,rgm,cntOFrg;//x和y坐标、金钱、步数、复活时花费的金额、复活次数
void home(){
textcolor(white);//设置字体为白色
money=stp=cntOFrg=0;//初始化金钱和步数
system("cls");//清屏
system("title 躲避障碍 - 主页");//设置标题
textcolor(lakeblue);
cout<<"▲▼▲▼▲▼★躲★避★障★碍★▼▲▼▲▼▲"<<endl;
textcolor(red);
cout<<"----------按下合适的按键进行操作----------"<<endl;
textcolor(white);
cout<<"1\t开始游戏"<<endl;
cout<<"2\t游戏规则"<<endl;
switch(getch()){//用switch查找按键内容
case '1':
game();
break;
case '2':
ifmtn();
break;
default:
cout<<"无效按键。"<<endl;
Sleep(100);//暂停程序0.1秒
home();
}
}
void game(){
textcolor(white);
x=5,y=18;//初始化x和y坐标
system("title 躲避障碍 - 游戏进行中...");
gm:
system("cls");
textcolor(green);
cout<<"目前金钱:"<<money<<endl;//输出金钱
textcolor(blue);
cout<<"目前步数:"<<stp<<endl;//输出步数(移动的次数)
for(int i=1;i<19;i++){
for(int j=0;j<16;j++){
if(x==j&&y==i){//找到当前定位
textcolor(yellow);
cout<<"i";//输出当前位置
}
else{
textcolor(white);
cout<<mp[i][j];//输出地图
}
}
cout<<endl;
}
if(kbhit()){//按下按键
switch(getch()){
case 'a':
case 'A'://左移
if(x>1)//x可以左移
--x;//左移
break;
case 'd':
case 'D'://右移
if(x<9)//x可以右移
++x;//右移
break;
case ' '://暂停
textcolor(white);
cout<<endl<<"已暂停。"<<endl;
cout<<"请按任意键继续游戏..."<<endl;
getch();
break;
}
}
if(mp[y][x]=='$'){//碰到金钱
money+=100;
textcolor(green);
cout<<"获得100个金钱!"<<endl;
}
if(mp[y][x]=='#'){//碰到障碍
lose();
}
--y;//每一次都要上移
if(y<2){//y达到极限
y=18;//y恢复初始值
goto gm;
}
money+=20,++stp;//增加金钱和步数
Sleep(200);
goto gm;//回到gm
}
void ifmtn(){
textcolor(white);
system("title 躲避障碍 - 规则");
system("cls");
textcolor(lakeblue);
cout<<"规则"<<endl;
textcolor(white);
cout<<"a) 用a与d左右移动 \"i\" 小人; "<<endl;
cout<<"b) 躲避 \"#\" 障碍; "<<endl;
cout<<"c) 获得 \"$\" 金钱, 一次100个金钱; "<<endl;
cout<<"d) 碰到障碍后, 可以使用金钱来复活; "<<endl;
cout<<"e) 中途可以按下空格键暂停. "<<endl;
Sleep(1000);
cout<<endl<<"按任意键关闭此页面...";
getch();
home();
}
void lose(){
textcolor(white);
system("title 躲避障碍 - 失败");
system("cls");
rgm=450+stp/3*20;//计算复活需要的金钱(复活金钱数=450+下取整(步数÷3)×20)
textcolor(red);
cout<<"你碰到了障碍,失败了!"<<endl;
cout<<"是否花费"<<rgm<<"金钱复活?(是:1,否:2)"<<endl;
cout<<"剩余金钱:"<<money<<endl;
Sleep(1000);
for(int i=10;i>=0;i--){//倒计时
system("cls");
cout<<"你碰到了障碍,失败了!"<<endl;
cout<<"是否花费"<<rgm<<"金钱复活?(是:1,否:2)"<<endl;
cout<<"剩余金钱:"<<money<<endl;
cout<<"!!!!! 倒计时剩余 "<<i<<" 秒 !!!!!"<<endl;
Sleep(1000);
if(kbhit()){
switch(getch()){
case '1'://选择了复活
if(money>=rgm){//支持复活
money-=rgm;
system("cls");
textcolor(white);
cout<<"复活成功!剩余金钱:"<<money<<endl;
cout<<"花费的金钱:"<<rgm<<endl;
++cntOFrg;//复活次数加1次
Sleep(1000);
cout<<endl<<"按任意键关闭此页面...";
getch();
game();//复活成功返回游戏
}
else{//不支持复活
system("cls");
cout<<"金钱不足!剩余金钱:"<<money<<endl;
cout<<"复活所需要的金钱:"<<rgm<<endl;
Sleep(1000);
textcolor(white);
cout<<endl<<"按任意键关闭此页面...";
getch();
system("cls");
textcolor(lightgreen);
cout<<"----------游戏结果----------"<<endl;
textcolor(green);
cout<<">金钱数:"<<money<<endl;
textcolor(blue);
cout<<">总步数:"<<stp<<endl;
textcolor(lightred);
cout<<">复活数:"<<cntOFrg<<endl;
cout<<endl<<"按任意键关闭此页面...";
getch();
home();
}
break;
case '2'://选择不复活
system("cls");
cout<<"你碰到了障碍,失败了!(你选择了不复活)"<<endl;
cout<<endl<<"按任意键关闭此页面...";
getch();
system("cls");
textcolor(lightgreen);
cout<<"----------游戏结果----------"<<endl;
textcolor(green);
cout<<">金钱数:"<<money<<endl;
textcolor(blue);
cout<<">总步数:"<<stp<<endl;
textcolor(lightred);
cout<<">复活数:"<<cntOFrg<<endl;
cout<<endl<<"按任意键关闭此页面...";
getch();
home();
break;
}
}
}
textcolor(white);
cout<<endl<<"按任意键关闭此页面...";//未选择任何内容
getch();
system("cls");
textcolor(lightgreen);
cout<<"----------游戏结果----------"<<endl;
textcolor(green);
cout<<">金钱数:"<<money<<endl;
textcolor(blue);
cout<<">总步数:"<<stp<<endl;
textcolor(lightred);
cout<<">复活数:"<<cntOFrg<<endl;
cout<<endl<<"按任意键关闭此页面...";
getch();
home();
}
int main(){
home();
return 0;
}