《雷霆戰機》是一種飛行類的飛行射擊遊戲。遊戲需要動很多腦筋,任何一個輕率的失誤都可能導致遊戲的失敗。
在繼承了經典飛行射擊遊戲爽快體驗的同時,更添加了革命性的創新玩法,引爆了全世界玩家的打飛機熱情!!!
每天晚上20:00我都會開直播給大家分享C/C++遊戲編程學習知識和路線方法,群裡會不定期更新最新的教程和學習方法,大家都是學習C/C++的,或是轉行,或是大學生,還有工作中想提升自己能力的前端黨,如果你是正在學習C/C++的小夥伴可以加入學習。最後祝所有程序員都能夠走上人生巔峰,讓代碼將夢想照進現實,非常適合新手學習,有不懂的問題可以隨時問我,工作不忙的時候希望可以給大家解惑。
#include
#include
#include
#pragma comment(lib,"winmm.lib")
#define IMG_WIDTH 800/4*3
#define IMG_HEIGHT 2408/4*3
#define WND_WIDTH IMG_WIDTH
#define WND_HEIGHT 800
#define MyPlaneSpeed 10
#define EnemySpeed 5
#define BulletSpeed 1
#define EnemyCreate 1000
#define BulletCreate 500
typedef struct _node {
int x;
int y;
struct _node *next;
}node;
HBITMAP bmp, backbmp,myplanebmp,enemybmp,bulletbmp;
HDC hdc, mdc, bdc;
int backoffset = 0;
node *MyPlane, *Enemy, *Bullet;
DWORD dwBulletCreatePre, dwBulletCreateNow;
DWORD dwEnemyCreatePre, dwEnemyCreateNow;
DWORD dwMyPlaneMovePre, dwMyPlaneMoveNow;
DWORD dwEnemyMovePre, dwEnemyMoveNow;
DWORD dwBgPre, dwBgNow;
DWORD dwBulletMovePre, dwBulletMoveNow;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL InitGame(HWND hWnd);
void GamePaint(HWND hWnd);
void CreateNode(node *p, int n);
void Move(node *p, int n);
void KillEnemy(HWND hWnd);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPreInstance,
LPSTR lpCmdLine,
int nCmdShow
) {
TCHAR szAppClassName[] = TEXT("WindowProcedure");
WNDCLASS wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.lpszClassName = szAppClassName;
wndClass.lpszMenuName = NULL;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = NULL;
wndClass.hIcon = NULL;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WindowProc;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
RegisterClass(&wndClass);
HWND hWnd = CreateWindow(
szAppClassName,
TEXT("自制雷霆戰機"),
WS_OVERLAPPEDWINDOW,
400,
10,
WND_WIDTH,
WND_HEIGHT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
if (! InitGame(hWnd)) {
return -1;
}
MSG msg = ;
while (msg.message!=WM_QUIT) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else GamePaint(hWnd);
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
) {
switch (uMsg) {
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
switch (wParam) {
case VK_UP:
MyPlane->y -= 10;
if (MyPlane->y
MyPlane->y = 0;
break;
case VK_DOWN:
MyPlane->y += 10;
if (MyPlane->y > WND_HEIGHT-100)
MyPlane->y = WND_HEIGHT-100;
break;
case VK_RIGHT:
MyPlane->x += 10;
if (MyPlane->x > WND_WIDTH-100)
MyPlane->x = WND_WIDTH-100;
break;
case VK_LEFT:
MyPlane->x -= 10;
if (MyPlane->x
MyPlane->x = 0;
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
BOOL InitGame(HWND hWnd) {
mciSendString(L"open ./res/李榮浩-老街.mp3 alias music", 0, 0, NULL);
mciSendString(L"play music repeat", 0, 0, 0);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc, WND_WIDTH, WND_HEIGHT);
backbmp = (HBITMAP)LoadImage(NULL, L"./res/BG2.bmp", IMAGE_BITMAP, IMG_WIDTH, IMG_HEIGHT, LR_LOADFROMFILE);
myplanebmp = (HBITMAP)LoadImage(NULL, L"./res/ZIJI600X300.bmp", IMAGE_BITMAP, 200, 100, LR_LOADFROMFILE);
enemybmp = (HBITMAP)LoadImage(NULL, L"./res/DIJI_230X80.bmp", IMAGE_BITMAP, 100, 34, LR_LOADFROMFILE);
bulletbmp = (HBITMAP)LoadImage(NULL, L"./res/ZIDAN_120X25 .bmp", IMAGE_BITMAP, 120, 25, LR_LOADFROMFILE);
MyPlane = (node*)malloc(sizeof(node));
Enemy = (node*)malloc(sizeof(node));
Bullet = (node*)malloc(sizeof(node));
Bullet->next = NULL;
MyPlane->next = NULL;
Enemy->next = NULL;
MyPlane->x = WND_WIDTH/2-50;
MyPlane->y = WND_HEIGHT-120;
dwBulletCreatePre = GetTickCount();
dwEnemyCreatePre = GetTickCount();
dwMyPlaneMovePre = GetTickCount();
dwBgPre = GetTickCount();
dwEnemyMovePre = GetTickCount();
dwBulletMovePre = GetTickCount();
return TRUE;
}
void GamePaint(HWND hWnd) {
SelectObject(mdc, bmp);
//背景
SelectObject(bdc, backbmp);
dwBgNow = GetTickCount();
if (dwBgNow-dwBgPre>=5) {
BitBlt(mdc, 0, 0, WND_WIDTH,backoffset, bdc, 0, IMG_HEIGHT-backoffset, SRCCOPY);
BitBlt(mdc, 0, backoffset, WND_WIDTH, IMG_HEIGHT, bdc, 0, 0, SRCCOPY);
dwBgPre = dwBgNow;
if (++backoffset == IMG_HEIGHT) {
backoffset = 0;
}
}
//我的飛機
SelectObject(bdc, myplanebmp);
dwMyPlaneMoveNow = GetTickCount();
if (dwMyPlaneMoveNow - dwMyPlaneMovePre >= MyPlaneSpeed) {
BitBlt(mdc, MyPlane->x, MyPlane->y, 100, 100 , bdc, 0, 0, SRCAND); //SRCAND 顏色越淺,去色的程度越高 留深
BitBlt(mdc, MyPlane->x, MyPlane->y, 100, 100 , bdc, 100, 0, SRCPAINT); //SRCPAINT 顏色越深,去色的程度越高 留淺
dwMyPlaneMovePre = dwMyPlaneMoveNow;
}
//敵機
SelectObject(bdc, enemybmp);
dwEnemyCreateNow = GetTickCount();
if (dwEnemyCreateNow - dwEnemyCreatePre >= EnemyCreate) {
CreateNode(Enemy, 2);
dwEnemyCreatePre = dwEnemyCreateNow;
}
dwEnemyMoveNow = GetTickCount();
if (dwEnemyMoveNow - dwEnemyMovePre >= EnemySpeed) {
node *petemp = Enemy->next;
while (petemp != NULL) {
BitBlt(mdc, petemp->x, petemp->y, 50, 34, bdc, 0, 0, SRCAND);
BitBlt(mdc, petemp->x, petemp->y, 50, 34, bdc, 50, 0, SRCPAINT);
Move(petemp, 2);
petemp = petemp->next;
}
dwEnemyMovePre= dwEnemyMoveNow ;
}
//子彈
SelectObject(bdc, bulletbmp);
dwBulletCreateNow = GetTickCount();
if (dwBulletCreateNow - dwBulletCreatePre >= BulletCreate) {
CreateNode(Bullet, 1);
dwBulletCreatePre = dwBulletCreateNow;
}
dwBulletMoveNow = GetTickCount();
if (dwBulletMoveNow - dwBulletMovePre >= BulletSpeed) {
node *pbtemp = Bullet->next;
while (pbtemp != NULL) {
BitBlt(mdc, pbtemp->x, pbtemp->y, 60, 25, bdc, 60, 0, SRCAND);
BitBlt(mdc, pbtemp->x, pbtemp->y, 60, 25, bdc, 0, 0, SRCPAINT);
Move(pbtemp, 1);
pbtemp = pbtemp->next;
}
dwBulletMovePre = dwBulletMoveNow;
}
//繪製在窗口上
BitBlt(hdc, 0, 0, WND_WIDTH, WND_HEIGHT, mdc, 0, 0, SRCCOPY);
KillEnemy(hWnd);
}
void CreateNode(node *p,int n) {
node *pnew = (node*)malloc(sizeof(node));
pnew->next = NULL;
if (n == 1) {
pnew->x = MyPlane->x + 20;
pnew->y = MyPlane->y - 10;
}
else if (n == 2) {
pnew->x = rand() % (WND_WIDTH - 50);
pnew->y = 0;
}
pnew->next = p->next;
p->next = pnew;
}
void Move(node *p,int n) {
if (n == 1) {
p->y -= 10;
}
else if (n == 2) {
p->y += 5;
}
}
void KillEnemy(HWND hWnd) {
node *pbtemp = Bullet->next;
node *petemp = Enemy->next;
while (petemp !=NULL)
{
pbtemp = Bullet->next;
while (pbtemp !=NULL) {
if (petemp->x >= pbtemp->x - 50 && petemp->x x + 50 && petemp->y y + 50 && petemp->y >= pbtemp->y - 50) {
petemp->x = WND_WIDTH;
pbtemp->x = WND_WIDTH;
}
pbtemp = pbtemp->next;
}
if (petemp->x > MyPlane->x - 40 && petemp->x x + 90 && petemp->y > MyPlane->y - 20 && petemp->y y + 30) {
MessageBox(hWnd, L"老司機翻車了,遊戲結束!!", L"消息", MB_OK);
PostQuitMessage(0);
break;
}
petemp = petemp->next;
}
}
閱讀更多 往事C語言 的文章