这是自己实训写的代码,现在分享给大家,直接贴代码,主要用到光标定位,和多线程
运行效果
下面是全部代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <conio.h>
#include <windows.h>
int time_=400;
void gotoxy (int ,int);
void printAirport(); //打印飞机
void printAirport2();
void printfdank(int x,int y);//发射子弹
void printBorder();//打印边框
void HideCursor();//隐藏光标
void cleanAirport(int x,int y);//清除飞机
void cleanAll();
void start();
void start2();
void printRule();//打印规则
void printRule2();
int welCome();
void StringShan(int x,int y,char s[],int n);
void switchWelcome(int *weizhi,int IsUp);
void moveAirPort();
void moveAirPort2();
void moveWel();
void gameRule();
void menue();
void Myexit();
HANDLE h1,h2;
CRITICAL_SECTION cs;//定义临界区全局变量
int max_x=62,max_y=23,min_x=14,min_y=5;
int x,y=23,current_y=0,current_x=0;
int lastx,lasty;
char dank[5]="↑";
char t_dank[5]="↑";
char *p=t_dank;
int score=0,guan=1;
char score_r[50];
int flag=1;
FILE *fp;
struct qiang
{
int x_p,y_p;
int start_head,start_end;
int end_head,end_end;
int last_x;
}qiang[10000];
int instence=8;
struct enemy
{
int x_p,y_p;
}enemy[60];
DWORD WINAPI qiangMove(LPVOID lpParameter);
DWORD WINAPI enemyMove(LPVOID lpParameter);
DWORD WINAPI downAirport(LPVOID lpParameter); //声明线程函数
void main()
{
//初始化临界区
InitializeCriticalSection(&cs);
HideCursor();
menue();
}
void menue()
{
int xuanzhe;
while(1)
{
flag=1;
score=0;
guan=1;
system("cls");
xuanzhe=welCome();
system("cls");
switch(xuanzhe)
{
case 0: start();break;
case 1: start2();break;
case 2:gameRule();break;
case 3:exit(0);break;
}
}
}
int welCome()
{
int i=0;
char jian[]="→";
char xuanzhe;
int weizhi=0;
printBorder();
gotoxy(26,3);
printf("欢迎来到飞机大战小游戏!!");
gotoxy(30,12);
printf("1.射击模式");
gotoxy(30,14);
printf("2.障碍模式");
gotoxy(30,16);
printf("3.游戏规则");
gotoxy(30,18);
printf("4.退 出");
gotoxy(30,22);
printf("(按↑↓选择,回车键确认)");
moveWel();
StringShan(26,12,jian,sizeof(jian)/sizeof(jian[0]));
while(1)
{
switch(xuanzhe=getch())
{
case 72: switchWelcome(&weizhi,1);break;
case 80: switchWelcome(&weizhi,0);break;
case 13:if(weizhi==0)StringShan(30,12,"1.射击模式",10);
else if(weizhi==1)
StringShan(30,14,"2.障碍模式",10);
else if(weizhi==2)
StringShan(30,16,"3.游戏规则",10);
else if(weizhi==3)
StringShan(30,18,"4.退 出",10);
return weizhi;
default:
break;
}
}
}
void switchWelcome(int *weizhi,int IsUp)
{
gotoxy(26,12);
printf(" ");
gotoxy(26,14);
printf(" ");
gotoxy(26,16);
printf(" ");
gotoxy(26,18);
printf(" ");
if((*weizhi)==0)
{
if(IsUp)
{
*weizhi=3;
gotoxy(26,18);
printf("→");
}else{
*weizhi=1;
gotoxy(26,14);
printf("→");
}
}else if((*weizhi)==1)
{
if(IsUp)
{
*weizhi=0;
gotoxy(26,12);
printf("→");
}else{
*weizhi=2;
gotoxy(26,16);
printf("→");
}
}else if((*weizhi)==2)
{
if(IsUp)
{
*weizhi=1;
gotoxy(26,14);
printf("→");
}else{
*weizhi=3;
gotoxy(26,18);
printf("→");
}
}else if((*weizhi)==3)
{
if(IsUp)
{
*weizhi=2;
gotoxy(26,16);
printf("→");
}else{
*weizhi=0;
gotoxy(26,12);
printf("→");
}
}
}
void StringShan(int x,int y,char s[],int n)
{
int i,j;
for(i=0;i<3;i++)
{
gotoxy(x,y);
for(j=0;j<n;j++)
printf(" ");
gotoxy(x,y);
Sleep(200);
printf("%s",s);
printf(" ");
gotoxy(x,y);
Sleep(200);
printf("%s",s);
}
}
void moveWel()
{ int i;
x=3;y=23;
for(i=y;i>=3;i--)
{
y=i;
printAirport();
Sleep(30);
cleanAirport(x,i);
}
printAirport();
}
void start()
{
char ch;
x=max_x/2+4;
y=23;
printRule();
printAirport();
printBorder();
h1 = CreateThread(NULL, 0, enemyMove, NULL, 0, NULL); //创建线程
while(1)
{
if(!flag)break;
switch(ch=getch())
{
case 72: if(y==min_y)break;lastx=x;lasty=y;y--;moveAirPort();Sleep(5);break;
case 80: if(y==max_y)break;lastx=x;lasty=y;y++;moveAirPort();Sleep(5);break;
case 75: if(x==min_x)break;lastx=x;lasty=y;x--;moveAirPort();Sleep(5);break;
case 77: if(x==max_x)break;lastx=x;lasty=y;x++;moveAirPort();Sleep(5);break;
case 32: printfdank(x,y); break;
default:
break;
}
}
}
void start2()
{
char c;
printBorder();
x=20;y=12;
printAirport2();
printRule2();
h1 = CreateThread(NULL, 0, qiangMove, NULL, 0, NULL);
h2 = CreateThread(NULL, 0, downAirport, NULL, 0, NULL);
while(1)
{
if(!flag)break;
c=getch();
if(c==32){
lastx=x;
lasty=y;
y--;
moveAirPort2();
}
Sleep(20);
}
}
void moveAirPort()
{
if(!flag)return;
EnterCriticalSection(&cs);
gotoxy(lastx,lasty);
printf(" ");
gotoxy(lastx-2,lasty+1);
printf(" ");
gotoxy(lastx,lasty+1);
printf(" ");
gotoxy(lastx+2,lasty+1);
printf(" ");
gotoxy(lastx,lasty+2);
printf(" ");
gotoxy(x,y);
printf("▲");
gotoxy(x-2,y+1);
printf("▇");
gotoxy(x,y+1);
printf("①");
gotoxy(x+2,y+1);
printf("▇");
gotoxy(x,y+2);
printf("█");
LeaveCriticalSection(&cs);
}
void moveAirPort2()
{
EnterCriticalSection(&cs);
gotoxy(lastx,lasty);
printf(" ");
gotoxy(lastx-2,lasty+1);
printf(" ");
gotoxy(lastx,lasty+1);
printf(" ");
gotoxy(lastx+2,lasty+1);
printf(" ");
gotoxy(lastx,lasty+2);
printf(" ");
gotoxy(x,y);
printf("▇");
gotoxy(x-2,y+1);
printf("〓");
gotoxy(x,y+1);
printf("▇");
gotoxy(x+2,y+1);
printf(">");
gotoxy(x,y+2);
printf("▇");
LeaveCriticalSection(&cs);
}
void printRule()
{
char c;
int i=0;
if((fp=fopen("d:\\score.txt","r"))==NULL)
{
gotoxy(68,5);
printf("历史最高得分");
gotoxy(68,6);
printf("0分");
}else
{ i=0;
while((c=fgetc(fp))!=EOF)
{
score_r[i++]=c;
}
fclose(fp);
score_r[i]='\0';
gotoxy(68,5);
printf("历史最高得分");
gotoxy(68,6);
printf("%s分",score_r);
}
gotoxy(34,3);
printf("第%d关",guan);
gotoxy(70,14);
printf("得分:%d",score);
gotoxy(1,13);
printf("位置控制");
gotoxy(4,14);
printf("↑");
gotoxy(2,15);
printf("←");
gotoxy(6,15);
printf("→");
gotoxy(4,16);
printf("↓");
gotoxy(2,19);
printf("发子弹");
gotoxy(2,20);
printf(" space");
gotoxy(1,22);
printf("防御线----");
gotoxy(68,22);
printf("----防御线");
}
void printBorder()
{
int i;
gotoxy(10,4);
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
for(i=0;i<22;i++){
gotoxy(10,5+i);
printf("┃");
gotoxy(66,5+i);
printf("┃");
}
gotoxy(10,26);
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
}
void cleanAirport(int x,int y)
{
gotoxy(x,y);
printf(" ");
gotoxy(x-2,y+1);
printf(" ");
gotoxy(x,y+1);
printf(" ");
gotoxy(x+2,y+1);
printf(" ");
gotoxy(x,y+2);
printf(" ");
}
void printAirport()
{
gotoxy(x,y);
printf("▲");
gotoxy(x-2,y+1);
printf("▇");
gotoxy(x,y+1);
printf("①");
gotoxy(x+2,y+1);
printf("▇");
gotoxy(x,y+2);
printf("█");
}
void printAirport2()
{
gotoxy(x,y);
printf("▇");
gotoxy(x-2,y+1);
printf("〓");
gotoxy(x,y+1);
printf("▇");
gotoxy(x+2,y+1);
printf(">");
gotoxy(x,y+2);
printf("▇");
}
DWORD WINAPI downAirport(LPVOID lpParameter)
{
while(1)
{
if(!flag)break;
EnterCriticalSection(&cs);
if(y==min_y||y==max_y)
{
Myexit();
LeaveCriticalSection(&cs);
return 0;
}
if(!flag)break;
lastx=x;
lasty=y;
y++;
moveAirPort2();
LeaveCriticalSection(&cs);
Sleep(200);
}
return 0;
}
void printfdank(int x,int y)
{
int temp_x=x,temp_y=y,i=0;
if(!flag)return;
current_x=temp_x;
while(*p)
{
dank[i++]=*p;
p++;
}
dank[i]='\0';
p=t_dank;
for(current_y=temp_y-2;current_y>=min_y;current_y--)
{
EnterCriticalSection(&cs);
if(current_y==min_y)
{
gotoxy(temp_x,current_y+1);
printf(" ");
}else{
gotoxy(temp_x,current_y+1);
printf(" ");
gotoxy(temp_x,current_y);
printf("%s",dank);
}
LeaveCriticalSection(&cs);
Sleep(45);
}
}
void gameRule()
{
system("cls");
printBorder();
gotoxy(34,3);
printf("游戏规则");
gotoxy(18,6);
printf("游戏分为射击模式和障碍模式,射击模式:通过↑←→");
gotoxy(12,8);
printf("↓控制飞机移动,按空格发射子弹。敌人在最上方随机");
gotoxy(12,10);
printf("掉落,敌人到达防御线则游戏结束!敌人随得分越高掉");
gotoxy(12,12);
printf("落速度越快! 障碍模式:飞机会子自动下落和右移动,");
gotoxy(12,14);
printf("通过空格控制让飞机上下调节越过障碍墙!");
gotoxy(34,22);
printf("任意键返回主菜单");
getch();
system("cls");
menue();
}
void printRule2()
{
char c;
int i=0;
if((fp=fopen("d:\\score2.txt","r"))==NULL)
{
gotoxy(68,5);
printf("历史最高得分");
gotoxy(68,6);
printf("0分");
}else
{ i=0;
while((c=fgetc(fp))!=EOF)
{
score_r[i++]=c;
}
fclose(fp);
score_r[i]='\0';
gotoxy(68,5);
printf("历史最高得分");
gotoxy(68,6);
printf("%s分",score_r);
}
gotoxy(2,19);
printf("控制");
gotoxy(2,20);
printf(" space");
gotoxy(70,14);
printf("得分:%d",score);
}
void Myexit()
{
int k,temp;
char c;
flag=0;
if((fp=fopen("d:\\score2.txt","r"))==NULL)
{
fp=fopen("d:\\score2.txt","w");
itoa(score, score_r, 10);
k=0;
while(score_r[k])
{
fputc(score_r[k],fp);
k++;
}
fclose(fp);
}else
{
k=0;
while((c=fgetc(fp))!=EOF)
{
score_r[k++]=c;
}
score_r[k]='\0';
fclose(fp);
temp=atoi(score_r);
if(score>temp)
{
fp=fopen("d:\\score2.txt","w");
itoa(score, score_r, 10);
k=0;
while(score_r[k])
{
fputc(score_r[k],fp);
k++;
}
fclose(fp);
}
}
system("cls");
printRule2();
printBorder();
EnterCriticalSection(&cs);
gotoxy(30,15);
printf("游戏结束!!任意键结束");
LeaveCriticalSection(&cs);
getch();
}
DWORD WINAPI qiangMove(LPVOID lpParameter)
{
int i,j,num,temp;
srand(time(0));
for(i=0;i<100;i++)
{
qiang[i].x_p=35+12*i;
qiang[i].last_x=qiang[i].x_p;
qiang[i].start_head=5;
num=5+rand()%6+2;
qiang[i].start_end=num;
qiang[i].end_head=num+instence;
qiang[i].end_end=25;
}
while(1)
{
if(!flag)break;
for(i=0;i<100;i++)
{
if(!flag)break;
if(qiang[i].last_x==min_x-2)
{
score++;
gotoxy(70,14);
printf("得分:%d",score);
for(j=min_y;j<=max_y+2;j++)
{ EnterCriticalSection(&cs);
gotoxy(qiang[i].last_x,j);
printf(" ");
LeaveCriticalSection(&cs);
}
}else
if(qiang[i].x_p>min_x-3&&qiang[i].x_p<max_x)
{
if(qiang[i].x_p==22)
{
if(y+1<=qiang[i].start_end||y+2>=qiang[i].end_head)
{
Myexit();
CloseHandle(h2);
CloseHandle(h1);
return 0;
}
}else if(qiang[i].x_p==20)
{
if(y<=qiang[i].start_end||y+3>=qiang[i].end_head)
{
Myexit();
CloseHandle(h2);
CloseHandle(h1);
return 0;
}
}else if(qiang[i].x_p==18)
{
if(y+1<=qiang[i].start_end||y+2>=qiang[i].end_head)
{
Myexit();
CloseHandle(h2);
CloseHandle(h1);
return 0;
}
}
EnterCriticalSection(&cs);
for(j=qiang[i].start_head;j<=qiang[i].start_end;j++)
{
gotoxy(qiang[i].last_x,j);
printf(" ");
}
for(j=qiang[i].start_head;j<=qiang[i].start_end;j++)
{
gotoxy(qiang[i].x_p,j);
printf("▇");
}
for(j=qiang[i].end_head;j<=qiang[i].end_end;j++)
{
gotoxy(qiang[i].last_x,j);
printf(" ");
}
for(j=qiang[i].end_head;j<=qiang[i].end_end;j++)
{
gotoxy(qiang[i].x_p,j);
printf("▇");
}
LeaveCriticalSection(&cs);
}
}
if(!flag)break;
for(i=0;i<100;i++)
{ if(!flag)break;
qiang[i].last_x=qiang[i].x_p;
qiang[i].x_p--;
}
if(!flag)break;
Sleep(200);
}
}
DWORD WINAPI enemyMove(LPVOID lpParameter)
{
int n,m,i,j,temp,k;
int srand_x[63]={0};
char c='0';
srand(time(0));
for(i=0;i<20;i++)
{
while(1)
{
n=(int)(rand()%48)+14;
if(srand_x[n]!=1)
{
srand_x[n]=1;
break;
}
}
m=6;
enemy[i].x_p=n;
enemy[i].y_p=m;
}
for(i=1;i<=20;i++)
{
if(i%5==0)
{
time_-=100;
guan++;
EnterCriticalSection(&cs);
gotoxy(34,3);
printf("第%d关",guan);
gotoxy(34,3);
printf("第%d关",guan);
printf(" ");
gotoxy(34,3);
LeaveCriticalSection(&cs);
}
Sleep(20);
while(1)
{
if(time==0)
time_=100;
for(j=0;j<5;j++)
{
EnterCriticalSection(&cs);
gotoxy(enemy[i].x_p,enemy[i].y_p-1);
printf(" ");
gotoxy(enemy[i].x_p,enemy[i].y_p);
printf("●");
LeaveCriticalSection(&cs);
}
if(enemy[i].y_p==22)
{
EnterCriticalSection(&cs);
gotoxy(enemy[i].x_p,enemy[i].y_p);
printf(" ");
system("cls");
printRule();
printBorder();
gotoxy(30,15);
if((fp=fopen("d:\\score.txt","r"))==NULL)
{
fp=fopen("d:\\score.txt","w");
itoa(score, score_r, 10);
k=0;
while(score_r[k])
{
fputc(score_r[k],fp);
k++;
}
fclose(fp);
}
else
{
k=0;
while((c=fgetc(fp))!=EOF)
{
score_r[k++]=c;
}
score_r[k]='\0';
fclose(fp);
temp=atoi(score_r);
if(score>temp)
{
fp=fopen("d:\\score.txt","w");
itoa(score, score_r, 10);
k=0;
while(score_r[k])
{
fputc(score_r[k],fp);
k++;
}
fclose(fp);
}
}
LeaveCriticalSection(&cs);
flag=0;
printf("游戏结束!!任意键结束");
getch();
CloseHandle(h1);
return 0;
}
if(enemy[i].x_p==current_x&&enemy[i].y_p>=current_y)
{
EnterCriticalSection(&cs);
score+=10;
gotoxy(70,14);
printf("得分:%d",score);
gotoxy(enemy[i].x_p,enemy[i].y_p);
printf(" ");
dank[0]='\0';
LeaveCriticalSection(&cs);
break;
}
Sleep(time_);
enemy[i].y_p++;
}
}
gotoxy(30,15);
printf(" ");
gotoxy(30,15);
printf("恭喜您通关了!!!");
if(time_<=0)
return 0;
CloseHandle(h1);
return 0;
}
void HideCursor() // 用于隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy (int x1,int y1){
HANDLE hout;
COORD coord;
coord.X=x1;
coord.Y=y1;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,coord);
}
转载:https://blog.csdn.net/qq_40173146/article/details/104325838