C++编写简易的飞机大战

C++编写简易的飞机大战,第1张

概述初学C/C++的小伙伴可以用做这个小游戏来熟悉一下编程的乐趣。#include<windows.h>

初学C/C++的小伙伴可以用做这个小游戏来熟悉一下编程的乐趣。

#include<windows.h>#include"resource.h"#include<stdlib.h>#include<time.h>#include<stdio.h> #define TIMER_DIREN 101      //定义定时器#define TIMER_DIRENMOVE 102#define TIMER_ZIDAN 103#define TIMER_DIRENRELEASE 104 typedef struct Node    //敌人,自己,子d结构体{  int x;  int y;  struct Node *pnext;}DiRen,FeiJi,ZIDan;voID ZaoDiRen();                //造敌人voID ShowDiRen(DiRen *phead,HWND hWnd);     //显示敌人voID ZaoZIDan();                //造子dvoID ShowZIDan(ZIDan *phead,HWND hWnd);     //显示子dvoID DiRenMove(DiRen *phead);          //敌人移动voID ZIDanMove(DiRen *phead);          //子d移动voID shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZIDan **zIDan);//判断是否射中   voID ReleaseDiren(DiRen **phead);        //释放出去的敌人voID ReleaseZIDan(ZIDan **phead);        //释放出去的子dvoID ZaoZiJi(HWND hWnd);            //造自己LRESulT CALLBACK pp(HWND hWnd,UINT msg,WParaM wParam,LParaM lParam);//回调函数int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdline,int nCmdshow){  WNDCLASSEX wc;  HWND hWnd;  MSG msg;  wc.hInstance=hInstance;  wc.cbClsExtra=0;  wc.cbSize=sizeof(WNDCLASSEX);  wc.cbWndExtra=0;  wc.hIcon=NulL ;  wc.hCursor=NulL ;  wc.hIconSm=NulL;  wc.lpfnWndProc=pp;  wc.lpszClassname="hello";  wc.lpszMenuname=NulL;  wc.style=CS_HREDRAW|CS_VREDRAW | CS_OWNDC ;  wc.hbrBackground=(HBrush)5;  RegisterClassEx(&wc);  hWnd=CreateWindow("hello","world",WS_OVERLAPPEDWINDOW,100,600,NulL,hInstance,NulL);  ShowWindow(hWnd,nCmdshow);  while(GetMessage(&msg,0))  {    TranslateMessage(&msg);    dispatchMessage(&msg);  }  return 0;}DiRen *pDiRen=NulL;  //敌人ZIDan *pZIDan=NulL;  //子dFeiJi *pZiJi=NulL;   //自己static int score=0;   //分数static char sco[20];  //装分数的字符窜LRESulT CALLBACK pp(HWND hWnd,LParaM lParam){  int i=1,//位    jscore;  HDC hdc;  HDC memdc;  HBITMAP hbm;  BITMAP bminfo;  switch(msg)  {  case WM_TIMER:   //定时器    hdc=GetDC(hWnd); //得到设备句柄    hbm=LoadBitmap(GetModuleHandle(NulL),MAKEINTRESOURCE(IDB_BITMAP4));//载入背景位图    Getobject(hbm,sizeof(bminfo),&bminfo);     memdc=CreateCompatibleDC(hdc);    SelectObject(memdc,hbm);    BitBlt(hdc,memdc,SRCcopY);    /*itoa(score,sco,10);*/    sprintf(sco,"%d",score);  //将分数装入字符窜    jscore=score;    while((jscore=jscore/10)>0) //判断分数有几位      i++;    textout(hdc,"分数",4);    textout(hdc,30,i); //显示分数    DeleteDC(memdc);    ReleaseDC(hWnd,hdc);  //释放句柄    DeleteObject(hbm);    ZaoZiJi(hWnd);     //造自己    if(TIMER_ZIDAN==wParam)  //定时器101    {      ZIDanMove(pZIDan);   //子d移动      ReleaseZIDan(&pZIDan); //释放出屏幕的子d    }    else if( TIMER_DIREN==wParam) //定时器102    {       ZaoDiRen();       //造敌人        }    else if(TIMER_DIRENRELEASE==wParam)  //定时器103    {      ReleaseDiren(&pDiRen);  //释放出屏幕的敌人    }    ShowDiRen(pDiRen,hWnd);       //显示敌人    DiRenMove(pDiRen);       //敌人移动    ShowZIDan(pZIDan,hWnd);     //显示子d    shoot(hWnd,pZiJi,&pDiRen,&pZIDan);   //是否射中    break;  case WM_CLOSE:    //关闭    PostQuitMessage(0);    break;  case WM_KEYDOWN:    //判断按键    switch(wParam)       {    case VK_left:   //左移      if(pZiJi->x>0)        pZiJi->x-=20;      break;    case VK_RIGHT:  //右移      if(pZiJi->x<530)      pZiJi->x+=20;      break;    case VK_UP:   //上移      if(pZiJi->y>0)      pZiJi->y-=20;      break;    case VK_DOWN:  //下移      if(pZiJi->y<520)      pZiJi->y+=20;      break;    case VK_SPACE:  //空格发射子d      ZaoZIDan();      break;    }    break;  case WM_CREATE:   //创建    srand(time(NulL));      pZiJi=(struct Node*)malloc(sizeof(struct Node));    pZiJi->x=200;     //自己的x    pZiJi->y=500;     //自己的y    SetTimer(hWnd,TIMER_DIREN,1000,NulL);  //设置定时器    SetTimer(hWnd,TIMER_DIRENMOVE,200,NulL);    SetTimer(hWnd,TIMER_ZIDAN,TIMER_DIRENRELEASE,300,NulL);    break;  }  return DefWindowProc(hWnd,msg,wParam,lParam);}  voID ZaoDiRen()  //造子d{  DiRen *u;  u=(struct Node*)malloc(sizeof(struct Node));   u->x=rand()%550;   //子d的x随机出现  u->y=-10;      //出现的纵坐标固定  u->pnext=NulL;  if(NulL==pDiRen)    {    pDiRen=u;  }  else  {    u->pnext=pDiRen;   //将新产生的链表放在头    pDiRen=u;   }}voID ShowDiRen(struct Node *phead,HWND hWnd)  //显示敌人{  HDC hdc;  HDC memdc;  HBITMAP hbm;  BITMAP bminfo;  hdc=GetDC(hWnd);  hbm=LoadBitmap(GetModuleHandle(NulL),MAKEINTRESOURCE(IDB_BITMAP1));//载入敌人位图  Getobject(hbm,&bminfo);   memdc=CreateCompatibleDC(hdc);  SelectObject(memdc,hbm);  while(phead!=NulL) //敌人链表不为空,显示敌机  {     BitBlt(hdc,phead->x,phead->y,40,SRCcopY);    phead=phead->pnext;   }  DeleteDC(memdc);  ReleaseDC(hWnd,hdc);  DeleteObject(hbm);}voID ZaoZiJi(HWND hWnd){  HDC hdc;  HDC memdc;  HBITMAP hbm;  BITMAP bminfo;  hdc=GetDC(hWnd);  hbm=LoadBitmap(GetModuleHandle(NulL),MAKEINTRESOURCE(IDB_BITMAP3));//载入自己的位图  Getobject(hbm,hbm);  BitBlt(hdc,pZiJi->x,pZiJi->y,SRCcopY); //显示自己  DeleteDC(memdc);  ReleaseDC(hWnd,hdc);  DeleteObject(hbm);}voID ZaoZIDan()   //造子d{  ZIDan *u;  u=(ZIDan*)malloc(sizeof(ZIDan));  u->x=pZiJi->x+15;  u->y=pZiJi->y+10;  u->pnext=NulL;  if(pZIDan==NulL)  {    pZIDan=u;  }    else  {    u->pnext=pZIDan;  //将子d放在链表头    pZIDan=u;  }}voID ShowZIDan(ZIDan *phead,HWND hWnd) //显示子d{  HDC hdc;  HDC memdc;  HBITMAP hbm;  BITMAP bminfo;  hdc=GetDC(hWnd);  hbm=LoadBitmap(GetModuleHandle(NulL),MAKEINTRESOURCE(IDB_BITMAP2)); //插入子d位图  Getobject(hbm,hbm);  while(phead!=NulL)  //子d链表不为空,显示子d  {    /*Ellipse(hdc,phead->x+5,phead->y+5);*/    BitBlt(hdc,10,SRCcopY);    phead=phead->pnext;  }    DeleteDC(memdc);  ReleaseDC(hWnd,hdc);  DeleteObject(hbm);} voID DiRenMove(DiRen *phead)  //敌人移动{  while(phead!=NulL)  //链表不为空,敌人移动  {      if(score<500)    {      phead->y+=10;      phead=phead->pnext;     }    else    {      phead->y+=20;      phead=phead->pnext;    }  }}voID ZIDanMove(DiRen *phead)  //子d移动{  while(phead!=NulL)  //链表不为空子d移动  {    phead->y-=20;    phead=phead->pnext;   }} voID shoot(HWND hWnd,ZIDan **zIDan) //判断是否中{  DiRen *Js1=*diren;  ZIDan *Js2=*zIDan;  int n = 1;  while(Js1!=NulL) //判断自己是否撞机  {    //撞击释放定时器游戏结束    if((ziji->x-Js1->x<30&&ziji->x-Js1->x>-38)&&(ziji->y-Js1->y<25&&ziji->y-Js1->y>-38))    {      KillTimer(hWnd,TIMER_DIREN);      KillTimer(hWnd,TIMER_ZIDAN);      KillTimer(hWnd,TIMER_DIRENMOVE);      KillTimer(hWnd,TIMER_DIRENRELEASE);      MessageBox(hWnd,"You Lose","窗口",MB_OK);      PostQuitMessage(0);      break;    }    else      Js1=Js1->pnext;  //没有判断下一个敌机  }   Js1=*diren;  //敌机回到头  while((Js1=*diren)!=NulL)  //判断敌人是否为空  {    zIDan = &pZIDan;      n = 0;    while((Js2=*zIDan)!=NulL) //判断子d是否为空    {          //敌机中d      if((Js2->x - Js1->x <= 40&&Js2->x - Js1->x>=-5)&&(Js2->y - Js1->y <= 40&&Js2->y - Js1->y>=-8))      {        score+=100;        n = 1;        *zIDan = Js2->pnext;        if(Js1->pnext!=NulL) //链表下节不为空,指向下一个释放中d的飞机子d        {          *diren = Js1->pnext;          diren = &pDiRen;          free(Js1);          free(Js2);        }        else          *diren = NulL;          break;      }      else      {        zIDan = &Js2->pnext;  //没中看下一个      }    }    if(n != 1)  //判断是否是中d出来的    {      diren = &Js1->pnext;     }  }}voID ReleaseDiren(DiRen **phead) //释放飞出屏幕的敌人{  DiRen *Js=*phead;  while((Js=*phead)!=NulL)  {    if(Js->y>600)    //飞出屏幕释放    {      *phead=Js->pnext;      free(Js);    }    else    {      phead = &Js->pnext;  //看下一个    }  }}voID ReleaseZIDan(ZIDan **phead)  //释放子d{  ZIDan *Js=*phead;  while((Js=*phead)!=NulL)  {    if(Js->y<0)    //飞出的子d释放    {      *phead=Js->pnext;        free(Js);    }    else      phead=&Js->pnext;  //没飞出看下一个  }}

同时分享一个网友的方法

//mytestVIEw.cpp:Cmytest;//;#include"stdafx.h;#include"mytest.h;#include"mytestDoc;#include"mytestVIEw;#ifdef_DEBUG;#definenewDEBUG_NEW;#endif;//CmytestVIE// mytestVIEw.cpp : CmytestVIEw 类的实现//#include "stdafx.h"#include "mytest.h"#include "mytestDoc.h"#include "mytestVIEw.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CmytestVIEwIMPLEMENT_DYNCREATE(CmytestVIEw,CVIEw)BEGIN_MESSAGE_MAP(CmytestVIEw,CVIEw)ON_WM_CREATE()ON_WM_TIMER()ON_WM_KEYDOWN()END_MESSAGE_MAP()// CmytestVIEw 构造/析构CmytestVIEw::CmytestVIEw(){// Todo: 在此处添加构造代码m_x_me=0;m_x_enemy=0;m_y_enemyone=0;m_y_enemytwo=0;m_y_bomb=0;m_x_bomb=0;m_x_ball=0;m_y_ball=0;m_x_explsion=0;}CmytestVIEw::~CmytestVIEw(){}BOol CmytestVIEw::PreCreateWindow(CREATESTRUCT& cs){// Todo: 在此处通过修改// CREATESTRUCT cs 来修改窗口类或样式return CVIEw::PreCreateWindow(cs);}// CmytestVIEw 绘制voID CmytestVIEw::OnDraw(CDC* pDC){CmytestDoc* pDoc = Getdocument();ASSERT_VALID(pDoc);if (!pDoc)return;// Todo: 在此处为本机数据添加绘制代码/*CBitmap bitmap;bitmap.LoadBitmapW(IDB_ME);*///画图/*pDC->BitBlt(100,50,60,&MemDC,SRCcopY);*//*POINT pt;pt.x=200;pt.y=200;CImageList imageList;imageList.Create(50,ILC_color24|ILC_MASK,1,0);imageList.Add(&bitmap,RGB(0,0));imageList.Draw(pDC,pt,ILD_transparent);CDC MemDC;MemDC.CreateCompatibleDC(NulL);MemDC.SelectObject(&bitmap);*///RECT rc;//GetClIEntRect(&rc);//CBrush brush;//brush.CreateSolIDBrush(RGB(3,108,254));//pDC->SelectObject(&brush);//CBrush *oldbrush=pDC->SelectObject(&brush);//pDC->Rectangle(&rc);//pDC->SelectObject(oldbrush);//CBitmap bitmap;//bitmap.LoadBitmapW(IDB_ME);//POINT pt;//pt.x=200;//pt.y=200;//CImageList imageList;//imageList.Create(60,0);//imageList.Add(&bitmap,0));//imageList.Draw(pDC,ILD_transparent);// CDC MemDC;//MemDC.CreateCompatibleDC(NulL);//MemDC.SelectObject(&bitmap);//刷新RECT rc;GetClIEntRect(&rc);CBrush brush;brush.CreateSolIDBrush(RGB(3,254));pDC->SelectObject(&brush);CBrush *oldbrush=pDC->SelectObject(&brush);pDC->Rectangle(&rc);pDC->SelectObject(oldbrush);//敌机CBitmap bitmap1;bitmap1.LoadBitmapW(IDB_enemy);POINT pt1;pt1.x=200;pt1.y=m_y_enemyone;POINT pt1_2;pt1_2.x=300;pt1_2.y=m_y_enemytwo;CImageList imageList1;imageList1.Create(35,35,0);imageList1.Add(&bitmap1,0));imageList1.Draw(pDC,pt1,ILD_transparent);imageList1.Draw(pDC,pt1_2,ILD_transparent);//战机CBitmap bitmap2;bitmap2.LoadBitmapW(IDB_ME);POINT pt2;pt2.x=m_x_me;pt2.y=100;CImageList imageList2;imageList2.Create(50,0);imageList2.Add(&bitmap2,0));imageList2.Draw(pDC,pt2,ILD_transparent);//子dCBitmap bitmap3;bitmap3.LoadBitmapW(IDB_ball);POINT pt3;pt3.x=150;pt3.y=m_y_ball;CImageList imageList3;imageList3.Create(8,8,0);imageList3.Add(&bitmap3,0));imageList3.Draw(pDC,pt3,ILD_transparent);//炸dCBitmap bitmap4;bitmap4.LoadBitmapW(IDB_bomb);POINT pt4;pt4.x=m_x_bomb;pt4.y=250;CImageList imageList4;imageList4.Create(10,20,0);imageList4.Add(&bitmap4,0));imageList4.Draw(pDC,pt4,ILD_transparent);//爆炸CBitmap bitmap5;bitmap5.LoadBitmapW(IDB_explsion);POINT pt5_1;pt5_1.x=310;pt5_1.y=310;POINT pt5_2;pt5_2.x=330;pt5_2.y=330;POINT pt5_3;pt5_3.x=350;pt5_3.y=450;POINT pt5_4;pt5_4.x=470;pt5_4.y=470;POINT pt5_5;pt5_5.x=510;pt5_5.y=510;POINT pt5_6;pt5_6.x=530;pt5_6.y=530;POINT pt5_7;pt5_7.x=540;pt5_7.y=540;POINT pt5_8;pt5_8.x=450;pt5_8.y=250;CImageList imageList5;imageList5.Create(66,66,0);imageList5.Add(&bitmap5,0));imageList5.Draw(pDC,pt5_1,ILD_transparent);imageList5.Draw(pDC,pt5_2,2,pt5_3,3,pt5_4,4,pt5_5,5,pt5_6,6,pt5_7,7,pt5_8,ILD_transparent);/*CDC MemDC;MemDC.CreateCompatibleDC(NulL);MemDC.SelectObject(&bitmap2);*/}// CmytestVIEw 诊断#ifdef _DEBUGvoID CmytestVIEw::AssertValID() const{CVIEw::AssertValID();}voID CmytestVIEw::Dump(CDumpContext& dc) const{CVIEw::Dump(dc);}CmytestDoc* CmytestVIEw::Getdocument() const // 非调试版本是内联的 {ASSERT(m_pdocument->IsKindOf(RUNTIME_CLASS(CmytestDoc))); return (CmytestDoc*)m_pdocument;}#endif //_DEBUG// CmytestVIEw 消息处理程序int CmytestVIEw::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CVIEw::OnCreate(lpCreateStruct) == -1)return -1;// Todo: 在此添加您专用的创建代码SetTimer(1,NulL);return 0;}voID CmytestVIEw::OnTimer(UINT_PTR nIDEvent){// Todo: 在此添加消息处理程序代码和/或调用默认值CVIEw::OnTimer(nIDEvent);CDC *pDC=GetDC();//刷新背景RECT rc;GetClIEntRect(&rc);CBrush brush;brush.CreateSolIDBrush(RGB(3,254));pDC->SelectObject(&brush);CBrush *oldbrush=pDC->SelectObject(&brush);pDC->Rectangle(&rc);pDC->SelectObject(oldbrush);//战机CBitmap bitmap2;bitmap2.LoadBitmapW(IDB_ME);POINT pt2;pt2.x=m_x_me;pt2.y=100;CImageList imageList2;imageList2.Create(50,ILD_transparent);//子d

欢迎试玩一下游戏 方向键:w,a,s,d  控制键:J,K

总结

以上是内存溢出为你收集整理的C++编写简易的飞机大战全部内容,希望文章能够帮你解决C++编写简易的飞机大战所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1254511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存