因为装不了图形库(其实是不会)又想做个小游戏玩而诞生的的神奇字符游戏。。。
效果图:
啊哈~~贴代码~~(最下面附下载链接)
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")
#include<fstream>
#include<conio.h>
#define N 50
#define EDGE 21
#define up 72
#define left 75
#define right 77
using namespace std;
void run();
void yn();
void print(int [][N]);
void movebul(int [][N]);
void movepla(int [][N]);
void movepac(int [][N]);
void setting();
void menu();
char command[50];
int scr[EDGE+1][N]={0},pl=9,command_num,summonable=1,best_num,player_num,width=24,beat=0,best_score,speed=4,game_clock=0,shield=0,power=0,anger=0,defence=0,ready_def=500,alive=0,reborn=0,reload,atk_time=100,atk_way,lingshi1,boss_hp=0,summon_speed,timer=0,wave=1000,cold=700,density=EDGE-1,score=0,death=0,mode,fuck=0,super=0,randoms,mark=0,gun=0,bomb=0,bomb_num=3;
unsigned int seed;
char best_name[100],player_name[100];
int main(){
ifstream ifs("record.psII");
ifs>>best_num;
for(int i=0;i<=best_num-1;i++)
ifs>>best_name[i];
ifs>>best_score;
ifs.close();//最高分
menu();
run();
}
void color(int a,int b=0){//颜色,可不用
if(b==0)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
else if(b==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a|FOREGROUND_INTENSITY);
else if(b==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a|BACKGROUND_INTENSITY);
else if(b==3)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a|BACKGROUND_INTENSITY|FOREGROUND_INTENSITY);
}
void print(int a[][N]){//绘图
COORD c={0,0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);//清屏
int i,j;
for(i=0;i<EDGE+1;i++){
a[i][width-1]=4;
for(j=0;j<width;j++){
if(a[i][j]==0){
color(07);
printf(" ");
}
if(a[i][j]==1){
color(04,1);
printf("^");
}
if(a[i][j]==2){
color(06,1);
printf(".");
}
if(a[i][j]==3){
color(02,1);
printf("'");
}
if(a[i][j]==4){
color(07,1);
printf("!");
}
if(a[i][j]==5){
color(06,1);
printf("H");
}
if(a[i][j]==6){
color(01,1);
printf("B");
}
if(a[i][j]==7){
color(04);
printf("S");
}
if(a[i][j]==8){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_INTENSITY);
printf(" ");
}
if(a[i][j]==9){
color(16,3);
printf(" ");
}
if(a[i][j]==10){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
printf("#");
}
if(a[i][j]==11){
color(51,3);
printf("|");
}
if(a[i][j]==12){
color(51,1);
printf("#");
}
if(a[i][j]==13){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
printf("#");
}
if(i==0&&j==width-1)
printf("seed:%d",seed);
if(i==1&&j==width-1)
printf("player name:%s",player_name);
if(i==2&&j==width-1)
printf("score:%d",score);
if(i==3&&j==width-1)
printf("Press Esc to exit");
if(i==4&&j==width-1)
printf("Lifes: %d",5-death);
if(i==10&&j==width/2-7&&timer>0)
color(07),printf("**wave %d**",game_clock/wave+1);
if(i==11&&j==width/2-11&&(game_clock/wave+1)%5==4&&game_clock%wave>=800)
color(04),printf("*CAUTION!DANGEROUS!*");
if(i==5&&j==width-1)
printf("Bombs: %d",bomb_num);
if(i==6&&j==width-1)
printf("Plane level: %d",super);
if(i==7&&j==width-1)
printf("Boost time rest: %d",cold);
if(i==8&&j==width-1)
printf("Wave: %d",game_clock/wave+1);
if(i==10&&j==width-1&&alive==1)
printf("Boss HP: %d",boss_hp);
if(i==9&&j==width-1)
printf("Anger: %d",anger);
if(i==EDGE-3&&j==width-1&&score>=1000)
printf("LEGENDARY!");
if(i==EDGE-3&&j==width-1&&score>=2000)
printf("Perfect!");
if(i==EDGE-2&&j==width-1&&score>=500)
printf("Great!");
if(i==EDGE-1&&j==width-1&&score>=50)
printf("Good job!");
}
printf("\n");
}
}
void movebul(int a[][N]){//移动子弹
int i,j;
for(i=0;i<EDGE+1;i++)
for(j=0;j<width;j++){
if(i==0&&a[i][j]==2)
a[i][j]=0;
if(a[i][j]==11){
if(a[i-1][j]==3){
a[i-1][j]=0;
score+=1;
/*printf("\7");*/
}
if(a[i][j-1]==3){
a[i][j-1]=0;
score+=1;
/*printf("\7");*/
}
if(a[i][j+1]==3){
a[i][j+1]=0;
score+=1;
/*printf("\7");*/
}
if(a[i-1][j]==9)
a[i-1][j]=0;
if(a[i-1][j]==8)
boss_hp--;
}
if(a[i][j]==2){
if(a[i-1][j]==3){
a[i-1][j]=0;
a[i][j]=0;
anger++;
score+=1;
/*printf("\7");*/
}
if(a[i-1][j]==9)
a[i-1][j]=0,a[i][j]=9,a[i+1][j]=9;
if(a[i-1][j]==8){
anger++;
boss_hp--;
}
a[i][j]=0,a[i-1][j]=2;
}
}
}
void shield_gen(int a[][N]){//护盾生成
for(int i=pl-2;i<=pl+2;i++)
if(a[EDGE-2][i]!=2&&a[EDGE-2][i]!=5&&a[EDGE-2][i]!=6&&a[EDGE-2][i]!=7&&a[EDGE-2][i]!=13&&a[EDGE-1][i]!=2&&a[EDGE-1][i]!=5&&a[EDGE-3][i]!=6&&a[EDGE-3][i]!=7&&a[EDGE-3][i]!=13)
a[EDGE-2][i]=12;
if(a[EDGE-1][pl-3]!=2)
a[EDGE-1][pl-3]=12;
if(a[EDGE-1][pl+3]!=2)
a[EDGE-1][pl+3]=12;
a[EDGE][pl+3]=12;
a[EDGE][pl-3]=12;
for(int i=EDGE;i>=EDGE-2;i--){
if(a[i][pl+4]!=2)
a[i][pl+4]=0;
if(a[i][pl-4]!=2)
a[i][pl-4]=0;
}
for(int i=pl-2;i<=pl+2;i++)
if(a[EDGE-1][i]!=2&&a[EDGE-1][i]!=5&&a[EDGE-1][i]!=6&&a[EDGE-1][i]!=7&&a[EDGE-1][i]!=13)
a[EDGE-1][i]=0;
if(a[EDGE-2][pl+3]!=2)
a[EDGE-2][pl+3]=0;
if(a[EDGE-2][pl-3]!=2)
a[EDGE-2][pl-3]=0;
a[EDGE][pl-1]=0;
a[EDGE][pl+1]=0;
a[EDGE][pl-2]=0;
a[EDGE][pl+2]=0;
}
void clean_shield(int a[][N]){//清理护盾周围的残渣
for(int i=pl-2;i<=pl+2;i++)
if(a[EDGE-2][i]!=2&&a[EDGE-2][i]!=5&&a[EDGE-2][i]!=6&&a[EDGE-2][i]!=7&&a[EDGE-2][i]!=13&&a[EDGE-1][i]!=2&&a[EDGE-1][i]!=5&&a[EDGE-3][i]!=6&&a[EDGE-3][i]!=7&&a[EDGE-3][i]!=13)
a[EDGE-2][i]=0;
if(a[EDGE-1][pl-3]!=2)
a[EDGE-1][pl-3]=0;
if(a[EDGE-1][pl+3]!=2)
a[EDGE-1][pl+3]=0;
a[EDGE][pl+3]=0;
a[EDGE][pl-3]=0;
}
void gen_boss(int a[][N]){//boss生成
for(int i=1;i<=2;i++)
for(int p=i;p<=width-i-2;p++){
if(a[i][p]!=3&&a[i][p]!=5&&a[i][p]!=6&&a[i][p]!=7&&a[i][p]!=13)
a[i][p]=10;
}
for(int i=3;i<=7;i++)
if(a[i][width-9]!=3&&a[i][width-9]!=5&&a[i][width-9]!=6&&a[i][width-9]!=7&&a[i][width-9]!=13&&a[i][7]!=3&&a[i][7]!=5&&a[i][7]!=6&&a[i][7]!=7&&a[i][7]!=13)
a[i][width-9]=10,a[i][7]=10;
for(int i=3;i<=5;i++){
if(a[i][width-7]!=3&&a[i][width-7]!=5&&a[i][width-7]!=6&&a[i][width-7]!=7&&a[i][width-7]!=11&&a[i][width-7]!=13&&a[i][5]!=3&&a[i][5]!=5&&a[i][5]!=6&&a[i][5]!=7&&a[i][5]!=13)
a[i][width-7]=10,a[i][5]=10;
a[i][width-11]=8,a[i][9]=8,a[i][width/2-1]=8;
}
for(int i=10;i<=width-12;i++)
a[5][i]=8;
a[4][10]=a[3][10]=a[4][10]=a[3][10]=0;
}
void movepla(int a[][N]){//敌机移动
int i,j;
for(i=EDGE;i>=0;i--)
for(j=0;j<width;j++){
if(i==EDGE&&(a[i][j]==3 || a[i][j]==9))
a[i][j]=0;
if(bomb==1 && a[i][j]==3)
a[i][j]=0,score++;
if(a[i][j]==3)
a[i][j]=0,a[i+1][j]=3;
if(a[i][j]==9)
a[i][j]=0,a[i+1][j]=9;
}
bomb=0;
if((a[EDGE-1][pl]==9 || a[EDGE-1][pl]==3)&&a[EDGE][pl]==1){
death++;
a[EDGE-1][pl]=0;
/*printf("\7");*/
}
}
void movepac(int a[][N]){//补给移动
int i,j;
for(i=EDGE;i>=0;i--)
for(j=0;j<width;j++){
if(i==EDGE&&a[i][j]==5)
a[i][j]=0;
if(a[i][j]==5)
a[i][j]=0,a[i+1][j]=5;
if(i==EDGE&&a[i][j]==6)
a[i][j]=0;
if(a[i][j]==6)
a[i][j]=0,a[i+1][j]=6;
if(i==EDGE&&a[i][j]==7)
a[i][j]=0;
if(a[i][j]==7)
a[i][j]=0,a[i+1][j]=7;
if(a[i][j]==13)
a[i][j]=0,a[i+1][j]=13;
}
if(a[EDGE-1][pl]==5&&a[EDGE][pl]==1){
a[EDGE-1][pl]=0;
death--;
/*printf("\7");*/
}
if(a[EDGE-1][pl]==6&&a[EDGE][pl]==1){
a[EDGE-1][pl]=0;
bomb_num++;
/*printf("\7");*/
}
if(a[EDGE-1][pl]==7&&a[EDGE][pl]==1){
a[EDGE-1][pl]=0;
super++;
cold=2000;
/*printf("\7");*/
}
if(a[EDGE-1][pl]==13&&a[EDGE][pl]==1){
a[EDGE-1][pl]=0;
shield=300;
/*printf("\7");*/
}
}
void yn(){//KO以后
system("cls");
system("color 07");
if(score>best_score){
beat=1;
}
PlaySound("end.a",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);//停止bgm
printf("\n");
printf("\n");
printf("\t\t\t\t *****GAME OVER*****\n\a");
printf("\t\t\t *****YOU DESTROYED %d ENERMIES!*****\n",score);
if(beat==1){
printf("\t\t *****YOU BEAT THE HIGH SCORE %d made by %s!***** \n\n\n",best_score,best_name);
ofstream ofs("record.psII",ios::trunc);
ofs<<player_num;
for(int i=0;i<=player_num-1;i++){
ofs<<player_name[i];
}
ofs<<score;
ofs.close();//记录最高分;
}
else
printf("\t\t\t *****BEST SCORE:%s %d*****\n",best_name,best_score);
printf("\t\t\t press y to continue,n to close(y/n)?\n");
printf("\t\t\t");
end:;
switch(getch()){
case 'y':
case 'Y':memset(scr,0,sizeof(scr));death=0,score=0,super=0;density=EDGE-1;bomb_num=3;shield=0,anger=0,game_clock=0,alive=0,reborn=0,system("cls"),run();break;
case 'n':
case 'N':break;
default :goto end;break;
}
}
void setting(){//设置
int sw=0,i,j;
system("cls");
do{
sw=0;
printf("\n map size:1.big 2.small>> ");
switch(getche()){
case '1':width=49;break;
case '2':width=24;break;
default:printf("\n Wrong value...\n"); sw=1;
}
}while(sw);
do{
sw=0;
printf("\n Difficulty:1.hard 2.middle 3.slow>> ");
switch(getche()){
case '1':speed=3;break;
case '2':speed=4;break;
case '3':speed=5;break;
default:printf("\n Wrong value...\n");sw=1;
}
}while(sw);
for(i=0;i<EDGE+1;i++)
for(j=0;j<45;j++)
scr[i][j]=0;
scr[EDGE][pl=9]=1;
printf("\n Press any key to save settings...");
getch();
system("cls");
}
void level_up(){//升级
timer=EDGE-1;
}
void loading(){//过场
system("cls");
printf("\n\n\n\n\n\n\n\n\n");
printf("\t*****Loading*****\n");
PlaySound("title.a",NULL,SND_FILENAME|SND_SYNC);
system("cls");
}
void get_command(){//内置指令
printf("/");
scanf("%s",&command);
if(command[0]=='t' && command[1]=='p'){
scanf("%d",&command_num);
game_clock=(command_num-1)*wave;
puts("read command success!");
}
else if(command[0]=='s' && command[1]=='m'){
scanf("%d",&command_num);
alive=1;
reborn=1;
boss_hp=command_num*250;
PlaySound("boss.a",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
puts("read command success!");
}
else if(command[0]=='k' && command[1]=='i' && command[2]=='l' && command[3]=='l'){
death=10;
}
else if(command[0]=='g' && command[1]=='r'){
summonable=0;
puts("read command success!");
}
else if(command[0]=='a' && command[1]=='d' && command[2]=='m' && command[3]=='i' && command[4]=='n'){
death=-50;
super=11;
density=10;
bomb_num=500;
shield=1000000000;
anger=100000;
puts("read command success!");
}
else
printf("undefined command!");
}
void s_power(int a[][N]){//激光
for(int i=pl-2;i<=pl+2;i++)
for(int p=EDGE-1;p>=defence*10;p--)
a[p][i]=11;
for(int p=EDGE-1;p>=0;p--)
a[p][pl-3]=0,a[p][pl+3]=0;
}
void clean_s(int a[][N]){//清除激光残渣
for(int i=pl-2;i<=pl+2;i++)
for(int p=EDGE-1;p>=defence*10;p--)
a[p][i]=0;
}
void run(){//运行
system("color 07");
int i=0,j=0;
scr[EDGE][pl]=1;
scr[0][5]=3;
PlaySound("source.a",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
summonable=1;
while(death<5){
gun++;
if(--shield>0)//生成护盾
shield_gen(scr);
if(shield==0)//清空护盾
clean_shield(scr),shield--;
if(power>0)//激光
power--,s_power(scr);
if(power==0)//清空激光
clean_s(scr),power--;
if(power<=0)//boss防御
defence=0;
if(boss_hp>0&&alive==1)//绘画boss
gen_boss(scr);
if((game_clock/wave+1)%5==0){//生成boss
if(alive!=1&&reborn==0){
alive=1;
reborn=1;
boss_hp=(game_clock/wave+1)*250;
PlaySound("boss.a",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
}
}
else
reborn=0;
if(boss_hp<=0&&alive==1)//杀死boss
alive=0,score+=(game_clock/wave+1)*100,system("cls"),PlaySound("source.a",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);;
cold--;
if(game_clock%wave==0)
level_up();
game_clock++;//主时间
if(cold==0){
cold=2000;
if(super>0)
super--;//子弹数减少
}
if(timer>0)
timer--;//计时
if(kbhit())//玩家移动
switch(getch()){
case left:
case 'a':
case 'A':if(pl>0)scr[EDGE][pl]=0,scr[EDGE][--pl]=1;break;
case right:
case 'd':
case 'D':if(pl<width-2) scr[EDGE][pl]=0,scr[EDGE][++pl]=1;break;
case up :
case 'W':
case 'w':if(gun>=3){
gun=0;
if(super>=1){
for(int v=1;v<=super;v++){
scr[EDGE-1][pl-v]=2;
scr[EDGE-1][pl+v]=2;
}
}
scr[EDGE-1][pl]=2;
}break;
case 27 :exit(0);break;//esc
case 13 :system("pause");break;//回车
case 9 :get_command();break;//tab
case 'q':
case 'Q':
case '0':if(bomb_num>0){bomb=1,boss_hp-=50,bomb_num--,mark=40,mode=0;}else mark=40,mode=1;break;
case 'e':
case 'E':
case '1':if(anger>=100){anger-=100;power=200;ready_def=50;} break;
}
summon_speed=density-game_clock/wave;
if(summon_speed<3)//生成飞机速度
summon_speed=3;
if(atk_time>0)//boss攻击计时
atk_time--;
else if(alive==1){//boss攻击
atk_time=100;
atk_way=rand()%2;
if(atk_way==0&&defence==0){
for(i=EDGE;i>=0;i--)
for(j=0;j<width;j++)
if(scr[i][j]==8)
scr[i+1][j]=3;
}
if(atk_way==1&&defence==0){
lingshi1=rand()%5+7;
for(i=lingshi1;i<=width-lingshi1-2;i++){
for(int p=8;p<=12;p++)
scr[p][i]=9;
}
}
}
if(ready_def>0&&alive==1&&power>0)//boss挡激光
ready_def--;
else if(alive==1&&power>0)
defence=1;
if(defence==1&&alive==1){
for(int v=1;v<=34;v++)
scr[9][v]=10;
for(int i=9;i<=width-11;i++)
for(int p=6;p<9;p++)
scr[p][i]=0;
}
if(++j%summon_speed==0 && summonable==1){//小怪和补给生成器
j=0;
fuck++;
lingshi1=rand()%width;
for(int i=1;i<=(game_clock/wave+1)/5;i++)
scr[i][lingshi1]=3;
scr[0][lingshi1]=3;
if(fuck%10==0){
scr[0][rand()%width/2]=6;
}
if(fuck%30==0){
scr[0][rand()%width]=5;
}
randoms=rand()%30;
if(randoms==1 && super<=7){
randoms=0;
scr[0][rand()%width]=7;
}
randoms=rand()%30;
if(randoms==1)
randoms=0,scr[0][rand()%width]=13;
}
if(++i%speed==0){//移动
movepla(scr);
movepac(scr);
}
movebul(scr);
print(scr);//绘制
}
yn();//死亡
}
void menu(){
PlaySound("menu.a",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);//音乐,可不选
system("color 07");
printf("\t\t\t\t*****Plane Shooting II*****\t\t\t\t\nHelp:\nPress A and D to move,Press W to fire;\nPress Q to use a bomb,Press E to use laser;\n");
printf("Seed(number):");
cin>>seed;
srand(seed);
printf("Name:");
scanf("%s",player_name);
player_num=strlen(player_name);
printf("Press Esc to enter settings;\nPress any button to start:\n");
if(getch()==27)
setting();
ShowCursor(false);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=false;
SetConsoleCursorInfo(hOut,&cci);//禁止显示光标
system("cls");
loading();
}
加bgm的话要建一个项目,加链接:-lwinmm。。。
同时还能加图标。。。
bgm和图标:http://pan.baidu.com/s/1eS4u9Aa点击打开
程序本体全套下载:http://pan.baidu.com/s/1slcTQ9n点击打开