#include <iostream>
#include<fstream>
#include <ctime>
#include <cmath>
#include <stdlib.h>
#include<stdio.h> //时间 //文件
#include <string>
#define random(x)(rand()%x)
using namespace std;
void thunder(int Dif,int Row,int Column,char *USer)
{
int r,c,alls[22][22],backstage[22][22]={0};
srand((int)time(0));
for(r=1;r<=Row;r++) // 生成alls(0~1)1是雷
{
for(c=1;c<=Column;c++)
{
if(random(6)<1) {alls[r][c]=1;} else{alls[r][c]=0;};
}
};
for(r=0;r<=Row+1;r++) //生成 backstage(正确答案)
{
for(int c=0;c<=Column+1;c++)
{
if(alls[r][c]==1)
{
backstage[r][c]='*'; //将1变为 * 代表雷
}
else
{
for(int i=r-1;i<=r+1;i++) //将0变为数字 (代表周围雷数)
for(int j=c-1;j<=c+1;j++)
{
if(alls[i][j]!=alls[r][c]&&alls[i][j]==1){backstage[r][c]++;};
}
}; //else 结束
}; // for 结束
}; // for 结束
cout<<"======================*********================================"<<endl;
char surface[22][22]; //生成surface(用户界面)
for(r=0;r<22;r++) //全部为零
for(c=0;c<22;c++)
{
surface[r][c]='0';
}
for(r=1;r<=Row;r++) //中间化 # 形成0包围#的形式 (通过数 #-->(*||数字) 的个数 赢的时候停止循环)
for(c=1;c<=Column;c++)
{
surface[r][c]='#';
}
for(r=1;r<=Row;r++) //输出 surface 界面 便于检查
{
for(c=1;c<=Column;c++) {cout<<" "<<surface[r][c];};
cout<<endl;
};
cout<<"请按格式输入"<<endl
<<"前两个数字为坐标,最后一个数字“1”表示此位置为雷,“0”则表示不是。"<<endl
<<"如:1 3 1 表示一行三列是雷;2 4 0 表示二行四列不是雷"<<endl
<<"提示:当数字周围雷都被扫出时,可再次按要求输入此位置,可得到周围数字。"<<endl;
long i=10000000L; //计算时间开始
clock_t start,finish;
double duration;
start=clock();
while(i--); //计算时间开始
int num=Row*Column; //计算#号个数
while(num!=0) //控制 是否点完所有位置
{
int x,y,judge;
cin>>x>>y>>judge;
if(alls[x][y]!=judge)
{
cout<<"you lose!!!"<<endl;
cout<<"The answer is:"<<endl;
for(r=1;r<=Row;r++) //输了 输出backstage 显示正确答案
{
for(int c=1;c<=Column;c++)
{
cout<<" "<<(char)(backstage[r][c]==42?backstage[r][c]:backstage[r][c]+'0'); //输出backstage
}
cout<<endl;
}
break;
}
else
{
if(alls[x][y]==1) {if(surface[x][y]=='#'){num--;}surface[x][y]='@'; } // 雷 判断正确 显示“@”;数“#”
else
{
if(backstage[x][y]!=0) // 数字 判断正确 显示数字
{
if(surface[x][y]=='#'){num--; surface[x][y]=backstage[x][y]+'0'; } // 数“#”
else
{
int lei_num=0;
for(int i=x-1;i<=x+1;i++) //数 数字周围 雷的个数
for(int j=y-1;j<=y+1;j++)
{
if(surface[i][j]=='@')
lei_num++;
}
if(backstage[x][y]==lei_num) // 看数字周围雷是否全部扫出 提示 显示数字周围
{
for(int i=x-1;i<=x+1;i++)
for(int j=y-1;j<=y+1;j++)
if(surface[i][j]=='#') //数“#”
{
surface[i][j]=backstage[i][j]+'0';
num--;
}
}
}
}
else // 数字为零时 显示零周围的零
{
if(surface[x][y]=='#'){num--;}; //数“#”
surface[x][y]=backstage[x][y]+'0';
for(int i=x-1;i<=x+1;i++) // 显示零周围的数字
for(int j=y-1;j<=y+1;j++)
if(surface[i][j]=='#') // 避免 死循环
{
surface[i][j]=backstage[i][j]+'0';
num--; //数“#”
}
for(int k=0;k<20;k++) //最多20层零 (点出最边上的零)
{
for (int R=1;R<=Row;R++) //检查所有零
for(int C=1;C<=Column;C++) //再次显示零周围的数字
{
if(surface[R][C]=='0')
{
for(int i=R-1;i<=R+1;i++)
for(int j=C-1;j<=C+1;j++)
{
if(surface[i][j]=='#') // 避免 死循环 数“#”
{
surface[i][j]=backstage[i][j]+'0';
num--;
}
}
}
} //匹配for 内
} //匹配 for 外
}//匹配else
}//匹配else
}//匹配els
cout<<endl;
cout<<"======================*********================================"<<endl;
for(r=1;r<=Row;r++) //输出界面(已修改)
{
for(c=1;c<=Column;c++) {cout<<" "<<surface[r][c];};
cout<<endl;
};
} //匹配while
finish=clock(); //计算时间结束
duration=(double)(finish-start)/CLOCKS_PER_SEC; //时间变量
if(num==0) //所有
{
cout<<" You win! Congratulations!! "<<endl;
cout<<" Your time is: "<<duration<<endl;
if(Dif==1) //读取 简单扫雷 的存储文件
{
string Name;
string name[6];
double Time,rang;
double times[6];
int i=0;
ifstream inf("扫雷 简单.txt");
for(i=0;i<5;i++) //文件中信息导入到数组里
{
inf>>Name;inf>>rang>>Time;
name[i]=Name;
times[i]=Time;
}
inf.close();
name[5]=USer; //本轮玩家信息
times[5]=duration;
double t1=0;
string t2;
for(int j=0;j<5;j++) //冒泡排序法
{
for(i=0;i<5-j;i++)
{
if(times[i]>times[i+1])
{
t1=times[i];
times[i]=times[i+1];
times[i+1]=t1;
t2=name[i];
name[i]=name[i+1];
name[i+1]=t2;
}
}
}
ofstream outf("扫雷 简单.txt");
for(i=0;i<5;i++) //将前五名玩家信息存储到文件中
{
cout<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
outf<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
}
outf.close();
}
if(Dif==2) //读取 一般扫雷 的存储文件
{
string Name;
string name[6];
double Time,rang;
double times[6];
int i=0;
ifstream inf("扫雷 一般.txt");
for(i=0;i<5;i++) //文件中信息导入到数组里
{
inf>>Name;inf>>rang>>Time;
name[i]=Name;
times[i]=Time;
}
inf.close();
name[5]=USer; //本轮玩家信息
times[5]=duration;
double t1=0;
string t2;
for(int j=0;j<5;j++) //冒泡排序法
{
for(i=0;i<5-j;i++)
{
if(times[i]>times[i+1])
{
t1=times[i];
times[i]=times[i+1];
times[i+1]=t1;
t2=name[i];
name[i]=name[i+1];
name[i+1]=t2;
}
}
}
ofstream outf("扫雷 一般.txt");
for(i=0;i<5;i++) //将前五名玩家信息存储到文件中 并输出
{
cout<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
outf<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
}
outf.close();
}
if(Dif==3) //读取 困难扫雷 的存储文件
{
string Name;
string name[6];
double Time,rang;
double times[6];
int i=0;
ifstream inf("扫雷 困难.txt");
for(i=0;i<5;i++) //文件中信息导入到数组里
{
inf>>Name;inf>>rang>>Time;
name[i]=Name;
times[i]=Time;
}
inf.close();
name[5]=USer; //本轮玩家信息
times[5]=duration;
double t1=0;
string t2;
for(int j=0;j<5;j++) //冒泡排序法
{
for(i=0;i<5-j;i++)
{
if(times[i]>times[i+1])
{
t1=times[i];
times[i]=times[i+1];
times[i+1]=t1;
t2=name[i];
name[i]=name[i+1];
name[i+1]=t2;
}
}
}
ofstream outf("扫雷 困难.txt");
for(i=0;i<5;i++) //将前五名玩家信息存储到文件中
{
cout<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
outf<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
}
outf.close();
}
}
}
void scale(int dif,char *User) //选择难度
{
int row,column;
if(dif==1) {row=3;column=3;}
if(dif==2) {row=7;column=7;}
if(dif==3) {row=10;column=10;}
cout<<"The scale is: "<<row<<"*"<<column<<endl;
thunder(dif,row,column,User);
};
int main()
{
int Continue=1;
int difficulty;
char user[10];
cout<<" Welcom to the game! "<<endl
<<" 请输入用户名! "<<endl;
cin>>user;
while(Continue==1)
{
cout<<"=======================*******************======================="<<endl
<<" 请选择难度! "<<endl
<<" 简单——1 "<<endl
<<" 一般——2 "<<endl
<<" 困难——3 "<<endl;
cin>>difficulty;
scale(difficulty,user);
cout<<"继续游戏——1 结束游戏——0"<<endl;
cin>>Continue;
}
return 0;
}
扫雷游戏
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
int food[2] = { 9, 9 };//初始食物坐标
int snake[1000][2];//蛇身坐标
int length = 1;//初始蛇长
int headX, headY;//蛇头坐标
int speed = 500;//游戏难度
int score = 0;//分数
int level = 1;//难度等级
string name;//玩家姓名
void gotoxy(short x, short y);//移动光标
int setdirection(int x);//确定方向变量
void changesnake(int x);//改变蛇身坐标
void ifchangefood();//判断蛇是否吃到食物
void makefood();//创造新食物
bool judgelife();//判断蛇是否存活
void drawsnake();//画蛇
void drawfood();//画食物
void drawwall();//画墙
void drawscore();//画数据
void draw();//绘图
int main()
{
SetConsoleTitle("贪吃蛇游戏");
int po = 2;//初始方向变量
snake[0][0] = 7;
snake[0][1] = 7;//初始蛇头坐标
headX = snake[0][0];
headY = snake[0][1];
gotoxy(30, 7);
cout << "欢迎来到贪吃蛇游戏";
gotoxy(30, 11);
cout << "请输入你的姓名:";
cin >> name;
system("cls");
gotoxy(30, 7);
cout << "游戏控制方式:";
gotoxy(30, 9);
cout << "W键:向上 S键:向下";
gotoxy(30, 11);
cout << "A键:向左 D键:向右";
gotoxy(30, 13);
cout << "空格键:暂停";
gotoxy(30, 15);
cout << "将游戏窗口最大化之后";
gotoxy(30, 17);
cout << "按回车键开始游戏...";
cin.get();
cin.get();
system("cls");
while (true)
{
po = setdirection(po);
system("cls");
changesnake(po);
ifchangefood();
if (!judgelife())
break;
draw();
Sleep(speed);
}
gotoxy(30, 10);
cout << "Game Over!!!";
Sleep(2000);
gotoxy(28, 12);
system("pause");
return 0;
}
void gotoxy(short x, short y)
{
COORD position = { x, y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
}
int setdirection(int x)
{
char ch;
if (_kbhit())
{
ch = _getch();
switch (ch)
{
case 'w':
x = 1;
break;
case 's':
x = 2;
break;
case 'a':
x = 3;
break;
case 'd':
x = 4;
break;
case ' ':
gotoxy(37, 16);
cout << "游 戏 暂 停. . .";
gotoxy(37, 18);
system("pause");
break;
default:
break;
}
}
return x;
}
void changesnake(int x)
{
switch (x)
{
case 1:
headY -= 1;
break;
case 2:
headY += 1;
break;
case 3:
headX -= 1;
break;
case 4:
headX += 1;
break;
default:
break;
}
for (int i = length; i > 0; --i)
{
for (int j = 0; j < 2; ++j)
{
snake[i][j] = snake[i - 1][j];
}
}
snake[0][0] = headX;
snake[0][1] = headY;
}
void ifchangefood()
{
if (snake[0][0] == food[0] && snake[0][1] == food[1])
{
length++;
makefood();
++score;
if (length > 5)
{
speed = 450;
level = 2;
}
if (length > 10)
{
speed = 400;
level = 3;
}
if (length > 15)
{
speed = 350;
level = 4;
}
if (length > 20)
{
speed = 300;
level = 5;
}
if (length > 25)
{
speed = 250;
level = 6;
}
if (length > 30)
{
speed = 200;
level = 7;
}
if (length > 35)
{
speed = 150;
level = 8;
}
if (length > 40)
{
speed = 100;
level = 9;
}
if (length > 45)
{
speed = 50;
level = 10;
}
}
}
void makefood()
{
srand((unsigned)time(NULL));
food[0] = rand() % 30 + 2;
food[1] = rand() % 30 + 4;
for (int m = 0; m < length; ++m)
{
if (food[0] == snake[m][0] && food[1] == snake[m][1])
{
makefood();
break;
}
}
}
bool judgelife()
{
for (int x = 1; x < length; ++x)
{
if (headX == snake[x][0] && headY == snake[x][1])
{
return false;
}
}
if (headX < 1 || headY < 3 || headX > 34 || headY > 34)
return false;
else
return true;
}
void drawsnake()
{
gotoxy(snake[0][0], snake[0][1]);
cout << "@";
for (int n = 1; n < length; ++n)
{
gotoxy(snake[n][0], snake[n][1]);
cout << "#";
}
}
void drawfood()
{
gotoxy(food[0], food[1]);
cout << "$";
}
void drawwall()
{
gotoxy(0, 0);
cout << "------------------------------------";
gotoxy(16, 1);
cout << "贪吃蛇";
gotoxy(0, 2);
cout << "++++++++++++++++++++++++++++++++++++";
gotoxy(0, 35);
cout << "------------------------------------";
for (int x = 0; x < 35; ++x)
{
gotoxy(0, x);
cout << "|";
gotoxy(35, x);
cout << "|";
}
}
void drawscore()
{
gotoxy(37, 10);
cout << "分数:" << score;
gotoxy(37, 12);
cout << "等级:" << level;
gotoxy(37, 14);
cout << "玩家姓名:" << name;
}
void draw()
{
drawsnake();
drawfood();
drawwall();
drawscore();
}
贪吃蛇
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
//武器类
class Weapon{
protected:
int Price; //武器价格
int ATK; //武器攻击力
string Name; //武器名称
string Rank; //武器等级
public:
Weapon(int price=0,int atk=0,string name="",string rank=""){Price=price;ATK=atk;Name=name;Rank=rank;}
int GetPrice(){return Price;}
int GetATK(){return ATK;}
string GetName(){return Name;}
string GetRank(){return Rank;}
virtual ~Weapon(){}
};
//战士武器类
class WarriorWeapon:public Weapon{
public:
WarriorWeapon(int price=0,int atk=0,string name="",string rank=""):Weapon(price,atk,name,rank){}
~WarriorWeapon(){}
};
class WarriorWeapon1:public WarriorWeapon{
public:
WarriorWeapon1(int price=500,int atk=70,string name="巨剑",string rank="白银"):WarriorWeapon(price,atk,name,rank){}
~WarriorWeapon1(){}
};
class WarriorWeapon2:public WarriorWeapon{
public:
WarriorWeapon2(int price=1000,int atk=130,string name="巨剑",string rank="黄金"):WarriorWeapon(price,atk,name,rank){}
~WarriorWeapon2(){}
};
class WarriorWeapon3:public WarriorWeapon{
public:
WarriorWeapon3(int price=1800,int atk=220,string name="巨剑",string rank="白金"):WarriorWeapon(price,atk,name,rank){}
~WarriorWeapon3(){}
};
class WarriorWeapon4:public WarriorWeapon{
public:
WarriorWeapon4(int price=3000,int atk=350,string name="巨剑",string rank="钻石"):WarriorWeapon(price,atk,name,rank){}
~WarriorWeapon4(){}
};
//法师武器类
class WizardWeapon:public Weapon{
public:
WizardWeapon(int price=0,int atk=0,string name="",string rank=""):Weapon(price,atk,name,rank){}
~WizardWeapon(){}
};
class WizardWeapon1:public WizardWeapon{
public:
WizardWeapon1(int price=400,int atk=50,string name="法杖",string rank="白银"):WizardWeapon(price,atk,name,rank){}
~WizardWeapon1(){}
};
class WizardWeapon2:public WizardWeapon{
public:
WizardWeapon2(int price=800,int atk=100,string name="法杖",string rank="黄金"):WizardWeapon(price,atk,name,rank){}
~WizardWeapon2(){}
};
class WizardWeapon3:public WizardWeapon{
public:
WizardWeapon3(int price=1500,int atk=180,string name="法杖",string rank="白金"):WizardWeapon(price,atk,name,rank){}
~WizardWeapon3(){}
};
class WizardWeapon4:public WizardWeapon{
public:
WizardWeapon4(int price=2500,int atk=300,string name="法杖",string rank="钻石"):WizardWeapon(price,atk,name,rank){}
~WizardWeapon4(){}
};
//妖怪武器类
class MonsterWeapon:public Weapon{
public:
MonsterWeapon(int price=0,int atk=0,string name="",string rank=""):Weapon(price,atk,name,rank){}
~MonsterWeapon(){}
};
class MonsterWeapon1:public MonsterWeapon{
public:
MonsterWeapon1(int price=450,int atk=60,string name="灵刃",string rank="白银"):MonsterWeapon(price,atk,name,rank){}
~MonsterWeapon1(){}
};
class MonsterWeapon2:public MonsterWeapon{
public:
MonsterWeapon2(int price=900,int atk=120,string name="灵刃",string rank="黄金"):MonsterWeapon(price,atk,name,rank){}
~MonsterWeapon2(){}
};
class MonsterWeapon3:public MonsterWeapon{
public:
MonsterWeapon3(int price=1650,int atk=200,string name="灵刃",string rank="白金"):MonsterWeapon(price,atk,name,rank){}
~MonsterWeapon3(){}
};
class MonsterWeapon4:public MonsterWeapon{
public:
MonsterWeapon4(int price=2750,int atk=320,string name="灵刃",string rank="钻石"):MonsterWeapon(price,atk,name,rank){}
~MonsterWeapon4(){}
};
//盔甲类
class Armor{
protected:
int Price; //盔甲价格
int DEF; //盔甲防御力
string Name; //盔甲名称
string Rank; //盔甲等级
public:
Armor(int price=0,int def=0,string name="",string rank=""){Price=price;DEF=def;Name=name;Rank=rank;}
int GetPrice(){return Price;}
int GetDEF(){return DEF;}
string GetName(){return Name;}
string GetRank(){return Rank;}
//虚析构函数
~Armor(){}
};
//战士盔甲类
class WarriorArmor:public Armor{
public:
WarriorArmor(int price=0,int def=0,string name="",string rank=""):Armor(price,def,name,rank){}
~WarriorArmor(){}
};
class WarriorArmor1:public WarriorArmor{
public:
WarriorArmor1(int price=300,int def=30,string name="铠甲",string rank="白银"):WarriorArmor(price,def,name,rank){}
~WarriorArmor1(){}
};
class WarriorArmor2:public WarriorArmor{
public:
WarriorArmor2(int price=600,int def=60,string name="铠甲",string rank="黄金"):WarriorArmor(price,def,name,rank){}
~WarriorArmor2(){}
};
class WarriorArmor3:public WarriorArmor{
public:
WarriorArmor3(int price=1200,int def=100,string name="铠甲",string rank="白金"):WarriorArmor(price,def,name,rank){}
~WarriorArmor3(){}
};
class WarriorArmor4:public WarriorArmor{
public:
WarriorArmor4(int price=2000,int def=200,string name="铠甲",string rank="钻石"):WarriorArmor(price,def,name,rank){}
~WarriorArmor4(){}
};
//法师盔甲类
class WizardArmor:public Armor{
public:
WizardArmor(int price=0,int def=0,string name="",string rank=""):Armor(price,def,name,rank){}
~WizardArmor(){}
};
class WizardArmor1:public WarriorArmor{
public:
WizardArmor1(int price=400,int def=40,string name="面纱",string rank="白银"):WarriorArmor(price,def,name,rank){}
~WizardArmor1(){}
};
class WizardArmor2:public WarriorArmor{
public:
WizardArmor2(int price=800,int def=80,string name="面纱",string rank="黄金"):WarriorArmor(price,def,name,rank){}
~WizardArmor2(){}
};
class WizardArmor3:public WarriorArmor{
public:
WizardArmor3(int price=1500,int def=160,string name="面纱",string rank="白金"):WarriorArmor(price,def,name,rank){}
~WizardArmor3(){}
};
class WizardArmor4:public WarriorArmor{
public:
WizardArmor4(int price=2500,int def=250,string name="面纱",string rank="钻石"):WarriorArmor(price,def,name,rank){}
~WizardArmor4(){}
};
//妖怪盔甲类
class MonsterArmor:public Armor{
public:
MonsterArmor(int price=0,int def=0,string name="",string rank=""):Armor(price,def,name,rank){}
~MonsterArmor(){}
};
class MonsterArmor1:public MonsterArmor{
public:
MonsterArmor1(int price=350,int def=35,string name="鳞甲",string rank="白银"):MonsterArmor(price,def,name,rank){}
~MonsterArmor1(){}
};
class MonsterArmor2:public MonsterArmor{
public:
MonsterArmor2(int price=700,int def=70,string name="鳞甲",string rank="黄金"):MonsterArmor(price,def,name,rank){}
~MonsterArmor2(){}
};
class MonsterArmor3:public MonsterArmor{
public:
MonsterArmor3(int price=1350,int def=130,string name="鳞甲",string rank="白金"):MonsterArmor(price,def,name,rank){}
~MonsterArmor3(){}
};
class MonsterArmor4:public MonsterArmor{
public:
MonsterArmor4(int price=2300,int def=220,string name="鳞甲",string rank="钻石"):MonsterArmor(price,def,name,rank){}
~MonsterArmor4(){}
};
//角色类
class Character{
protected:
string Name; //职业名称
int Health; //生命值
int ATK; //攻击力
int DEF; //防御力
int Money;
//武器,盔甲,药(暂用治疗术代替药水)
Weapon *weapon;
Armor *armor;
//Medicine medicine;
public:
Character(int health=2000,int atk=0,int def=0,int money=500,string name="")
{Health=health;ATK=atk;DEF=def;Money=money;Name=name;weapon=new Weapon();armor=new Armor();}
//得到角色的信息
string GetName(){return Name;}
int GetHealth(){return Health;}
int GetATK(){return ATK;}
int GetDEF(){return DEF;}
int GetMoney(){return Money;}
Weapon* GetWeapon(){return weapon;}
Armor* GetArmor(){return armor;}
//获得金钱,武器,装备
void GetMoney(int money){Money+=money;cout<<"获得了"<<money<<"金币"<<endl;}
void GetWeapon(Weapon *w)
{
Money-=w->GetPrice()-weapon->GetPrice(); //金币减少
ATK+=w->GetATK()-weapon->GetATK(); //攻击力上升
weapon=w;
}
void GetArmor(Armor *a)
{
Money-=a->GetPrice()-armor->GetPrice(); //金币减少
DEF+=a->GetDEF()-armor->GetDEF(); //防御力上升
armor=a;
}
/*void GetMedicine(Medicine m)
{
Medicine=m;
Money-=m.Price;
}*/
//受到攻击
void TakeAttack(int atk)
{
if(atk>DEF)
Health=Health-atk+DEF;
if(Health<0)
Health=0;
}
//使用治疗术
void Treatment()
{
if((Health+200)<=2000)
Health+=200;
else
Health=2000;
}
/*void TakeMedicine()
{
if((Health+medicine.value)<=2000)
Health+=medicine.value;
else
Health=2000;
}*/
//展示角色信息
void Show()
{
cout<<"职业:"<<Name<<endl;
cout<<"生命值:"<<Health<<endl;
cout<<"武器:"<<weapon->GetRank()<<weapon->GetName()<<"\t攻击力:"<<ATK<<endl;
cout<<"盔甲:"<<armor->GetRank()<<armor->GetName()<<"\t防御力:"<<DEF<<endl;
cout<<"金币:"<<Money<<endl;
}
//虚析构函数
virtual ~Character()
{
if(!weapon)delete weapon;
if(!armor) delete armor;
}
};
//战士类
class Warrior:public Character{
public:
Warrior(int health=2000,int atk=70,int def=20,int money=500,string name="战士"):Character(health,
atk,def,money,name){}
~Warrior()
{
if(!weapon)delete weapon;
if(!armor) delete armor;
}
};
//法师类
class Wizard:public Character{
public:
Wizard(int health=2000,int atk=50,int def=40,int money=500,string name="法师"):Character(health,
atk,def,money,name){}
~Wizard()
{
if(!weapon)delete weapon;
if(!armor) delete armor;
}
};
//妖怪类
class Monster:public Character{
public:
Monster(int health=2000,int atk=60,int def=30,int money=500,string name="妖怪"):Character(health,
atk,def,money,name){}
~Monster()
{
if(!weapon)delete weapon;
if(!armor) delete armor;
}
};
class CharacterFactory{
public:
Character *CreatCharacter(int flag)
{
switch(flag)
{
case 1:
return new Warrior();
break;
case 2:
return new Wizard();
break;
case 3:
return new Monster();
break;
}
}
};
//武器商店类
class WeaponStore{
public:
virtual void Show()=0; //武器信息展示
virtual Weapon *CreatWeapon(int flag)=0; //创建武器
//虚析构函数,防止析构一般,泄漏一半
virtual ~WeaponStore(){}
};
//战士武器商店类
class WarriorWeaponStore:public WeaponStore{
public:
void Show()
{
cout<<"1.白银巨剑 500金币 +70攻击力"<<endl;
cout<<"2.黄金巨剑 1000金币 +130攻击力"<<endl;
cout<<"3.白金巨剑 1800金币 +220攻击力"<<endl;
cout<<"4.钻石巨剑 3000金币 +350攻击力"<<endl;
}
Weapon *CreatWeapon(int flag)
{
switch(flag)
{
case 1:
return new WarriorWeapon1();break;
case 2:
return new WarriorWeapon2();break;
case 3:
return new WarriorWeapon3();break;
case 4:
return new WarriorWeapon4();break;
}
}
~WarriorWeaponStore(){}
};
//法师武器商店类
class WizardWeaponStore:public WeaponStore{
public:
void Show()
{
cout<<"1.白银法杖 400金币 +50攻击力"<<endl;
cout<<"2.黄金法杖 800金币 +100攻击力"<<endl;
cout<<"3.白金法杖 1500金币 +180攻击力"<<endl;
cout<<"4.钻石法杖 2500金币 +300攻击力"<<endl;
}
Weapon *CreatWeapon(int flag)
{
switch(flag)
{
case 1:
return new WizardWeapon1();break;
case 2:
return new WizardWeapon2();break;
case 3:
return new WizardWeapon3();break;
case 4:
return new WizardWeapon4();break;
}
}
~WizardWeaponStore(){}
};
//妖怪武器商店类
class MonsterWeaponStore:public WeaponStore{
public:
void Show()
{
cout<<"1.白银灵刃 450金币 +60攻击力"<<endl;
cout<<"2.黄金灵刃 900金币 +120攻击力"<<endl;
cout<<"3.白金灵刃 1650金币 +200攻击力"<<endl;
cout<<"4.钻石灵刃 2750金币 +320攻击力"<<endl;
}
Weapon *CreatWeapon(int flag)
{
switch(flag)
{
case 1:
return new MonsterWeapon1();break;
case 2:
return new MonsterWeapon2();break;
case 3:
return new MonsterWeapon3();break;
case 4:
return new MonsterWeapon4();break;
}
}
~MonsterWeaponStore(){}
};
//盔甲商店类
class ArmorStore{
public:
virtual void Show()=0; //盔甲信息展示
virtual Armor* CreatArmor(int flag)=0; //创建盔甲
virtual ~ArmorStore(){}
};
//战士盔甲商店类
class WarriorArmorStore:public ArmorStore{
public:
void Show()
{
cout<<"1.白银铠甲 300金币 +30防御力"<<endl;
cout<<"2.黄金铠甲 600金币 +60防御力"<<endl;
cout<<"3.白金铠甲 1200金币 +100防御力"<<endl;
cout<<"4.钻石铠甲 2000金币 +200防御力"<<endl;
}
Armor *CreatArmor(int flag)
{
switch(flag)
{
case 1:
return new WarriorArmor1();break;
case 2:
return new WarriorArmor2();break;
case 3:
return new WarriorArmor3();break;
case 4:
return new WarriorArmor4();break;
}
}
~WarriorArmorStore(){}
};
//法师盔甲商店类
class WizardArmorStore:public ArmorStore{
public:
void Show()
{
cout<<"1.白银面纱 400金币 +40防御力"<<endl;
cout<<"2.黄金面纱 800金币 +80防御力"<<endl;
cout<<"3.白金面纱 1500金币 +160防御力"<<endl;
cout<<"4.钻石面纱 2500金币 +250防御力"<<endl;
}
Armor *CreatArmor(int flag)
{
switch(flag)
{
case 1:
return new WizardArmor1();break;
case 2:
return new WizardArmor2();break;
case 3:
return new WizardArmor3();break;
case 4:
return new WizardArmor4();break;
}
}
~WizardArmorStore(){}
};
//妖怪盔甲商店类
class MonsterArmorStore:public ArmorStore{
public:
void Show()
{
cout<<"1.白银鳞甲 350金币 +35防御力"<<endl;
cout<<"2.黄金鳞甲 700金币 +70防御力"<<endl;
cout<<"3.白金鳞甲 1350金币 +130防御力"<<endl;
cout<<"4.钻石鳞甲 2300金币 +220防御力"<<endl;
}
Armor *CreatArmor(int flag)
{
switch(flag)
{
case 1:
return new MonsterArmor1();break;
case 2:
return new MonsterArmor2();break;
case 3:
return new MonsterArmor3();break;
case 4:
return new MonsterArmor4();break;
}
}
~MonsterArmorStore(){}
};
//管理商店类
class StoreManager{
public:
WeaponStore *CreatWeaponStore(string name)
{
if(name=="战士")
return new WarriorWeaponStore();
if(name=="法师")
return new WizardWeaponStore();
if(name=="妖怪")
return new MonsterWeaponStore();
}
ArmorStore *CreatArmorStore(string name)
{
if(name=="战士")
return new WarriorArmorStore();
if(name=="法师")
return new WizardArmorStore();
if(name=="妖怪")
return new MonsterArmorStore();
}
};
int main()
{
CharacterFactory characterFactory; //角色产生工厂
Character *player1=0; //玩家一角色
Character *player2=0; //玩家二角色
StoreManager storeManager; //商店产生工厂(决定产生哪种职业的武器和盔甲)
WeaponStore* player1weaponStore=0; //玩家一武器商店
ArmorStore* player1armorStore=0; //玩家一盔甲商店
WeaponStore* player2weaponStore=0; //玩家二武器商店
ArmorStore* player2armorStore=0; //玩家二盔甲商店
Weapon *player1Weapon=0; //玩家一武器
Weapon *player2Weapon=0; //玩家二武器
Armor *player1Armor=0; //玩家一盔甲
Armor *player2Armor=0; //玩家二盔甲
int money; //每回合随机生成金币
srand((unsigned)time(NULL));
money=rand()%100+200; //每回合生成200到300的金币
int flag=0,count=0; //用于switch的flag和记录回合数的count
const int treatCD=4; //治疗术的CD,即四个回合
int player1NextTreat=1,player2NextTreat=1; //玩家一和玩家二下次可以使用治疗术的回合
//游戏开始
cout<<"游戏开始!"<<endl;
//玩家一创建角色
cout<<"玩家一请选择职业:\n"<<"1.战士\t2.法师\t3.妖怪"<<endl;
while(1)
{
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=1&&flag!=2&&flag!=3)
cout<<"输入错误,请重新输入!\n";
else
break;
}
player1=characterFactory.CreatCharacter(flag);
player1weaponStore=storeManager.CreatWeaponStore(player1->GetName());
player1armorStore=storeManager.CreatArmorStore(player1->GetName());
//玩家二创建角色
cout<<"玩家二请选择职业:\n"<<"1.战士\t2.法师\t3.妖怪"<<endl;
while(1)
{
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=1&&flag!=2&&flag!=3)
cout<<"输入错误,请重新输入!";
else
break;
}
player2=characterFactory.CreatCharacter(flag);
player2weaponStore=storeManager.CreatWeaponStore(player2->GetName());
player2armorStore=storeManager.CreatArmorStore(player2->GetName());
//无限循环,直到有一方生命值下降为0
while(1)
{
count++;
cout<<"\n*************"<<endl;
cout<<"***第"<<count<<"回合***"<<endl;
cout<<"*************\n"<<endl;
//玩家一进行游戏
cout<<"玩家一:"<<endl;
money=rand()%100+200;
player1->GetMoney(money);
player1->Show();
while(1) //为了能退到上一级,即选择攻击或买武器等,需要这个循环
{
cout<<"请选择操作:"<<"\n1.攻击\n2.买武器\n3.买盔甲\n4.治疗术(回复200生命值,冷却时间为四个回合)"<<endl;
while(1)
{
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=1&&flag!=2&&flag!=3&&flag!=4)
cout<<"输入错误,请重新输入"<<endl;
else
break;
}
switch(flag)
{
case 1: //攻击
player2->TakeAttack(player1->GetATK());break;
case 2: //买武器
player1weaponStore->Show();
cout<<"0.返回上一级"<<endl;
while(1)
{
cout<<"请选择操作"<<endl;
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=0&&flag!=1&&flag!=2&&flag!=3&&flag!=4)
cout<<"输入错误,请重新输入"<<endl;
else if(flag==0)
break;
else
{
Weapon* tempWeapon=player1weaponStore->CreatWeapon(flag);
if(player1->GetMoney()<tempWeapon->GetPrice()-player1->GetWeapon()->GetPrice())
{
cout<<"你的金币不够,请重新选择或输入0返回上一级"<<endl;
delete tempWeapon;
}
else
{
delete tempWeapon;
break;
}
}
}
if(flag==0)
break;
player1Weapon=player1weaponStore->CreatWeapon(flag);
player1->GetWeapon(player1Weapon);
break;
case 3: //买盔甲
player1armorStore->Show();
cout<<"0.返回上一级"<<endl;
while(1)
{
cout<<"请选择盔甲"<<endl;
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=0&&flag!=1&&flag!=2&&flag!=3&&flag!=4)
cout<<"输入错误,请重新输入"<<endl;
else if(flag==0)
break;
else
{
Armor* tempArmor=player1armorStore->CreatArmor(flag);
if(player1->GetMoney()<tempArmor->GetPrice()-player1->GetArmor()->GetPrice())
{
cout<<"你的金币不够,请重新选择或输入0返回上一级"<<endl;
delete tempArmor;
}
else
{
delete tempArmor;
break;
}
}
}
if(flag==0)
break;
player1Armor=player1armorStore->CreatArmor(flag);
player1->GetArmor(player1Armor);
break;
case 4: //补血
if(count>=player1NextTreat)
{
player1->Treatment();
player1NextTreat+=treatCD;
}
else
{
cout<<"治疗术正在冷却中,还剩"<<(player1NextTreat-count)<<"个回合"<<endl;
while(1)
{
cout<<"按0退回上一级"<<endl;
cin>>flag;
if(flag==0)
break;
}
}
break;
}
if(flag!=0)
break;
}
//判断输赢
if(player2->GetHealth()<=0)
{
cout<<"玩家二生命值下降为0,玩家一胜利"<<endl;
break;
}
//玩家二进行游戏
cout<<"玩家二:"<<endl;
money=rand()%100+200;
player2->GetMoney(money);
player2->Show();
while(1) //为了能退到上一级,即选择攻击或买武器等,需要这个循环
{
cout<<"请选择操作:"<<"\n1.攻击\n2.买武器\n3.买盔甲\n4.治疗术(回复200生命值,冷却时间为四个回合)"<<endl;
while(1)
{
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=1&&flag!=2&&flag!=3&&flag!=4)
cout<<"输入错误,请重新输入"<<endl;
else
break;
}
switch(flag)
{
case 1: //攻击
player1->TakeAttack(player2->GetATK());break;
case 2: //买武器
player2weaponStore->Show();
cout<<"0.返回上一级"<<endl;
while(1)
{
cout<<"请选择操作"<<endl;
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=0&&flag!=1&&flag!=2&&flag!=3&&flag!=4)
cout<<"输入错误,请重新输入"<<endl;
else if(flag==0)
break;
else
{
Weapon* tempWeapon=player2weaponStore->CreatWeapon(flag);
if(player2->GetMoney()<tempWeapon->GetPrice()-player2->GetWeapon()->GetPrice())
{
cout<<"你的金币不够,请重新选择或输入0返回上一级"<<endl;
delete tempWeapon;
}
else
{
delete tempWeapon;
break;
}
}
}
if(flag==0)
break;
player2Weapon=player2weaponStore->CreatWeapon(flag);
player2->GetWeapon(player2Weapon);
break;
case 3: //买盔甲
player2armorStore->Show();
cout<<"0.返回上一级"<<endl;
while(1)
{
cout<<"请选择盔甲"<<endl;
cin>>flag;
if(!cin) //输入类型错误,即int型的flag却输入了字母
{
cin.clear(); //防止输入字母后程序崩溃,将之前的输入错误标识改回为0,即输入无错误
cin.sync(); //清除之前错误的数据流,以便重新输入
cout<<"输入错误请重新输入!\n"<<endl;
continue;
}
if(flag!=0&&flag!=1&&flag!=2&&flag!=3&&flag!=4)
cout<<"输入错误,请重新输入"<<endl;
else if(flag==0)
break;
else
{
Armor* tempArmor=player2armorStore->CreatArmor(flag);
if(player2->GetMoney()<tempArmor->GetPrice()-player2->GetArmor()->GetPrice())
{
cout<<"你的金币不够,请重新选择或输入0返回上一级"<<endl;
delete tempArmor;
}
else
{
delete tempArmor;
break;
}
}
}
if(flag==0)
break;
player2Armor=player2armorStore->CreatArmor(flag);
player2->GetArmor(player2Armor);
break;
case 4: //补血
if(count>=player2NextTreat)
{
player2->Treatment();
player2NextTreat+=treatCD;
}
else
{
cout<<"治疗术正在冷却中,还剩"<<(player2NextTreat-count)<<"个回合"<<endl;
while(1)
{
cout<<"按0退回上一级"<<endl;
cin>>flag;
if(flag==0)
break;
}
}
break;
}
if(flag!=0)
break;
}
//判断输赢
if(player1->GetHealth()<=0)
{
cout<<"玩家一生命值下降为0,玩家二胜利"<<endl;
break;
}
}
//销毁申请的空间
if(!player1)
delete player1;
if(!player2)
delete player2;
if(!player1weaponStore)
delete player1weaponStore;
if(!player1armorStore)
delete player1armorStore;
if(!player2weaponStore)
delete player2weaponStore;
if(!player2armorStore)
delete player2armorStore;
if(!player1Weapon)
delete player1Weapon;
if(!player2Weapon)
delete player2Weapon;
if(!player1Armor)
delete player1Armor;
if(!player2Armor)
delete player2Armor;
system("pause");
return 0;
}
RPG游戏
1、 无名游戏
#include<stdlib.h>
#include<stdio.h>
#include<time.h> //suiji
#include<string.h>
#include<windows.h> //SLEEP函数
struct Player //玩家结构体,并初始化player
{
char name[21];
int attack;
int defense;
int health;
long int max_health;
int level;
int exp;
int range_exp;
long int max_exp;
}player={"勇者",50,40,100,100,1,0,0,100};
struct Enemy //怪的结构体,并初始化各种怪
{
char name[20];
char wupin[12];
int attack;
int defense;
int health;
int money;
long int exp;
int wupin_sign;
int wupinpro;
int double_attack;
int miss;
}strongman={40,50,350,200,100,1,2,1,0},
witch={25,15,100,50,50,2,2,1,1},
xiyi={18,10,50,30,35,3,3,2,2},
big_strongman={40*5,50*5,350*5,200*5,100*5,4,4,2,0},
lion={60,30,280,200,100,5,2,1,0},
horse={28,12,90,50,50,6,2,1,1},
bee={17,11,60,30,35,7,3,2,2},
shitu={"\0",60*8,30*8,280*8,200*8,100*8,9,1,1,0},
guai={"\0","\0",0,0,0,0,0,0,0,0,0};
struct Place
{
int bar,hotel,forest1,forest2,forest3,grass1,grass2,grass3;
}place={1,2,3,4,5,6,7,8};
int max_exp=0;
int choose_number=0,s=0,strongman_arm=0,battle=0,money=500,place_sign=9;
int cao=3,jijiubao=2,baiyao=2,superbaiyao=1,boom=3,dubiao=2,atom_boom=1;
int fang=0,fang1=10,fang1n=0,fang2=20,fang2n=0,fang3=40,fang3n=0,fang4=100,fang4n=0;
int gong=0,gong1=8,gong1n=0,gong2=15,gong2n=0,gong3=25,gong3n=0,gong4=60,gong4n=0;
char gongname[20]="无",fangname[20]="无";
char proof;
void AddWupin(int);
int AttackResult();
void BattleAct();
void ChooseWupin();
void DisplayState();
void OrdinaryAct();
int SuiJi();
int SuiJi100();
void WhetherLevelUp();
void SlowDisplay(char *);
int main()
{
int i=0,j=0,k=0;
char player_name[21];
SlowDisplay("这是一个勇者的世界! 绑架雅莉萨斯国的罗茜公主!\n\n 伟大的勇者啊~拿起你们的武器. 绑架公主!\n\n\n输入你的名字: (20个字符)\n\n\n");
// sndPlaySound("res\\F005.wav",SND_LOOP);
scanf("%s",player_name);
strncpy(player.name,player_name,20);
if(strcmp(player.name,"EVA初号机")==0)
{
SlowDisplay("绑架多年的 EVA初号机 啊!你终于可以绑架天日了!\n\n\n EVA初号机 重新绑架... 随即... 绑架!!!\n\n\n");
player.attack=999;
player.defense=999;
player.health=9999;
player.max_health=9999;
}
getchar();
OrdinaryAct();
return 0;
}
int SuiJi()
{
srand((unsigned)time(NULL));
return rand()%10;
}
int SuiJi100()
{
srand((unsigned)time(NULL));
return rand()%100;
}
void ChooseWupin() //选择物品 并使用
{
printf("物品: 1,止血草%d个 2,急救包%d个 3,云南白药%d个 4,超级云南白药%d个 5,手雷%d个 6,毒标%d个 7,手抛式原子弹%d个 0,返回\n\n\n",cao,jijiubao,baiyao,superbaiyao,boom,dubiao,atom_boom);
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
if(cao>0)
{
SlowDisplay("使用绑架,HP增加60\n\n\n");
cao--;
if(player.health+60>player.max_health)player.health=player.max_health;
else player.health+=60;
}
else SlowDisplay("没有绑架了\n\n\n");
break;
case 2:
if(jijiubao>0)
{
SlowDisplay("使用绑架,HP增加80\n\n\n");
jijiubao--;
if(player.health+80>player.max_health)player.health=player.max_health;
else player.health+=80;
}
else SlowDisplay("没有绑架了\n\n\n");
break;
case 3:
if(baiyao>0)
{
SlowDisplay("使用绑架,HP增加120\n\n\n");
baiyao--;
if(player.health+120>player.max_health)player.health=player.max_health;
else player.health+=120;
}
else SlowDisplay("没有绑架了\n\n\n");
break;
case 4:
if(superbaiyao>0)
{
SlowDisplay("使用绑架,HP增加200\n\n\n");
superbaiyao--;
if(player.health+200>player.max_health)player.health=player.max_health;
else player.health+=200;
}
else SlowDisplay("没有绑架了\n\n\n");
break;
case 5:
if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
{
if(boom>0)
{
SlowDisplay("使用绑架,敌人HP减少100\n\n\n");
boom--;
guai.health-=100;
AttackResult();
}
}
else SlowDisplay("非战斗状态,不能使用绑架!\n\n\n");
break;
case 6:
if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
{
if(dubiao>0)
{
SlowDisplay("使用毒标,敌人HP减少200\n\n\n");
dubiao--;
guai.health-=200;
AttackResult();
}
}
else SlowDisplay("非战斗状态,不能使用毒标!\n\n\n");
break;
case 7:
if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
{
if(atom_boom>0)
{
SlowDisplay("使用手抛式原子弹,敌人HP减少500\n\n\n");
atom_boom--;
guai.health-=500;
AttackResult();
}
}
else SlowDisplay("非战斗状态,不能使用手抛式原子弹!\n\n\n");
break;
case 0:break;
default:printf("ChooseWupin error!\n\n\n");
}
}
int AttackResult() //攻击结果:判断是否获胜 是否获得物品 和 是否升级
{
if(guai.health<=0)
{
battle=0;
printf("绑架胜利!获得绑架%d,绑架%d\n\n\n",guai.money,guai.exp);
player.exp+=guai.exp;
player.range_exp+=guai.exp;
money+=guai.money;
s=SuiJi();
if(s<guai.wupinpro)
{
SlowDisplay("从敌人尸骸中发现");
printf("%s\n\n\n",guai.wupin);
AddWupin(guai.wupin_sign);
}
WhetherLevelUp();
if(strcmp(guai.name,"使徒")==0)
{
puts("战斗胜利,绑架公主!!!");
getchar();
getchar();
exit(0);
}
return 1; //攻击有结果了返回1,否则返回0,用于判断是否继续做战斗行为
}
else
{
int s=SuiJi();
if((guai.attack+s-player.defense/3)<0)
{
player.health-=1;
printf("%s反击,你的HP减少了 1\n\n",guai.name);
}
else
{
player.health-=guai.attack+s-player.defense/3;
printf("%s反击,你的HP减少了%d\n\n",guai.name,guai.attack+s-player.defense/3);
}
if(player.health<0)
{
battle=0;
printf("%s战死!金币掉落%d\n\n\n",player.name,player.level*500);
money-=player.level*500;
player.health=player.max_health/5;
OrdinaryAct();//////////
return 1;
}
}
return 0;
}
void AddWupin(int wupin_sign)
{
switch(wupin_sign)
{
case 1:fang4n++; break;
case 2:fang3n++; break;
case 3:fang2n++; break;
case 4:strongman_arm=1;break;
case 5:gong4n++; break;
case 6:gong3n++; break;
case 7:gong2n++; break;
default:printf("AddWupin error\n\n\n");
}
}
void WhetherLevelUp()
{
int i=0,j=0;
int l1=player.range_exp/100;
int l2=player.range_exp/300;
int l3=player.range_exp/600;
if(player.level<=15&&l1>0) //15级以下,经验足够 都满足则升级
{
if(l1==1)
{
printf("%s",player.name);
SlowDisplay(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3;
player.defense+=2;
player.max_health+=20;
player.health=player.max_health;
player.level++;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=100;
}
else
{
printf("好厉害!连升%d级!",l1);
printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l1,2*l1,20*l1);
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3*l1;
player.defense+=2*l1;
player.max_health+=20*l1;
player.health=player.max_health;
player.level+=l1;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=100*l1;
}
}
else if(player.level<=40&&l2>0)
{
if(l2==1)
{
printf("%s",player.name);
SlowDisplay(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3;
player.defense+=2;
player.max_health+=20;
player.health=player.max_health;
player.level++;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=300;
}
else
{
printf("好厉害!连升%d级!",l1);
printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l2,2*l2,20*l2);
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3*l2;
player.defense+=2*l2;
player.max_health+=20*l2;
player.health=player.max_health;
player.level+=l2;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=300*l2;
}
}
else if(l3>0)
{
if(l3==1)
{
printf("%s",player.name);
SlowDisplay(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3;
player.defense+=2;
player.max_health+=20;
player.health=player.max_health;
player.level++;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=600;
}
else
{
printf("好厉害!连升%d级!",l1);
printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l3,2*l3,20*l3);
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3*l3;
player.defense+=2*l3;
player.max_health+=20*l3;
player.health=player.max_health;
player.level+=l3;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=600*l3;
}
}
}
void OrdinaryAct() //正常行为菜单(移动,物品,对话,查看状态,装备,退出游戏)
{
while(1)
{
// \(1000);
// system("cls");
puts("=============================================================================");
printf("要做什么?\n\n\n 1,移动 2,道具 3,对话 4,查看状态 5,装备 0,退出游戏\n\n\n");
puts("=============================================================================");
switch(scanf("%d",&choose_number),choose_number)
{
case 1: //显示移动菜单
SlowDisplay("要去哪里?\n\n\n");
printf("1,酒吧 2,旅馆 3,森林一层 4,森林二层 5,森林三层 6,草原一层 7,草原二层 8,草原三层\n\n\n");
switch(scanf("%d",&choose_number),choose_number)
{
case 1:place_sign=place.bar; //记录目前位置-酒吧
// OrdinaryAct();
break;
case 2:place_sign=place.hotel; //进入旅店
SlowDisplay("要住店吗? 200个金币 1,是 0,否\n\n\n");
choose_number=1;
switch(scanf("%d",&choose_number),choose_number)
{
case 1:if(money-200<0) //判断钱是否够
{
SlowDisplay("Sorry,你的钱不够~\n\n\n");
}
else
{
SlowDisplay("好好休息\n\tHP满\n\t\t第二天了\n\n");
money-=200; //花费200住店费
player.health=player.max_health; //体力满
}
break;
case 0:SlowDisplay("下次再来!\n\n\n");break;
default:printf("hotel talk error!\n\n\n");
}
place_sign=0;
break;
case 3:
place_sign=place.forest1;
s=SuiJi();
if(s<7)
{
battle=1;
guai=xiyi;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=witch;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
SlowDisplay("这里安全\n\n\n");
//不用调用OAct函数,会自动执行OAct函数;
}
break;
case 4:
place_sign=place.forest2;
s=SuiJi();
if(s<7)
{
battle=1;
guai=witch;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=strongman;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
SlowDisplay("这里安全\n\n\n");
}
break;
case 5:
place_sign=place.forest3;
s=SuiJi();
if(s<7)
{
battle=1;
guai=strongman;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=big_strongman;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
SlowDisplay("这里安全\n\n\n");
}
break;
case 6:
place_sign=place.grass1;
s=SuiJi();
if(s<7)
{
battle=1;
guai=bee;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=horse;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
SlowDisplay("这里安全\n\n\n");
}
break;
case 7:
place_sign=place.grass2;
s=SuiJi();
if(s<7)
{
battle=1;
guai=horse;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=lion;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
SlowDisplay("这里安全\n\n\n");
}
break;
case 8:
place_sign=place.grass3;
s=SuiJi();
if(s<7)
{
battle=1;
guai=lion;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
if(strongman_arm)
{
SlowDisplay("神秘老人:\n\n\n 哈哈,年轻人,做的不错,不过...嘿嘿,你上当啦!巨人之臂我要了,公主你也别想带走!\n\n\n");
guai=shitu;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else SlowDisplay("神秘老人:\n\n\n 年轻人,你好啊.如果你有巨人之臂,我可以告诉你公主的下落哦~\n\n\n");
}
else
{
SlowDisplay("这里安全\n\n\n");
}
break;
default:printf("choose map error!\n\n\n");
}
break;
case 2: ChooseWupin();break; //显示道具,并可以使用.
case 3: //对话选项
if(place_sign==place.bar)
{
SlowDisplay("要和谁说话?\n\n\n1,红发女郎 2,赏金猎人 3,酒吧老板\n\n\n"); //显示对话人物
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
SlowDisplay("红发女郎:\n\n\n 吧台边那个Hunter好帅啊!(~脸红~)\n\n\n听说他经常外出打猎,外面的路他应该很熟悉的!\n\n\n");
break;
case 2:
if(fang1n<1&&gong1n<1)
{
SlowDisplay("赏金猎人:\n\n\n 你要救公主啊!好胆量!\n\n\n 不过外面的丛林和草原很险恶,而且越深越危险,这是匕首和布衣,对你会有些帮助的,拿去吧!\n\n\n");
printf("%s心想:(哇,这位大叔人真好啊!\n\n\n)",player.name);
gong1n++;
fang1n++;
}
else SlowDisplay("赏金猎人:\n\n\n 加油吧,年轻人!\n\n\n 不要被外面丛林和草原所吓倒!\n\n\n");
break;
case 3:
printf("要喝点什么?\n\n\n 1,二锅头25金币 HP+20 2,XO酒80金币 HP+50 3,人头马面150金币 HP+100 0,返回\n\n\n");
choose_number=1;
while(choose_number)
{
switch(scanf("%d",&choose_number),choose_number)
{
case 1:if(money<25)
{
SlowDisplay("钱不够!");
}
else
{
if(player.health+20<=player.max_health)
{
SlowDisplay("HP+20.");
money-=25;
player.health+=20;
}
else
{
SlowDisplay("HP满了");
player.health=player.max_health;
}
}
break;
case 2:if(money<80)
{
SlowDisplay("钱不够!");
}
else
{
if(player.health+50<=player.max_health)
{
SlowDisplay("HP+50.");
money-=80;
player.health+=50;
}
else
{
SlowDisplay("HP满了");
player.health=player.max_health;
}
}
break;
case 3:if(money<150)
{
SlowDisplay("钱不够!");
}
else
{
if(player.health+100<=player.max_health)
{
SlowDisplay("HP+100.");
money-=150;
player.health+=100;
}
else
{
SlowDisplay("HP满了");
player.health=player.max_health;
}
}
break;
case 0:SlowDisplay("下次在来!\n");break;
default:SlowDisplay("输入错误\n\n\n");
}
}
}
}
else if(place_sign==place.hotel)
SlowDisplay("“老板娘!我...”\n\n\n“我忙着呢,没空理你~”\n\n\n");
else SlowDisplay("这里好像没人可以聊天\n\n\n");
break;
case 4: DisplayState(); break; //显示状态
case 5: //装备
printf("攻装: 1,匕首:%d个 2,长剑:%d个 3,碧血剑:%d个 4,绝世好剑:%d个\n\n\n",gong1n,gong2n,gong3n,gong4n);
printf("防装: 5,布衣:%d个 6,铁甲:%d个 7,银甲:%d个 8,黄金圣衣:%d个\t\t0,返回\n\n\n",fang1n,fang2n,fang3n,fang4n);
SlowDisplay("选择要装备的武器或防具:\n\n\n");
switch(scanf("%d",&choose_number),choose_number)
{
case 1:if(gong1n>=1)
{
SlowDisplay("拿起了匕首\n\n\n");
gong=gong1;
strcpy(gongname,"匕首");
}
else SlowDisplay("你没有匕首可以装备\n\n\n");
break;
case 2:if(gong2n>=1)
{
SlowDisplay("拿起了长剑\n\n\n");
gong=gong2;
strcpy(gongname,"长剑");
}
else SlowDisplay("你没有长剑可以装备\n\n\n");
break;
case 3:if(gong3n>=1)
{
SlowDisplay("拿起了碧血剑\n\n\n");
gong=gong3;
strcpy(gongname,"碧血剑");
}
else SlowDisplay("你没有碧血剑可以装备\n\n\n");
break;
case 4:if(gong4n>=1)
{
SlowDisplay("拿起了绝世好剑\n\n\n");
gong=gong4;
strcpy(gongname,"绝世好剑");
}
else SlowDisplay("你没有绝世好剑可以装备\n\n\n");
break;
case 5:if(fang1n>=1)
{
SlowDisplay("穿上了布衣\n\n\n");
fang=fang1;
strcpy(fangname,"布衣");
}
else SlowDisplay("你没有布衣可以装备\n\n\n");
break;
case 6:if(fang2>=1)
{
SlowDisplay("穿上了铁甲\n\n\n");
fang=fang2;
strcpy(fangname,"铁甲");
}
else SlowDisplay("你没有铁甲可以装备\n\n\n");
break;
case 7:if(fang3n>=1)
{
SlowDisplay("穿上了银甲\n\n\n");
fang=fang3;
strcpy(fangname,"银甲");
}
else SlowDisplay("你没有银甲可以装备\n\n\n");
break;
case 8:if(fang4n>=1)
{
SlowDisplay("穿上了黄金圣衣\n\n\n");
fang=fang4;
strcpy(fangname,"黄金圣衣");
}
else SlowDisplay("你没有黄金圣衣可以装备\n\n\n");
break;
case 0:SlowDisplay("未更换装备\n\n\n");break;
default:printf("change error!");
}
break;
case 0:
SlowDisplay("确定推出游戏?(Y/N)\n\n\n");
getchar();
proof=getchar();
if(proof=='y'||proof=='Y')
{
SlowDisplay("数据存储中...");
//向文件中更新数据;
getchar();
SlowDisplay("按回车退出");
getchar();
return;
}
else if(proof=='n'||proof=='N')printf("继续游戏!\n\n\n");
else SlowDisplay("继续!\n\n\n");
break;
default:SlowDisplay("输入错误!\n\n\n");
}
}
}
void DisplayState()
{
printf("%s 攻击力:%d+%d=%d 防御力:%d+%d=%d HP:%d/%d \n\n\n",player.name,player.attack,gong,player.attack+gong,player.defense,fang,player.defense+fang,player.health,player.max_health);
printf("武器: %s 防具: %s \n\n\n",gongname,fangname);
printf("等级:%d 经验:%d/%d 还需要%d经验升级 金币:%d \n\n\n",player.level,player.exp,player.max_exp,player.max_exp-player.exp,money);
}
void BattleAct()
{
while(1)
{
puts("=============================================================================");
printf("要怎么办?\n\n\n 1,攻击 2,物品 3,查看状态 4,逃跑\n\n\n");
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
s=SuiJi();
printf("%s攻击! %sHP减少%d\n\n\n",player.name,guai.name,player.attack+s+gong-guai.defense/3);
guai.health-=player.attack+s+gong-guai.defense/3;
if(AttackResult())return; //如果攻击有结果(敌人或玩家战死)退出函数
else continue;
case 2: ChooseWupin();break; //选择物品,可以使用,战斗中允许使用攻击性物品
case 3:DisplayState();break; //显示状态
case 4:
s=SuiJi();
if(s<4) //40%的概率可以逃跑
{
printf("%s逃跑了~\n\n\n",player.name);
battle=0;
return;
}
else printf("%s逃跑失败!\n\n\n",player.name);
break;
default:SlowDisplay("输入错误,重新输入!\n\n\n");
}
}
}
void SlowDisplay(char *p)
{
while(1)
{
if(*p!=0)
printf("%c",*p++);
else
break;
Sleep(100);
}
}
2、贪吃蛇(1)
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 21
int apple[3],num;
char score[3];
char tail[3];
void gotoxy(int x, int y) //输出坐标
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int b) //颜色函数
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole,b) ;
}
int Block(char head[2]) //判断出界
{
if ((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))
return 1;
return 0;
}
int Eat(char snake[2]) //吃了苹果
{
if ((snake[0] == apple[0]) && (snake[1] == apple[1]))
{
apple[0] = apple[1] = apple[2] = 0;
gotoxy(N+44,10);
color(13);
printf("%d",score[0]*10);
color(11);
return 1;
}
return 0;
}
void Draw(char **snake, int len) //蛇移动
{
if (apple[2])
{
gotoxy(apple[1] * 2, apple[0]);
color(12);
printf("●");
color(11);
}
gotoxy(tail[1] * 2, tail[0]);
if (tail[2])
{
color(num);
printf("★");
color(num);
}
else
printf("■");
gotoxy(snake[0][1] * 2, snake[0][0]);
color(num);
printf("★");
color(num);
putchar('\n');
}
char** Move(char **snake, char dirx, int *len) //控制方向
{
int i, full = Eat(snake[0]);
memcpy(tail, snake[(*len)-1], 2);
for (i = (*len) - 1; i > 0; --i)
memcpy(snake[i], snake[i-1], 2);
switch (dirx)
{
case 'w': case 'W': --snake[0][0]; break;
case 's': case 'S': ++snake[0][0]; break;
case 'a': case 'A': --snake[0][1]; break;
case 'd': case 'D': ++snake[0][1]; break;
default: ;
}
if (full)
{
snake = (char **)realloc(snake, sizeof(char *) * ((*len) + 1));
snake[(*len)] = (char *)malloc(sizeof(char) * 2);
memcpy(snake[(*len)], tail, 2);
++(*len);
++score[0];
if(score[3] < 16)
++score[3];
tail[2] = 1;
}
else
tail[2] = 0;
return snake;
}
void init(char plate[N+2][N+2], char ***snake_x, int *len) //初始化
{
int i, j;
char **snake = NULL;
*len = 3;
score[0] = score[3] =3;
snake = (char **)realloc(snake, sizeof(char *) * (*len));
for (i = 0; i < *len; ++i)
snake[i] = (char *)malloc(sizeof(char) * 2);
for (i = 0; i < 3; ++i)
{
snake[i][0] = N/2 + 1;
snake[i][1] = N/2 + 1 + i;
}
for (i = 1; i <= N; ++i)
for (j = 1; j <= N; ++j)
plate[i][j] = 1;
apple[0] = rand()%N + 1; apple[1] = rand()%N + 1;
apple[2] = 1;
for (i = 0; i < N + 2; ++i)
{
gotoxy(0, i);
for (j = 0; j < N + 2; ++j)
{
switch (plate[i][j])
{
case 0:
color(12);printf("□");color(11); continue;
case 1: printf("■"); continue;
default: ;
}
}
putchar('\n');
}
for (i = 0; i < (*len); ++i)
{
gotoxy(snake[i][1] * 2, snake[i][0]);
printf("★");
}
putchar('\n');
*snake_x = snake;
}
void Manual()
{
gotoxy(N+30,2);
color(10);
printf("按 W S A D 移动方向");
gotoxy(N+30,4);
printf("按 space 键暂停");
gotoxy(N+30,8);
color(11);
printf("历史最高分为: ");
color(12);
gotoxy(N+44,8);
printf("%d",score[1]*10);
color(11);
gotoxy(N+30,12);
printf("你现在得分为: 0");
}
int File_in() //取记录的分数
{
FILE *fp;
if((fp = fopen("C:\\tcs.txt","a+")) == NULL)
{
gotoxy(N+18, N+2);
printf("文件不能打开\n");
exit(0);
}
if((score[1] = fgetc(fp)) != EOF);
else
score[1] = 0;
return 0;
}
int File_out() //存数据
{
FILE *fp;
if(score[1] > score[0])
{gotoxy(10,10);
color(12);
puts("闯关失败 加油耶");
gotoxy(0,N+2);
return 0;
}
if((fp = fopen("C:\\tcs.txt","w+")) == NULL)
{
printf("文件不能打开\n");
exit(0);
}
if(fputc(--score[0],fp)==EOF)
printf("输出失败\n");
gotoxy(10,10);
color(12);
puts("恭喜您打破记录");
gotoxy(0,N+2);
return 0;
}
void Free(char **snake, int len) //释放空间
{
int i;
for (i = 0; i < len; ++i)
free(snake[i]);
free(snake);
}
int main(void)
{
int len;
char ch = 'g';
char a[N+2][N+2] = {{0}};
char **snake;
srand((unsigned)time(NULL));
color(11);
File_in();
init(a, &snake, &len);
Manual();
while (ch != 0x1B) // 按 ESC 结束
{
Draw(snake, len);
if (!apple[2]) {
apple[0] = rand()%N + 1;
apple[1] = rand()%N + 1;
apple[2] = 1;
num++;
if(num>8)
num=0;
}
Sleep(200-score[3]*10);
setbuf(stdin, NULL);
if (kbhit())
{
gotoxy(0, N+2);
ch = getche();
}
snake = Move(snake, ch, &len);
if (Block(snake[0])==1)
{
gotoxy(N+2, N+2);
puts("你输了");
File_out();
Free(snake, len);
getche();
exit(0);
}
}
Free(snake, len);
exit(0);
}
3、剪刀石头布
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int main()
{
printf("版权所有,不得个人名义翻录,但是使用不要钱\n");
printf("注意小心按键……\n\n");
srand(time(0));
int i,j,t,q,n,q1;
int m,b,y,z1,z2,h,o,v;
long k;
char c;
printf("\t\t\t剪刀石头布\n");
printf("你想让电脑出什么?1:剪刀 2:石子 3:布\n");
scanf("%d",&j);
printf("1:剪刀 2:石头 3:布\n");
scanf("%ld",&k);
t=0;q=0;
b=y=z1=z2=h=o=n=0;
m=0;
while(1)
{
while(k!=0)
{
i=k%10;
k=k/10;
if(i>0&&i<4)
{
if(i==1) printf("你出的是剪刀! \n");
if(i==2) printf("你出的是石头!\n");
if(i==3) printf("你出的是布!\n");
if(b!=0)
{
printf("电脑思考中!\n");
system("pause");
j=i-1;
if(j==0) j=3;
if(j==1) printf("电脑出的是剪刀!\n");
if(j==2) printf("电脑出的是石头!\n");
if(j==3) printf("电脑出的是布!\n");
t++;printf("你赢了!%d连胜!\n\n",t);q=0;
b--;
if(b==0){printf("百战百胜的时效到了!\n\n");n=0;}
}
else
if(y!=0)
{
printf("电脑晕菜中!\n");
t++;printf("你赢了!%d连胜!\n\n",t);q=0;
y--;
if(y==0)
{
j=rand()%3;
if(j==1||j==2){printf("电脑醒了,但你踩了他一脚,他又晕了!\n");y=5;}else{printf("电脑醒了!\n");n=0;}
}
system("pause");
}
else
if(z2!=0)
{
printf("电脑正在往回赶!\n");
t++;printf("你赢了!%d连胜!\n\n",t);q=0;
z2--;
if(z2==0){printf("他终于回来了!\n");n=0;}
system("pause");
}
else
if(z1!=0)
{
printf("电脑正在天上飞!\n");
t++;printf("你赢了!%d连胜!\n\n",t);q=0;
z1--;
if(z1==0){printf("电脑终于落地了!\n\n");z2=rand()%6+5;}
system("pause");
}
else
{
printf("电脑思考中!\n");
system("pause");
if(j==1) printf("电脑出的是剪刀 \n");
if(j==2) printf("电脑出的是石头!\n");
if(j==3) printf("电脑出的是布!\n");
o=rand()%4;
if(o<3&&h!=0){printf("电脑突然病发,晕过去了!");t++;printf("你赢了!%d连胜!加%d元钱!\n\n",t,t);q=0;m+=t;h--;if(h==0){printf("电脑的病好了!\n\n");n=0;}}else
if(i==j)printf("平局不算!\n\n");else
if(j-i==1){q++;printf("电脑赢了!电脑%d连胜!\n\n",q);t=0;}else
if(j==1&&i==3){q++;printf("电脑赢了!电脑%d连胜!\n\n",q);t=0;}else
{t++;printf("你赢了!%d连胜!加%d元钱!\n\n",t,t);m+=t;q=0;}
}
if(t==30){printf("强人!连胜三十回合!游戏结束!\n"); system("pause");return 0;}
if(q==10){printf("电脑十连胜,你不要活了……\n");system("pause");return 0;}
}else{printf("你个疯子!!!\n");system("pause");return 0;}
}
if(m>=10&&n==0){printf("你有%d元钱,是否购买百战百胜?10元 1:要 0:不要",m);scanf("%d",&v);if(v==1){printf("已购买!\n\n");m-=10;b=5;n=1;}}
if(m>=15&&n==0){printf("你有%d元钱,是否购买导弹飞飞?15元 1:要 0:不要",m);scanf("%d",&v);if(v==1){printf("你把电脑绑在导弹上,碰!!!\n\n");m-=15;z1=3;n=1;}}
if(m>=20&&n==0){printf("你有%d元钱,是否购买晕眩药?20元 1:要 0:不要",m);scanf("%d",&v);if(v==1){printf("电脑中了晕眩药!\n\n");m-=20;y=5;n=1;}}
if(m>=25&&n==0){printf("你有%d元钱,是否购买灰鸽子+熊猫烧香+蠕虫王?1:要 0:不要",m);scanf("%d",&v);if(v==1){printf("电脑中病毒了!\n\n");m-=25;h=20;n=1;}}
printf("1:剪刀 2:石头 3:布\n");
scanf("%ld",&k);
q1=-1;
}
printf("你个疯子!!!\n");
system("pause");
}
4、俄罗斯方块
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
using namespace std;
int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } };
int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } };
int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } };
int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } };
int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } };
int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } };
int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } };
int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } };
int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } };
int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } };
int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } };
int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } };
int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } };
void initialWindow(HANDLE hOut);//初始化窗口
void initialPrint(HANDLE hOut);//初始化界面
void gotoXY(HANDLE hOut, int x, int y);//移动光标
void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置
bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞
void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块
void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落
void myStop(HANDLE hOut, int block[4][4]);//游戏暂停
void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值
int main()
{
int map[21][12];
int blockA[4][4];//候选区的方块
int blockB[4][4];//下落中的方块
int positionX, positionY;//方块左上角的坐标
bool check;//检查方块还能不能下落
char key;//用来存储按键
int val;//用来控制下落速度
int fraction;//用来存储得分
int checkpoint;//用来存储关卡
int times;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
initialWindow(hOut);
initial:
gotoXY(hOut, 0, 0);
initialPrint(hOut);
check = true;
val = 50;
fraction = 0;
checkpoint = 1;
times = val;
for (int i = 0; i < 20; ++i)
{
for (int j = 1; j < 11; ++j)
{
map[i][j] = 0;
}
}
for (int i = 0; i < 20; ++i)
{
map[i][0] = map[i][11] = 1;
}
for (int i = 0; i < 12; ++i)
{
map[20][i] = 1;
}
srand((unsigned)time(NULL));
roundBlock(hOut, blockA);
while (true)
{
if (check)
{
eliminateRow(hOut, map, val, fraction, checkpoint);
check = false;
positionX = -3;
positionY = 4;
if (collisionDetection(blockA, map, positionX, positionY))
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
blockB[i][j] = blockA[i][j];
}
}
roundBlock(hOut, blockA);
}
else
{
gameOver(hOut, blockA, map);
goto initial;
}
}
printBlock(hOut, blockB, positionX, positionY);
if (_kbhit())
{
key = _getch();
switch (key)
{
case 72:
myUp(hOut, blockB, map, positionX, positionY);
break;
case 75:
myLeft(hOut, blockB, map, positionX, positionY);
break;
case 77:
myRight(hOut, blockB, map, positionX, positionY);
break;
case 80:
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
break;
case 32:
myStop(hOut, blockA);
break;
case 27:
exit(0);
default:
break;
}
}
Sleep(20);
if (0 == --times)
{
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
times = val;
}
}
cin.get();
return 0;
}
void initialWindow(HANDLE hOut)
{
SetConsoleTitle("俄罗斯方块");
COORD size = { 80, 25 };
SetConsoleScreenBufferSize(hOut, size);
SMALL_RECT rc = { 0, 0, 79, 24 };
SetConsoleWindowInfo(hOut, true, &rc);
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(hOut, &cursor_info);
}
void initialPrint(HANDLE hOut)
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
for (int i = 0; i < 20; ++i)
{
cout << "■ ■☆ ☆" << endl;
}
gotoXY(hOut, 26, 0);
cout << "☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 0, 20);
cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 26, 1);
cout << "分 数: ";
gotoXY(hOut, 26, 2);
cout << "关 卡: ";
gotoXY(hOut, 26, 4);
cout << "下一方块:";
gotoXY(hOut, 26, 9);
cout << "操作方法:";
gotoXY(hOut, 30, 11);
cout << "↑:旋转 ↓:速降";
gotoXY(hOut, 30, 12);
cout << "→:右移 ←:左移";
gotoXY(hOut, 30, 13);
cout << "空格键:开始/暂停";
gotoXY(hOut, 30, 14);
cout << "Esc 键:退出";
gotoXY(hOut, 26, 16);
cout << "关 于:";
gotoXY(hOut, 30, 18);
cout << "俄罗斯方块V1.0";
gotoXY(hOut, 35, 19);
cout << "作者:李国良";
}
void gotoXY(HANDLE hOut, int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void roundBlock(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
switch (rand() % 19)
{
case 0:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
break;
case 1:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
break;
case 2:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
break;
case 3:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
break;
case 4:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
break;
case 5:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
break;
case 6:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
break;
case 7:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
break;
case 8:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
break;
case 9:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
break;
case 10:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
break;
case 11:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
break;
case 12:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
break;
case 13:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
break;
case 14:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
break;
case 15:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
break;
case 16:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
break;
case 17:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
break;
case 18:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block18[i][j];
}
}
break;
default:
break;
}
printBlock(hOut, block, 5, 15);
}
bool collisionDetection(int block[4][4], int map[21][12], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1)
{
return false;
}
}
}
return true;
}
void printBlock(HANDLE hOut, int block[4][4], int x, int y)
{
switch (block[0][0])
{
case 10:
case 11:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
break;
case 12:
case 13:
case 14:
case 15:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 16:
case 17:
case 18:
case 19:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 20:
case 21:
case 22:
case 23:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 24:
case 25:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 26:
case 27:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 28:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
default:
break;
}
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
}
}
void clearBlock(HANDLE hOut, int block[4][4], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << " ";
}
}
}
}
}
void gameOver(HANDLE hOut, int block[4][4], int map[21][12])
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 9, 8);
cout << "GAME OVER";
gotoXY(hOut, 8, 9);
cout << "空格键:重来";
gotoXY(hOut, 8, 10);
cout << "ESC键:退出";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
return;
}
if (key == 27)
{
exit(0);
}
}
}
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y)
{
if (collisionDetection(block, map, x + 1, y))
{
clearBlock(hOut, block, x, y);
++x;
return 0;
}
if (x < 0)
{
return 2;
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
map[x + i][y + j] = 1;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
return 1;
}
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
--y;
}
}
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
++y;
}
}
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
switch (block[0][0])
{
case 10:
if (collisionDetection(block01, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
}
break;
case 11:
if (collisionDetection(block00, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
}
else if (collisionDetection(block00, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
--y;
}
else if (collisionDetection(block00, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
++y;
}
else if (collisionDetection(block00, map, x, y - 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y - 2;
}
else if (collisionDetection(block00, map, x, y + 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y + 2;
}
break;
case 12:
if (collisionDetection(block03, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
}
else if (collisionDetection(block03, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
--y;
}
else if (collisionDetection(block03, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
++y;
}
break;
case 13:
if (collisionDetection(block04, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
}
else if (collisionDetection(block04, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
--y;
}
else if (collisionDetection(block04, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
++y;
}
break;
case 14:
if (collisionDetection(block05, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
}
else if (collisionDetection(block05, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
--y;
}
else if (collisionDetection(block05, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
++y;
}
break;
case 15:
if (collisionDetection(block02, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
}
else if (collisionDetection(block02, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
--y;
}
else if (collisionDetection(block02, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
++y;
}
break;
case 16:
if (collisionDetection(block07, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
}
else if (collisionDetection(block07, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
--y;
}
else if (collisionDetection(block07, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
++y;
}
break;
case 17:
if (collisionDetection(block08, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
}
else if (collisionDetection(block08, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
--y;
}
else if (collisionDetection(block08, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
++y;
}
break;
case 18:
if (collisionDetection(block09, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
}
else if (collisionDetection(block09, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
--y;
}
else if (collisionDetection(block09, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
++y;
}
break;
case 19:
if (collisionDetection(block06, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
}
else if (collisionDetection(block06, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
--y;
}
else if (collisionDetection(block06, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
++y;
}
break;
case 20:
if (collisionDetection(block11, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
}
else if (collisionDetection(block11, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
--y;
}
else if (collisionDetection(block11, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
++y;
}
break;
case 21:
if (collisionDetection(block12, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
}
else if (collisionDetection(block12, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
--y;
}
else if (collisionDetection(block12, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
++y;
}
break;
case 22:
if (collisionDetection(block13, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
}
else if (collisionDetection(block13, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
--y;
}
else if (collisionDetection(block13, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
++y;
}
break;
case 23:
if (collisionDetection(block10, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
}
else if (collisionDetection(block10, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
--y;
}
else if (collisionDetection(block10, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
++y;
}
break;
case 24:
if (collisionDetection(block15, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
}
else if (collisionDetection(block15, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
--y;
}
else if (collisionDetection(block15, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
++y;
}
break;
case 25:
if (collisionDetection(block14, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
}
else if (collisionDetection(block14, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
--y;
}
else if (collisionDetection(block14, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
++y;
}
break;
case 26:
if (collisionDetection(block17, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
}
else if (collisionDetection(block17, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
--y;
}
else if (collisionDetection(block17, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
++y;
}
break;
case 27:
if (collisionDetection(block16, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
}
else if (collisionDetection(block16, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
--y;
}
else if (collisionDetection(block16, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
++y;
}
break;
default:
break;
}
}
void myStop(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 30, 7);
cout << "游戏暂停";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
gotoXY(hOut, 30, 7);
cout << " ";
printBlock(hOut, block, 5, 15);
return;
}
if (key == 27)
{
exit(0);
}
}
}
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint)
{
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 19; i >= 0; --i)
{
int x = 0;
for (int j = 1; j < 11; ++j)
{
x += map[i][j];
}
if (x == 10)
{
fraction += 100;
if (val > 1 && fraction / 1000 + 1 != checkpoint)
{
checkpoint = fraction / 1000 + 1;
val -= 5;
}
for (int m = i; m > 0; --m)
{
for (int n = 1; n < 11; ++n)
{
map[m][n] = map[m - 1][n];
gotoXY(hOut, 2 * n, m);
if (map[m][n] == 1)
{
cout << "■";
}
else
{
cout << " ";
}
}
}
++i;
}
}
gotoXY(hOut, 36, 1);
cout << fraction;
gotoXY(hOut, 36, 2);
cout << checkpoint;
}
5、打砖块
#include<string.h>
#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
int scr[22][50], q=14, w=30, ban=20, score=1, sx=-1 , zy=-1 ,speed=1, chang=30;
void moveqiu(void);
void penzhuangqiu(void);
void printf(void);
void menu(void);
main(void)
{
int i=0,j=0;
menu();
for(;j<50;j++)
scr[i][j]=4;
j=0;i=1;
;for(;i<3;i++)
for(j=2;j<48;j++)
scr[i][j]=3;
//这里设置砖头位置
scr[q][w]=2;
while(1)
{
for(j=0;j<20;j++)//将板位置归0
scr[21][ban+j]=0;
if(kbhit())//控制板的移动
switch(getch())
{
case'a':case'A':if(ban>1&&ban<=29) ban--;
break;
case'd':case'D':if(ban>0&&ban<=28) ban++;
break;
case'w':case'W':getch();break;//暂停键
}
for(j=0;j<20;j++)//建立板位置为1
scr[21][ban+j]=1;
penzhuangqiu();
moveqiu();
printf();
}
}
void penzhuangqiu(void)
{
int b;
b=0;
if(zy==1)//以下四个if用来检测撞砖,并改变球的运动方向
if(scr[q][w+1]==3||w+1==49)
{
scr[q][w+1]=0;
zy=-zy;
b=1;
}
if(zy==-1)//判断运动又有方向
if(scr[q][w-1]==3||w-1==0)
{
scr[q][w-1]=0;
zy=-zy;
b=1;//用来确认是否敲打左右上下的方块
}
if(sx==1)
if(scr[q-1][w]==3||q-1==0)
{
scr[q-1][w]=0;
sx=-sx;
b=1;
}
if(sx==-1)
if(scr[q+1][w]==3||q+1==21)
{
if(scr[q+1][w]==3)
{scr[q+1][w]=0;
sx=-sx;
b=1;
}
if(q+1==21)
{if(scr[q+1][w]==1)
sx=-sx;
else
{
printf("lose!!!!!!退出游戏");
if(getch())
main();
}
}
}
if(b==0)//斜的砖块
{
if(zy==1&&sx==1)
if(scr[q-1][w+1]==3)
{
scr[q-1][w+1]=0;
zy=-zy;
sx=-sx;
}
if(zy==-1&&sx==1)
if(scr[q-1][w-1]==3)
{
scr[q-1][w-1]=0;
zy=-zy;
sx=-sx;
}
if(zy==1&&sx==-1)
if(scr[q+1][w+1]==3)
{
scr[q+1][w+1]=0;
zy=-zy;
sx=-sx;
}
if(zy==-1&&sx==-1)
if(scr[q+1][w-1]==3)
{
scr[q+1][w-1]=0;
zy=-zy;
sx=-sx;
}//斜砖完毕
}
}
void moveqiu(void)
{
scr[q][w]=0;//使原来的球位置为0,并建立新的球位置
if(sx==1&&zy==1)
{
q--;w++;
}
if(sx==1&&zy==-1)
{
q--;w--;
}
if(sx==-1&&zy==1)
{
q++;w++;
}
if(sx==-1&&zy==-1)
{
q++;w--;
}
scr[q][w]=2;
}
void printf(void)
{
int i, j;
system("cls");
for(i=0;i<22;i++)
{
for(j=0;j<50;j++)
{
if(scr[i][j]==0)printf(" ");
if(scr[i][j]==1)printf("\3");
if(scr[i][j]==2)printf("\4");
if(scr[i][j]==3)printf("\5");
if(scr[i][j]==4)printf("_");
if(j==49)
printf("|\n");
}
}
}
void menu(void)
{
q=14, w=30, ban=20, score=1, sx=-1 , zy=-1 ,speed=1, chang=30;memset(scr,0, sizeof(int)*1100);
system("cls");
printf("A向左,D向右,W暂停\n开始游戏:任意键\n ");
if(getch()) printf("aaaaaa");
}
6、无名游戏
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
using namespace std;
/*=============== all the structures ===============*/
typedef struct Frame
{
COORD position[2];
int flag;
}Frame;
/*=============== all the functions ===============*/
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)// set cursor
{
COORD pos={i, j};
SetPos(pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y);
for(int i = 0; i <= (x2-x1); i++)
cout<<ch;
}
//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{
if(a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch);
else
{
SetPos(0, 25);
cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
system("pause");
}
}
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{
int y=y1;
while(y!=y2+1)
{
SetPos(x, y);
cout<<ch;
y++;
}
}
//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch)
{
if(a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch);
else
{
SetPos(0, 25);
cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
system("pause");
}
}
//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD b, char row, char col)
{
drawRow(a.Y, a.X+1, b.X-1, row);
drawRow(b.Y, a.X+1, b.X-1, row);
drawCol(a.X, a.Y+1, b.Y-1, col);
drawCol(b.X, a.Y+1, b.Y-1, col);
}
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);
}
void drawFrame(Frame frame, char row, char col)
{
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
void drawPlaying()
{
drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
drawFrame(49, 0, 79, 4, '-', '|');// draw output frame
drawFrame(49, 4, 79, 9, '-', '|');// draw score frame
drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame
drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame
SetPos(52, 6);
cout<<"得分:";
SetPos(52, 7);
cout<<"称号:";
SetPos(52,10);
cout<<"操作方式:";
SetPos(52,12);
cout<<" a,s,d,w 控制战机移动。";
SetPos(52,14);
cout<<" p 暂停游戏。";
SetPos(52,16);
cout<<" e 退出游戏。";
}
//在[a, b)之间产生一个随机整数
int random(int a, int b)
{
int c=(rand() % (a-b))+ a;
return c;
}
//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{
int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;
}
bool judgeCoordInFrame(Frame frame, COORD spot)
{
if(spot.X>=frame.position[0].X)
if(spot.X<=frame.position[1].X)
if(spot.Y>=frame.position[0].Y)
if(spot.Y<=frame.position[0].Y)
return true;
return false;
}
void printCoord(COORD a)
{
cout <<"( "<<a.X<<" , "<<a.Y<<" )";
}
void printFrameCoord(Frame a)
{
printCoord(a.position[0]);
cout <<" - ";
printCoord(a.position[1]);
}
int drawMenu()
{
SetPos(30, 1);
cout<<"P l a n e W a r";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
cout<<"w 和 s 选择, k 确定";
SetPos(15, 11);
cout<<"1. 简单的敌人";
SetPos(15, 13);
cout<<"2. 冷酷的敌人";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度。";
SetPos(24, 21);
cout<<"制作:LJF神犇";
int j=11;
SetPos(12, j);
cout<<">>";
//drawFrame(45, 9, 79, 17, '=', '|');
while(1)
{ if( _kbhit() )
{
char x=_getch();
switch (x)
{
case 'w' :
{
if( j == 13)
{
SetPos(12, j);
cout<<" ";
j = 11;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度,比较容易对付";
}
break;
}
case 's' :
{
if( j == 11 )
{
SetPos(12, j);
cout<<" ";
j = 13;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"冷酷的敌人:";
SetPos(51, 13);
cout<<"冷酷的敌人移动速度较快,难对付哟。";
}
break;
}
case 'k' :
{
if (j == 8) return 1;
else return 2;
}
}
}
}
}
/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
//DWORD OBJ;
sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
return 0;
}
*/
/*================== the Game Class ==================*/
class Game
{
public:
COORD position[10];
COORD bullet[10];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;
Game ();
//初始化所有
void initPlane();
void initBullet();
void initEnemy();
//初始化其中一个
//void initThisBullet( COORD );
//void initThisEnemy( Frame );
void planeMove(char);
void bulletMove();
void enemyMove();
//填充所有
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();
//填充其中一个
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );
void Pause();
void Playing();
void judgePlane();
void judgeEnemy();
void Shoot();
void GameOver();
void printScore();
};
Game::Game()
{
initPlane();
initBullet();
initEnemy();
score = 0;
rank = 25;
rankf = 0;
flag_rank = 0;
}
void Game::initPlane()
{
COORD centren={39, 22};
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i<=4; i++)
position[i].Y=centren.Y;
for(int i=6; i<=8; i++)
position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;
}
void Game::drawPlane()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
if(i!=5)
cout<<"O";
else if(i==5)
cout<<"|";
}
}
void Game::drawPlaneToNull()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
cout<<" ";
}
}
void Game::initBullet()
{
for(int i=0; i<10; i++)
bullet[i].Y = 30;
}
void Game::drawBullet()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
SetPos(bullet[i]);
cout<<"^";
}
}
}
void Game::drawBulletToNull()
{
for(int i=0; i<10; i++)
if( bullet[i].Y != 30 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
SetPos(pos);
cout<<" ";
}
}
void Game::initEnemy()
{
COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i<8; i++)
{
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
void Game::drawEnemy()
{
for(int i=0; i<8; i++)
drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
void Game::drawEnemyToNull()
{
for(int i=0; i<8; i++)
{
drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}
}
void Game::Pause()
{
SetPos(61,2);
cout<<" ";
SetPos(61,2);
cout<<"暂停中...";
char c=_getch();
while(c!='p')
c=_getch();
SetPos(61,2);
cout<<" ";
}
void Game::planeMove(char x)
{
if(x == 'a')
if(position[1].X != 1)
for(int i=0; i<=9; i++)
position[i].X -= 2;
if(x == 's')
if(position[7].Y != 23)
for(int i=0; i<=9; i++)
position[i].Y += 1;
if(x == 'd')
if(position[4].X != 47)
for(int i=0; i<=9; i++)
position[i].X += 2;
if(x == 'w')
if(position[5].Y != 3)
for(int i=0; i<=9; i++)
position[i].Y -= 1;
}
void Game::bulletMove()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
bullet[i].Y -= 1;
if( bullet[i].Y == 1 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
drawThisBulletToNull( pos );
bullet[i].Y=30;
}
}
}
}
void Game::enemyMove()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<2; j++)
enemy[i].position[j].Y++;
if(24 == enemy[i].position[1].Y)
{
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
void Game::judgePlane()
{
for(int i = 0; i < 8; i++)
for(int j=0; j<9; j++)
if(judgeCoordInFrame(enemy[i], position[j]))
{
SetPos(62, 1);
cout<<"坠毁";
drawFrame(enemy[i], '+', '+');
Sleep(1000);
GameOver();
break;
}
}
void Game::drawThisBulletToNull( COORD c)
{
SetPos(c);
cout<<" ";
}
void Game::drawThisEnemyToNull( Frame f )
{
drawFrame(f, ' ', ' ');
}
void Game::judgeEnemy()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 10; j++)
if( judgeCoordInFrame(enemy[i], bullet[j]) )
{
score += 5;
drawThisEnemyToNull( enemy[i] );
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull( bullet[j] );
bullet[j].Y = 30;
}
}
void Game::Shoot()
{
for(int i=0; i<10; i++)
if(bullet[i].Y == 30)
{
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y-1;
break;
}
}
void Game::printScore()
{
if(score == 120 && flag_rank == 0)
{
rank -= 3;
flag_rank = 1;
}
else if( score == 360 && flag_rank == 1)
{
rank -= 5;
flag_rank = 2;
}
else if( score == 480 && flag_rank == 2)
{
rank -= 5;
flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
cout<<score;
if( rank!=rankf )
{
SetPos(60, 7);
if( x == 5)
title="初级飞行员";
else if( x == 4)
title="中级飞行员";
else if( x == 3)
title="高级飞行员";
else if( x == 2 )
title="王牌飞行员";
cout<<title;
}
rankf = rank;
}
void Game::Playing()
{
//HANDLE MFUN;
//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
drawEnemy();
drawPlane();
int flag_bullet = 0;
int flag_enemy = 0;
while(1)
{
Sleep(8);
if(_kbhit())
{
char x = _getch();
if ('a' == x || 's' == x || 'd' == x || 'w' == x)
{
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if ('p' == x)
Pause();
else if( 'k' == x)
Shoot();
else if( 'e' == x)
{
//CloseHandle(MFUN);
GameOver();
break;
}
}
/* 处理子弹 */
if( 0 == flag_bullet )
{
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if( 5 == flag_bullet )
flag_bullet = 0;
/* 处理敌人 */
if( 0 == flag_enemy )
{
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if( flag_enemy >= rank )
flag_enemy = 0;
/* 输出得分 */
printScore();
}
}
void Game::GameOver()
{
system("cls");
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, '=', '|');
SetPos(36,12);
string str="Game Over!";
for(int i=0; i<str.size(); i++)
{
Sleep(80);
cout<<str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
cout<<"击落敌机:"<<score/5<<" 架";
SetPos(31, 12);
cout<<"得 分:"<<score;
SetPos(31, 13);
cout<<"获得称号:"<<title;
SetPos(30, 16);
Sleep(1000);
cout<<"继续? 是(y)| 否(n)制作:最牛的LJF";
as:
char x=_getch();
if (x == 'n')
exit(0);
else if (x == 'y')
{
system("cls");
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
else goto as;
}
/*================== the main function ==================*/
int main()
{
//游戏准备
srand((int)time(0)); //随机种子
HideCursor(); //隐藏光标
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
7、贪吃蛇(2)
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
// 刷新当前屏幕
inline void Refresh(char q[][22], int grade, int gamespeed){
system("cls"); // 清屏
int i,j;
cout << endl;
for(i=0;i<22;i++){
cout << "\t";
for(j=0;j<22;j++)
cout<<q[i][j]<<' '; // 输出贪吃蛇棋盘
if(i==0) cout << "\t等级为:" << grade;
if(i==4) cout << "\t自动前进时间";
if(i==6) cout << "\t间隔为:" << gamespeed << "ms";
cout<<endl;
}
}
int main(){
char tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
int i,j;
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
tcsQipan[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分
for(i=0;i<=21;i++)
tcsQipan[0][i] = tcsQipan[21][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for(i=1;i<=20;i++)
tcsQipan[i][0] = tcsQipan[i][21] = '|'; //初始化贪吃蛇棋盘左右墙壁
int tcsZuobiao[2][100]; //蛇的坐标数组
for(i=0; i<4; i++){
tcsZuobiao[0][i] = 1;
tcsZuobiao[1][i] = i + 1;
}
int head = 3,tail = 0;
for(i=1;i<=3;i++)
tcsQipan[1][i]='*'; //蛇身
tcsQipan[1][4]='#'; //蛇头
int x1, y1; // 随机出米
srand(time(0));
do{
x1=rand()%20+1;
y1=rand()%20+1;
}while(tcsQipan[x1][y1]!=' ');
tcsQipan[x1][y1]='=';
cout<<"\n\n\t\t贪吃蛇游戏即将开始 !"<<endl;//准备开始;;
long start;
int grade = 1, length = 4;
int gamespeed = 500; //前进时间间隔
for(i=3;i>=0;i--){
start=clock();
while(clock()-start<=1000);
system("cls");
if(i>0)
cout << "\n\n\t\t进入倒计时:" << i << endl;
else
Refresh(tcsQipan,grade,gamespeed);
}
int timeover;
char direction = 77; // 初始情况下,向右运动
int x,y;
while(1){
timeover = 1;
start = clock();
while((timeover=(clock()-start<=gamespeed))&&!kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if(timeover){
getch();direction = getch();
}
switch(direction){
case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上
case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下
case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左
case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1; // 向右
}
if(!(direction==72||direction==80||direction==75 ||direction==77)){ // 按键非方向键
cout << "\tGame over!" << endl;return 0;
}
if(x==0 || x==21 ||y==0 || y==21){ // 碰到墙壁
cout << "\tGame over!" << endl;return 0;
}
if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ // 蛇头碰到蛇身
cout << "\tGame over!" << endl;return 0;
}
if(x==x1 && y==y1){ // 吃米,长度加1
length ++;
if(length>=8){
length -= 8;
grade ++;
if(gamespeed>=200)
gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
}
tcsQipan[x][y]= '#';
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
head = (head+1)%100;
tcsZuobiao[0][head] = x;
tcsZuobiao[1][head] = y;
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}while(tcsQipan[x1][y1]!=' ');
tcsQipan[x1][y1]='=';
Refresh(tcsQipan,grade,gamespeed);
}
else{ // 不吃米
tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';
tail=(tail+1)%100;
tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';
head=(head+1)%100;
tcsZuobiao[0][head]=x;
tcsZuobiao[1][head]=y;
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';
Refresh(tcsQipan,grade,gamespeed);
}
}
return 0;
}
8、五子棋
#include<iostream>
#include<vector>
using namespace std;
class qipan
{
public:
qipan() {}
~qipan() {};
//向上下左右,斜的方向
char left(int x, int y)
{//检查是否合适
if (x >= 1 && x <= hight&& y - 1 >= 1 && y - 1 <= width)
{
return q[x][y - 1];
}
else {
return 'F';
}
}
char right(int x, int y)
{
if (x >= 1 && x <= hight&&y + 1 >= 1 && y + 1 <= width)
{
return q[x][y + 1];
}
else {
return 'F';
}
}
char up(int x, int y)
{
if (x - 1 >= 1 && x - 1 <= hight && y >= 1 && y <= width)
{
return q[x - 1][y];
}
else {
return 'F';
}
}
char down(int x, int y)
{
if (x + 1 >= 1 && x + 1 <= hight && y >= 1 && y <= width)
{
return q[x + 1][y];
}
else {
return 'F';
}
}
char left_up(int x, int y)
{
if (x - 1 >= 1 && x - 1 <= hight && y - 1 >= 1 && y - 1 <= width)
{
return q[x - 1][y - 1];
}
else {
return 'F';
}
}
char left_down(int x, int y)
{
if (x + 1 >= 1 && x + 1 <= hight && y - 1 >= 1 && y - 1 <= width)
{
return q[x + 1][y - 1];
}
else {
return 'F';
}
}
char right_up(int x, int y)
{
if (x - 1 >= 1 && x - 1 <= hight && y + 1 >= 1 && y + 1 <= width)
{
return q[x - 1][y + 1];
}
else {
return 'F';
}
}
char right_down(int x, int y)
{
if (x + 1 >= 1 && x + 1 <= hight && y + 1 >= 1 && y + 1 <= width)
{
return q[x + 1][y + 1];
}
else {
return 'F';
}
}
void init_cur() {
h_cur = hang;
l_cur = lie;
}
bool win()
{
bool WIN = false;
char temp = q[hang][lie];
//以上为例,每次先看上面的5个,假如一样,iter++;否则 break;再加上下的方向,同样iter++;最后iter+1==5,WIN=true;并且退出
//各个方向重复,上下左右,斜着的。
//赢了直接退出。
//显示是谁赢了
//左右
int count_lr = 1;
init_cur();
for (int i = 0; i < 4; i++)
{
if (left(h_cur, l_cur) == temp)
count_lr++;
else
break;
l_cur--;
}
init_cur();
for (int i = 0; i < 4; i++)
{
if (right(h_cur, l_cur) == temp)
count_lr++;
else
break;
l_cur++;
}
if (count_lr == 5)
WIN = true;
//上下
int count_ud = 1;
init_cur();
for (int i = 0; i < 4; i++)
{
if (up(h_cur, l_cur) == temp)
count_ud++;
else
break;
h_cur--;
}
init_cur();
for (int i = 0; i < 4; i++)
{
if (down(h_cur, l_cur) == temp)
count_ud++;
else
break;
h_cur++;
}
if (count_ud == 5)
WIN = true;
//左斜
int count_lt = 1;
init_cur();
for (int i = 0; i < 4; i++)
{
if (left_up(h_cur, l_cur) == temp)
count_lt++;
else
break;
h_cur--;
l_cur--;
}
init_cur();
for (int i = 0; i < 4; i++)
{
if (left_down(h_cur, l_cur) == temp)
count_lt++;
else
break;
h_cur++;
l_cur--;
}
if (count_lt == 5)
WIN = true;
//右边斜
int count_rt = 1;
init_cur();
for (int i = 0; i < 4; i++)
{
if (right_up(h_cur, l_cur) == temp)
count_rt++;
else
break;
h_cur--;
l_cur++;
}
init_cur();
for (int i = 0; i < 4; i++)
{
if (right_down(h_cur, l_cur) == temp)
count_rt++;
else
break;
h_cur++;
l_cur++;
}
if (count_rt == 5)
WIN = true;
return WIN;
}
void qipan_array()
{
for (int i = 0; i < hight; i++) {
for (int j = 0; j < width; j++)
q[i][j] = '+';
}
}
void prin_qipan()
{
//打印二维数组;每一行要打印上行号,以及列号
for (int i = 0; i <hight; i++)
{
for (int j = 0; j < width; j++)
{
cout << q[i][j] << " ";
}
cout << i;
cout << endl;
}
for (int j = 0; j <width; j++)
{
cout << j << " ";
}
cout << endl << "________________________________" << endl;
}
int xiaqi(int player)
{
if (player == 1) {
q[hang][lie] = '*';
}
else if (player == 2)
{
q[hang][lie] = '@';
}
else
cout << "input player is wrong" << endl;
return 0;
}
//初始化行列
int gethang(int h)
{
hang = h;
return 0;
}
int getlie(int l)
{
lie = l;
return 0;
}
int h_cur;
int l_cur;
const int hight = 9;
const int width = 9;
int hang; int lie;
char q[9][9];
};
int main()
{
int hang, lie;
qipan wzq;
wzq.qipan_array();
cout << "A 与B 玩五子棋" << endl;
cout << "A use * and B use @" << endl;
cout << "________________________________" << endl;
for (int i = 0; i < 15; i++) {
cout << "A 输入行: ";
cin >> hang;
cout << "A 输入列: ";
cin >> lie;
if (wzq.q[hang][lie] != '+')
cout << "输入的行列数字已经有人占据了" << endl;
else {
wzq.gethang(hang);
wzq.getlie(lie);
wzq.xiaqi(1);
wzq.prin_qipan();
if (wzq.win())
{
cout << "A is winner" << endl;
exit(0);
}
}
//b 开始了
cout << "B 输入行: ";
cin >> hang;
cout << "B 输入列: ";
cin >> lie;
if (wzq.q[hang][lie] != '+')
cout << "输入的行列数字已经有人占据了" << endl;
else {
wzq.gethang(hang);
wzq.getlie(lie);
wzq.xiaqi(2);
wzq.prin_qipan();
if (wzq.win())
{
cout << "B is winner" << endl;
exit(0);
}
}
}
return 0;
}
我只知到贪吃蛇的。
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define framex 5
#define framey 5
#define wide 20
#define high 20
int i,j,a[2];
//将光标移动到指定位置
void gotoxy(HANDLE hout,int x,int y){
//COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标
COORD pos;
pos.X=x;
pos.Y=y;
//SetConsoleCursorPosition是API中定位光标位置的函数。
SetConsoleCursorPosition(hout,pos);
}
//游戏封面
void cover (HANDLE hout){
gotoxy(hout,framex+wide,framey);
printf("欢迎使用贪吃蛇游戏2.0");
gotoxy(hout,framex+wide,framey+5);
printf("开始游戏前请关闭中文输入法");
gotoxy(hout,framex+wide*2,framey+20);
printf( "游戏制作者:zzy");
gotoxy(hout,framex+wide*2,framey+22);
printf("制作时间:2017年12月");
char a;
a=getchar();
system("cls");
}
//定义蛇的结构体
struct Snake{
int x[100];
int y[100];
int speed;
int length;
int count;
};
//定义食物的结构体
struct Food{
int x;
int y;
};
//制作框架
void makeframe(struct Snake snake){
//定义显示器变量句柄
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
gotoxy(hout,framex+wide*2+5,framey);
printf( " 贪吃蛇游戏");
gotoxy(hout,framex+wide*2+5,framey+3);
printf("使用方向键或wasd移动");
gotoxy(hout,framex+wide*2+5,framey+5);
printf("长按方向键可加速");
gotoxy(hout,framex+wide*2+5,framey+7);
printf("按任意键暂停,方向键继续");
//打印上边框
for(i=0;i<wide*2+1;i++){
gotoxy(hout,framex+i,framey);
printf("*");
}
//打印下边框
for(i=0;i<wide*2+2;i++){
gotoxy(hout,framex+i,framey+high);
printf("*");
}
//打印左边框
for(i=0;i<high;i++){
gotoxy(hout,framex,framey+i);
printf("*");
}
//打印右边框
for(i=0;i<high;i++){
gotoxy(hout,framex+wide*2+1,framey+i);
printf("*");
}
}
//游戏信息
void infor(HANDLE hout,struct Snake* snake){
gotoxy(hout,framex+wide*2+5,framey+15);
printf("当前速度:%d",-snake->speed+500);
gotoxy(hout,framex+wide*2+5,framey+17);
printf("当前得分:%d",snake->count);
gotoxy(hout,framex+wide*2+5,framey+19);
printf("当前长度:%d",snake->length);
}
//初始化蛇
void initsnake(struct Snake *snake){
snake->x[0]=framex+2;
snake->y[0]=framey+high/2;
snake->count=0;
snake->length=3;
snake->speed=400;
//初始化由蛇尾至蛇头的坐标
for(i=1;i<snake->length;i++){
snake->x[i]=snake->x[i-1]+1;
snake->y[i]=snake->y[i-1];
}
}
//打印蛇
void printsnake(HANDLE hout ,struct Snake *snake){
for(i=0;i<snake->length;i++){
gotoxy(hout,snake->x[i],snake->y[i]);
if(i==snake->length-1)
printf("@");
else if(i==0)
printf("-");
else
printf("=");
}
}
//移动蛇
void movesnake(HANDLE hout,struct Snake *snake){
gotoxy(hout,snake->x[0],snake->y[0]);
//清除蛇尾
printf(" ");
//蛇的后一节坐标变成前一节的坐标
for(i=1;i<snake->length;i++){
snake->x[i-1]=snake->x[i];
snake->y[i-1]=snake->y[i];
}
}
//打印食物
void printfood (HANDLE hout,struct Snake *snake,struct Food* food){
//用计算机时间获取随机值
srand((unsigned)time(NULL));
while(1){
//将食物的横纵坐标的大小限定在窗口大小内
food->x=rand()%(wide-2)+1;
food->y=rand()%(high-2)+1;
//避免食物出现在边框上
if(food->x==0&&food->y==0)
continue;
//将食物的坐标放置在窗口内
food->x=2*food->x+framex;
food->y+=framey;
//如果食物出现在蛇上,重新产生食物
for(i=0;i<snake->length;i++){
if(food->x==snake->x[i]&&food->y==snake->y[i])
break;
}
//食物不在蛇上,打印食物,结束循环
if(i==snake->length){
gotoxy(hout,food->x,food->y);
printf("*");
break;
}
}
}
//吃食物
void eatfood(HANDLE hout,struct Snake * snake,struct Food *food){
//如果蛇头的坐标等于食物的坐标
if(snake->x[snake->length-1]==food->x&&snake->y[snake->length-1]==food->y){
//蛇长加一
snake->length++;
//蛇身(不包括蛇尾)整体像前移动一格
for(i=snake->length-1;i>0;i--){
snake->x[i]=snake->x[i-1];
snake->y[i]=snake->y[i-1];
}
//得到蛇尾的坐标(即蛇移动前的蛇尾坐标)
snake->x[0]=a[0];
snake->y[0]=a[1];
printfood(hout,snake,food);
snake->count++;
if(snake->count%3==0)
snake->speed-=50;
}
}
//死亡判断
int ifdead(struct Snake* snake){
//如果碰到左边界 返回0;
if(snake->x[snake->length-1]==framex)
return 0;
//如果碰到右边界 返回0;
if(snake->x[snake->length-1]==framex+wide*2)
return 0;
//如果碰到上边界 返回0;
if(snake->y[snake->length-1]==framey)
return 0;
//如果碰到下边界 返回0;
if(snake->y[snake->length-1]==framey+high)
return 0;
//如果碰到自己身体 返回0;
for(i=0; i<snake->length-1; i++)
if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] )
return 0;
//否则返回1;
return 1;
}
//开始游戏
int main(){
unsigned char ch =77;
//定义显示器句柄
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
cover(hout);
struct Snake s, *snake=&s;
struct Food f, *food=&f;
makeframe(*snake);
initsnake(snake);
printfood(hout,snake,food);
Sleep(500);
while(1){
infor(hout,snake);
//保存蛇尾横纵坐标
a[0]=snake->x[0];
a[1]=snake->y[0];
j=0;
//如果用户敲击键盘
if(kbhit()){
//用ch接收输入 ,注意是getch(),不需敲击回车
ch=getch();
if(kbhit()){
//长按时间
Sleep(20);
j=1;
}
}
switch(ch){
//向上移动
case'W':
case 'w' :
case 72:{
movesnake(hout,snake);
//蛇头纵坐标减一
snake->y[snake->length-1]-=1;
break;
}
//向下移动
case'S':
case 's':
case 80:{
movesnake(hout,snake);
//蛇头纵坐标加一
snake->y[snake->length-1]+=1;
break;
}
//向左移动
case'A':
case 'a':
case 75:{
movesnake(hout,snake);
//蛇头横坐标减二
snake->x[snake->length-1]-=2;
break;
}
//向右移动
case'D':
case 'd':
case 77:{
movesnake(hout,snake);
//蛇头横坐标加二
snake->x[snake->length-1]+=2;
break;
}
}
eatfood(hout,snake,food);
printsnake(hout,snake);
//如果判断蛇死亡,跳出循环
if(ifdead(snake)==0||ch==27||snake->speed==0){
gotoxy(hout,framex+wide/2,framey-2);
if(snake->speed==0)
printf("恭喜你通关了!!!");
else
printf("糟糕 T_T");
break;
}
if(j==0)
//如果没有长按,蛇滞留的时间为
Sleep(snake->speed);
//如果长按,蛇滞留的时间为 20ms,即加速
else
Sleep(20);
}
//跳出循环时 ,滞留死亡场景一段时间
Sleep(3000);
//清屏
system("cls");
//显示结束界面
if(snake->speed==0)
printf("\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你得到了满分:24\n\n\n");
else
printf("\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:%d\n\n\n",snake->count);
Sleep(3000);
return 0;
}
uses crt;
var
rf:array[1..10000] of longint;
a,b,c,r,p,k,oo,m:longint;
o:char;
t,tt,t3:text;
procedure save;
begin
assign(t,'d:\mgr\user.dll');
rewrite(t);
write(t,rf[1]);
close(t);
assign(tt,'d:\mgr\user1.dll');
rewrite(tt);
write(tt,m);
close(tt);
assign(t3,'d:\mgr\user2.dll');
rewrite(t3);
write(t3,rf[6]);
close(t3);
writeln('保存成功!!!');
delay(500);
end;
procedure wt;
begin
writeln('火星的经验:',rf[1],' 生命值:',rf[2],' 能量值:',rf[3]);
writeln('对手的难度:',a,' 生命值:',b,' 能量值:',c);
writeln('1:爪击(0) 2:躲避防御(10) 3:预言之力(20) 4:群星闪耀(35) 5:觉醒之技');
delay(1000);
end;
procedure juexing;
var
ax:longint;
begin
writeln('技1:光明之火(1)');
read(ax);
if ax=1 then if rf[6]>1 then begin b:=b-round(rf[2]*1.7); rf[4]:=rf[4]+round(rf[4]*0.3); rf[6]:=rf[6]-1; end else if (ax=1) and (rf[6]<1) then writeln('能量不足');
end;
procedure fight;
begin
writeln('你的回合');
wt;
read(r);
if r=1 then b:=b-rf[4];
if (r=2) and (rf[3]>=10) then begin rf[2]:=rf[2]+round(rf[2]*0.2); rf[3]:=rf[3]-10; end else if (r=2) and (rf[3]<10) then writeln('能量不足');
if (r=3) and (rf[3]>=20)then begin b:=b-rf[4] div 2; rf[5]:=rf[5]+round(rf[5]*0.3); rf[3]:=rf[3]-20; end else if (r=3) and (rf[3]<20) then writeln('能量不足');
if (r=4) and (rf[3]>=35)then begin rf[4]:=rf[4]+round(rf[4]*0.3); rf[5]:=rf[5]+round(rf[5]*0.5); rf[3]:=rf[3]-35; end else if (r=4) and (rf[3]<35) then writeln('能量不足');
if r=5 then juexing;
end;
procedure back;
begin
rf[2]:=50+round(rf[1]/3); rf[3]:=20+round(rf[1]/6); rf[4]:=10+round(rf[1]/10); rf[5]:=3+round(rf[1]/15);
end;
procedure kingtigerm;
var
pp:longint;
bb:boolean;
begin
pp:=1;
writeln('战斗!');
delay(1000);
a:=20; b:=1000; c:=120;
repeat
if b<=0 then begin bb:=true; break; end;
if rf[2]<=0 then begin bb:=false; break; end;
fight;
writeln('对手的回合');
delay(1500);
rf[2]:=rf[2]-(c-rf[5]);
wt;
delay(1500);
clrscr;
until pp=0;
if bb=true then begin writeln('战斗胜利!'); writeln('+150经验值 +20星灵'); rf[1]:=rf[1]+150; m:=m+20; end;
if bb=false then begin writeln('战斗失败:-('); end;
back;
end;
procedure kingtigere;
var
pp:longint;
bb:boolean;
begin
pp:=1;
writeln('战斗!');
delay(1000);
a:=5; b:=100; c:=20;
repeat
if b<=0 then begin bb:=true; break; end;
if rf[2]<=0 then begin bb:=false; break; end;
fight;
writeln('对手的回合');
rf[2]:=rf[2]-(c-rf[5]);
delay(1500);
wt;
delay(1500);
clrscr;
until pp=0;
if bb=true then begin writeln('战斗胜利!'); writeln('+30经验值 +5星灵'); rf[1]:=rf[1]+30; m:=m+5; end;
if bb=false then begin writeln('战斗失败:-('); end;
back;
end;
procedure xiaobing;
var
pp:longint;
bb:boolean;
begin
pp:=1;
writeln('战斗!');
delay(1000);
a:=1; b:=30; c:=5;
repeat
if b<=0 then begin bb:=true; break; end;
if rf[2]<=0 then begin bb:=false; break; end;
fight;
writeln('对手的回合');
delay(1500);
rf[2]:=rf[2]-(c-rf[5]);
wt;
delay(1500);
clrscr;
until pp=0;
if bb=true then begin writeln('战斗胜利!'); writeln('+5经验值 +1星灵'); rf[1]:=rf[1]+5; m:=m+1; end;
if bb=false then begin writeln('战斗失败:-('); end;
back;
end;
procedure kingtigerh;
var
pp:longint;
bb:boolean;
begin
pp:=1;
writeln('战斗!');
delay(1000);
a:=50; b:=12000; c:=1400;
repeat
if b<=0 then begin bb:=true; break; end;
if rf[2]<=0 then begin bb:=false; break; end;
fight;
writeln('对手的回合');
delay(1500);
rf[2]:=rf[2]-(c-rf[5]);
wt;
delay(1500);
clrscr;
until pp=0;
if bb=true then begin writeln('战斗胜利!'); writeln('+500经验值 +50星灵'); rf[1]:=rf[1]+500; m:=m+50; end;
if bb=false then begin writeln('战斗失败:-('); end;
back;
end;
procedure kingtigerl;
var
pp:longint;
bb:boolean;
begin
pp:=1;
writeln('战斗!');
delay(1000);
a:=350; b:=450000; c:=50000;
repeat
if b<=0 then begin bb:=true; break; end;
if rf[2]<=0 then begin bb:=false; break; end;
fight;
writeln('对手的回合');
delay(1500);
rf[2]:=rf[2]-(c-rf[5]);
wt;
delay(1500);
clrscr;
until pp=0;
if bb=true then begin writeln('战斗胜利!'); writeln('+3500经验值 +350星灵'); rf[1]:=rf[1]+3500; m:=m+350; end;
if bb=false then begin writeln('战斗失败:-('); end;
back;
end;
procedure kingtiger;
var
ss:longint;
begin
writeln('请选择难度');
writeln('1:简单 2:中等 3:困难 4:炼狱');
read(ss);
case ss of
1:kingtigere;
2:kingtigerm;
3:kingtigerh;
4:kingtigerl;
end;
end;
procedure fightline;
var aaa:longint;
begin
writeln('选择你的对手');
writeln('1:黑森林小兵 2:虎星');
read(aaa);
case aaa of
1:xiaobing;
2:kingtiger;
end;
end;
procedure shangdian;
var
kkk:longint;
begin
writeln('拥有星灵:',m);
writeln('1:觉醒能量*1: 售价:200星灵');
read(kkk);
case kkk of
1:if m<200 then writeln('星灵不足!') else begin m:=m-200; rf[6]:=rf[6]+1; writeln('购买成功!'); end;
end;
end;
begin
assign(t,'d:\mgr\user.dll');
assign(tt,'d:\mgr\user1.dll');
assign(t3,'d:\mgr\user2.dll');
oo:=1;
rf[1]:=1; rf[2]:=50; rf[3]:=20;
rf[4]:=10; rf[5]:=3;
writeln('群星之战V0.2.3');
delay(1000);
writeln('按b重新开始');
writeln('按s继续');
writeln('按e退出');
writeln('按o打开设置');
repeat
read(o);
if o='s' then begin reset(t); read(t,rf[1]); close(t); reset(tt); read(tt,m); close(tt); reset(t3); read(t3,rf[6]); close(t3); break; end;
if o='b' then break;
if o='e' then writeln('你难道不会用Alt+F4吗');
if o='o' then writeln('很遗憾,程序猿还没有做设置');
until oo=0;
back;
writeln('某个人制作');
delay(1500);
writeln('在很久很久很久很久很久很久很久很久很久很久很久......');
delay(1500);
writeln('蛤蛤~~太长不说了');
delay(1500);
writeln('想了解自己去看剧情.txt吧');
delay(1500);
writeln('先开始游戏吧');
delay(2000);
clrscr;
p:=1;
repeat
writeln('请输入操作');
writeln('1:攻击 2:查看火星状态 3:商店 4:存档 5:退出');
read(k);
case k of
1:fightline;
2:writeln('火星的经验:',rf[1],' 生命值:',rf[2],' 能量值:',rf[3],' 攻击力:',rf[4],' 防御力:',rf[5],' 觉醒能量:',rf[6],' 星灵:',m);
3:shangdian;
4:save;
5:exit;
end;
until p=0;
end.
#include<iostream>
#include<cstdio>
#include<windows.h>
#include<cstdlib>
#include<cstring>
#include<conio.h>
using namespace std;
int i,ii=1,xx,b,bb,bbb,k,kk,tl,xl_sls,lw,xl_ss;
char x;
string s[10],ss1,ss2;
int csh();
int wstart();
int day1_morning();
int wend();
int day1_night();
int day1();
int day1_w();
int fight_sls();
int fight_ss();
int fight_w_sls();
int fight_w_ss();
int main(){
csh();
wstart();
day1();
wend();
return 0;
}
int csh(){
bbb=0;
bb=0;
b=0;
tl=100;
return 0;
}
int wstart(){
cout<<"群星之战VC2.1.3.2!"<<endl;
Sleep(2000);
cout<<"本游戏由fpc公司雷族工作室出品!"<<endl;
Sleep(2000);
for(int i=1;i<=75;i++){
for(int j=1;j<=i;j++){
cout<<"-";
}
Sleep(30);
system("cls");
}
system("cls");
cout<<"启动成功"<<endl;
Sleep(1500);
system("cls");
cout<<"如果没看请看我.txt,请不要玩。"<<endl;
Sleep(1500);
system("cls");
return 0;
}
int day1_morning(){
cout<<"故事开始!"<<endl;
Sleep(2500);
system("cls");
cout<<"你的名字是火爪。"<<endl;
Sleep(3000);
system("cls");
cout<<"你在雷族。"<<endl;
Sleep(3000);
system("cls");
while (b==0){
if (bb>0&&bb<=5) cout<<"你不能不去!!!!!!!!!!"<<endl;
if (bb>5){
cout<<"因为你对雷族不忠诚,所以被蓝星踢出了雷族!"<<endl;
bbb=1;
break;
}
cout<<"我们今天要去打猎。"<<endl;
Sleep(3000);
cout<<"你想去么?(去:y 不去:n)"<<endl;
cin>>x;
if (x=='y') {
system("cls");
cout<<"你想打几次?(小于15次)"<<endl;
cin>>ii;
while (ii>=15){
cout<<"你想打几次?(小于15次)"<<endl;
cin>>ii;
}
system("cls");
break;
}
else {
bb++;
continue;
}
}
while (i<ii){
if (bbb==1) break;
cout<<"你走在树林里。"<<endl;
Sleep(1500);
cout<<"你看见了一只大水老鼠和一只松鼠!"<<endl;
Sleep(2500);
cout<<"你想捕哪个?(1=水老鼠 2=松鼠)"<<endl;
cin>>x;
system("cls");
cout<<"你扑了过去!"<<endl;
Sleep(2500);
system("cls");
if (x=='1') fight_sls();
else fight_ss();
i++;;
}
if (bbb==0){
cout<<"太晚了,你回家了。"<<endl;
Sleep(4000);
system("cls");
if (lw>=35) cout<<"蓝星表扬了你!!!"<<endl;
else cout<<"蓝星劈了你!!!!!"<<endl;
Sleep(3000);
system("cls");
}
cout<<"DAY1 morning END!!!!!!!!"<<endl;
Sleep(1500);
system("cls");
return 0;
}
int wend(){
cout<<"本游戏现在是正式版本中的半预告版本!即将完工DAY1晚上的剧情!敬请期待!"<<endl;
Sleep(2000);
cout<<"本次更新把代码改为了C++!"<<endl;
Sleep(2000);
cout<<"做的有点差请谅解!(特别是语文水品!)"<<endl;
Sleep(2000);
cout<<"你们的游玩是对fpc公司和雷族工作室最大的支持!(VC作者QQ:1769517864)"<<endl;
Sleep(4000);
return 0;
}
int day1_night(){
int x;
cout<<"伴随蓝星的话,你进入了梦乡!"<<endl;
Sleep(1500);
system("cls");
cout<<"你回到了几天前,你还是一只叫拉斯特的宠物猫!"<<endl;
Sleep(1500);
system("cls");
cout<<"这一天,你来到森林边,看见了一只灰色的猫,那猫也看见了你!"<<endl;
Sleep(1500);
system("cls");
cout<<"它迅即俯下身子,准备向你发起进攻!"<<endl;
Sleep(1500);
system("cls");
while (1){
cout<<"你是打还是不打?(y=打,n=不打)"<<endl;
cin>>x;
Sleep(1500);
system("cls");
if (x=='n') {
cout<<"你没有勇敢的与他打架,你回家了!"<<endl;
Sleep(1500);
system("cls");
cout<<"突然,时间来到了几天后!"<<endl;
Sleep(1500);
cout<<"你被主人带到了一个奇怪的地方。"<<endl;
Sleep(1500);
cout<<"忽然,你看到了“阉割场”三个字,从梦中惊醒了!"<<endl;
Sleep(1500);
system("cls");
break;
}
else {
cout<<"战斗开始!"<<endl;
Sleep(2500);
break;
}
}
cout<<"未完待续···"<<endl;
Sleep(1500);
cout<<"敬请期待群星之战VC2.1.3.1···"<<endl;
system("cls");
Sleep(1500);
return 0;
}
int day1(){
day1_w();
day1_morning();
day1_night();
}
int day1_w(){
cout<<endl<<endl<<endl<<endl<<"day";
cout<<" *******"<<endl;
cout<<" *** "<<endl;
cout<<" *** "<<endl;
cout<<" *** "<<endl;
cout<<" *** "<<endl;
cout<<" *** "<<endl;
cout<<" *** "<<endl;
cout<<" *** "<<endl;
cout<<" *******"<<endl;
Sleep(5000);
system("cls");
return 0;
}
int fight_sls(){
xl_sls=30;
while(1){
fight_w_sls();
cin>>x;
Sleep(2500);
if (x=='1'){
if (tl-5<0) {
cout<<"体力不足!"<<endl;
Sleep(1500);
system("cls");
}
else {
tl-=5;
xl_sls-=10;
}
}
else if(x=='2'){
tl+=50;
cout<<"回复50体力"<<endl;
Sleep(2500);
system("cls");
}
else {
cout<<"无效操作"<<endl;
Sleep(2500);
system("cls");
}
if (xl_sls<=0) {
fight_w_sls();
cout<<"战斗胜利!"<<endl;
Sleep(1500);
system("cls");
break;
}
}
return 0;
}
int fight_w_sls(){
cout<<"战斗!!!"<<endl;
cout<<"你:火爪 血量:50 体力:"<<tl<<endl;
cout<<"对手:水老鼠 血量:"<<xl_sls<<" 体力:100"<<endl;
cout<<"1 爪击 2 恢复体力"<<endl;
return 0;
}
int fight_ss(){
xl_ss=20;
while(1){
fight_w_ss();
Sleep(2500);
cin>>x;
if (x=='1'){
if (tl-10<0) {
cout<<"体力不足!"<<endl;
Sleep(1500);
system("cls");
}
else {
tl-=10;
xl_ss-=10;
}
}
else if(x=='2'){
tl+=50;
}
else {
cout<<"无效操作"<<endl;
Sleep(2500);
system("cls");
}
if (xl_ss<=0) {
fight_w_ss();
cout<<"战斗胜利!"<<endl;
Sleep(1500);
system("cls");
break;
}
}
return 0;
}
int fight_w_ss(){
cout<<"战斗!!!"<<endl;
cout<<"你:火爪 血量:50 体力:"<<tl<<endl;
cout<<"对手:松鼠 血量:"<<xl_ss<<" 体力:100"<<endl;
cout<<"1 爪击 2 恢复体力"<<endl;
return 0;
}