C + + Write a simple aircraft war _c language

Source: Internet
Author: User
Tags textout

Beginners can be used as a small partner for this small game to familiarize yourself with the fun of programming.

#include <windows.h> #include "resource.h" #include <stdlib.h> #include <time.h> #include <stdio.h  > #define TIMER_DIREN 101//Definition Timer #define TIMER_DIRENMOVE 102 #define Timer_zidan #define Timer_direnrelease
  The struct Node//enemy, itself, bullet structure body {int x;
  int y;
struct Node *pnext;
}diren,feiji,zidan;                void Zaodiren ();     Create enemy void Showdiren (Diren *phead,hwnd HWND);                Show enemy void Zaozidan ();     Bullet void Showzidan (Zidan *phead,hwnd HWND);          Show bullet void Direnmove (Diren *phead);          The enemy Moves the void Zidanmove (Diren *phead);        The bullet moves void shoot (HWND hwnd,feiji *ziji,diren **diren,zidan **zidan);//Determine whether to hit void Releasediren (Diren **phead);        Release the Enemy void Releasezidan (Zidan **phead);            Release the Bullet void Zaoziji (HWND hwnd); Build yourself Lresult CALLBACK pp (HWND hwnd,uint msg,wparam wparam,lparam LPARAM);/callback function int __stdcall WinMain (hinstance Hinstanc E,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 (&AMP;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 (&AMP;MSG);
return 0;  } Diren *pdiren=null;  The enemy Zidan *pzidan=null;   Bullets Feiji *pziji=null;   own static int score=0;  Fractional static char sco[20];
  Loaded fractions of character channeling Lresult CALLBACK pp (HWND hwnd,uint msg,wparam wparam,lparam LPARAM) {int i=1,//bit jscore;
  HDC HDC;
  HDC MEMDC;
  Hbitmap hbm;
  BITMAP Bminfo; Switch (msg) {case WM_TIMER://Timer HDC=GETDC (HWND);//Get device handle hbm=LoadBitmap (GetModuleHandle (NULL), Makeintresource (IDB_BITMAP4))//Load background bitmap 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);
    The score is loaded into the character channeling Jscore=score;
    while ((JSCORE=JSCORE/10) >0)//Judgment score has several i++;
    TextOut (hdc,0,0, "score", 4); TextOut (Hdc,30,0,sco,i);
    Show fractional DeleteDC (MEMDC);  ReleaseDC (HWND,HDC);
    Release handle DeleteObject (HBM);     Zaoziji (HWND);   Build yourself if (timer_zidan==wparam)//Timer zidanmove (Pzidan); Bullet Movement Releasezidan (&pzidan);       Release screen of bullets} else if (Timer_diren==wparam)//Timer 102 {Zaodiren ();  Build enemies} else if (Timer_direnrelease==wparam)//Timer (Releasediren) (&pdiren);       Release the enemy of the screen} showdiren (Pdiren,hwnd);       Show enemy Direnmove (Pdiren);     The enemy moved Showzidan (Pzidan,hwnd); Show Bullet Shoot (HWNd,pziji,&pdiren,&pzidan);
  Whether to hit the break;
    Case WM_CLOSE://Close PostQuitMessage (0);
  Break Case WM_KEYDOWN://Judge key switch (WParam) {case Vk_left://Left-shift if (pziji->x>0) pziji-&
      gt;x-=20;
    Break
      Case Vk_right://Right Shift if (pziji->x<530) pziji->x+=20;
    Break
      Case VK_UP://Move Up if (pziji->y>0) pziji->y-=20;
    Break
      Case Vk_down://Move Down if (pziji->y<520) pziji->y+=20;
    Break
      Case Vk_space://Space Fired bullets Zaozidan ();
    Break
  } break;  
    Case WM_CREATE://Create Srand (Time (NULL));
    pziji= (struct node*) malloc (sizeof (struct Node));     pziji->x=200;     His own x pziji->y=500;.  Own y SetTimer (hwnd,timer_diren,1000,null);
    Set Timer 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 ()//Build bullets {Diren *u; 
  u= (struct node*) malloc (sizeof (struct Node));   U->x=rand ()%550;      The x random appearance of the bullet u->y=-10;
  The Ordinate fixed u->pnext=null; appears
  if (Null==pdiren) {pdiren=u;   else {u->pnext=pdiren;
 
  Placing the newly generated linked list in the head pdiren=u;
  } void Showdiren (struct Node *phead,hwnd HWND)//show enemy {HDC HDC;
  HDC MEMDC;
  Hbitmap hbm;
  BITMAP Bminfo;
  HDC=GETDC (HWND); Hbm=loadbitmap (GetModuleHandle (NULL), Makeintresource (IDB_BITMAP1))/load enemy bitmap GetObject (hbm, sizeof (Bminfo), & 
  Bminfo);
  MEMDC=CREATECOMPATIBLEDC (HDC);
  SelectObject (MEMDC,HBM);
    while (Phead!=null)//Enemy list is not empty, show enemy aircraft {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));Load your own bitmap GetObject (hbm, sizeof (Bminfo), &bminfo);
  MEMDC=CREATECOMPATIBLEDC (HDC);
  SelectObject (MEMDC,HBM); BitBlt (hdc,pziji->x,pziji->y,40,40,memdc,0,0,srccopy);
  Show themselves DeleteDC (MEMDC);
  ReleaseDC (HWND,HDC);
DeleteObject (HBM);
  } void Zaozidan ()//Build bullets {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;
  The bullet is placed in the head of the chain Pzidan=u;
  } void Showzidan (Zidan *phead,hwnd HWND)//Show bullets {HDC HDC;
  HDC MEMDC;
  Hbitmap hbm;
  BITMAP Bminfo;
  HDC=GETDC (HWND); Hbm=loadbitmap (GetModuleHandle (NULL), Makeintresource (IDB_BITMAP2)); 
  Insert Bullet bitmap GetObject (hbm, sizeof (Bminfo), &bminfo);
  MEMDC=CREATECOMPATIBLEDC (HDC);
  SelectObject (MEMDC,HBM); while (phead!=null)//Bullet list is not empty, showing bullets {/*ellipse (hdc,phead->x,phead->y,phead->x+5,phead->y+5); BitBl
  T (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)//enemy moves {while (phead!=null)//list is not empty, the enemy moves {if (score<500) {Phead
      ->y+=10; 
    phead=phead->pnext;
      else {phead->y+=20;
    phead=phead->pnext;
    } void Zidanmove (Diren *phead)//Bullets Move {while (phead!=null)//list does not move for empty bullets {phead->y-=20; 
  phead=phead->pnext;
  } void Shoot (HWND hwnd,feiji *ziji,diren **diren,zidan **zidan)//Judge if {Diren *js1=*diren;
  Zidan *js2=*zidan;
  int n = 1; while (js1!=null)//Judge whether I crashed the machine {//Impact release Timer game End 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", "window",MB_OK);
      PostQuitMessage (0);
    Break  else js1=js1->pnext;  Did not judge the next enemy aircraft} Js1=*diren;  
    Enemy aircraft back to head while ((Js1=*diren)!=null)//judge whether the enemy is empty {Zidan = &pZiDan;
    n = 0; while ((Js2=*zidan)!=null)//judge whether the bullet is empty {//The enemy plane is shot 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)//list below the section is not empty, point to the next release shot of the bullet {*diren = js1->pnext;
          Diren = &pDiRen;
          Free (JS1);
        Free (JS2);  
        else *diren = NULL;
      Break  else {Zidan = &js2->pnext; 
    I didn't see the next.} if (n!= 1)//judge whether it was popped out {Diren = &js1->pnext;
  }} void Releasediren (Diren **phead)//Release the enemy of the flying out screen {Diren *js=*phead; while (js=*phEAD)!=null {if (js->y>600)//Fly out screen release {*phead=js->pnext;
    Free (JS);  else {phead = &js->pnext;
  See next}} void Releasezidan (Zidan **phead)//release bullets {Zidan *js=*phead;  
      while ((Js=*phead)!=null) {if (js->y<0)//Fly out of bullets release {*phead=js->pnext;
    Free (JS);  else phead=&js->pnext;
 Did not fly out to see the next}}

Ways to share a net friend at the same time

MytestView.cpp:Cmytest;//; #include "stdafx.h;
#include "mytest.h;
#include "Mytestdoc;
#include "Mytestview; #ifdef_DEBUG #definenewDEBUG_NEW #endif//cmytestvie//#include "stdafx.h" #inclu De "mytest.h" #include "mytestDoc.h" #include "mytestView.h" #ifdef _DEBUG #define NEW debug_new #endif//Cmytestview IMP Lement_dyncreate (Cmytestview, CView) begin_message_map (Cmytestview, CView) on_wm_create () On_wm_timer () ON_WM_ KEYDOWN () End_message_map ()//Cmytestview construction/destructor Cmytestview::cmytestview () {//TODO: Add construction code here 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::P Recreatewindow (createstruct& cs) {//TODO: Here by modification//Createstru
CT CS To modify the window class or style return CView::P Recreatewindow (CS);
}//Cmytestview draw void Cmytestview::ondraw (cdc* pDC) {cmytestdoc* PDoc = GetDocument ();
Assert_valid (PDOC);
if (!pdoc) return; TODO: Add native data hereAdd the drawing code/*cbitmap bitmap; Bitmap.
LOADBITMAPW (Idb_me);
*///Paint/*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 (&AMP;BITMAP); * *//rect RC;
GetClientRect (&AMP;RC);
CBrush Brush; Brush.
CreateSolidBrush (RGB (3,108,254));
Pdc->selectobject (&brush);
CBrush *oldbrush=pdc->selectobject (&brush);
Pdc->rectangle (&AMP;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 (&AMP;BITMAP);
Refresh RECT RC;
GetClientRect (&AMP;RC); CBrush Brush Brush.
CreateSolidBrush (RGB (3,108,254));
Pdc->selectobject (&brush);
CBrush *oldbrush=pdc->selectobject (&brush);
Pdc->rectangle (&AMP;RC);
Pdc->selectobject (Oldbrush);
Enemy aircraft 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);
Fighter 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);
Bullets 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);
Bombs 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);
Explosion 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 (&AMP;BITMAP2);//Cmytestview diagnostics #ifdef _DEBUG void Cmytestview::assertvalid () const {Cview::asse
Rtvalid ();
} void Cmytestview::D UMP (cdumpcontext& DC) const {CView::D UMP (DC);} The cmytestdoc* cmytestview::getdocument () const//non-debug version is inline with the {ASSERT (m_pdocument->iskindof runtime_class (cmytestdoc )));
Return (cmytestdoc*) m_pdocument; #endif//_debug//Cmytestview Message Handler int cmytestview::oncreate (Lpcreatestruct lpcreatestruct) {if CVIEW::ONCREATE (LP
createstruct) = = 1) return-1;
TODO: Add your dedicated Create code SetTimer (1,30,null) here;
return 0;
} void Cmytestview::ontimer (Uint_ptr nidevent) {//TODO: Add Message Handler code here and/or invoke default value Cview::ontimer (nidevent);
CDC *pdc=getdc ();
Refresh Background RECT RC;
GetClientRect (&AMP;RC);
CBrush Brush; Brush.
CreateSolidBrush (RGB (3,108,254));
Pdc->selectobject (&brush);CBrush *oldbrush=pdc->selectobject (&brush);
Pdc->rectangle (&AMP;RC);
Pdc->selectobject (Oldbrush);
Fighter 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); Bullet

Welcome to play the game direction key:w,a,s,d  Control key: J,k

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.