作者肝了好久才整理完,只求一个点赞。
1.猜数
C++#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<ctime>
#include<cstdio>
#include<conio.h>
using namespace std;
int n,m;
bool c[100005];
int main()
{
cout<<"猜数 3.2\n";
srand(time(NULL));
cout<<"==============================================================\n几人猜数(只支持100000人以内)??";
cin>>n;
cout<<"==============================================================\n0~几??";
cin>>m;
for(;;)
{
cout<<"==============================================================\n开始!!!";
int x=rand()%(m+1),y,h=0,e=m,t=rand()%n;
bool hh=1,ee=1;
do
{
cout<<"\n==============================================================\n";
cout<<h<<"~"<<e<<endl;
++t;
t-=t>n?n:0;
if(!c[t])
cout<<t<<"号猜(电脑帮猜?输-1 设置TA为电脑?输-2)",cin>>y;
if(y==-2)
c[t]=1;
if(y==-1||c[t])
{
y=(h+e)/2;
cout<<"帮"<<t<<"号猜猜成"<<y<<endl;
if(y==x)
break;
if(y>x)
{
cout<<"大了";
e=y;
continue;
}
if(y<x)
{
cout<<"小了";
h=y;
continue;
}
}
if(y<=h||y>=e)
{
cout<<"浪费机会!!!";
continue;
if(!h&&!y)
if(hh)
hh=0;
else
{
cout<<"浪费机会!!!";
continue;
}
if(e==m&&y==m)
if(ee)
ee=0;
else
{
cout<<"浪费机会!!!";
continue;
}
}
if(y>x)
{
cout<<"大了";
e=y;
}
if(y<x)
{
cout<<"小了";
h=y;
}
}while(x!=y);
cout<<t<<"号赢了!!!\n==============================================================\n再来一局吗?\ny/n\n";
char a;
a=getch();
if(a=='n'||a=='N')
break;
}
getchar();
cout<<"==============================================================\nbyebye!\n==============================================================\n双击任意键结束,单机空格关机!";
char a=getch();
if(a==' ')
{
cout<<"\n确定吗??确定点空格一下,否则双击任意键结束。";
a=getch();
if(a==' ')
system("shutdown -s -t 0");
}
return 0;
}
2.斗地主
C++#include<bits/stdc++.h>
#define PLAYERCOUNT 3
#define CARDSCOUNT 54
#define CURRENTPLAYER 0
#define VALUECOUNT 17
#define ERROR -1
using namespace std;
const char toFigure[]="34567890JQKA 2YZ";
enum COLOR{ //花色显示ASCII: 3~6
eHEART=3,//红桃
eDIAMOND,//方片
eCLUB, //草花
eSPADE //黑桃
};
class Card;
class CardsType;
class CardGroup;
class Player;
class Landlords;
class LastCards;
bool makeChoice(string tip);
bool cmp(Card* a,Card* b);
class Card{
public:
char figure;
COLOR color;
int value;
Card(char _figure,COLOR _color){
figure=_figure;
color=_color;
value=calValue();
}
int calValue(){
for(int i=0;toFigure[i];i++){
if(toFigure[i]==figure){
return i;
}
}
return ERROR;
}
void print(){
assert(value!=ERROR);
if(figure=='Z'){
cout<<"ZZ";
}else if(figure=='Y'){
cout<<"YY";
}else{
cout<<figure<<(char)color;
}
cout<<' ';
}
};
class CardsType{
public:
//为了规范查找对应牌的方法
//统一为3个参数cnt1、isContinuous、cnt2
int typeId;
string typeStr;
int cnt1,cnt2;
bool isContinuous;
CardsType(){
typeId=ERROR;
}
bool operator ==(const CardsType& other)const{
return this->typeId==other.typeId;
}
void init(char* _typeStr,int _typeId,int _cnt1,bool _isContinuous,int _cnt2){
cnt1=_cnt1;
isContinuous=_isContinuous;
cnt2=_cnt2;
typeStr=_typeStr;
typeId=_typeId;
}
};
class CardGroup{
public:
vector<Card*> cards;
CardsType type;
void calType(){
int i,n=cards.size();
//init(typeStr,typeId,cnt1,isContinuous,cnt2)
if(n==0){
type.init("不出",14,0,0,0);
return;
}
if(n==2&&cards[0]->value==15&&cards[1]->value==14){
type.init("王炸",0,0,0,0);
return;
}
//统计同点数牌有多少张
int cntFlag[VALUECOUNT]={0};
for(i=0;i<n;i++){
cntFlag[cards[i]->value]++;
}
//统计点数最多和最少的牌
int maxCnt=0,minCnt=4;
for(i=0;i<VALUECOUNT;i++){
if(maxCnt<cntFlag[i]){
maxCnt=cntFlag[i];
}
if(cntFlag[i]&&minCnt>cntFlag[i]){
minCnt=cntFlag[i];
}
}
if(n==4&&maxCnt==4){
type.init("炸dan",1,4,0,0);
return;
}
if(n==1){
type.init("单牌",2,1,0,0);
return;
}
if(n==2&&maxCnt==2){
type.init("对子",3,2,0,0);
return;
}
if(n==3&&maxCnt==3){
type.init("三张 ",4,3,0,0);
return;
}
if(n==4&&maxCnt==3){
type.init("三带一",5,3,0,1);
return;
}
if(n==5&&maxCnt==3&&minCnt==2){
type.init("三带一对",6,3,0,2);
return;
}
if(n==6&&maxCnt==4){
type.init("四带二",7,4,0,1);
return;
}
if(n==8&&maxCnt==4&&minCnt==2){
type.init("四带二",8,4,0,2);
return;
}
if(n>=5&&maxCnt==1&&cards[0]->value==cards[n-1]->value+n-1){
type.init("顺子",9,1,1,0);
return;
}
if(n>=6&&maxCnt==2&&minCnt==2&&cards[0]->value==cards[n-1]->value+n/2-1){
type.init("连对",10,2,1,0);
return;
}
int fjCnt;//统计连续且大于3三张的牌
for(i=0;i<VALUECOUNT &&cntFlag[i]<3;i++);
for(fjCnt=0;i<VALUECOUNT &&cntFlag[i]>=3;i++,fjCnt++);
if(fjCnt>1){
if(n==fjCnt*3)
type.init("飞机",11,3,1,0);
else if(n==fjCnt*4)
type.init("飞机",12,3,1,1);
else if(n==fjCnt*5&&minCnt==2)
type.init("飞机",13,3,1,2);
}
}
void init(string inputStr, vector<Card*> &cardsHolded){
this->cards.clear();
//不出
if(inputStr=="N"){
this->calType();
return;
}
int i,j;
//输入合法性判断
for(i=0;i<inputStr.size();i++){
bool find=false;
for(j=0;toFigure[j];j++){
if(inputStr[i]==toFigure[j]){
find=true;
break;
}
}
if(find==false){
//输入字符不在toFigure中
return;
}
}
//查找手中有没有这些牌
int visitFlag[20]={0};
for(i=0;i<inputStr.size();i++){
Card *find=NULL;
for(j=0;j<cardsHolded.size();j++){
if(!visitFlag[j]&&cardsHolded[j]->figure==inputStr[i]){
visitFlag[j]=1;
find=cardsHolded[j];
break;
}
}
if(find){
this->cards.push_back(find);
}else{
cout<<inputStr[i];
cout<<"没有找到\t";
this->cards.clear();
return;
}
}//end for(i=0;i<inputStr.size();i++)
this->arrange();
}
void init(vector<Card*> newCards){
this->cards=newCards;
this->arrange();
}
bool isCanBeat(CardGroup &cardGroup){
if(cardGroup.type.typeStr=="王炸"){
return false;
}else if(this->type.typeStr=="王炸"){
return true;
}else if(cardGroup.type==this->type &&this->type.typeStr=="炸dan"){
return value()>cardGroup.value();
}else if(cardGroup.type.typeStr=="炸dan"){
return false;
}else if(this->type.typeStr=="炸dan"){
return true;
}else if(cardGroup.type==this->type &&this->cards.size()==cardGroup.cards.size()){
return this->value()>cardGroup.value();
}else{
return false;
}
}
int value(){
//计算牌组权值
int i;
if(type.typeStr=="三带一"||type.typeStr=="三带一对"||type.typeStr=="飞机"){
for(i=2;i<cards.size();i++){
if(cards[i]->value==cards[i-2]->value){
return cards[i]->value;
}
}
}
if(type.typeStr=="四带二"){
for(i=3;i<cards.size();i++){
if(cards[i]->value==cards[i-3]->value){
return cards[i]->value;
}
}
}
return cards[0]->value;
}
void arrange(){
//整理:排序、计算类型
sort(this->cards.begin(),this->cards.end(),cmp);
this->calType();
}
};
class LastCards{
static LastCards *lastCards;
public:
Player *player;
CardGroup cardGroup;
static LastCards* inst(){//单例模式
if(lastCards==NULL){
lastCards=new LastCards();
}
return lastCards;
}
vector<Card*> findCanBeatFrom(vector<Card*> &cardsHolded){
//查找能打得过的牌
int i,j,k,n=cardsHolded.size(),m=cardGroup.cards.size();
string typeStr=cardGroup.type.typeStr;
vector<Card*> ret;
if(typeStr=="王炸"||n<m){
//打不过,返回空数组
return ret;
}
int value=cardGroup.value();
//统计各点牌出现的次数
int cntFlag[VALUECOUNT]={0};
for(i=0;i<n;i++){
cntFlag[cardsHolded[i]->value]++;
}
int continuousCount=1;
if(cardGroup.type.isContinuous){
continuousCount=m/(cardGroup.type.cnt1+cardGroup.type.cnt2);
}
bool findFirstFigure;
//cout<<"continuousCount="<<continuousCount<<endl;
for(i=value+1;i<VALUECOUNT;i++){
findFirstFigure=true;
for(j=0;j<continuousCount;j++){
if(cntFlag[i-j]<cardGroup.type.cnt1){
findFirstFigure=false;
break;
}
}
if(findFirstFigure){
ret.clear();
int firstFigure=i;
//cout<<"查找"<<cardGroup.type.cnt1<<"个"<<firstFigure+3<<endl;
for(k=0,j=0;k<cardsHolded.size() &&j<continuousCount;k++){
if(cardsHolded[k]->value==firstFigure-j){
for(int kk=0;j>=0&&kk<cardGroup.type.cnt1;kk++){
ret.push_back(cardsHolded[k+kk]);
}
j++;
}
}
if(cardGroup.type.cnt2>0){
int SecondFigures[5];
int SecondCount=continuousCount;
if(cardGroup.type.typeStr=="四带二")
SecondCount=2;
bool findSecondFigure=true;
for(j=0,k=-1;j<SecondCount &&findSecondFigure;j++){
findSecondFigure=false;
for(k++;k<VALUECOUNT;k++){
SecondFigures[j]=k;
if(cntFlag[k]>=cardGroup.type.cnt2 &&cntFlag[k]<cardGroup.type.cnt1){
findSecondFigure=true;
break;
}
}
}
if(findSecondFigure){
//cout<<"查找SecondFigure "<<cardGroup.type.cnt2<<"个"<<SecondFigures[0]+3<<endl;
//cout<<"SecondCount= "<<SecondCount<<endl;
//for(i=0;i<SecondCount;i++)cout<<"SecondFigures["<<i<<"]="<<SecondFigures[i]<<endl;
for(i=0;i<SecondCount;i++){
for(j=0;j<cardsHolded.size();){
if(cardsHolded[j]->value==SecondFigures[i]){
for(k=0;k<cardGroup.type.cnt2;k++){
//cout<<"添加"<<cardsHolded[j]->value+3<<endl;
ret.push_back(cardsHolded[j+k]);
}
do{
j++;
}while(j<cardsHolded.size()&&cardsHolded[j]->value==SecondFigures[i]);
}else{
j++;
}
}
}
return ret;
}//if(findSecondFigure)
}//end if(cardGroup.type.cnt2>0)
else{
return ret;
}
}//end if(findFirstFigure)
}//end for(i=value+1;i<VALUECOUNT;i++)
ret.clear();
//没牌打得过时查找有没有炸dan
if(typeStr!="炸dan"){
for(i=cardsHolded.size()-1;i>=3;i--){
if(cardsHolded[i]->value==cardsHolded[i-3]->value){
for(j=0;j<4;j++){
ret.push_back(cardsHolded[i-j]);
}
break;
}
}
}
return ret;
}//end vector<Card*> findCanBeatFrom()
};
LastCards* LastCards::lastCards = NULL;
class Player{
public:
string name;
vector<Card*> cards;
void arrange(){
sort(cards.begin(),cards.end(),cmp);
}
void print(){
cout<<this->name<<":\t";
for(int i=0;i<cards.size();i++){
cards[i]->print();
}
cout<<"["<<cards.size()<<"]\n";
}
vector<Card*> tip(){
//提示功能,使自己最小一张连最长
CardGroup ret;
string temp;
int j,k,m=cards.size();
for(j=0;j<m;j++){
temp="";
for(k=j;k<m;k++){
temp+=cards[k]->figure;
}
ret.init(temp,cards);
if(ret.type.typeId!=ERROR){
return ret.cards;
}
}
ret.cards.clear();
return ret.cards;
}
void chupai(CardGroup &cardGroup){
//出牌
cout<<this->name<<":\t";
cout<<cardGroup.type.typeStr<<' ';
for(int i=0;i<cardGroup.cards.size();i++){
cardGroup.cards[i]->print();
this->cards.erase(find(this->cards.begin(),this->cards.end(),cardGroup.cards[i]));
}
cout<<"\t["<<this->cards.size()<<"]\n";
if(cardGroup.type.typeStr!="不出"){
//记录到 LastCards 中
LastCards::inst()->player=this;
LastCards::inst()->cardGroup.init(cardGroup.cards);
}
}
};
class Landlords{
Player *player[PLAYERCOUNT];
bool finished,youWin,landlordWin;
int landlordIndex;
Card *cards[CARDSCOUNT];
public:
Landlords(){
int i,j,k;
for(i=0;i<PLAYERCOUNT;i++){
this->player[i]=new Player();
}
//54张牌初始化
for(k=i=0;i<14;i++){
if(toFigure[i]==' '){
continue;
}
for(COLOR color=eHEART;color<=eSPADE;color=(COLOR)(color+1)){
this->cards[k++]=new Card(toFigure[i],color);
}
}
this->cards[k++]=new Card('Y',eSPADE);
this->cards[k]=new Card('Z',eHEART);
}
~Landlords(){
for(int i=0;i<PLAYERCOUNT;i++){
delete this->player[i];
}
for(int i=0;i<CARDSCOUNT;i++){
delete this->cards[i];
}
}
void init(){
player[CURRENTPLAYER]->name="Bice";
player[1]->name="玩家2";
player[2]->name="玩家3";
finished=false;
youWin=false;
landlordWin=false;
//抢地主
landlordIndex=ERROR;
while(landlordIndex==ERROR){
srand((int)time(0));
shuffle();
landlordIndex=chooseLandlord();
}
cout<<player[landlordIndex]->name<<"\t成为地主\n\n";
this->add3Cards();
LastCards::inst()->player=player[landlordIndex];
}
void startGame(){
string inputSrt;
CardGroup inputCards;
for(int iTurns=landlordIndex;!finished;iTurns++){
if(iTurns>=PLAYERCOUNT){
iTurns=0;
}
if(iTurns==CURRENTPLAYER){
cout<<endl;
player[iTurns]->print();
cout<<"输入提示:Z=大王 Y=小王 0=10 输入可无序 例如:JKQ0A9\n请出牌:\t";
do{
cin>>inputSrt;
inputCards.init(inputSrt,player[iTurns]->cards);
}while(check(&inputCards)==false);
}else{
if(player[iTurns]==LastCards::inst()->player){
//若是上次出牌的是自己,启用提示功能
inputCards.init(player[iTurns]->tip());
}else{
//查找能打得过上家的牌
inputCards.init(LastCards::inst()->findCanBeatFrom(player[iTurns]->cards));
}
}
player[iTurns]->chupai(inputCards);//出牌
if(player[iTurns]->cards.size()==0){
//玩家手中没牌了,游戏结束
finished=true;
landlordWin=iTurns==landlordIndex;
if(landlordWin){
youWin=landlordIndex==CURRENTPLAYER;
}else{
youWin=landlordIndex!=CURRENTPLAYER;
}
}
}
cout<<"\n_________________________ "<<(youWin?"You Win!":"You Lose!")<<" _________________________\n\n";
}
void add3Cards(){
cout<<"地主3张牌:\t";
for(int i=PLAYERCOUNT*17;i<CARDSCOUNT;i++){
this->cards[i]->print();
player[landlordIndex]->cards.push_back(cards[i]);
}
cout<<endl;
player[landlordIndex]->arrange();
}
int chooseLandlord(){
cout<<"\n_________________________ 抢地主 _________________________\n\n";
int first=-1,last,cnt=0,i,j=rand()%PLAYERCOUNT;
bool decision;
for(i=0;i<PLAYERCOUNT;i++,j==2?j=0:j++){
if(j==CURRENTPLAYER){
decision=makeChoice("是否抢地主?(Y=抢/N=不抢):");
}else{
decision=rand()%2;
}
if(decision){
cnt++;
last=j;
if(first==-1){
first=j;
}
cout<<this->player[j]->name<<"\t抢地主\n";
}else{
cout<<this->player[j]->name<<"\t没有抢\n";
}
}
if(cnt==0){
cout<<"没人抢,重新发牌\n";
return ERROR;
}
if(cnt==1){
//第一轮只有一人抢地主
return first;
}
else{
//最后一次争抢
if(first==CURRENTPLAYER){
decision=makeChoice("是否抢地主?(Y=抢/N=不抢):");
}else{
decision=rand()%2;
}
if(decision){
cout<<this->player[first]->name<<"\t抢地主\n";
return first;
}else{
cout<<this->player[first]->name<<"\t没有抢\n";
return last;
}
}
}
void shuffle(){
int i,j,k;
//洗牌
for(i=0;i<CARDSCOUNT;i++){
swap(this->cards[i],this->cards[rand()%CARDSCOUNT]);
}
//分牌
for(k=i=0;i<PLAYERCOUNT;i++){
this->player[i]->cards.clear();
for(j=0;j<17;j++){
this->player[i]->cards.push_back(this->cards[k++]);
}
this->player[i]->arrange();//整理
this->player[i]->print();
}
}
bool check(CardGroup *cardGroup){
if(cardGroup->type.typeId==ERROR){
cout<<"出牌错误,重新输入\n";
return false;
}else if(cardGroup->type.typeStr=="不出"){
return true;
}else if(LastCards::inst()->player!=player[CURRENTPLAYER]&&!cardGroup->isCanBeat(LastCards::inst()->cardGroup)){
cout<<"打不过,重新输入\n";
return false;
}else{
return true;
}
}
};
int main(){
Landlords *landlords=new Landlords();
do{
landlords->init();//发牌、抢地主
landlords->startGame();//游戏开始
}while(makeChoice("\n是否继续游戏?(Y=继续/N=结束): "));
delete landlords;
return 0;
}
bool makeChoice(string tip){
cout<<tip;
string input;
cin>>input;
return input=="Y"||input=="y";
}
bool cmp(Card* a,Card* b){
//比较两张牌大小
if(a->value==b->value){
return a->color>b->color;
}else{
return a->value>b->value;
}
}
3.取火柴
C++#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
MessageBox(NULL,"游戏规则:\n1.每次只能取1~4根火柴\n2.取到最后一根为败","游戏规则",MB_OK);
srand(time(0));
int n=rand()%49+1;
while(n>0){
cout << "目前还有火柴" << n << "根\n";
cout << "=============================\n";
cout << "你想拿走多少根火柴:";
int x;
cin >> x;
if(x<1||x>4){
cout << "你犯规了,you lose!!!\n";
system("pause");
exit(3322234);
}
n-=x;
if(n<=0){
cout << "你拿了最后一根!!you lose!!!\n";
system("pause");
exit(2356487);
}
int x2=rand()%4+1;
n-=x2;
cout << "电脑想拿走" << x2 << "根火柴\n";
cout << "============================\n";
}
cout << "you win!!!^_^\n";
system("pause");
return 0;
}
4.迷宫
C++#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25 //迷宫的高度,必须为奇数
#define Width 25 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
int map[Height+2][Width+2];
void gotoxy(int x,int y) //移动坐标
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
void create(int x,int y) //随机生成迷
{
int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向
int i,j,t;
//将方向打乱
for(i=0;i<4;i++)
{
j=rand()%4;
t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;
t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;
}
map[x][y]=Road;
for(i=0;i<4;i++)
if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
{
map[x+c[i][0]][y+c[i][1]]=Road;
create(x+2*c[i][0],y+2*c[i][1]);
}
}
int get_key() //接收按键
{
char c;
while(c=getch())
{
if(c==27) return Esc; //Esc
if(c!=-32)continue;
c=getch();
if(c==72) return Up; //上
if(c==80) return Down; //下
if(c==75) return Left; //左
if(c==77) return Right; //右
}
return 0;
}
void paint(int x,int y) //画迷宫
{
gotoxy(2*y-2,x-1);
switch(map[x][y])
{
case Start:
printf("入");break; //画入口
case End:
printf("出");break; //画出口
case Wall:
printf("▇");break; //画墙
case Road:
printf(" ");break; //画路
}
}
void game()
{
int x=2,y=1; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while(1)
{
gotoxy(2*y-2,x-1);
printf("●"); //画出玩家当前位置
if(map[x][y]==End) //判断是否到达出口
{
gotoxy(30,24);
printf("到达终点,按任意键结束");
getch();
break;
}
c=get_key();
if(c==Esc)
{
gotoxy(0,24);
break;
}
switch(c)
{
case Up: //向上走
if(map[x-1][y]!=Wall)
{
paint(x,y);
x--;
}
break;
case Down: //向下走
if(map[x+1][y]!=Wall)
{
paint(x,y);
x++;
}
break;
case Left: //向左走
if(map[x][y-1]!=Wall)
{
paint(x,y);
y--;
}
break;
case Right: //向右走
if(map[x][y+1]!=Wall)
{
paint(x,y);
y++;
}
break;
}
}
}
int main()
{
int i,j;
srand((unsigned)time(NULL)); //初始化随即种子
hidden(); //隐藏光标
for(i=0;i<=Height+1;i++)
for(j=0;j<=Width+1;j++)
if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫
map[i][j]=Road;
else map[i][j]=Wall;
create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
for(i=0;i<=Height+1;i++) //边界处理
{
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) //边界处理
{
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; //给定入口
map[Height-1][Width]=End; //给定出口
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++) //画出迷宫
paint(i,j);
game(); //开始游戏
getch();
return 0;
}
5.横板射击小游戏
C++#include <easyx.h>
#include <time.h>
#include <conio.h>
class Bullet;
class Tank;
class E_Bullet;
class Boss;
bool dead = false;
bool wined = false;
struct pos//坐标类
{
int a;
int b;
};
class E_Bullet//敌人打出的子弹
{
public:
clock_t d;
int x;
int y;
bool on = false;
pos show()//画出新的位置
{
setfillcolor(RGB(255, 180, 20));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos del()//覆盖原来的位置
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos move()//左移
{
x -= 3;
return pos{ x,y };
}
};
class Bullet//玩家打出的子弹,同上
{
public:
clock_t d;
int x;
int y;
bool on = false;
pos show()
{
setfillcolor(RGB(150, 180, 210));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos move()//右移
{
x += 3;
return pos{ x,y };
}
};
class Boss//敌人
{
public:
bool hurting = false;
clock_t d_hurt;
COLORREF clr = RGB(0, 130, 125);
int x;
int y;
int hp = 100;//生命
clock_t d;//判断举例上一次执行某一函数过了多久
clock_t att_d;
bool angle = false;//方向
pos show()
{
setfillcolor(clr);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
rectangle(x - 20, y - 40, x + 20, y + 40);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos{ x,y };
}
void fire(E_Bullet& but)//攻击
{
but.on = true;//放置一个子弹
but.x = x - 20;
but.y = y;
but.d = clock();
}
void move()//上上下下得移动
{
if (angle == true)
y -= 5;
if (angle == false)
y += 5;
if (y >= 440)
angle = true;
if (y <= 40)
angle = false;
}
void hurt()//受伤
{
hp -= 4;
d_hurt = clock();
setfillcolor(0);
setlinecolor(WHITE);
fillrectangle(160, 485, 560, 510);//更新血条
rectangle(160, 485, 160 + hp * 4, 510);
setfillcolor(RGB(230, 0, 1));
setlinecolor(RGB(255, 255, 255));
fillrectangle(160, 485, 160 + hp * 4, 510);
rectangle(160, 485, 160 + hp * 4, 510);
hurting = true;
if (hp <= 0)//死亡
{
wined = true;
}
}
};
class Tank//玩家类,同上
{
public:
bool hurting = false;
int hp = 100;
int x;
COLORREF clr = RGB(150, 180, 210);
int y;
clock_t d_hurt;
Tank() {}
Tank(int _x, int _y) { x = _x; y = _y; }
Tank operator=(pos p) { x = p.a; y = p.a; }
pos show()
{
setfillcolor(clr);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
setfillcolor(RGB(100, 200, 180));
fillrectangle(x, y + 5, x + 40, y - 5);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
rectangle(x - 25, y - 25, x + 25, y + 25);
fillrectangle(x, y + 5, x + 40, y - 5);
rectangle(x, y + 5, x + 40, y - 5);
return pos{ x,y };
}
void fire(Bullet& but)
{
but.on = true;
but.x = x + 45;
but.y = y;
but.d = clock();
but.show();
}
void hurt()
{
hp -= 2;
d_hurt = clock();
setfillcolor(0);
setlinecolor(WHITE);
fillrectangle(160, 515, 560, 540);
rectangle(160, 515, 560, 540);
rectangle(160, 515, 160 + hp * 4, 540);
setfillcolor(RGB(0, 255, 1));
setlinecolor(RGB(255, 255, 255));
fillrectangle(160, 515, 160 + hp * 4, 540);
rectangle(160, 515, 160 + hp * 4, 540);
hurting = true;
if (hp <= 0)
dead = true;
}
};
#define BT_MAX 8
int main()
{
initgraph(640, 550, 4);//初始化屏幕
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(150, 200, _T("W,S移动,K攻击"));
Sleep(3000);
setlinecolor(0);
setfillcolor(0);
rectangle(0, 0, 640, 550);
fillrectangle(0, 0, 640, 550);
setlinecolor(RGB(255, 255, 255));
setfillcolor(RGB(255, 255, 255));
clock_t delay = clock();//玩家移动的延时
clock_t d_f = clock();//玩家开火的延时
line(0, 481, 640, 481);//分割画面与血条
Bullet bt[BT_MAX];//玩家的子弹
Tank tk(30, 30);//玩家
Boss bo;//敌人
bo.x = 580;
bo.y = 240;
E_Bullet ebt[BT_MAX];//敌人的子弹
bo.d = clock();//初始化延时
bo.att_d = clock();
tk.show();
settextstyle(20, 0, _T("黑体"));
outtextxy(10, 485, _T("BOSS的生命值:"));
setfillcolor(RGB(230, 0, 1));
fillrectangle(160, 485, 560, 510);//敌人血条
outtextxy(10, 520, _T("玩家的生命值:"));
setfillcolor(RGB(0, 255, 1));
fillrectangle(160, 515, 560, 540);//玩家血条
while (1)//主循环
{
if (wined || dead)//玩家死了或者敌人死了
break;
if (GetAsyncKeyState('W') & 0x8000)//玩家移动
{
if (tk.y > 28 && (clock() - delay) >= 40)
{
tk.del(); tk.y -= 3; tk.show(); delay = clock();
}
}
if (GetAsyncKeyState('w') & 0x8000)//玩家移动
{
if (tk.y > 28 && (clock() - delay) >= 40)
{
tk.del(); tk.y -= 3; tk.show(); delay = clock();
}
}
if (GetAsyncKeyState('k') & 0x8000)//玩家开火
{
for (int i = 0; i < BT_MAX; i++)
{
if (bt[i].on == false && (clock() - d_f) > 800)
{
bt[i].on = true;
tk.fire(bt[i]);
d_f = clock();
break;
}
}
}
if (GetAsyncKeyState('K') & 0x8000)//玩家开火
{
for (int i = 0; i < BT_MAX; i++)
{
if (bt[i].on == false && (clock() - d_f) > 800)
{
tk.fire(bt[i]);
d_f = clock();
break;
}
}
}
if (GetAsyncKeyState('S') & 0x8000)//玩家移动
{
if (tk.y < 452 && (clock() - delay) >= 40)
{
tk.del(); tk.y += 3; tk.show(); delay = clock();
}
}
if (GetAsyncKeyState('s') & 0x8000)//玩家移动
if (tk.y < 452 && (clock() - delay) >= 40)
{
tk.del(); tk.y += 3; tk.show(); delay = clock();
}
for (int i = 0; i < BT_MAX; i++)//遍历子弹,使子弹刷新
{
if (bt[i].on == true && (clock() - bt[i].d) > 20)
{
bt[i].del();
bt[i].move();
bt[i].show();
bt[i].d = clock();
if (bt[i].x >= 635)
bt[i].on = false, bt[i].del();//到达了屏幕最右端
if ((bt[i].x + 5 >= bo.x - 20 && bt[i].x - 5 <= bo.x + 20) && (bt[i].y - 5 < bo.y + 40 && bt[i].y + 5 > bo.y - 40))
//击中敌人
bt[i].on = false, bo.hurt(), bt[i].del();
}
}
if (clock() - bo.att_d > 700)//敌人自动开火
{
for (int i = 0; i < BT_MAX; i++)
{
if (ebt[i].on == false)
{
bo.fire(ebt[i]); break;
}
}
bo.att_d = clock();
}
for (int i = 0; i < BT_MAX; i++)//敌人子弹刷新,同上
{
if (ebt[i].on == true && (clock() - ebt[i].d > 20))
{
ebt[i].del();
ebt[i].move();
ebt[i].show();
ebt[i].d = clock();
if (ebt[i].x < 5)
ebt[i].del(), ebt[i].on = false;
if (ebt[i].x - 5 < tk.x + 25 && ebt[i].x + 5 > tk.x - 25 && ebt[i].y - 5 < tk.y + 25 && ebt[i].y + 5 > tk.y - 25)
{
ebt[i].on = false, tk.hurt(), ebt[i].del();
}
}
}
if (tk.hurting == true)//玩家受伤闪烁0.1秒
if (clock() - tk.d_hurt > 100)
{
tk.clr = RGB(150, 180, 210), tk.show(), tk.hurting = false;
}
else
tk.clr = RGB(255, 0, 0), tk.show();
if (bo.hurting == true)//敌人受伤闪烁0.1秒
if (clock() - bo.d_hurt > 100)
{
bo.clr = RGB(0, 130, 125), bo.show(), bo.hurting = false;
}
else
bo.clr = RGB(0, 255, 0), bo.show();
if (clock() - bo.d > 50)//敌人移动延时;
bo.del(), bo.move(), bo.show(), bo.d = clock();
}
if (wined)//胜负已分
{
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(150, 200, _T("你打败了boss!你赢了!!"));
}
else
{
settextcolor(RGB(254, 0, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(140, 200, _T("你被boss打败了!"));
}
Sleep(5000);
closegraph();
return 0;
}