C++编写简单的打靶游戏
来源:本站原创|时间:2020-01-10|栏目: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); //构造画笔 Pen _p( Color::Red, //颜色 2.0F); //笔宽(默认:1.0F) _g.DrawEllipse(&_p,x, y, 50, 50); //设置笔宽与颜色 _p.SetColor(Color(255,111,222,55)); //设置颜色 _p.SetWidth(3.0F);//设置笔宽 //获得笔宽与颜色 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,"次数为 %d",times); TextOut(hDC,900,280,szText,8); cxcreat = (int)LOWORD( lparam ) ; //获取鼠标位置x坐标信息 cycreat = (int)HIWORD( lparam ) ; //获取鼠标位置y坐标信息 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("次数超过了"),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++编写的打靶小游戏的代码了,希望大家能够喜欢。
您可能感兴趣的文章
- 04-02c语言编写函数冒泡排序 c语言冒泡排序法函数
- 04-02c语言没有round函数 round c语言
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10c++中inline的用法分析
- 01-10用C++实现DBSCAN聚类算法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10C++大数模板(推荐)
- 01-10浅谈C/C++中的static与extern关键字的使用详解
- 01-10深入C/C++浮点数在内存中的存储方式详解
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10SublimeText编译C开发环境设置
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 01-11ajax实现页面的局部加载
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10C#中split用法实例总结
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05DEDE织梦data目录下的sessions文件夹有什