国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++編寫簡易的飛機大戰(zhàn)

2020-01-26 14:59:32
字體:
供稿:網(wǎng)友

初學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    //敵人,自己,子彈結(jié)構體{  int x;  int y;  struct Node *pnext;}DiRen,FeiJi,ZiDan;void ZaoDiRen();                //造敵人void ShowDiRen(DiRen *pHead,HWND hWnd);     //顯示敵人void ZaoZiDan();                //造子彈void ShowZiDan(ZiDan *pHead,HWND hWnd);     //顯示子彈void DiRenMove(DiRen *pHead);          //敵人移動void ZiDanMove(DiRen *pHead);          //子彈移動void shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZiDan **zidan);//判斷是否射中   void ReleaseDiren(DiRen **pHead);        //釋放出去的敵人void ReleaseZidan(ZiDan **pHead);        //釋放出去的子彈void ZaoZiJi(HWND hWnd);            //造自己LRESULT CALLBACK pp(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);//回調(diào)函數(shù)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,100,600,600,NULL,NULL,hInstance,NULL);  ShowWindow(hWnd,nCmdShow);  while(GetMessage(&msg,NULL,0,0))  {    TranslateMessage(&msg);    DispatchMessage(&msg);  }  return 0;}DiRen *pDiRen=NULL;  //敵人ZiDan *pZiDan=NULL;  //子彈FeiJi *pZiJi=NULL;   //自己static int score=0;   //分數(shù)static char sco[20];  //裝分數(shù)的字符竄LRESULT CALLBACK pp(HWND hWnd,UINT msg,WPARAM wParam,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,0,0,600,600,memdc,0,0,SRCCOPY);    /*itoa(score,sco,10);*/    sprintf(sco,"%d",score);  //將分數(shù)裝入字符竄    jscore=score;    while((jscore=jscore/10)>0) //判斷分數(shù)有幾位      i++;    TextOut(hdc,0,0,"分數(shù)",4);    TextOut(hdc,30,0,sco,i); //顯示分數(shù)    DeleteDC(memdc);    ReleaseDC(hWnd,hdc);  //釋放句柄    DeleteObject(hbm);    ZaoZiJi(hWnd);     //造自己    if(TIMER_ZIDAN==wParam)  //定時器101    {      ZiDanMove(pZiDan);   //子彈移動      ReleaseZidan(&pZiDan); //釋放出屏幕的子彈    }    else if( TIMER_DIREN==wParam) //定時器102    {       ZaoDiRen();       //造敵人        }    else if(TIMER_DIRENRELEASE==wParam)  //定時器103    {      ReleaseDiren(&pDiRen);  //釋放出屏幕的敵人    }    ShowDiRen(pDiRen,hWnd);       //顯示敵人    DiRenMove(pDiRen);       //敵人移動    ShowZiDan(pZiDan,hWnd);     //顯示子彈    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:  //空格發(fā)射子彈      ZaoZiDan();      break;    }    break;  case WM_CREATE:   //創(chuàng)建    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,100,NULL);    SetTimer(hWnd,TIMER_DIRENRELEASE,300,NULL);    break;  }  return DefWindowProc(hWnd,msg,wParam,lParam);}  void ZaoDiRen()  //造子彈{  DiRen *u;  u=(struct Node*)malloc(sizeof(struct Node));   u->x=rand()%550;   //子彈的x隨機出現(xiàn)  u->y=-10;      //出現(xiàn)的縱坐標固定  u->pnext=NULL;  if(NULL==pDiRen)    {    pDiRen=u;  }  else  {    u->pnext=pDiRen;   //將新產(chǎn)生的鏈表放在頭    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, sizeof(bminfo), &bminfo);   memdc=CreateCompatibleDC(hdc);  SelectObject(memdc,hbm);  while(pHead!=NULL) //敵人鏈表不為空,顯示敵機  {     BitBlt(hdc,pHead->x,pHead->y,40,40,memdc,0,0,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, sizeof(bminfo), &bminfo);   memdc=CreateCompatibleDC(hdc);  SelectObject(memdc,hbm);  BitBlt(hdc,pZiJi->x,pZiJi->y,40,40,memdc,0,0,SRCCOPY); //顯示自己  DeleteDC(memdc);  ReleaseDC(hWnd,hdc);  DeleteObject(hbm);}void ZaoZiDan()   //造子彈{  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;  //將子彈放在鏈表頭    pZiDan=u;  }}void ShowZiDan(ZiDan *pHead,HWND hWnd) //顯示子彈{  HDC hdc;  HDC memdc;  HBITMAP hbm;  BITMAP bminfo;  hdc=GetDC(hWnd);  hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP2)); //插入子彈位圖  GetObject(hbm, sizeof(bminfo), &bminfo);   memdc=CreateCompatibleDC(hdc);  SelectObject(memdc,hbm);  while(pHead!=NULL)  //子彈鏈表不為空,顯示子彈  {    /*Ellipse(hdc,pHead->x,pHead->y,pHead->x+5,pHead->y+5);*/    BitBlt(hdc,pHead->x,pHead->y,10,10,memdc,0,0,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)  //子彈移動{  while(pHead!=NULL)  //鏈表不為空子彈移動  {    pHead->y-=20;    pHead=pHead->pnext;   }} void shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZiDan **zidan) //判斷是否中{  DiRen *js1=*diren;  ZiDan *js2=*zidan;  int n = 1;  while(js1!=NULL) //判斷自己是否撞機  {    //撞擊釋放定時器游戲結(jié)束    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) //判斷子彈是否為空    {          //敵機中彈      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) //鏈表下節(jié)不為空,指向下一個釋放中彈的飛機子彈        {          *diren = js1->pnext;          diren = &pDiRen;          free(js1);          free(js2);        }        else          *diren = NULL;          break;      }      else      {        zidan = &js2->pnext;  //沒中看下一個      }    }    if(n != 1)  //判斷是否是中彈出來的    {      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)  //釋放子彈{  ZiDan *js=*pHead;  while((js=*pHead)!=NULL)  {    if(js->y<0)    //飛出的子彈釋放    {      *pHead=js->pnext;        free(js);    }    else      pHead=&js->pnext;  //沒飛出看下一個  }}

同時分享一個網(wǎng)友的方法

//mytestView.cpp:Cmytest;//;#include"stdafx.h;#include"mytest.h;#include"mytestDoc;#include"mytestView;#ifdef_DEBUG;#definenewDEBUG_NEW;#endif;//CmytestVie// mytestView.cpp : CmytestView 類的實現(xiàn)//#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: 在此處為本機數(shù)據(jù)添加繪制代碼/*CBitmap bitmap;bitmap.LoadBitmapW(IDB_ME);*///畫圖/*pDC->BitBlt(100,50,50,60,&MemDC,0,0,SRCCOPY);*//*POINT pt;pt.x=200;pt.y=200;CImageList imageList;imageList.Create(50,60,ILC_COLOR24|ILC_MASK,1,0);imageList.Add(&bitmap,RGB(0,0,0));imageList.Draw(pDC,0,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,50,ILC_COLOR24|ILC_MASK,1,0);//imageList.Add(&bitmap,RGB(0,0,0));//imageList.Draw(pDC,0,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 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,ILC_COLOR24|ILC_MASK,1,0);imageList1.Add(&bitmap1,RGB(0,0,0));imageList1.Draw(pDC,0,pt1,ILD_TRANSPARENT);imageList1.Draw(pDC,1,pt1_2,ILD_TRANSPARENT);//戰(zhàn)機CBitmap bitmap2;bitmap2.LoadBitmapW(IDB_ME);POINT pt2;pt2.x=m_x_me;pt2.y=100;CImageList imageList2;imageList2.Create(50,60,ILC_COLOR24|ILC_MASK,1,0);imageList2.Add(&bitmap2,RGB(0,0,0));imageList2.Draw(pDC,0,pt2,ILD_TRANSPARENT);//子彈CBitmap bitmap3;bitmap3.LoadBitmapW(IDB_ball);POINT pt3;pt3.x=150;pt3.y=m_y_ball;CImageList imageList3;imageList3.Create(8,8,ILC_COLOR24|ILC_MASK,1,0);imageList3.Add(&bitmap3,RGB(0,0,0));imageList3.Draw(pDC,0,pt3,ILD_TRANSPARENT);//炸彈CBitmap bitmap4;bitmap4.LoadBitmapW(IDB_bomb);POINT pt4;pt4.x=m_x_bomb;pt4.y=250;CImageList imageList4;imageList4.Create(10,20,ILC_COLOR24|ILC_MASK,1,0);imageList4.Add(&bitmap4,RGB(0,0,0));imageList4.Draw(pDC,0,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,ILC_COLOR24|ILC_MASK,1,0);imageList5.Add(&bitmap5,RGB(0,0,0));imageList5.Draw(pDC,0,pt5_1,ILD_TRANSPARENT);imageList5.Draw(pDC,1,pt5_2,ILD_TRANSPARENT);imageList5.Draw(pDC,2,pt5_3,ILD_TRANSPARENT);imageList5.Draw(pDC,3,pt5_4,ILD_TRANSPARENT);imageList5.Draw(pDC,4,pt5_5,ILD_TRANSPARENT);imageList5.Draw(pDC,5,pt5_6,ILD_TRANSPARENT);imageList5.Draw(pDC,6,pt5_7,ILD_TRANSPARENT);imageList5.Draw(pDC,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 // 非調(diào)試版本是內(nèi)聯(lián)的 {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: 在此添加您專用的創(chuàng)建代碼SetTimer(1,30,NULL);return 0;}void CmytestView::OnTimer(UINT_PTR nIDEvent){// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認值CView::OnTimer(nIDEvent);CDC *pDC=GetDC();//刷新背景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);//戰(zhàn)機CBitmap bitmap2;bitmap2.LoadBitmapW(IDB_ME);POINT pt2;pt2.x=m_x_me;pt2.y=100;CImageList imageList2;imageList2.Create(50,60,ILC_COLOR24|ILC_MASK,1,0);imageList2.Add(&bitmap2,RGB(0,0,0));imageList2.Draw(pDC,0,pt2,ILD_TRANSPARENT);//子彈

歡迎試玩一下游戲 方向鍵:w,a,s,d  控制鍵:J,K

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳信县| 昭觉县| 英吉沙县| 横山县| 来安县| 保德县| 临湘市| 突泉县| 射阳县| 灵丘县| 瓦房店市| 云霄县| 泸定县| 桂东县| 大姚县| 渭源县| 环江| 红桥区| 芷江| 苗栗县| 乌恰县| 浪卡子县| 裕民县| 南阳市| 岐山县| 含山县| 临湘市| 元朗区| 黄冈市| 腾冲县| 密山市| 舒兰市| 金坛市| 仁寿县| 社旗县| 南涧| 邳州市| 永德县| 阳原县| 武穴市| 黄平县|