C++ writing simple aircraft wars
- 2020-04-02 03:15:47
- OfStack
Beginners of C/C++ can use this small game to get familiar with the fun of programming.
#include<windows.h>
#include"resource.h"
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#define TIMER_DIREN 101 //Define timer
#define TIMER_DIRENMOVE 102
#define TIMER_ZIDAN 103
#define TIMER_DIRENRELEASE 104
typedef struct Node //Enemy, self, bullet structure
{
int x;
int y;
struct Node *pnext;
}DiRen,FeiJi,ZiDan;
void ZaoDiRen(); //To make enemies
void ShowDiRen(DiRen *pHead,HWND hWnd); //According to the enemy
void ZaoZiDan(); //Build a bullet
void ShowZiDan(ZiDan *pHead,HWND hWnd); //According to the bullets
void DiRenMove(DiRen *pHead); //The enemy move
void ZiDanMove(DiRen *pHead); //The bullet mobile
void shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZiDan **zidan);//Judge whether to hit or not
void ReleaseDiren(DiRen **pHead); //Freed enemies
void ReleaseZidan(ZiDan **pHead); //The discharged bullet
void ZaoZiJi(HWND hWnd); //Make yourself
LRESULT CALLBACK pp(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);//The callback function
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; //The enemy
ZiDan *pZiDan=NULL; //The bullet
FeiJi *pZiJi=NULL; //oneself
static int score=0; //score
static char sco[20]; // loading score The characters of running
LRESULT CALLBACK pp(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
int i=1, //position
jscore;
HDC hdc;
HDC memdc;
HBITMAP hbm;
BITMAP bminfo;
switch(msg)
{
case WM_TIMER: //The timer
hdc=GetDC(hWnd); //Gets the device handle
hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP4));// In the background position figure
GetObject(hbm, sizeof(bminfo), &bminfo);
memdc=CreateCompatibleDC(hdc);
SelectObject(memdc,hbm);
BitBlt(hdc,0,0,600,600,memdc,0,0,SRCCOPY);
sprintf(sco,"%d",score); // will score Load character channeling
jscore=score;
while((jscore=jscore/10)>0) // judge score There are a few position
i++;
TextOut(hdc,0,0,"score",4);
TextOut(hdc,30,0,sco,i); // According to score
DeleteDC(memdc);
ReleaseDC(hWnd,hdc); //Release the handle
DeleteObject(hbm);
ZaoZiJi(hWnd); //Make yourself
if(TIMER_ZIDAN==wParam) //The timer101
{
ZiDanMove(pZiDan); //The bullet mobile
ReleaseZidan(&pZiDan); // Releasing the screen The bullet
}
else if( TIMER_DIREN==wParam) //The timer102
{
ZaoDiRen(); //To make enemies
}
else if(TIMER_DIRENRELEASE==wParam) //The timer103
{
ReleaseDiren(&pDiRen); // Releasing the screen The enemy
}
ShowDiRen(pDiRen,hWnd); //According to the enemy
DiRenMove(pDiRen); //The enemy move
ShowZiDan(pZiDan,hWnd); //According to the bullets
shoot(hWnd,pZiJi,&pDiRen,&pZiDan); //Whether the shot
break;
case WM_CLOSE: //Shut down
PostQuitMessage(0);
break;
case WM_KEYDOWN: //To determine key
switch(wParam)
{
case VK_LEFT: //Shift to the left
if(pZiJi->x>0)
pZiJi->x-=20;
break;
case VK_RIGHT: //Moves to the right
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: // A space launch The bullet
ZaoZiDan();
break;
}
break;
case WM_CREATE: //create
srand(time(NULL));
pZiJi=(struct Node*)malloc(sizeof(struct Node));
pZiJi->x=200; //oneself the x
pZiJi->y=500; //oneself the y
SetTimer(hWnd,TIMER_DIREN,1000,NULL); // Set up the The 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 a bullet
{
DiRen *u;
u=(struct Node*)malloc(sizeof(struct Node));
u->x=rand()%550; //The bullet the x random
u->y=-10; //The y-coordinate is fixed
u->pnext=NULL;
if(NULL==pDiRen)
{
pDiRen=u;
}
else
{
u->pnext=pDiRen; //Place the newly generated list in the header
pDiRen=u;
}
}
void ShowDiRen(struct Node *pHead,HWND hWnd) //According to the enemy
{
HDC hdc;
HDC memdc;
HBITMAP hbm;
BITMAP bminfo;
hdc=GetDC(hWnd);
hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));// load The enemyposition figure
GetObject(hbm, sizeof(bminfo), &bminfo);
memdc=CreateCompatibleDC(hdc);
SelectObject(memdc,hbm);
while(pHead!=NULL) //The enemy The linked list is not empty, showing 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 oneself the position figure
GetObject(hbm, sizeof(bminfo), &bminfo);
memdc=CreateCompatibleDC(hdc);
SelectObject(memdc,hbm);
BitBlt(hdc,pZiJi->x,pZiJi->y,40,40,memdc,0,0,SRCCOPY); // According to oneself
DeleteDC(memdc);
ReleaseDC(hWnd,hdc);
DeleteObject(hbm);
}
void ZaoZiDan() //Build a bullet
{
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; // will The bullet Put it at the head of the chain
pZiDan=u;
}
}
void ShowZiDan(ZiDan *pHead,HWND hWnd) //According to the bullets
{
HDC hdc;
HDC memdc;
HBITMAP hbm;
BITMAP bminfo;
hdc=GetDC(hWnd);
hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP2)); // insert The bulletposition figure
GetObject(hbm, sizeof(bminfo), &bminfo);
memdc=CreateCompatibleDC(hdc);
SelectObject(memdc,hbm);
while(pHead!=NULL) //The bullet The list is not empty, According to the bullets
{
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) //The enemy move
{
while(pHead!=NULL) // The list is not empty, The enemy move
{
if(score<500)
{
pHead->y+=10;
pHead=pHead->pnext;
}
else
{
pHead->y+=20;
pHead=pHead->pnext;
}
}
}
void ZiDanMove(DiRen *pHead) //The bullet mobile
{
while(pHead!=NULL) // The list is not empty The bullet mobile
{
pHead->y-=20;
pHead=pHead->pnext;
}
}
void shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZiDan **zidan) //Judge whether or not
{
DiRen *js1=*diren;
ZiDan *js2=*zidan;
int n = 1;
while(js1!=NULL) // judge oneself Whether the crashing
{
// The impact to release The timer Game over
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 plane
}
js1=*diren; //The enemy plane turned back
while((js1=*diren)!=NULL) // judge The enemy Whether is empty
{
zidan = &pZiDan;
n = 0;
while((js2=*zidan)!=NULL) // judge The bullet Whether is empty
{
//The enemy plane was 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) // The bottom section of the list is not empty and points to the next plane to release a shot 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 one
}
}
if(n != 1) //To see if it came out
{
diren = &js1->pnext;
}
}
}
void ReleaseDiren(DiRen **pHead) // Release the ones that fly out of the screen The enemy
{
DiRen *js=*pHead;
while((js=*pHead)!=NULL)
{
if(js->y>600) //Fly out of the screen release
{
*pHead=js->pnext;
free(js);
}
else
{
pHead = &js->pnext; //Look at the next
}
}
}
void ReleaseZidan(ZiDan **pHead) // The release of The bullet
{
ZiDan *js=*pHead;
while((js=*pHead)!=NULL)
{
if(js->y<0) // Fly out of The bullet The release of
{
*pHead=js->pnext;
free(js);
}
else
pHead=&js->pnext; // Don't fly out Look at the next
}
}
Also share a netizen's method
//mytestView.cpp:Cmytest ;
// ;
#include"stdafx.h;
#include"mytest.h;
#include"mytestDoc;
#include"mytestView;
#ifdef_DEBUG ;
#definenewDEBUG_NEW ;
#endif ; //CmytestVie
//Mytestview.cpp: implementation of the CmytestView class
//
#include "stdafx.h"
#include "mytest.h"
#include "mytestDoc.h"
#include "mytestView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CmytestView
IMPLEMENT_DYNCREATE(CmytestView, CView)
BEGIN_MESSAGE_MAP(CmytestView, CView)
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
//CmytestView construct/destructor
CmytestView::CmytestView()
{
//TODO: add the build 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::PreCreateWindow(CREATESTRUCT& cs)
{
//TODO: pass the modification here
//CREATESTRUCT cs to modify the window class or style
return CView::PreCreateWindow(cs);
}
//CmytestView draw
void CmytestView::OnDraw(CDC* pDC)
{
CmytestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
//TODO: add the drawing code for the native data here
//drawing
//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);
//The refresh
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);
//Enemy planes flying in and out
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);
//aircraft
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);
//The bullet
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);
//The bomb
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);
//The 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);
}
//CmytestView diagnosis
#ifdef _DEBUG
void CmytestView::AssertValid() const
{
CView::AssertValid();
}
void CmytestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CmytestDoc* CmytestView::GetDocument() const //The non-debug version is inline {
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CmytestDoc))); return (CmytestDoc*)m_pDocument;
}
#endif //_DEBUG
//CmytestView message handler
int CmytestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//TODO: add your own creation code here
SetTimer(1,30,NULL);
return 0;
}
void CmytestView::OnTimer(UINT_PTR nIDEvent)
{
//TODO: add the message handler code and/or invoke the default values here
CView::OnTimer(nIDEvent);
CDC *pDC=GetDC();
//The refresh background
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);
//aircraft
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);
//The bullet
Welcome to play the game key: w,a,s,d Control key: J,K