用win32编的打飞机,不知道哪里错了,大家帮忙看看
大神们帮忙看看我下面这个代码有什么问题,老师让写一个模拟微信里打飞机的游戏的程序 ,用VC 2005里面 win32写的我现在写到飞机能动 空格键发射子弹能发,敌人飞机能下来,就差最后一步:子弹碰到敌人飞机,子弹和敌人同时消失,应该就是最后一个函数的问题,游戏一运行到子弹碰到敌机就崩溃,我是在是想不出来怎么改……麻烦大家帮忙看下,谢谢了!!! 主要是最后一个函数#include<windows.h>
#define FEIJI_TIME 1001
#define ZIDAN_TIME 1002
#define DIREN_TIME 1003
typedef struct Node
{
int x;
int y;
struct Node *pNext;
}PLANE,ZIDAN,DIREN;
PLANE *pHead = NULL;
ZIDAN *pZidan = NULL;
DIREN *pDiren = NULL;
LRESULT CALLBACK pp(HWND a, UINT b, WPARAM c, LPARAM d);
void zaoPLANE(int x,int y);
void showPLANE(int x,int y,HWND hWnd);
void showZIDAN(PLANE *pHead,HWND hWnd);
void moveZIDAN(PLANE *pHead);
void zaoZIDAN();
void moveDIREN(DIREN *pDiren);
void showDIREN(DIREN *pDiren,HWND hWnd);
void zaoDIREN();
void blast(ZIDAN *pZidan,DIREN *pDiren);
int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR pszCmdLine,int nShowCmd)
{
WNDCLASSEX wc;
HWND hWnd;
MSG msg;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)5;
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = pp;
wc.lpszClassName = "hahaha";
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wc);
hWnd = CreateWindow("hahaha","hehe",WS_OVERLAPPEDWINDOW,0,0,1370,745,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nShowCmd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK pp(HWND hWnd, UINT msg, WPARAM wParam, LPARAM d)
{
static int x = 600;
static int y = 600;
HDC hdc;
switch(msg)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_DOWN:
y+=10;
break;
case VK_UP:
y-=10;
break;
case VK_LEFT:
x-=30;
break;
case VK_RIGHT:
x+=30;
break;
case VK_SPACE:
zaoPLANE(x,y);
zaoZIDAN();
}
break;
case WM_CREATE:
SetTimer(hWnd,FEIJI_TIME,3,NULL);
SetTimer(hWnd,ZIDAN_TIME,40,NULL);
SetTimer(hWnd,DIREN_TIME,400,NULL);
break;
case WM_TIMER:
blast(pZidan,pDiren);
hdc = GetDC(hWnd);
Rectangle(hdc,0,0,10000,10000);
ReleaseDC(hWnd,hdc);
switch(wParam)
{
case FEIJI_TIME:
break;
case ZIDAN_TIME:
moveZIDAN(pZidan);
break;
case DIREN_TIME:
zaoDIREN();
moveDIREN(pDiren);
break;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
showPLANE(x,y,hWnd);
showZIDAN(pZidan,hWnd);
showDIREN(pDiren,hWnd);
return DefWindowProc(hWnd,msg,wParam,d);
}
void zaoPLANE(int x,int y)
{
PLANE *temp;
temp = (PLANE*)malloc(sizeof(PLANE));
temp->x = x;
temp->y = y;
temp->pNext = NULL;
if(NULL == pHead)
{
pHead = temp;
}
else
{
temp->pNext = pHead;
pHead = temp;
}
}
void showPLANE(int x,int y,HWND hWnd)
{
HDC hdc;
hdc = GetDC(hWnd);
Rectangle(hdc,x,y,x+30,y+30);
ReleaseDC(hWnd,hdc);
}
//造飞机,显示飞机
void zaoZIDAN()
{
ZIDAN *temp = (ZIDAN*)malloc(sizeof(ZIDAN));
temp->x = pHead->x;
temp->y = pHead->y;
temp->pNext = NULL;
if(NULL == pZidan)
{
pZidan = temp;
}
else
{
temp->pNext = pZidan;
pZidan = temp;
}
}
void showZIDAN(PLANE *pHead,HWND hWnd)
{
HDC hdc;
hdc = GetDC(hWnd);
while(pHead != NULL)
{
Rectangle(hdc,pHead->x+9,pHead->y,pHead->x+10+9,pHead->y+10);
pHead = pHead->pNext;
}
ReleaseDC(hWnd,hdc);
}
void moveZIDAN(PLANE *pHead)
{
while(pHead != NULL)
{
pHead->y-=10;
pHead = pHead->pNext;
}
}
void zaoDIREN()
{
DIREN *temp = (DIREN*)malloc(sizeof(DIREN));
temp->x = rand()%1200;
temp->y = -60;
temp->pNext = NULL;
if(NULL == pDiren)
{
pDiren = temp;
}
else
{
temp->pNext = pDiren;
pDiren = temp;
}
}
void showDIREN(DIREN *pDiren,HWND hWnd)
{
HDC hdc = GetDC(hWnd);
while(pDiren)
{
Ellipse(hdc,pDiren->x,pDiren->y,pDiren->x+30,pDiren->y+30);
pDiren = pDiren->pNext;
}
ReleaseDC(hWnd,hdc);
}
void moveDIREN(DIREN *pDiren)
{
while(pDiren)
{
pDiren->y+=30;
pDiren = pDiren->pNext;
}
}
void blast(ZIDAN *pZidan,DIREN *pDiren)
{
ZIDAN *ji_pZidan = pZidan;
DIREN *ji_pDiren = pDiren;
DIREN *ji_pDiren_2 = pDiren;
ZIDAN *a = NULL;
DIREN *b = NULL;
while(pZidan)
{
while(pDiren)
{
if( (pZidan->x >= pDiren->x && pZidan->x <= pDiren->x+30) && (pZidan->y >= pDiren->y && pZidan->y <= pDiren->y+30))
{
//MessageBox(NULL,"asa","asa",MB_OK);
//break;
a = pZidan;
b = pDiren;
/*ji_pDiren->pNext = pDiren->pNext;
ji_pZidan->pNext = pZidan->pNext;*/
free(a);
free(b);
break;
}
ji_pDiren = pDiren;
pDiren = pDiren->pNext;
}
pDiren = ji_pDiren_2;
ji_pZidan = pZidan;
pZidan = pZidan->pNext;
}
}