C语言飞机大战

158 阅读1分钟

`#include <stdio.h> #include <conio.h> #include <graphics.h> #include <math.h> //图形库 #include <time.h> #pragma commnt(lib,"Winmm.lib"); #include <stdlib.h> enum My { WIDTH = 680, HEIGHT = 750, //窗口宽度和高度 BULLET_NUM = 15, //玩家子弹数量 ENEMY_NUM = 10, ONE, TWO };

struct Plance //飞机机构 { double x; double y; bool live; //是否存活 int width; int height; int hp; int type; //敌机类型 }player,bull[BULLET_NUM],enemy[ENEMY_NUM];

IMAGE bk; IMAGE img_role[2]; //玩家图片 IMAGE img_bull[2]; //子弹 IMAGE img_enemy[2][2]; //敌机

int score=0;

void loadImage() { //加载图片 480 * 700 loadimage(&bk,"./images/background.png"); //背景 loadimage(&img_role[0],"./images/me1-1.png"); // 玩家飞机 loadimage(&img_role[1],"./images/me1-2.png"); loadimage(&img_bull[0],"./images/bullet1.png"); //子弹 loadimage(&img_bull[1],"./images/bullet2.png"); loadimage(&img_enemy[0][0],"./images/enemy2-1.png"); //敌机 loadimage(&img_enemy[0][1],"./images/enemy2-2.png"); loadimage(&img_enemy[1][0],"./images/enemy1-1.png"); loadimage(&img_enemy[1][1],"./images/enemy1-2.png");

}

//敌机生命值 void enemyHp(int i) { int flag = rand() % 10; if(flag >= 0 && flag <=2) { enemy[i].type = ONE; enemy[i].hp = 3; enemy[i].width = 69; enemy[i].height = 99; } else { enemy[i].type = TWO; enemy[i].hp = 1; enemy[i].width = 57; enemy[i].height = 43; } }

//游戏初始化 void gameInit() { loadImage(); player.x = WIDTH/2; player.y = HEIGHT-120; player.live = true; //初始化子弹 for(int i = 0;i < BULLET_NUM; i++) { bull[i].x = 0; bull[i].y = 0; bull[i].live = false; } //初始化敌机 for(int j = 0; j < ENEMY_NUM; j++) { enemy[j].live = false; enemyHp(i); }

}

//游戏测绘函数 void gameDraw() { putimage(0,0,&bk); //放置背景图 putimage(player.x, player.y,&img_role[0],NOTSRCERASE);
putimage(player.x, player.y,&img_role[1],SRCINVERT); for(int i = 0;i < BULLET_NUM; i++) { if(bull[i].live) { putimage(bull[i].x, bull[i].y,&img_bull[0],NOTSRCERASE);
putimage(bull[i].x, bull[i].y,&img_bull[1],SRCINVERT); }

}

for(int j = 0; j < ENEMY_NUM; j++)
{
  if(enemy[j].live)
  {
    if(enemy[j].type == TWO)
	{
	  putimage(enemy[j].x,enemy[j].y,&img_enemy[1][1],NOTSRCERASE);
	  putimage(enemy[j].x,enemy[j].y,&img_enemy[1][0],SRCINVERT);
	}
	else
	{
	  putimage(enemy[j].x,enemy[j].y,&img_enemy[0][1],NOTSRCERASE);
	  putimage(enemy[j].x,enemy[j].y,&img_enemy[0][0],SRCINVERT);
	}
  }
}
   outtextxy(WIDTH / 2 - 50, HEIGHT - 100, "击毁:");
char s[5];
sprintf(s, "%d", score);
outtextxy(WIDTH / 2, HEIGHT - 100, s);

FlushBatchDraw();

}

//产生子弹 void createBullet() { for(int i = 0;i < BULLET_NUM; i++) { if(!bull[i].live) { bull[i].x = player.x+51; bull[i].y = player.y; bull[i].live = true; break; } } }

//子弹移动 void BulletMove() { for(int i = 0;i < BULLET_NUM; i++) { if(bull[i].live) { bull[i].y -= 1; if(bull[i].y < 0) { bull[i].live = false; } } } }

//延迟 bool Timer(int ms,int id) { static DWORD t[10]; if(clock() - t[id] > ms) { t[id] = clock(); return true; } return false; }

//角色移动 void PlayerMove(double speed) { /if(_kbhit()) { char key = _getch(); switch(key) { case 'w': case 'W': player.y -= speed; break; case 's': case 'S': player.y +=speed; break; case 'a': case 'A': player.x -= speed; break; case 'd': case 'D': player.x +=speed; break; } }/ //阻塞函数

if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W'))
{
	if(player.y > 0)
	{
	  player.y -= speed;
	}		
}
if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S'))
{
	if(player.y <HEIGHT-126)
	{
	  player.y += speed;
	}
}
if(GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A'))
{
	if(player.x+51 > 0)
	{
	  player.x -= speed;
	}	
}
if(GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D'))
{
	if(player.x < WIDTH-51)
	{
	  player.x += speed;
	}	
}
if(GetAsyncKeyState(VK_SPACE) && Timer(50,1))
{
  //创建一个子弹
	createBullet();
}

}

//产生敌机 void createEnemy() { for(int i = 0; i<ENEMY_NUM; i++) { if(!enemy[i].live) { enemy[i].live = true; enemy[i].x = rand()%(WIDTH-100); enemy[i].y = 0; enemyHp(i); break; } } }

void playPlance() { for(int i = 0; i < ENEMY_NUM; i++) { if(!enemy[i].live) continue; for(int j = 0; j<BULLET_NUM; j++) { if(!bull[j].live) continue; if(bull[j].x>enemy[i].x && bull[j].x<enemy[i].x+enemy[i].width && bull[j].y>enemy[i].y && bull[j].y<enemy[i].y+enemy[i].height) { bull[j].live = false; enemy[i].hp--; } } if(enemy[i].hp <= 0) { enemy[i].live = false; score++; } } }

//敌机移动 void enemyMove(double speed) { for(int i = 0; i < ENEMY_NUM; i++) { if(enemy[i].live) { enemy[i].y += speed; if(enemy[i].y > HEIGHT) { // enemy[i].live = false; exit(0); } } } }

int main() { int a;

printf("请输入密码:");
scanf("%d",&a);
plane1:
if(a == 6666)
{
//创建一个窗口
initgraph(WIDTH,HEIGHT);
gameInit();
//双缓冲绘图
BeginBatchDraw();


while(1)
{
  gameDraw();
  FlushBatchDraw();
  PlayerMove(0.3);
  BulletMove();
  if(Timer(500,0))
  {
    createEnemy();
  }
  enemyMove(0.05);
  playPlance();
  if(player.live = false)
  {
    return 0;
  }
}
EndBatchDraw();
}
else
{
  plane2:
  printf("密码错误,请重修输入密码:");
  scanf("%d",&a);
  if(a == 6666)
  {
    goto plane1;
  }
  else
  {
    goto plane2;
  }
}
return 0;

}`

//空格发射子弹,wads或者方向键移动,密码6666