gpt4 book ai didi

C++编写简单的打靶游戏

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C++编写简单的打靶游戏由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

首次自己写程序,很不完善,还有许多问题需要解决。。。见谅见谅 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#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++编写的打靶小游戏的代码了,希望大家能够喜欢.

最后此篇关于C++编写简单的打靶游戏的文章就讲到这里了,如果你想了解更多关于C++编写简单的打靶游戏的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com