积累了许多C++的小游戏,今天和大家分享一下。
目录
这款游戏有着他独特的代码风格,以及不闪屏的画面。
效果:
经典代码:
void Move(){
if(X<3) X=3;if(Y<1) Y=1,Vy=0;if(Y>29) Y=29,Vy=0;
if(Ice!=0){X-=Vx/2.0;Y+=Vy/2.0;Vy=fmax(Vy-0.025,(float)0);if(T%6==0&&Up==0&&Y<Ding) Y+=0.25;if(T%6==3&&Up==0&&Y>=Ding) Y-=0.25;if(Up==0&&Y<=Ding-1.25) Vy=0.25;if(Up==0&&Y>=Ding+1.25&&Wind==0) Vy=-0.25;if(Up==0&&Down==0&&Vx>0&&X<=18) Up=0,Down=0,Vx=0,Vy=0,X=18,Setpos(20,Y-2.5),cout<<"==========";else if(Down==2&&X<=22) Up=0,Down=1,Vx=0,Vy=0,X=22,Setpos(20,Y-2.5),cout<<"==========";else if(Up==0&&Down==1&&Vx<0&&X>=22) Up=0,Down=1,Vx=0,Vy=0,X=22,Setpos(20,Y-2.5),cout<<"==========";else if(Up>0&&Down==0&&X>18) Up=0,Vx=0,Vy=0,X=18,Setpos(20,Y-2.5),cout<<"==========";else if(Down==2) Vx+=0.175;else if(Up>0&&Upt==0) Vx-=0.175;else if(Up>0&&Upt>0) {Vx=fmax(Vx-0.125,(float)0);if(Upt==1&&T%2==0)Map(-1,0);if(T%2==0)Upt--;}}
else{X-=Vx;Y+=Vy;Vy=fmax(Vy-0.05,(float)0);if(Wind==0){if(T%6==0&&Up==0&&Y<Ding) Y+=0.5;if(T%6==3&&Up==0&&Y>=Ding) Y-=0.5;}else{if(T%2==0&&Up==0&&Y<Ding) Y+=0.5;if(T%2==1&&Up==0&&Y>=Ding) Y-=0.5;}if(Up==0&&Y<=Ding-1.25) Vy=0.5;if(Up==0&&Y>=Ding+1.25&&Wind==0) Vy=-0.5;if(Up==0&&Down==0&&Vx>0&&X<=18) Up=0,Down=0,Vx=0,Vy=0,X=18,Setpos(20,Y-2.5),cout<<"==========";else if(Down==2&&X<=22) Up=0,Down=1,Vx=0,Vy=0,X=22,Setpos(20,Y-2.5),cout<<"==========";else if(Up==0&&Down==1&&Vx<0&&X>=22) Up=0,Down=1,Vx=0,Vy=0,X=22,Setpos(20,Y-2.5),cout<<"==========";else if(Up>0&&Down==0&&X>18) Up=0,Vx=0,Vy=0,X=18,Setpos(20,Y-2.5),cout<<"==========";else if(Down==2) Vx+=0.35;else if(Up>0&&Upt==0) Vx-=0.35;else if(Up>0&&Upt>0) {Vx=fmax(Vx-0.25,(float)0);if(Upt==1)Map(-1,0); Upt--;}}
for(int i=bl;i<=br;i++){
if(B[i].what<98)if(B[i].x-B[i].vx<=5||B[i].x-B[i].vx>=30||B[i].y-B[i].vy<=0||B[i].y-B[i].vy>=30){B[i].life=0;Map(1,i);}
for(int j=0;j<20;j++)if(B[i].what>0&&B[i].life!=0&&abs(B[i].x-I[j][0])<2&&B[i].y-I[j][1]<=2){Setpos(I[j][0],I[j][1]);if(I[j][0]==20) cout<<"===";else cout<<" ";I[j][0]=I[j][1]=-1;B[i].life=0;Exp+=2;}
if(B[i].t>=100)B[i].life=0;if(B[i].life==0&&i==bl) bl++;
Map(1,i);if(B[i].life==0) continue;
else{B[i].t++;
if(B[i].what==1){if(B[i].y<=25&&B[i].How==0) B[i].vy=0,B[i].How=1;if(B[i].t==30) B[i].y+=1.5,B[i].How=2;if(B[i].t==35) B[i].vy=1.5,B[i].How=3;}
if(B[i].what==2){if(B[i].t%3==0) B[i].How=!B[i].How;}
if(B[i].what==3||B[i].what==5){if(B[i].what==3&&B[i].y<=20) B[i].vy=0;if(B[i].what==5&&B[i].y<=21) B[i].vy=0;if(B[i].t>30&&B[i].t%2==0) B[i].How=!B[i].How;if(B[i].what==5&&B[i].t<=30&&B[i].x<X) B[i].vx=-0.2;else if(B[i].what==5&&B[i].t<=70&&B[i].x>X) B[i].vx=0.2;else B[i].vx=0;if(B[i].t==45){B[i].life=0;br++;B[br].what=4;B[br].x=B[i].x;B[br].y=32;B[br].vy=3;B[br].life=1;}}
if(B[i].what==6||B[i].what==8||B[i].what==9){if(B[i].vx<0.25&&B[i].vy<0.25&&B[i].t>=50){B[i].life=0;if(B[i].life==0&&i==bl) bl++;Map(1,i);break;}if(B[i].t%5==0) B[i].How=rand()%4;if(B[i].what==9){if(B[i].t==7){X9:float xx=(rand()%41)/40.0,yy=(rand()%41)/40.0;if(xx<=0.5&&yy<=0.5) goto X9;for(int j=1;j<=4;j++){br++,B[br].what=9;B[br].t=11;B[br].x=B[i].x,B[br].y=B[i].y,B[br].vx=xx,B[br].vy=yy;if(j%2==0)swap(B[br].vx,B[br].vy),B[br].vy*=-1;if(j<=2)B[br].vx*=-1,B[br].vy*=-1;B[br].life=1;}B[i].life=0;}}if(B[i].what==8){if(B[i].x>X&&B[i].vx<1.2) B[i].vx+=fmax((float)0,0.2-B[i].t/25);if(B[i].x<X&&B[i].vx>-1.2) B[i].vx-=fmax((float)0,0.2-B[i].t/25);if(B[i].y>Y&&B[i].vy<1.2) B[i].vy+=fmax((float)0,0.2-B[i].t/25);if(B[i].y<Y&&B[i].vy>-1.2) B[i].vy-=fmax((float)0,0.2-B[i].t/25);}}
if(B[i].what>=13&&B[i].what<=15&&B[i].How!=0){if(B[i].x==B[i].How)B[i].vx=0,B[i].How=0;}
if(B[i].what==16){if(B[i].x<X&&B[i].vx>=-1) B[i].vx-=0.2;else if(B[i].x>X&&B[i].vx<=1) B[i].vx+=0.2;}
}
if(B[i].life==1&&B[i].a==0&&B[i].what>0){if(B[i].y>Y&&abs(B[i].x-X)<=3&&((B[i].x-X)*(B[i].x-X)+(B[i].y-Y)*(B[i].y-Y))<Dis) Dis=(B[i].x-X)*(B[i].x-X)+(B[i].y-Y)*(B[i].y-Y),Disb=i;else if(((B[i].x-X)*(B[i].x-X)+(B[i].y-Y)*(B[i].y-Y))<Dis1) Dis1=(B[i].x-X)*(B[i].x-X)+(B[i].y-Y)*(B[i].y-Y),Disb1=i;}
}
}
小人移动的代码,一看就懂,丝毫不含糊。
注明:本游戏为转载游戏,但不属于本站上任何帖子。
效果:
经典代码:
int play() {
char cz;
biu=false;
kick=false;
if(kbhit()) {
cz=getch();
if(cz=='a') {
if(fx>1) {
fx--;
} else {
fx=4;
}
}
if(cz=='d') {
if(fx<4) {
fx++;
} else {
fx=1;
}
}
if(cz=='w') {
if(fx==1&&cmoounity[y][x-1]==0) {
x--;
}
if(fx==2&&cmoounity[y-1][x]==0) {
y--;
}
if(fx==3&&cmoounity[y][x+1]==0) {
x++;
}
if(fx==4&&cmoounity[y+1][x]==0) {
y++;
}
}
if(cz==' '&&zd>0) {
biu=true;
zd--;
if(fx==1&&y==dy&&x>dx) {
sw();
}
if(fx==2&&y<dy&&x==dx) {
sw();
}
if(fx==3&&y==dy&&x<dx) {
sw();
}
if(fx==4&&y>dy&&x==dx) {
sw();
}
}
if(cz=='f') {
zd=20;
}
if(x>14&&cz=='q'&&food<200&&first_food>=1000) {
food+=31;
first_food-=1000;
}
if(x>14&&cz=='s'&&water<200&&first_water>=500) {
water+=11;
first_water-=500;
}
if(cz=='v') {
kick=true;
if(x==dx&&y==dy) {
sw();
}
food-=2;
}
}
}
控制玩家的一段代码。
这是 诗意的叛逆 创作的一款游戏。
效果:
经典代码:
void joinPlayer(){ //加入玩家
string tmpStr = "";
cout<<"请输入玩家A的名字___" <<endl;
cin>>tmpStr;
while(tmpStr==""){
cout<<"玩家名字不能为空请重新输入___" <<endl;
cin>>tmpStr;
}
player[0] = "A" + tmpStr;
cout<<"请输入玩家B的名字___" <<endl;
cin>>tmpStr;
while(tmpStr==""){
cout<<"玩家名字不能为空请重新输入___" <<endl;
cin>>tmpStr;
}
while(tmpStr == player[0]){
cout<<"玩家名字不能重复,请重新输入___" <<endl;
cin>>tmpStr;
}
player[1] = "B" + tmpStr;
system("cls");
cout<<"***玩家加入成功..."<<endl;
cout<<"***地图中[A]表示玩家["<<player[0]<<"]的位置..."<<endl;
cout<<"***地图中[B]表示玩家["<<player[1]<<"]的位置..."<<endl;
}
添加玩家的一段代码。
这是WangYinxiAlex认为最有意思的游戏。
效果:
经典代码:
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)制作:<bits/stdc++.h>";
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;
}
效果:
经典代码:
void BuildAIBullet(Tank *tank) //AI子弹发射(建立)含有对my_tank的读取
{
if(tank->CD==15)
{
if(!(rand()%11)) //冷却结束后在随后的每个游戏周期中有10分之一的可能发射子弹
{
BuildBullet(*tank);
tank->CD=0;
}
}
else
tank->CD++;
if(tank->CD >= 14) //AI强化部分,在冷却到达一定范围即可使用
{
if(tank->y==38 ) //如果坦克在底部(这个最优先)
{
if(tank->x < 20) //在老家左边
{
if(tank->direction==RIGHT) //坦克方向朝左
{
BuildBullet(*tank); //发射子弹
tank->CD=0;
}
}
else //在老家右边
if(tank->direction==LEFT) //坦克方向朝右
{
BuildBullet(*tank); //发射子弹
tank->CD=0;
}
}
else if(tank->x==my_tank.x+1 || tank->x==my_tank.x || tank->x==my_tank.x-1) //AI坦克在纵向上"炮口"对准我的坦克
{
if(tank->direction==DOWN && my_tank.y > tank->y || tank->direction==UP && my_tank.y < tank->y)
{ //若是AI朝下并且我的坦克在AI坦克下方(数值大的在下面)或者AI朝上我的坦克在AI上方
int big=my_tank.y , smal=tank->y , i;
if(my_tank.y < tank->y)
{
big=tank->y;
smal=my_tank.y;
}
for(i=smal+2;i<=big-2;i++) //判断AI炮口的直线上两坦克间有无障碍
if(map[i][tank->x]!=0 || map[i][tank->x]!=5) //若有障碍
break;
if(i==big-1) //若i走到big-1说明无障碍
{
BuildBullet(*tank); //则发射子弹
tank->CD=0;
}
}
}
else if(tank->y==my_tank.y+1 || tank->y==my_tank.y || tank->y==my_tank.y-1) //AI坦克在横向上"炮口"对准我的坦克
{
if(tank->direction==RIGHT && my_tank.x > tank->x || tank->direction==LEFT && my_tank.x < tank->x)
{ //若是AI朝右并且我的坦克在AI坦克右方(数值大的在下面)或者AI朝左我的坦克在AI左方
int big=my_tank.y , smal=tank->y , i;
if(my_tank.x < tank->x)
{
big=tank->x;
smal=my_tank.x;
}
for(i=smal+2;i<=big-2;i++) //判断AI炮口的直线上两坦克间有无障碍
if(map[tank->y][i]!=0 || map[tank->y][i]!=5) //若有障碍
break;
if(i==big-1) //若i走到big-1说明无障碍
{
BuildBullet(*tank); //则发射子弹
tank->CD=0;
}
}
}
}
}
好了,小游戏(上)就分享完了,如果喜欢,点赞加关注,可以看到更多小游戏!