【c语言】飞机大战终

简介: 【c语言】飞机大战终

效果展示

效果演示

源码展示

#include<stdio.h>
#include <graphics.h>
#include <assert.h>
#include <stdlib.h>
#include<conio.h>//_getch();
#include <time.h>
#include <math.h>
#include<mmsystem.h>//包含多媒体设备接口头文件
#pragma comment(lib,"winmm.lib")//加载静态库
#define PI 3.1415926
#define HEIGHT  503
#define WIDTH 700
int score = 0;
static int enemy = 0;
static int mode = 0;
IMAGE img_bk, img_plane, img_a, img_b, img_c, img_abullet, img_bbullet, img_cbullet, img_planebullet, img_tmp, img_start, img_sta, img_fail, img_history,img_hp,img_nam,img_score,img_select;
typedef struct bullet
{
  float x, y;
  float vx, vy;
  int  isexist;
  struct bullet* next;
}list;
list* planebullet = NULL;
list* abullet = NULL;
list* bbullet = NULL;
void pushback2(list** pphead, float vx, float vy);
void pushback3(list** pphead, float vx, float vy);
void pushback1(list** pphead, list* newnode);//尾插;
struct aircraft
{
  int x, y;
  int width;
  int height;
  int speed;
  int bornflag;
  int hp;
};
aircraft plane, a, b, c;
static int dir1 = 1;
bool Timer(int ms, int id)
{
  static DWORD t[10];
  if (clock() - t[id] > ms)
  {
    t[id] = clock();
    return true;
  }
  return false;
}
void bgm()
{    //打开音乐
  mciSendString("open ./bgm.MP3", 0, 0, 0);//后面参数不用管
  //播放音乐
  mciSendString("play ./bgm.MP3", 0, 0, 0);//后面参数不用管
}
void bgm1()
{
  mciSendString("close ./shoot.MP3", 0, 0, 0);//后面参数不用管
  //打开音乐
  mciSendString("open ./shoot.MP3", 0, 0, 0);//后面参数不用管
  //播放音乐
  mciSendString("play ./shoot.MP3", 0, 0, 0);//后面参数不用管
}
void bgm2()
{    //打开音乐
  mciSendString("open ./boom.MP3", 0, 0, 0);//后面参数不用管
  //播放音乐
  mciSendString("play ./boom.MP3", 0, 0, 0);//后面参数不用管
}
void bgm3()
{
  mciSendString("close ./change.MP3", 0, 0, 0);//后面参数不用管
  //打开音乐
  mciSendString("open ./change.MP3", 0, 0, 0);//后面参数不用管
  //播放音乐
  mciSendString("play ./change.MP3", 0, 0, 0);//后面参数不用管
}
void bgm4()
{
  
  mciSendString("open ./win.MP3", 0, 0, 0);//后面参数不用管
  //播放音乐
  mciSendString("play ./win.MP3", 0, 0, 0);//后面参数不用管
}
void datainit()
{
  plane.x = 150;
  plane.y = 150;
  //a = { 0,0 };
  /*b = { 300,0 };*/
  /*c = { 450,0 };*/
  a.speed = 1;
  a.bornflag = 1;
  b.bornflag = 1;
  c.bornflag = 1;
  a.width = 100;
  a.height = 100;
  b.speed = 1;
  b.width = 80;
  b.height = 100;
  c.height = 70;
  c.width = 70;
  c.speed = 3;
  plane.hp = 500;
  a.hp = 500;
  b.hp = 500;
  c.hp = 500;
}
list* BuyabulletNode(float vx, float vy)
{
  list* newnode = (list*)malloc(sizeof(list));//空间申请
  assert(newnode);//断言,新结点是否申请到了
  newnode->vx = vx;//数据赋值
  newnode->vy = vy;//数据赋值
  newnode->x = a.x + a.width / 2-10;
  newnode->y = a.y+80;
  newnode->isexist = 1;
  newnode->next = NULL;//指向的地址赋值
  return newnode;//将申请好的空间首地址返回回去
}
list* BuybbulletNode(float vx, float vy)
{
  list* newnode = (list*)malloc(sizeof(list));//空间申请
  assert(newnode);//断言,新结点是否申请到了
  newnode->vx = vx;//数据赋值
  newnode->vy = vy;//数据赋值
  newnode->x = b.x + b.width / 2 - 10;
  newnode->y = b.y + 80;
  newnode->isexist = 1;
  newnode->next = NULL;//指向的地址赋值
  return newnode;//将申请好的空间首地址返回回去
}
list* BuyplanebulletNode(float vx, float vy)
{
  list* newnode = (list*)malloc(sizeof(list));//空间申请
  assert(newnode);//断言,新结点是否申请到了
  newnode->vx = vx;//数据赋值
  newnode->vy = vy;//数据赋值
  newnode->x = plane.x + plane.width / 2+17;
  newnode->y = plane.y;
  newnode->isexist = 1;
  newnode->next = NULL;//指向的地址赋值
  return newnode;//将申请好的空间首地址返回回去
}
void drawAlpha(IMAGE* picture, int  picture_x, int picture_y) //x为载入图片的X坐标,y为Y坐标
{
  // 变量初始化
  DWORD* dst = GetImageBuffer();    // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
  DWORD* draw = GetImageBuffer();
  DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
  int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
  int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
  int graphWidth = getwidth();       //获取绘图区的宽度,EASYX自带
  int graphHeight = getheight();     //获取绘图区的高度,EASYX自带
  int dstX = 0;    //在显存里像素的角标
  // 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
  for (int iy = 0; iy < picture_height; iy++)
  {
    for (int ix = 0; ix < picture_width; ix++)
    {
      int srcX = ix + iy * picture_width; //在显存里像素的角标
      int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
      int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
      int sg = ((src[srcX] & 0xff00) >> 8);   //G
      int sb = src[srcX] & 0xff;              //B
      if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
      {
        if ((ix + picture_x) >= 0 && (ix + picture_x) <= graphWidth)  //防止出边界后循环显示
        {
          dstX = (ix + picture_x) + (iy + picture_y) * graphWidth; //在显存里像素的角标
          int dr = ((dst[dstX] & 0xff0000) >> 16);
          int dg = ((dst[dstX] & 0xff00) >> 8);
          int db = dst[dstX] & 0xff;
          draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16)  //公式: Cp=αp*FP+(1-αp)*BP  ; αp=sa/255 , FP=sr , BP=dr
            | ((sg * sa / 255 + dg * (255 - sa) / 255) << 8)         //αp=sa/255 , FP=sg , BP=dg
            | (sb * sa / 255 + db * (255 - sa) / 255);              //αp=sa/255 , FP=sb , BP=db
        }
      }
    }
  }
}
void selectplane()
{
  while (1)
  {
    BeginBatchDraw();
    loadimage(&img_select, "./22.png");
    drawAlpha(&img_select, 0, 0);
    loadimage(&img_select, "./23.png");
    drawAlpha(&img_select, 0, 0);
    loadimage(&img_select, "./24.png");
    drawAlpha(&img_select, 0, 400);
    if (mode == 0)
    {
      
      loadimage(&img_plane, "./1.png");
      drawAlpha(&img_plane, 300, 200);
    }
    
    if (mode == 1)
    {
      loadimage(&img_plane, "./10.png");
      drawAlpha(&img_plane, 300, 200);
    }
    else if (mode == 2)
    {
      loadimage(&img_plane, "./11.png");
      drawAlpha(&img_plane, 300, 200);
    }
    else if (mode == 3)
    {
      loadimage(&img_plane, "./12.png");
      drawAlpha(&img_plane, 300, 200);
    }
    else if (mode == 4)
    {
      loadimage(&img_plane, "./13.png");
      drawAlpha(&img_plane, 300, 200);
    }
    else if (mode == 5)
    {
      loadimage(&img_plane, "./25.png");
      drawAlpha(&img_plane, 300, 200);
    }
    
  
    
    if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A') && Timer(300, 1))
    {
      bgm3();
      if (mode == 0)
      {
        mode = 0;
      }
      else
      {
        mode--;
      }
    }
    
    if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D') && Timer(300, 1))
    {
      bgm3();
      if (mode == 5)
      {
        mode = 5;
      }
      else
      {
        mode++;
      }
    }
    if ((GetAsyncKeyState(VK_SPACE)) && Timer(300, 1))
    {
      break;
    }
    EndBatchDraw();
  }
}
void load()
{
  loadimage(&img_bk, "./back.png");
  if (mode == 0)
  {
    loadimage(&img_plane, "./1.png");
  }
  loadimage(&img_a, "./2.png");
  loadimage(&img_b, "./3.png");
  loadimage(&img_c, "./4.png");
  loadimage(&img_abullet, "./5.png");
  loadimage(&img_bbullet, "./6.png");
  loadimage(&img_cbullet, "./7.png");
  if (mode == 0)
  {
    loadimage(&img_planebullet, "./8.png");
  }
  if (mode == 1)
  {
    loadimage(&img_planebullet, "./8.png");
  }
  if (mode == 2)
  {
    loadimage(&img_planebullet, "./5.png");
  }
  if (mode == 3)
  {
    loadimage(&img_planebullet, "./16.png");
  }
  if (mode == 4)
  {
    loadimage(&img_planebullet, "./7.png");
  }
  if (mode == 5)
  {
    loadimage(&img_planebullet, "./19.png");
  }
}
void draw()
{
  putimage(0, 0,&img_bk);
  loadimage(&img_nam, "./20.png");
  drawAlpha(&img_nam, 500, 0);
  
  if (plane.hp == 500)
  {
    //img_hp
    loadimage(&img_hp, "./19.png");
    drawAlpha(&img_hp, 628, 0);
  }
  if (plane.hp == 1000)
  {
    //img_hp
    loadimage(&img_hp, "./19.png");
    loadimage(&img_hp, "./19.png");
    drawAlpha(&img_hp, 628, 0);
    drawAlpha(&img_hp, 652, 0);
  }
  if (plane.hp == 1500)
  {
    //img_hp
    loadimage(&img_hp, "./19.png");
    loadimage(&img_hp, "./19.png");
    loadimage(&img_hp, "./19.png");
    drawAlpha(&img_hp, 628, 0);
    drawAlpha(&img_hp, 652, 0);
    drawAlpha(&img_hp, 676, 0);
  }
  if (plane.x > -1 && plane.x < WIDTH && plane.y>-1 && plane.y + 48< HEIGHT)
  {
    drawAlpha(&img_plane, plane.x, plane.y);
  }
  else
  {
    putimage(plane.x, plane.y, &img_plane);
  }
  if (a.x > -1 && a.x < WIDTH && a.y>0&& a.y + 98 < HEIGHT)
  {
    drawAlpha(&img_a, a.x, a.y);
  }
  else
  {
    putimage(a.x, a.y, &img_a);
  }
  if (b.x > -1 && b.x < WIDTH && b.y>-1 && b.y +120 < HEIGHT)
  {
    drawAlpha(&img_b, b.x, b.y);
  }
  else
  {
    putimage(b.x, b.y, &img_b);
  }
  if (c.x > -1 && c.x < WIDTH && c.y>-1 && c.y + 120 < HEIGHT)
  {
    drawAlpha(&img_c, c.x, c.y);
  }
  else
  {
    putimage(c.x, c.y, &img_c);
  }
  
  
  
    
}
void ufoamove()
{
  
  static int cnt = 0;
  if (a.bornflag == 1)
  {
    a.bornflag = 0;
    a.x = rand() % (WIDTH - a.width);
    a.y = -50;
  }
  if (a.y > 200)
  {
    dir1 = 0;
  }
  else if (a.y < -150)
  {
    dir1 = 1;
    a.bornflag = 1;
  }
  if (1 == dir1)
  {
    a.y += a.speed;
  }
  else
  {
    a.y -= a.speed;
  }
  if (++cnt % 50 == 0)
  {
    pushback2(&abullet, 0, 10);
  
  }
  if (cnt > 99999)
  {
    cnt = 0;
  }
}
void ufobmove()
{
  static int num = 0;
  static int step = b.speed;
  if (b.bornflag == 1)
  {
    b.bornflag = 0;
    b.x = rand() % (WIDTH - b.width);
    b.y = -b.height;
  }
  if (b.x <= 0 || b.x + b.width >= WIDTH)
  {
    step = -step;
  }
  b.x += step;
  b.y++;
  if (b.y >= HEIGHT)
  {
    b.bornflag = 1;
  }
  if (++num % 100 == 0)
  {
    pushback3(&bbullet, 0, 10);
    /*for (int i = 0; i < 10; i++)
    {
      float angle = i * 2 * PI / 10;
      float vx = 1* sin(angle);
      float vy = 1 * cos(angle);
      pushback3(&bbullet, vx, vy);
    }*/
  }
  if (num > 99999)
  {
    num = 0;
  }
}
void pushback1(list** pphead,float vx,float vy)//尾插
{
  
  
  list* newnode = BuyplanebulletNode(vx, vy);
  
  if (*pphead == NULL)//链表无结点
  {
    *pphead = newnode;// 将创建好的头节点的地址给给*pphead,作为新头节点的地址
    
  }
  else
  {
    
    list* tail = *pphead;//定义一个指针,先指向头结点的地址
    while (tail->next != NULL)//循环遍历找尾结点
    {
      tail = tail->next;//指针指向下一个结点
    }
    tail->next = newnode;//找到尾结点,将尾结点的next存放新接结点的地址
  }
}
void pushback2(list** pphead, float vx, float vy)//尾插
{
  
  list* newnode = BuyabulletNode(vx, vy);
  if (*pphead == NULL)//链表无结点
  {
    *pphead = newnode;// 将创建好的头节点的地址给给*pphead,作为新头节点的地址
  }
  else
  {
    
    list* tail = *pphead;//定义一个指针,先指向头结点的地址
    while (tail->next != NULL)//循环遍历找尾结点
    {
      tail = tail->next;//指针指向下一个结点
    }
    tail->next = newnode;//找到尾结点,将尾结点的next存放新接结点的地址
  }
}
void pushback3(list** pphead, float vx, float vy)//尾插
{
  
  list* newnode = BuybbulletNode(vx, vy);
  if (*pphead == NULL)//链表无结点
  {
    *pphead = newnode;// 将创建好的头节点的地址给给*pphead,作为新头节点的地址
  }
  else
  {
    
    list* tail = *pphead;//定义一个指针,先指向头结点的地址
    while (tail->next != NULL)//循环遍历找尾结点
    {
      tail = tail->next;//指针指向下一个结点
    }
    tail->next = newnode;//找到尾结点,将尾结点的next存放新接结点的地址
  }
}
void removebullet(list** pplist)
{
  if (*pplist == NULL)
    return;
  list* cur = *pplist;
  list* prev = NULL;
  while (cur != NULL)
  {
    if (cur->isexist == 0)
    {
      if (*pplist == cur)
      {
        *pplist = cur->next;
        free(cur);
        cur = *pplist;
      }
      else
      {
        prev->next = cur->next;
        free(cur);
        cur = prev;
      }
    }
    else
    {
      prev = cur;
      cur = cur->next;
    }
  }
}
void listchangexy(list** pplist)
{
  if (*pplist == NULL)
    return;
  list* cur = *pplist;
  while (cur != NULL)
  {
    cur->x += cur->vx;
    cur->y += cur->vy;
    
    if ((cur->y<0 )|| (cur->y>HEIGHT) || (cur->x >0) || (cur->x <WIDTH))
      cur->isexist = 0;
    cur = cur->next;
  }
}
void showbullet()
{
  static int count1 = 0;
  
  
  listchangexy(&planebullet);
  if (++count1 == 20)
  {
    removebullet(&planebullet);
    removebullet(&abullet);
    removebullet(&bbullet);
  }
  
  ///}
  if (count1 > 99999)
  {
    count1 = 0;
  }
  for (list* cur = planebullet; cur!= NULL; cur = cur ->next)
  {
    if (mode == 0)
    {
      if (cur->x > 5 && cur->x + 15 < WIDTH && cur->y > 5 && cur->y + 15 < HEIGHT)
      {
        drawAlpha(&img_planebullet, cur->x, cur->y);
      }
      else
      {
        putimage(cur->x, cur->y, &img_planebullet);
      }
    }
  if (mode == 1)
    {
      if (cur->x > 5 && cur->x + 15 < WIDTH && cur->y > 5 && cur->y + 15 < HEIGHT)
      {
        drawAlpha(&img_planebullet, cur->x, cur->y);
      }
      else
      {
        putimage(cur->x, cur->y, &img_planebullet);
      }
    }
    if (mode == 2)
    {
      if (cur->x > 5 && cur->x + 25 < WIDTH && cur->y > 5 && cur->y + 35 < HEIGHT)
      {
        drawAlpha(&img_planebullet, cur->x, cur->y);
      }
      else
      {
        putimage(cur->x, cur->y, &img_planebullet);
      }
    }
    if (mode == 3)
    {
      if (cur->x > 5 && cur->x + 15 < WIDTH && cur->y > 5 && cur->y + 40 < HEIGHT)
      {
        drawAlpha(&img_planebullet, cur->x, cur->y);
      }
      else
      {
        putimage(cur->x, cur->y, &img_planebullet);
      }
    }
    if (mode == 4)
    {
      if (cur->x > 5 && cur->x + 25< WIDTH && cur->y > 5 && cur->y + 43 < HEIGHT)
      {
        drawAlpha(&img_planebullet, cur->x, cur->y);
      }
      else
      {
        putimage(cur->x, cur->y, &img_planebullet);
      }
    }
      if (mode == 5)
    {
      if (cur->x > 5 && cur->x + 25< WIDTH && cur->y > 5 && cur->y + 43 < HEIGHT)
      {
        drawAlpha(&img_planebullet, cur->x, cur->y);
      }
      else
      {
        putimage(cur->x, cur->y, &img_planebullet);
      }
     }
    
    
    
    
    
    
    
    
    
    
    
    if (cur->x + 10 < a.x || cur->x > a.x + a.width || cur->y + 10 > a.y || cur->y > a.y + a.height)
    {
    }
    else
    {
      cur->isexist = 0;
      score += 600;
      a.hp -= 500;
    }
    if (cur->x + 10 < b.x || cur->x > b.x + b.width || cur->y + 10 > b.y || cur->y > b.y + b.height)
    {
    }
    else
    {
      cur->isexist = 0;
      score += 400;
      b.hp -= 500;
    }
    if (cur->x + 10 < c.x || cur->x > c.x + c.width || cur->y + 10 > c.y || cur->y > c.y + c.height)
    {
    }
    else
    {
      cur->isexist = 0;
      score += 200;
      c.hp -= 500;
    }
  }
  
  listchangexy(&abullet);
  
  
  
  for (list* cur = abullet; cur != NULL; cur = cur->next)
  {
    if (cur->x > 5 && cur->x + 25 < WIDTH && cur->y > 5 && cur->y + 30 < HEIGHT)
    {
      drawAlpha(&img_abullet, cur->x, cur->y);
      
    }
    else
    {
      putimage(cur->x, cur->y, &img_abullet);
    }
    
    
      if (cur->x + 20 < plane.x || cur->x > plane.x + plane.width || cur->y + 30 > plane.y || cur->y > plane.y + plane.height)
      {
        
      }
      else
      {
        cur->isexist = 0;
      
        plane.hp -= 1000;
      }
    
    
    
  
  }
  listchangexy(&bbullet);
  for (list* cur = bbullet; cur != NULL; cur = cur->next)
  {
    if (cur->x > 5 && cur->x + 15 < WIDTH && cur->y > 5 && cur->y + 20 < HEIGHT)
    {
      drawAlpha(&img_bbullet, cur->x, cur->y);
    }
    else
    {
      putimage(cur->x, cur->y, &img_bbullet);
    }
    
  
    if (cur->x + 10< plane.x || cur->x > plane.x + plane.width || cur->y + 15 > plane.y || cur->y > plane.y + plane.height)
    {
    }
    else
    {
      cur->isexist = 0;
      plane.hp -= 1000;
    }
  
  }
}
void ufocmove()
{
  static float disx = 0, disy = 0;
  static float tmpx = 0, tmpy = 0;
  static float vx = 0, vy = 0;
  
  float step = 1000 / c.speed;
  if (1 == c.bornflag)
  {
    c.bornflag = 0;
    tmpx = rand() % (WIDTH - c.width);
    tmpy = -c.height;
    disx = plane.x - tmpx;
    disy = plane.y - tmpy;
    vx = disx / step;
    vy = disy / step;
  }
  tmpx += vx;
  tmpy += vy;
  c.x = (int)(tmpx + 0.5);
  c.y = (int)(tmpy + 0.5);
  if (c.x < -c.width)
  {
    c.bornflag = 1;
  }
  if (c.x > WIDTH)
  {
    c.bornflag = 1;
  }
  if (c.y > HEIGHT)
  {
    c.bornflag = 1;
  }
  if (c.hp <= 0)
  {
    c.bornflag = 1;
    c.hp = 500;
  }
}
void player_move(int speed) //处理飞机移动
{
  int reload_time = 100;
  static int fire_start = 0;
  int tmp = clock();
  if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W'))
  {
    if (plane.y > 0)
      plane.y -= speed;
  }
  if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S'))
  {
    if (plane.y + 51 < HEIGHT)
      plane.y += speed;
  }
  if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A'))
  {
    if (plane.x > 0)
      plane.x -= speed;
  }
  if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D'))
  {
    if (plane.x + 51 < WIDTH)
      plane.x += speed;
  }
  if ((GetAsyncKeyState(VK_SPACE))&& Timer(300, 1))
  {
    
    
    if (mode == 0)
    {
      //bgm1();
      pushback1(&planebullet, 0, -20);
      
    }
    if (mode == 1)
    {
      //bgm1();
      pushback1(&planebullet, 0, -20);
    }
    if (mode == 2)
    {
      //bgm1();
      pushback1(&planebullet, 0, -10);
      
    }
    if (mode == 3)
    {
      //bgm1();
      pushback1(&planebullet, 0, -10);
      }
      
    if (mode == 4)
    {
      //bgm1();
      pushback1(&planebullet, 0, -10);
    }
    if (mode == 5)
    {
      
      
      pushback1(&planebullet, 0, -10);
      
    }
    
    
    
  }
  if (GetAsyncKeyState(VK_SPACE))
  {
    bgm1();
  }
}
void begin()
{
  
  char beginstring[] = "开始游戏";
  char closestring[] = "退出游戏";
  char tipstring[] = "游戏说明: 空格:发射子弹";
  setfillcolor(LIGHTBLUE);
  solidrectangle(240, 300, 380, 350);//画矩形
  solidrectangle(240, 360, 380,410);//左上坐标,右下坐标
  loadimage(&img_start,"./start.jpg");
  loadimage(&img_sta, "./sta.png");
  
  
  
  //putimage(0, 0, &img_start);
  drawAlpha(&img_start,0,0);
  drawAlpha(&img_sta, 0, 0);
    
  settextcolor(RED);
  setbkmode(TRANSPARENT);
  settextstyle(30, 0, "楷体");
  outtextxy(240 + 10, 300 + 10, beginstring);
  outtextxy(240 + 10, 360 + 10, closestring);
  outtextxy(130, 250, tipstring);
  while (1)
  {
    MOUSEMSG m = GetMouseMsg();//鼠标在矩形显示外框
    if (m.x >= 240 && m.x <= 380 && m.y >= 300 && m.y <= 350)
    {
      setlinecolor(RED);
      rectangle(240 - 5, 300 - 5, 380 + 5, 350 + 5);
      if (m.uMsg == WM_LBUTTONDOWN)
      {
        break;
      }
    }
    else if (m.x >= 240 && m.x <= 380 && m.y >= 360 && m.y <= 410)
    {
      setlinecolor(RED);
      rectangle(240 - 5, 360 - 5, 380 + 5, 410 + 5);
      //退出游戏
      if (m.uMsg == WM_LBUTTONDOWN)
      {
        exit(0);
      }
    }
    else//没在擦除
    {
      setlinecolor(WHITE);
      rectangle(240 - 5, 300 - 5, 380 + 5, 350 + 5);
      rectangle(240 - 5, 360 - 5, 380 + 5, 410 + 5);
    }
  }
}
void crash2()
{
  if (b.x + b.width<plane.x || b.x>plane.x + plane.width || b.y + b.height<plane.y || b.y>plane.y + plane.height)
  {
  }
  else
  {
    plane.hp=0;
    bgm2();
    Timer(4000, 1);
    if (plane.hp == 0)
    {
      loadimage(&img_fail, "./18.png");
      drawAlpha(&img_fail, 120, 0);
      loadimage(&img_plane, "./boom5.png");
      drawAlpha(&img_plane, plane.x, plane.y);
      
      if (Timer(3000, 1))
        exit(0);
    }
  }
}
void crash1()
{
  if (a.x + a.width<plane.x || a.x>plane.x + plane.width || a.y + a.height<plane.y || a.y>plane.y + plane.height)
  {
  }
  else
  {
    plane.hp -= 500;
    bgm2();
    Timer(4000, 1);
    if (plane.hp == 0)
    {
      loadimage(&img_fail, "./18.png");
      drawAlpha(&img_fail, 120, 0);
      loadimage(&img_plane, "./boom5.png");
      drawAlpha(&img_plane, plane.x, plane.y);
      
      if (Timer(3000, 1))
        exit(0);
    }
  
    
  }
  
}
void crash3()
{
  if (c.x + c.width<plane.x || c.x>plane.x + plane.width || c.y + c.height<plane.y || c.y>plane.y + plane.height)
  {
  }
  else
  {
    plane.hp = 0;
    bgm2();
    Timer(4000, 1);
    if (plane.hp == 0)
    {
      loadimage(&img_fail, "./18.png");
      drawAlpha(&img_fail, 120, 0);
      loadimage(&img_plane, "./boom5.png");
      drawAlpha(&img_plane, plane.x, plane.y);
      
      if (Timer(4000, 1))
        exit(0);
    }
  }
}
void showScore(int x, int y, int score)
{
  TCHAR time_text[50];
  _stprintf_s(time_text, _T("Score:%d"), score);
  outtextxy(x, y, time_text);
}
int main()
 {
  initgraph(WIDTH, HEIGHT,CONSOLE_FULLSCREEN);
  
  datainit();
  begin();
  selectplane();
  BeginBatchDraw();
  bgm();
  
  while (1)
  {
    
    
  
    
  
    
    load();
    draw();
    ufoamove();
    ufobmove();
    ufocmove();
    player_move(5);
    crash1();
    crash2();
    crash3();
    showScore(520, 50, score);
    printf("hp:%d\n", c.hp);
    if (plane.hp<=0)
    {
      loadimage(&img_fail, "./18.png");
      drawAlpha(&img_fail, 120, 0);
      loadimage(&img_plane, "./boom5.png");
      drawAlpha(&img_plane, plane.x, plane.y);
      
      if (Timer(3000, 1))
        exit(0);
    }
    if (a.hp <= 0)
    {
      
      a.bornflag = 1;
      a.hp = 500;
    }
    if (b.hp <= 0)
    {
      
      b.bornflag =1;
      b.hp = 500;
    }
    if (score > 10000)
    {
      bgm4();
      loadimage(&img_fail, "./26.png");
      drawAlpha(&img_fail, 100, 0);
    
      if (Timer(2000, 1))
      exit(0);
    }
    
  
  
    showbullet();
  FlushBatchDraw();
  }
  EndBatchDraw();
  getchar();
}

还有一些bug,等我考完试了在改改

目录
相关文章
|
9月前
|
算法 C语言 C++
【c语言】飞机大战(1)
【c语言】飞机大战(1)
82 1
|
C语言
c语言小游戏-飞机大战
c语言小游戏-飞机大战
146 0
|
存储 C语言
【C语言】飞机大战游戏还原,源码在文末,应用“循环”与“数组”实现游戏开发,一起玩一下经典小游戏吧
众所周知昂,飞机大战,重在大战嘛,你这一颗子弹一架敌机,打的那是一点儿激情也没有。所以我们这章内容,就来对前文做修改,运用上数组手法,给它盘圆咯 在二维数组中存储游戏画面数据,元素为0时输出“ (空格)”,元素为1时输出大灰机“ * ”,元素为2输出子弹“¥”元素为3时输出敌机“@”定义二维数组存储游戏画面中元素的数组暂且定为4.基础功能的实现在飞机大战中,用人类语言来描述相关内容,则有以下这些,积分计算(包括击毁敌机,未击毁,飞机升级子弹,多架敌机产生……)这些我们都要一一实现 多架敌机产生这块儿,
【C语言】飞机大战游戏还原,源码在文末,应用“循环”与“数组”实现游戏开发,一起玩一下经典小游戏吧
|
1月前
|
存储 算法 C语言
【C语言程序设计——函数】素数判定(头歌实践教学平台习题)【合集】
本内容介绍了编写一个判断素数的子函数的任务,涵盖循环控制与跳转语句、算术运算符(%)、以及素数的概念。任务要求在主函数中输入整数并输出是否为素数的信息。相关知识包括 `for` 和 `while` 循环、`break` 和 `continue` 语句、取余运算符 `%` 的使用及素数定义、分布规律和应用场景。编程要求根据提示补充代码,测试说明提供了输入输出示例,最后给出通关代码和测试结果。 任务核心:编写判断素数的子函数并在主函数中调用,涉及循环结构和条件判断。
62 23
|
1月前
|
算法 C语言
【C语言程序设计——函数】利用函数求解最大公约数和最小公倍数(头歌实践教学平台习题)【合集】
本文档介绍了如何编写两个子函数,分别求任意两个整数的最大公约数和最小公倍数。内容涵盖循环控制与跳转语句的使用、最大公约数的求法(包括辗转相除法和更相减损术),以及基于最大公约数求最小公倍数的方法。通过示例代码和测试说明,帮助读者理解和实现相关算法。最终提供了完整的通关代码及测试结果,确保编程任务的成功完成。
66 15
|
1月前
|
C语言
【C语言程序设计——函数】亲密数判定(头歌实践教学平台习题)【合集】
本文介绍了通过编程实现打印3000以内的全部亲密数的任务。主要内容包括: 1. **任务描述**:实现函数打印3000以内的全部亲密数。 2. **相关知识**: - 循环控制和跳转语句(for、while循环,break、continue语句)的使用。 - 亲密数的概念及历史背景。 - 判断亲密数的方法:计算数A的因子和存于B,再计算B的因子和存于sum,最后比较sum与A是否相等。 3. **编程要求**:根据提示在指定区域内补充代码。 4. **测试说明**:平台对代码进行测试,预期输出如220和284是一组亲密数。 5. **通关代码**:提供了完整的C语言代码实现
60 24
|
1月前
|
存储 C语言
【C语言程序设计——函数】递归求斐波那契数列的前n项(头歌实践教学平台习题)【合集】
本关任务是编写递归函数求斐波那契数列的前n项。主要内容包括: 1. **递归的概念**:递归是一种函数直接或间接调用自身的编程技巧,通过“俄罗斯套娃”的方式解决问题。 2. **边界条件的确定**:边界条件是递归停止的条件,确保递归不会无限进行。例如,计算阶乘时,当n为0或1时返回1。 3. **循环控制与跳转语句**:介绍`for`、`while`循环及`break`、`continue`语句的使用方法。 编程要求是在右侧编辑器Begin--End之间补充代码,测试输入分别为3和5,预期输出为斐波那契数列的前几项。通关代码已给出,需确保正确实现递归逻辑并处理好边界条件,以避免栈溢出或结果
63 16
|
1月前
|
存储 编译器 C语言
【C语言程序设计——函数】分数数列求和2(头歌实践教学平台习题)【合集】
函数首部:按照 C 语言语法,函数的定义首部表明这是一个自定义函数,函数名为fun,它接收一个整型参数n,用于指定要求阶乘的那个数,并且函数的返回值类型为float(在实际中如果阶乘结果数值较大,用float可能会有精度损失,也可以考虑使用double等更合适的数据类型,这里以float为例)。例如:// 函数体代码将放在这里函数体内部变量定义:在函数体中,首先需要定义一些变量来辅助完成阶乘的计算。比如需要定义一个变量(通常为float或double类型,这里假设用float。
36 3
|
1月前
|
存储 算法 安全
【C语言程序设计——函数】分数数列求和1(头歌实践教学平台习题)【合集】
if 语句是最基础的形式,当条件为真时执行其内部的语句块;switch 语句则适用于针对一个表达式的多个固定值进行判断,根据表达式的值与各个 case 后的常量值匹配情况,执行相应 case 分支下的语句,直到遇到 break 语句跳出 switch 结构,若没有匹配值则执行 default 分支(可选)。例如,在判断一个数是否大于 10 的场景中,条件表达式为 “num> 10”,这里的 “num” 是程序中的变量,通过比较其值与 10 的大小关系来确定条件的真假。常量的值必须是唯一的,且在同一个。
19 2
|
1月前
|
存储 编译器 C语言
【C语言程序设计——函数】回文数判定(头歌实践教学平台习题)【合集】
算术运算于 C 语言仿若精密 “齿轮组”,驱动着数值处理流程。编写函数求区间[100,500]中所有的回文数,要求每行打印10个数。根据提示在右侧编辑器Begin--End之间的区域内补充必要的代码。如果操作数是浮点数,在 C 语言中是不允许直接进行。的结果是 -1,因为 -7 除以 3 商为 -2,余数为 -1;注意:每一个数据输出格式为 printf("%4d", i);的结果是 1,因为 7 除以 -3 商为 -2,余数为 1。取余运算要求两个操作数必须是整数类型,包括。开始你的任务吧,祝你成功!
52 1