C++編寫簡單的打靶游戲
更新時間:2015年03月23日 09:45:35 投稿:hebedich
這篇文章主要介紹了使用C++編寫簡單的打靶游戲,本人也是個菜鳥,水平有限,有錯誤遺漏的地方在所難免,大家看看就好。
首次自己寫程序,很不完善,還有許多問題需要解決。。。見諒見諒
#define GDIPVER 0x0110
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ObjIdl.h>
#include <GdiPlus.h>
#include <windowsx.h>
#include <tchar.h>
#include <mmsystem.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "resource.h"
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib,"GdiPlus.lib")
using namespace Gdiplus;
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
static int cxball,cyball;
VOID OnPaint(HDC hDC,int x,int y)
{
Graphics _g(hDC);
//構(gòu)造畫筆
Pen _p(
Color::Red, //顏色
2.0F); //筆寬(默認(rèn):1.0F)
_g.DrawEllipse(&_p,x, y, 50, 50);
//設(shè)置筆寬與顏色
_p.SetColor(Color(255,111,222,55)); //設(shè)置顏色
_p.SetWidth(3.0F);//設(shè)置筆寬
//獲得筆寬與顏色
Color _c;_p.GetColor(&_c);//獲取顏色
REAL _r = _p.GetWidth();//獲取筆寬
}
LRESULT CALLBACK WinProc(HWND hWnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
static PMSG pmsg;
switch(msg)
{
PAINTSTRUCT ps ;
HDC hDC;
static int cxClient, cyClient ;
static int cxcreat,cycreat;
static int times,score;
TCHAR szText[256];
case WM_CREATE:
{
hDC = GetDC(hWnd);
PlaySound(MAKEINTRESOURCE(IDR_WAVE1), hinstance_app, SND_RESOURCE | SND_ASYNC);
times = 9;
score = 0;
ReleaseDC(hWnd,hDC);
return(0);
} break;
case WM_SIZE:
{
cxClient = LOWORD (lparam) ;
cyClient = HIWORD (lparam) ;
return 0 ;
}
break;
case WM_LBUTTONDOWN :
{
switch (wparam)
{
case MK_LBUTTON:
hDC = GetDC(hWnd);
sprintf(szText,"得分為 %d",score);
TextOut(hDC,900,240,szText,10);
times--;
sprintf(szText,"次數(shù)為 %d",times);
TextOut(hDC,900,280,szText,8);
cxcreat = (int)LOWORD( lparam ) ; //獲取鼠標(biāo)位置x坐標(biāo)信息
cycreat = (int)HIWORD( lparam ) ; //獲取鼠標(biāo)位置y坐標(biāo)信息
SetBkMode(hDC, OPAQUE);
if(cxcreat>cxball-50&&cxcreat<cxball+50)
{
if(cycreat>cyball-50&&cycreat<cyball+50)
{
score+=100;
}
}
if(times <= 0)
{
score = 0;
times = 0;
MessageBox(hWnd,TEXT("次數(shù)超過了"),TEXT("錯誤"),MB_ICONERROR);
}
ReleaseDC(hWnd,hDC);
break;
}
return 0;
}
break;
case WM_PAINT:
{
hDC = BeginPaint(hWnd,&ps);
MoveToEx(hDC,800,0,NULL);
LineTo(hDC,800,600);
MoveToEx(hDC,0,600,NULL);
LineTo(hDC,800,600);
EndPaint(hWnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
PlaySound(NULL, hinstance_app, SND_PURGE);
PostQuitMessage(0);
return(0);
} break;
default:break;
}
return (DefWindowProc(hWnd, msg, wparam, lparam));
}
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
HWND hwnd;
ULONG_PTR GdiplusToken;
GdiplusStartupInput GdiplusStartupInput;
Status sResult = GdiplusStartup(&GdiplusToken, &GdiplusStartupInput, NULL);
if(sResult != Ok)return 0;
WNDCLASSEX WndClassEx =
{
sizeof(WNDCLASSEX),
CS_HREDRAW | CS_VREDRAW,
WinProc,
0L,
0L,
GetModuleHandle(NULL),
LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1)),
LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR1)),
(HBRUSH)GetStockObject(WHITE_BRUSH),
NULL,
_T("SimpleWindowClass"),
LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1))
};
RegisterClassEx(&WndClassEx);
hwnd = CreateWindow(
_T("SimpleWindowClass"),
_T("pan's game ~~"),
WS_OVERLAPPEDWINDOW| WS_VSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
GetDesktopWindow(),
NULL,
WndClassEx.hInstance,
NULL);
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
HDC hdc;
hdc = GetDC(hwnd);
srand(GetTickCount());
cxball = WINDOW_WIDTH/2;
cyball = WINDOW_HEIGHT/2;
RECT rect;
rect.left=0;
rect.bottom=600;
rect.right=800;
rect.top=0;
int xv = -4+rand()%8;
int yv = -4+rand()%8;
MSG Msg;
do
{
GetMessage(&Msg, NULL, 0U, 0U);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
OnPaint(hdc,cxball,cyball);
cxball += xv;
cyball += yv;
if (cxball < 0 || cxball > WINDOW_WIDTH - 50)
{
xv=-xv;
cxball += xv;
}
else if (cyball < 0 || cyball > WINDOW_HEIGHT - 50)
{
yv=-yv;
cyball += yv;
}
OnPaint(hdc,cxball,cyball);
Sleep(10);
InvalidateRect(hwnd,&rect,TRUE);
}while(Msg.message != WM_QUIT);
ReleaseDC(hwnd,hdc);
UnregisterClass(
_T("SimpleWindowClass"),
WndClassEx.hInstance);
GdiplusShutdown(GdiplusToken);
return 0;
}
以上就是本文給大家分享的C++編寫的打靶小游戲的代碼了,希望大家能夠喜歡。
相關(guān)文章
Qt結(jié)合libqrencode生成二維碼的實現(xiàn)示例
本文主要介紹了Qt結(jié)合libqrencode生成二維碼的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)鏈表逆序并輸出
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)鏈表逆序并輸出的相關(guān)資料,需要的朋友可以參考下2017-04-04
解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細(xì)介紹
本篇文章是對wprintf 中使用%I64d格式化輸出LONGLONG進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++ 中約瑟夫環(huán)替換計數(shù)器m(數(shù)組解決)
這篇文章主要介紹了C++ 中約瑟夫環(huán)替換計數(shù)器m(數(shù)組解決)的相關(guān)資料,需要的朋友可以參考下2017-05-05

