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
| #include<ctime>
#include<Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE prev, LPSTR cmdline,int ncmdshow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wc;
char* ClassName="ClassName";
char* MenuName="MenuName";
wc.cbSize=sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInst,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MenuName;
wc.lpszClassName = ClassName;
wc.hIconSm = LoadIcon(hInst,IDI_APPLICATION);
RegisterClassEx(&wc);
hwnd = CreateWindow(ClassName, MenuName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL);
srand(unsigned(time(NULL)));
ShowWindow(hwnd,ncmdshow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
struct data //структура данных списка
{
RECT NewRect;
int xNewBounce;
int yNewBounce;
data* next;
}*first=NULL;
void Next(HWND hwnd,data* Node, const int cxClient, const int cyClient) //следующий элемент списка
{
if(Node->next == NULL)
{
HDC hdc;
Node->next=new data;
Node->next->xNewBounce=rand()%cxClient;
Node->next->yNewBounce=rand()%cyClient;
Node->next->next=NULL;
hdc=GetDC(hwnd);
Ellipse(hdc,Node->xNewBounce-10,Node->yNewBounce-10,Node->xNewBounce,Node->yNewBounce);
ReleaseDC(hwnd,hdc);
}else
Next(hwnd,Node->next,cxClient,cyClient);
}
data* Last(data* Node) //последний элемент списка
{
if(Node->next == NULL)
return Node;
else
Last(Node->next);
}
bool KTimerFunction(HWND hwnd,int NumOfTimer)
{
return KillTimer(hwnd,NumOfTimer);
}
void KTimer(HWND hwnd)
{
for(int i=1;;i++)
{
if(KTimerFunction(hwnd,i) == 1)
KillTimer(hwnd,i);
else
while(i != 5)
{
if(KTimerFunction(hwnd,i) == 0)
i++;
else
{
KillTimer(hwnd,i);
i++;
}
}
break;
}
}
void ClearRect(HWND hwnd,RECT &rect,int xBounce,int yBounce) //очищаем прямоугольник
{
HDC hdc;
rect.bottom=yBounce;
rect.left=xBounce;
rect.right=xBounce+10;
rect.top=yBounce+10;
hdc=GetDC(hwnd);
FillRect(hdc,&rect,WHITE_BRUSH);
ReleaseDC(hwnd,hdc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT imsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HBITMAP hbitmap;
static int xBounce,yBounce,cxClient,cyClient,num=6;
int yRandom, xRandom;
switch(imsg)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
case WM_CREATE:
SetTimer(hwnd,1,20,NULL);
SetTimer(hwnd,6,500,NULL);
break;
case WM_TIMER:
if(wParam == num)
{
if(first == NULL) //первый шар
{
first=new data;
first->xNewBounce=rand()%cxClient+10;
first->yNewBounce=rand()%cyClient+10;
first->next=NULL;
hdc=GetDC(hwnd);
Ellipse(hdc, first->xNewBounce-10, first->yNewBounce-10, first->xNewBounce, first->yNewBounce);
ReleaseDC(hwnd, hdc);
}else //последующие шары
{
data* now = Last(first); //инициализируем now последним элементом списка
if(xBounce == now->xNewBounce) //не срабатывает условие (если координаты х движущегося шара совпадают с координатами последнего созданного, то создается новый шар)
{
Next(hwnd, first, cxClient, cyClient);
num++;
SetTimer(hwnd,num,500,NULL);
}
}
}
switch(wParam)
{
case 1:
ClearRect(hwnd,rect,xBounce,yBounce);
if(xBounce == cxClient)
xBounce=0;
xBounce++;
InvalidateRect(hwnd,NULL,false);
break;
case 2:
ClearRect(hwnd,rect,xBounce,yBounce);
if(yBounce+10 == 0)
yBounce=cyClient;
yBounce--;
InvalidateRect(hwnd,NULL,false);
break;
case 3:
ClearRect(hwnd,rect,xBounce,yBounce);
if(yBounce == cyClient)
yBounce=0;
yBounce++;
InvalidateRect(hwnd,NULL,false);
break;
case 4:
ClearRect(hwnd,rect,xBounce,yBounce);
if(xBounce+10 == 0)
xBounce=cxClient;
xBounce--;
InvalidateRect(hwnd,NULL,false);
break;
}
break;
case WM_KEYUP:
if(wParam == VK_UP)
{
KTimer(hwnd);
SetTimer(hwnd,2,20,NULL);
}
if(wParam == VK_DOWN)
{
KTimer(hwnd);
SetTimer(hwnd,3,20,NULL);
}
if(wParam == VK_LEFT)
{
KTimer(hwnd);
SetTimer(hwnd,4,20,NULL);
}
if(wParam == VK_RIGHT)
{
KTimer(hwnd);
SetTimer(hwnd,1,20,NULL);
}
if(wParam == VK_SHIFT)
{
KTimer(hwnd);
SetTimer(hwnd,5,1,NULL);
}
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
Ellipse(hdc,xBounce,yBounce,xBounce+10,yBounce+10);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
KTimer(hwnd);
KillTimer(hwnd,5);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,imsg,wParam,lParam);
}
} |