Игра Ping-Pong, не отображается название окна
07.07.2016, 12:50. Показов 4310. Ответов 2
Доброго времени суток, однофорумчане! Нужна ваша помощь... Создал игруху пинг-понг, но не отображается название окна, из-за чего едет вся картинка... не могу понять в чем дело)
main:
| 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
| #include "window.h"
#include "pong.h"
Window* myWin = new Window();
Pong* pong = new Pong();
Gamer* gamer1 = new Gamer();
Gamer* gamer2 = new Gamer();
LRESULT CALLBACK Proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
{
pong->setup(gamer1, gamer2);
SetFocus(hwnd);
}break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, pong->getRed());
MoveToEx(hdc, 350, 0, NULL);
LineTo(hdc, 350, 500);
SelectObject(hdc, pong->getBlack());
SelectObject(hdc, pong->getWhite());
Ellipse(hdc, pong->getBolPosX() - 9, pong->getBolPosY() - 9,
pong->getBolPosX() + 9, pong->getBolPosY() + 9);
SelectObject(hdc, pong->getHBlack());
SelectObject(hdc, pong->getBlack());
Rectangle(hdc, gamer1->getposX() - 5, gamer1->getposY() - 30,
gamer1->getposX() + 5, gamer1->getposY() + 30);
SelectObject(hdc, pong->getHBlack());
SelectObject(hdc, pong->getBlack());
Rectangle(hdc, gamer2->getposX() - 5, gamer2->getposY() - 30,
gamer2->getposX() + 5, gamer2->getposY() + 30);
SetBkMode(hdc, TRANSPARENT);
SelectObject(hdc, pong->getScoreFont());
TextOut(hdc, 680, 10, gamer1->getScoreString().c_str(), gamer1->getScoreString().length());
TextOut(hdc, 10, 10, gamer2->getScoreString().c_str(), gamer2->getScoreString().length());
EndPaint(hwnd, &ps);
}break;
case WM_CLOSE:
{
delete gamer1;
delete gamer2;
delete pong;
DestroyWindow(hwnd);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
}break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
myWin->setHinst(hInstance);
if (!myWin->setWCE())
return 1;
RECT area = { 0, 0, 700, 500 };
::AdjustWindowRect(&area, WS_OVERLAPPEDWINDOW, FALSE);
myWin->setWidth(area.right - area.left);
myWin->setHeight(area.bottom - area.top);
if (!myWin->createWindow())
return 1;
myWin->show(nCmdShow);
myWin->messages(myWin->getHwnd(),
gamer1, gamer2, pong);
return 0;
} |
|
windows.h:
| 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
| #pragma once
#include <Windows.h>
#include "pong.h"
LRESULT CALLBACK Proc(HWND, UINT, WPARAM, LPARAM);
class Window {
private:
HINSTANCE hInst;
MSG msg;
LPWSTR windowName = L"Pong"; //вот название окна
LPWSTR className = L"pingpong";
WNDCLASSEXW wce;
HWND hwnd;
int width, height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
void setHinst(HINSTANCE hInst) {
this->hInst = hInst;
}
void setWindowName(LPWSTR lpWindowName) {
this->windowName = lpWindowName;
}
void setClassName(LPWSTR lpClassName) {
this->className = lpClassName;
}
void setHwnd(HWND hwnd) {
this->hwnd = hwnd;
}
int getWidth() {
return this->width;
}
int getHeight() {
return this->height;
}
HWND getHwnd() {
return this->hwnd;
}
LPWSTR getWindowName() {
return this->windowName;
}
LPWSTR getClassName() {
return this->className;
}
HINSTANCE getHinst() {
return this->hInst;
}
bool setWCE() {
this->wce.cbSize = sizeof(WNDCLASSEX);
wce.style = CS_HREDRAW | CS_VREDRAW;
this->wce.hInstance = this->getHinst();
this->wce.lpfnWndProc = Proc;
this->wce.cbClsExtra = 0;
this->wce.lpszClassName = this->getClassName();
this->wce.hbrBackground = (HBRUSH)(BLACK_BRUSH);
this->wce.hIcon = LoadIconW(this->getHinst(), MAKEINTRESOURCEW(IDI_APPLICATION));
this->wce.hCursor = LoadCursor(NULL, IDC_ARROW);
this->wce.cbWndExtra = 0;
this->wce.hIconSm = LoadIconW(wce.hInstance, MAKEINTRESOURCEW(IDI_APPLICATION));
this->wce.lpszMenuName = NULL;
if (!RegisterClassExW(&this->wce))
return false;
else
return true;
}
bool createWindow() { //WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU
this->setHwnd(CreateWindowExW(WS_EX_COMPOSITED, this->getClassName(), this->getWindowName(),
WS_POPUP | WS_VISIBLE,
(this->getWidth() / 2), (this->getHeight() / 2), this->getWidth(), this->getHeight(), NULL, NULL, this->getHinst(), NULL));
if (!this->getHinst())
return false;
else
return true;
}
void show(int iCmdShow) {
ShowWindow(this->getHwnd(), iCmdShow);
UpdateWindow(this->getHwnd());
}
int messages(HWND hwnd, Gamer* gamer1, Gamer* gamer2, Pong* pong) {
while (true) {
srand(GetTickCount());
while (PeekMessage(&this->msg, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&this->msg);
DispatchMessage(&this->msg);
}
if (this->msg.message == WM_QUIT)
break;
pong->setTime(GetTickCount());
while (GetTickCount() - pong->getTime() < 5);
pong->input(gamer1, gamer2);
pong->moverBol(gamer1, gamer2);
InvalidateRect(hwnd, NULL, true);
}
return (int)this->msg.wParam;
}
}; |
|
pong.h:
| 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
| #pragma once
#include <Windows.h>
#include <ctime>
#include <string>
using namespace std;
class Gamer {
private:
int posX, posY, score;
RECT rect;
public:
Gamer() {
this->posX = 0;
this->posY = 0;
this->rect.left = 0;
this->rect.right = 0;
this->rect.top = 0;
this->rect.bottom = 0;
this->score = 0;
}
void setRect(int x, int y, int w, int h) {
rect.left = x;
rect.right = y;
rect.top = w;
rect.bottom = h;
}
void setposX(int posX) {
this->posX = posX;
}
void setposY(int posY) {
this->posY = posY;
}
void setScore(int score) {
this->score = score;
}
int getScore() {
return this->score;
}
string getScoreString() {
return std::to_string(this->score);
}
int getposX() {
return this->posX;
}
int getposY() {
return this->posY;
}
};
class Pong {
private:
HPEN red, green, blue, black;
HBRUSH white, hBlue, hGreen, hBlack;
int gamer1PosX, gamer1PosY, gamer2PosX, gamer2PosY;
int bolPosX, bolPosY, directX, directY;
HFONT scoreFont;
HBITMAP metal;
HBRUSH hMetal;
DWORD timeStart;
public:
Pong() {
this->setBolPosX(350);
this->setBolPosY(250);
}
HFONT getScoreFont() {
return this->scoreFont;
}
HBRUSH getWhite() {
return this->white;
}
HBRUSH getBlack() {
return this->hBlack;
}
HBRUSH getHBlue() {
return this->hBlue;
}
HBRUSH getHGreen() {
return this->hGreen;
}
HPEN getRed() {
return this->red;
}
HPEN getBlue() {
return this->blue;
}
HPEN getGreen() {
return this->green;
}
HPEN getHBlack() {
return this->black;
}
int getGamer1PosX() {
return this->gamer1PosX;
}
int getGamer1PosY() {
return this->gamer1PosY;
}
int getGamer2PosX() {
return this->gamer2PosX;
}
int getGamer2PosY() {
return this->gamer2PosY;
}
int getBolPosX() {
return this->bolPosX;
}
int getBolPosY() {
return this->bolPosY;
}
int getDirectX() {
return this->directX;
}
int getDirectY() {
return this->directY;
}
DWORD getTime() {
return this->timeStart;
}
void setTime(DWORD time) {
this->timeStart = time;
}
void setBolPosX(int bolPosX) {
this->bolPosX = bolPosX;
}
void setBolPosY(int bolPosY) {
this->bolPosY = bolPosY;
}
void setDirectX(int x) {
this->directX = x;
}
void setDirectY(int y) {
this->directY = y;
}
void setup(Gamer* gamer1, Gamer* gamer2) {
this->red = CreatePen(PS_DOT, 1, RGB(255, 0, 0));
this->green = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
this->blue = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
this->black = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
this->white = CreateSolidBrush(RGB(255, 255, 255));
this->hBlack = CreateSolidBrush(RGB(0, 0, 0));
this->hBlue = CreateSolidBrush(RGB(0, 0, 255));
this->hGreen = CreateSolidBrush(RGB(0, 255, 0));
this->scoreFont = CreateFont(20, 0, 0, 0, FW_BOLD,
FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
gamer1->setposX(30);
gamer1->setposY(225 - 20);
gamer2->setposX(700 - 30);
gamer2->setposY(225 - 20);
gamer1->setRect(175 - 30, 175 + 50, 20, 40);
gamer2->setRect(175 * 3 - 50, 175 * 3 + 50, 20, 40);
}
void input(Gamer* gamer1, Gamer* gamer2) {
/* first gamer */
if (GetAsyncKeyState(0x41) && gamer1->getposY() > 0)
gamer1->setposY(gamer1->getposY() - 10);
if (GetAsyncKeyState(0x5A) && gamer1->getposY() < 460)
gamer1->setposY(gamer1->getposY() + 10);
/* second gamer */
if (GetAsyncKeyState(VK_UP) && gamer2->getposY() > 0)
gamer2->setposY(gamer2->getposY() - 10);
if (GetAsyncKeyState(VK_DOWN) && gamer2->getposY() < 460)
gamer2->setposY(gamer2->getposY() + 10);
/* start game */
if (GetAsyncKeyState(VK_SPACE)
&& this->getBolPosX() == 350
&& this->getBolPosY() == 250) {
if (rand() % 10 < 5)
this->setDirectX(-5);
else
this->setDirectX(5);
if (rand() & 10 < 5)
this->setDirectY(-5);
else
this->setDirectY(5);
}
/* exit */
if (GetAsyncKeyState(VK_ESCAPE))
PostQuitMessage(0);
}
void moverBol(Gamer* gamer1, Gamer* gamer2) {
if (this->getBolPosX() + this->getDirectX() >= 700) {
this->setBolPosX(350);
this->setBolPosY(250);
this->setDirectX(0);
this->setDirectY(0);
gamer2->setScore(gamer2->getScore() + 1);
}
if (this->getBolPosX() + this->getDirectX() <= 0) {
this->setBolPosX(350);
this->setBolPosY(250);
this->setDirectX(0);
this->setDirectY(0);
gamer1->setScore(gamer1->getScore() + 1);
}
if (this->getBolPosX() + this->getDirectX() <= gamer1->getposX() + 9
&&
(this->getBolPosY() <= gamer1->getposY() + 30
&& this->getBolPosY() >= gamer1->getposY() - 30)
) {
this->setDirectX(-this->getDirectX());
}
if (this->getBolPosX() + this->getDirectX() >= gamer2->getposX() - 9
&&
(this->getBolPosY() <= gamer2->getposY() + 30
&& this->getBolPosY() >= gamer2->getposY() - 30)
) {
this->setDirectX(-this->getDirectX());
}
this->setBolPosX(this->getBolPosX() + this->getDirectX());
if (this->getBolPosY() + this->getDirectY() < 500
&& this->getBolPosY() + this->getDirectY() > 0)
this->setBolPosY(this->getBolPosY() + this->getDirectY());
else {
this->setDirectY(-this->getDirectY());
this->setBolPosY(this->getBolPosY() + this->getDirectY());
}
}
}; |
|
1
|