飞机⼤战c++语⾔源代码,C++编写简易的飞机⼤战初学C/C++的⼩伙伴可以⽤做这个⼩游戏来熟悉⼀下编程的乐趣。
#include
#include"resource.h"
#include
#include
#include
#define TIMER_DIREN 101 //定义定时器
#define TIMER_DIRENMOVE 102
#define TIMER_ZIDAN 103
#define TIMER_DIRENRELEASE 104
typedef struct Node //敌⼈,⾃⼰,⼦弹结构体
{
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);//回调函数
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; //分数
static char sco[20]; //装分数的字符窜
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); //将分数装⼊字符窜
jscore=score;
while((jscore=jscore/10)>0) //判断分数有⼏位
i++;
TextOut(hdc,0,0,"分数",4);
TextOut(hdc,30,0,sco,i); //显⽰分数
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: //空格发射⼦弹
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,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随机出现
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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论