// plane.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "math.h"
#define MAX_LOADSTRING 100
#define WNDWIDTH 400
#define WNDHEIGHT 600
#define MAXBULLETS 70
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HWND hMainWnd;
typedef struct unit{
int x;
int y;
HBITMAP hbmp;
int heal;
}*punit;
bool g_bActive = false;
HDC hdc;
HDC hMemDC, hMemDC2;
HBITMAP hBmp, hbmpPlane, hbmpEnemy, hbmpBullet;
HBRUSH hbr= CreateSolidBrush(RGB(255,0,0));
HANDLE TimerHandle, RenderHandle;
HICON hIcon;
unit plane, enemy, bullet[MAXBULLETS];
int newbullet=0;
RECT rc={20,8,20+enemy.heal*(300/30), 16}, rc2={19,7,21+300,17};
bool bUp=false, bDown=false, bLeft=false, bRight=false;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
BOOL InitDC();
void Render();
void OnKeyDown(UINT nKey, UINT nFlags);
void OnKeyUp(UINT nKey, UINT nFlags);
DWORD WINAPI TimerProc(LPVOID lpParameter);
DWORD WINAPI RenderProc(LPVOID lpParameter);
void Reset();
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PLANE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
if(!InitDC())
return FALSE;
hIcon=LoadIcon(hInst, (LPCTSTR)IDI_ENEMY);
DWORD threadID;
TimerHandle=CreateThread(0, 0, (LPTHREAD_START_ROUTINE)TimerProc, NULL, 0, &threadID);
RenderHandle=CreateThread(0, 0, (LPTHREAD_START_ROUTINE)RenderProc, NULL, 0, &threadID);
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PLANE);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_PLANE);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;//(LPCSTR)IDC_PLANE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
CW_USEDEFAULT, 0, WNDWIDTH, WNDHEIGHT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
hMainWnd=hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_ACTIVATE: // 窗口的活动状态
{
switch((LOWORD(wParam)))
{
case WA_ACTIVE:
case WA_CLICKACTIVE:
g_bActive = true;
break;
case WA_INACTIVE:
g_bActive = false;
break;
default:
break;
}
}
break;
case WM_CREATE:
g_bActive=true;
break;
// case WM_PAINT:
// Render();
// break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
OnKeyDown(wParam, lParam);
Render();
break;
case WM_KEYUP:
OnKeyUp(wParam, lParam);
Render();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
BOOL InitDC()
{
hdc=GetDC(hMainWnd);
if(hdc==NULL)
return FALSE;
hMemDC=::CreateCompatibleDC(NULL);
if(hMemDC==NULL)
return FALSE;
hBmp=::CreateCompatibleBitmap(hdc, WNDWIDTH, WNDHEIGHT);
if(hBmp==NULL)
return FALSE;
SelectObject(hMemDC, hBmp);
hbmpBullet=LoadBitmap(hInst, (LPCTSTR)IDB_BITMAP_BULLET);
enemy.hbmp=LoadBitmap(hInst, (LPCTSTR)IDB_BITMAP_ENEMY);
plane.hbmp=LoadBitmap(hInst, (LPCTSTR)IDB_BITMAP_PLANE);
hMemDC2=::CreateCompatibleDC(hdc);
plane.x=WNDWIDTH/2;
plane.y=WNDHEIGHT-200;
plane.heal=3;
enemy.x=0;
enemy.y=200;
enemy.heal=30;
for(int i=0; i<MAXBULLETS; i++)
{
bullet[i].heal=1;
bullet[i].x=-1;
bullet[i].y=-1;
bullet[i].hbmp=hbmpBullet;
}
return TRUE;
}
void Render()
{
::Rectangle(hMemDC, -1,-1, WNDWIDTH, WNDHEIGHT);
DrawIconEx(hMemDC, 4, 6, hIcon, 12, 12, 0, NULL, DI_NORMAL);
rc.right=20+enemy.heal*(300/30);
FrameRect(hMemDC, &rc2, hbr);
FillRect(hMemDC, &rc, hbr);
SetTextColor(hMemDC, RGB(255,0,0));
if(plane.heal>0)
{
SelectObject(hMemDC2, plane.hbmp);
BitBlt(hMemDC, plane.x, plane.y, 32, 32, hMemDC2, 0, 0, SRCCOPY);
}
else
TextOut(hMemDC, 110, 20, "You loss! Retry-F1", 18);
if(enemy.heal>0)
{
SelectObject(hMemDC2, enemy.hbmp);
BitBlt(hMemDC, enemy.x, enemy.y, 32, 32, hMemDC2, 0, 0, SRCCOPY);
}
else
TextOut(hMemDC, 110, 20, "You win! Retry-F1", 17);
for(int i=0; i<MAXBULLETS; i++)
{
if(bullet[i].heal>0)
{
if(bullet[i].x!=-1 && bullet[i].y!=-1)
{
SelectObject(hMemDC2, hbmpBullet);
BitBlt(hMemDC, bullet[i].x, bullet[i].y, 8, 7, hMemDC2, 0, 0, SRCCOPY);
}
}
else
{
bullet[i].x=-1;
bullet[i].y=-1;
bullet[i].heal=1;
}
}
BitBlt(hdc, 0, 0, WNDWIDTH, WNDHEIGHT,
WIN32GDI双缓冲打螃蟹小游戏
5星 · 超过95%的资源 需积分: 12 130 浏览量
2008-09-13
22:09:07
上传
评论 2
收藏 17KB RAR 举报
wxjman1987
- 粉丝: 0
- 资源: 1
最新资源
- 基于Python的在线学习与推荐系统设计与实现(论文+源码)-kaic
- 蓝桥杯2024年第十五届省赛真题-前缀总分
- com.qihoo.appstore_300101305-1.apk
- tensorflow-gpu-2.7.1-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-2.7.2-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-2.7.1-cp39-cp39-manylinux2010-x86-64.whl
- 蓝桥杯2024年第十五届省赛真题-传送阵
- com.qihoo.appstore_300101305.apk
- linux之线程同步一.doc
- keil5配色方案10种
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论1