質問編集履歴

1

2020/05/23 17:02

投稿

hacch
hacch

スコア15

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