質問編集履歴

1

2020/05/23 17:03

投稿

hacch
hacch

スコア15

test CHANGED
File without changes
test CHANGED
@@ -79,3 +79,287 @@
79
79
  としてみたのですが、それでは
80
80
 
81
81
  バーがあるx軸全体が反転の対象になってしまいました。
82
+
83
+
84
+
85
+ ```Qt
86
+
87
+
88
+
89
+
90
+
91
+ #include "ofApp.h"
92
+
93
+ float fx; //長方形(枠)の中心のx座標
94
+
95
+ float fy; //長方形(枠)の中心のy座標
96
+
97
+ float fw; //長方形(枠)の幅
98
+
99
+ float fh; //長方形(枠)の高さ
100
+
101
+
102
+
103
+ float bx; //円の中心のx座標
104
+
105
+ float by; //円の中心のy座標
106
+
107
+ float radius; //円の半径
108
+
109
+
110
+
111
+
112
+
113
+ float rx; //パドルの中心のx座標
114
+
115
+ float ry; //パドルの中心のy座標
116
+
117
+ float rw; //パドルの幅
118
+
119
+ float rh; //パドルの高さ
120
+
121
+
122
+
123
+ float x_speed; //パドルのx移動速度
124
+
125
+ float y_speed; //パドルのy移動速度
126
+
127
+
128
+
129
+ float bx_speed; //円のx方向速度
130
+
131
+ float by_speed; //円のy方向速度
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+ //--------------------------------------------------------------
140
+
141
+ void ofApp::setup(){
142
+
143
+ ofBackground(0, 0, 0); //背景を黒
144
+
145
+ ofSetCircleResolution(64); //円の解像度
146
+
147
+
148
+
149
+ //長方形の中心を描画位置にする
150
+
151
+ ofSetRectMode(OF_RECTMODE_CENTER);
152
+
153
+ ofSetFrameRate(30); //フレームレートの設定
154
+
155
+
156
+
157
+ fx = ofGetWidth()/2.0; //枠の描画位置 x座標
158
+
159
+ fy = ofGetHeight()/2.0; //枠の描画位置 y座標
160
+
161
+ fw = 900; //幅
162
+
163
+ fh = 570; //高さ
164
+
165
+ bx = 300; //円の描画位置 x座標
166
+
167
+ by = 300; //円の描画位置 y座標
168
+
169
+ bx_speed = 8; //円のx軸方向の移動速度
170
+
171
+ by_speed = 3; //円のy軸方向の移動速度
172
+
173
+ radius = 25; //円の半径
174
+
175
+
176
+
177
+ rx = ofGetWidth()/2; //パドルの初期位置 x座標
178
+
179
+ ry = ofGetHeight()/2; //パドルの初期位置 y座標
180
+
181
+ rw = 100; //パドルの幅
182
+
183
+ rh = 10; //パドルの高さ
184
+
185
+
186
+
187
+ x_speed = 50; //パドルのx移動速度
188
+
189
+ y_speed = 25; //パドルのy移動速度
190
+
191
+
192
+
193
+
194
+
195
+ }
196
+
197
+
198
+
199
+ //--------------------------------------------------------------
200
+
201
+ void ofApp::update(){
202
+
203
+ bx += bx_speed ; //x軸方向の移動
204
+
205
+ by += by_speed ; //y軸方向の移動
206
+
207
+
208
+
209
+ //円のx軸方向の範囲制限
210
+
211
+ if ((bx<fx-fw/2+radius)||
212
+
213
+
214
+
215
+ (bx>fx+fw/2-radius)){
216
+
217
+
218
+
219
+ bx_speed *= -1 ; //円のy軸方向の移動方向反転
220
+
221
+ }
222
+
223
+ //円のy軸方向の範囲制限
224
+
225
+ if ((by<fy-fh/2+radius)||
226
+
227
+
228
+
229
+ (by>fy+fh/2-radius)){
230
+
231
+
232
+
233
+ by_speed *= -1 ; //円のy軸方向の移動方向反転
234
+
235
+ }
236
+
237
+
238
+
239
+ //パドルのx軸方向の移動範囲制限
240
+
241
+ if(rx<fx-fw/2+rw/2){//パドル左端の制限
242
+
243
+ rx=fx-fw/2+rw/2;
244
+
245
+ }
246
+
247
+ if(rx>fx+fw/2-rw/2){//右端の制限
248
+
249
+ rx=fx+fw/2-rw/2;
250
+
251
+ }
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+ //y軸方向の移動範囲制限
260
+
261
+ if(ry<fy-fh/2+rh/2){//パドル上の制限
262
+
263
+ ry=fy-fh/2+rh/2;
264
+
265
+ }
266
+
267
+ if(ry>fy+fh/2-rh/2){//パドル下の制限
268
+
269
+ ry=fy+fh/2-rh/2;
270
+
271
+ }
272
+
273
+
274
+
275
+
276
+
277
+ if(by+radius>ry){
278
+
279
+ by_speed*=-1;
280
+
281
+ }
282
+
283
+
284
+
285
+ }
286
+
287
+
288
+
289
+ //--------------------------------------------------------------
290
+
291
+ void ofApp::draw(){
292
+
293
+ ofNoFill();//塗りつぶしなし
294
+
295
+ ofSetLineWidth(4);//線の太さを4
296
+
297
+ ofSetColor(255,255,255);//白色に
298
+
299
+ ofDrawRectangle(fx,fy,fw,fh);//長方形を描く
300
+
301
+
302
+
303
+ ofFile();//塗りつぶしあり
304
+
305
+ ofSetColor(255,0,0);//赤色
306
+
307
+ ofDrawCircle(bx,by,radius);//円を描く
308
+
309
+
310
+
311
+ //パドルの描画
312
+
313
+ ofFile();
314
+
315
+ ofSetColor(255,255,255);//描画色 白
316
+
317
+ ofDrawRectangle(rx,ry,rw,rh);//パドルの描画
318
+
319
+
320
+
321
+
322
+
323
+ }
324
+
325
+
326
+
327
+ //--------------------------------------------------------------
328
+
329
+ void ofApp::keyPressed(int key){
330
+
331
+
332
+
333
+ //左矢印キー
334
+
335
+ if(key==OF_KEY_LEFT){
336
+
337
+ rx-=x_speed;
338
+
339
+ }
340
+
341
+ //右矢印キー
342
+
343
+ if(key==OF_KEY_RIGHT){
344
+
345
+ rx+=x_speed;
346
+
347
+ }
348
+
349
+ if(key==OF_KEY_UP){
350
+
351
+ ry-=y_speed;
352
+
353
+ }
354
+
355
+ if(key==OF_KEY_DOWN){
356
+
357
+ ry+=y_speed;
358
+
359
+ }
360
+
361
+ }
362
+
363
+
364
+
365
+ ```